Files
sam3/emit-options.go

370 lines
9.2 KiB
Go
Raw Normal View History

2019-12-07 22:06:50 -05:00
package sam3
import (
"fmt"
"strconv"
"strings"
)
2024-01-07 12:09:13 -05:00
// Option is a SAMEmit Option
2019-12-07 22:06:50 -05:00
type Option func(*SAMEmit) error
2024-01-07 12:09:13 -05:00
// SetType sets the type of the forwarder server
2019-12-07 22:06:50 -05:00
func SetType(s string) func(*SAMEmit) error {
return func(c *SAMEmit) error {
if s == "STREAM" {
c.Style = s
return nil
} else if s == "DATAGRAM" {
c.Style = s
return nil
2019-12-07 22:10:55 -05:00
} else if s == "RAW" {
c.Style = s
return nil
}
return fmt.Errorf("Invalid session STYLE=%s, must be STREAM, DATAGRAM, or RAW", s)
2019-12-07 22:06:50 -05:00
}
}
// SetSAMAddress sets the SAM address all-at-once
func SetSAMAddress(s string) func(*SAMEmit) error {
return func(c *SAMEmit) error {
sp := strings.Split(s, ":")
if len(sp) > 2 {
return fmt.Errorf("Invalid address string: %s", sp)
}
if len(sp) == 2 {
c.I2PConfig.SamPort = sp[1]
}
c.I2PConfig.SamHost = sp[0]
return nil
}
}
2024-01-07 12:09:13 -05:00
// SetSAMHost sets the host of the SAMEmit's SAM bridge
2019-12-07 22:06:50 -05:00
func SetSAMHost(s string) func(*SAMEmit) error {
return func(c *SAMEmit) error {
c.I2PConfig.SamHost = s
return nil
}
}
2024-01-07 12:09:13 -05:00
// SetSAMPort sets the port of the SAMEmit's SAM bridge using a string
2019-12-07 22:06:50 -05:00
func SetSAMPort(s string) func(*SAMEmit) error {
return func(c *SAMEmit) error {
port, err := strconv.Atoi(s)
if err != nil {
return fmt.Errorf("Invalid SAM Port %s; non-number", s)
}
if port < 65536 && port > -1 {
c.I2PConfig.SamPort = s
return nil
}
return fmt.Errorf("Invalid port")
}
}
2024-01-07 12:09:13 -05:00
// SetName sets the host of the SAMEmit's SAM bridge
2019-12-07 22:06:50 -05:00
func SetName(s string) func(*SAMEmit) error {
return func(c *SAMEmit) error {
c.I2PConfig.TunName = s
return nil
}
}
2024-01-07 12:09:13 -05:00
// SetInLength sets the number of hops inbound
2019-12-07 22:06:50 -05:00
func SetInLength(u int) func(*SAMEmit) error {
return func(c *SAMEmit) error {
if u < 7 && u >= 0 {
c.I2PConfig.InLength = strconv.Itoa(u)
return nil
}
return fmt.Errorf("Invalid inbound tunnel length")
}
}
2024-01-07 12:09:13 -05:00
// SetOutLength sets the number of hops outbound
2019-12-07 22:06:50 -05:00
func SetOutLength(u int) func(*SAMEmit) error {
return func(c *SAMEmit) error {
if u < 7 && u >= 0 {
c.I2PConfig.OutLength = strconv.Itoa(u)
return nil
}
return fmt.Errorf("Invalid outbound tunnel length")
}
}
2024-01-07 12:09:13 -05:00
// SetInVariance sets the variance of a number of hops inbound
2019-12-07 22:06:50 -05:00
func SetInVariance(i int) func(*SAMEmit) error {
return func(c *SAMEmit) error {
if i < 7 && i > -7 {
c.I2PConfig.InVariance = strconv.Itoa(i)
return nil
}
return fmt.Errorf("Invalid inbound tunnel length")
}
}
2024-01-07 12:09:13 -05:00
// SetOutVariance sets the variance of a number of hops outbound
2019-12-07 22:06:50 -05:00
func SetOutVariance(i int) func(*SAMEmit) error {
return func(c *SAMEmit) error {
if i < 7 && i > -7 {
c.I2PConfig.OutVariance = strconv.Itoa(i)
return nil
}
return fmt.Errorf("Invalid outbound tunnel variance")
}
}
2024-01-07 12:09:13 -05:00
// SetInQuantity sets the inbound tunnel quantity
2019-12-07 22:06:50 -05:00
func SetInQuantity(u int) func(*SAMEmit) error {
return func(c *SAMEmit) error {
if u <= 16 && u > 0 {
c.I2PConfig.InQuantity = strconv.Itoa(u)
return nil
}
return fmt.Errorf("Invalid inbound tunnel quantity")
}
}
2024-01-07 12:09:13 -05:00
// SetOutQuantity sets the outbound tunnel quantity
2019-12-07 22:06:50 -05:00
func SetOutQuantity(u int) func(*SAMEmit) error {
return func(c *SAMEmit) error {
if u <= 16 && u > 0 {
c.I2PConfig.OutQuantity = strconv.Itoa(u)
return nil
}
return fmt.Errorf("Invalid outbound tunnel quantity")
}
}
2024-01-07 12:09:13 -05:00
// SetInBackups sets the inbound tunnel backups
2019-12-07 22:06:50 -05:00
func SetInBackups(u int) func(*SAMEmit) error {
return func(c *SAMEmit) error {
if u < 6 && u >= 0 {
c.I2PConfig.InBackupQuantity = strconv.Itoa(u)
return nil
}
return fmt.Errorf("Invalid inbound tunnel backup quantity")
}
}
2024-01-07 12:09:13 -05:00
// SetOutBackups sets the inbound tunnel backups
2019-12-07 22:06:50 -05:00
func SetOutBackups(u int) func(*SAMEmit) error {
return func(c *SAMEmit) error {
if u < 6 && u >= 0 {
c.I2PConfig.OutBackupQuantity = strconv.Itoa(u)
return nil
}
return fmt.Errorf("Invalid outbound tunnel backup quantity")
}
}
2024-01-07 12:09:13 -05:00
// SetEncrypt tells the router to use an encrypted leaseset
2019-12-07 22:06:50 -05:00
func SetEncrypt(b bool) func(*SAMEmit) error {
return func(c *SAMEmit) error {
if b {
c.I2PConfig.EncryptLeaseSet = "true"
return nil
}
c.I2PConfig.EncryptLeaseSet = "false"
return nil
}
}
2024-01-07 12:09:13 -05:00
// SetLeaseSetKey sets the host of the SAMEmit's SAM bridge
2019-12-07 22:06:50 -05:00
func SetLeaseSetKey(s string) func(*SAMEmit) error {
return func(c *SAMEmit) error {
c.I2PConfig.LeaseSetKey = s
return nil
}
}
2024-01-07 12:09:13 -05:00
// SetLeaseSetPrivateKey sets the host of the SAMEmit's SAM bridge
2019-12-07 22:06:50 -05:00
func SetLeaseSetPrivateKey(s string) func(*SAMEmit) error {
return func(c *SAMEmit) error {
c.I2PConfig.LeaseSetPrivateKey = s
return nil
}
}
2024-01-07 12:09:13 -05:00
// SetLeaseSetPrivateSigningKey sets the host of the SAMEmit's SAM bridge
2019-12-07 22:06:50 -05:00
func SetLeaseSetPrivateSigningKey(s string) func(*SAMEmit) error {
return func(c *SAMEmit) error {
c.I2PConfig.LeaseSetPrivateSigningKey = s
return nil
}
}
2024-01-07 12:09:13 -05:00
// SetMessageReliability sets the host of the SAMEmit's SAM bridge
2019-12-07 22:06:50 -05:00
func SetMessageReliability(s string) func(*SAMEmit) error {
return func(c *SAMEmit) error {
c.I2PConfig.MessageReliability = s
return nil
}
}
2024-01-07 12:09:13 -05:00
// SetAllowZeroIn tells the tunnel to accept zero-hop peers
2019-12-07 22:06:50 -05:00
func SetAllowZeroIn(b bool) func(*SAMEmit) error {
return func(c *SAMEmit) error {
if b {
c.I2PConfig.InAllowZeroHop = "true"
return nil
}
c.I2PConfig.InAllowZeroHop = "false"
return nil
}
}
2024-01-07 12:09:13 -05:00
// SetAllowZeroOut tells the tunnel to accept zero-hop peers
2019-12-07 22:06:50 -05:00
func SetAllowZeroOut(b bool) func(*SAMEmit) error {
return func(c *SAMEmit) error {
if b {
c.I2PConfig.OutAllowZeroHop = "true"
return nil
}
c.I2PConfig.OutAllowZeroHop = "false"
return nil
}
}
2024-01-07 12:09:13 -05:00
// SetCompress tells clients to use compression
2019-12-07 22:06:50 -05:00
func SetCompress(b bool) func(*SAMEmit) error {
return func(c *SAMEmit) error {
if b {
c.I2PConfig.UseCompression = "true"
return nil
}
c.I2PConfig.UseCompression = "false"
return nil
}
}
2024-01-07 12:09:13 -05:00
// SetFastRecieve tells clients to use compression
2019-12-07 22:06:50 -05:00
func SetFastRecieve(b bool) func(*SAMEmit) error {
return func(c *SAMEmit) error {
if b {
c.I2PConfig.FastRecieve = "true"
return nil
}
c.I2PConfig.FastRecieve = "false"
return nil
}
}
2024-01-07 12:09:13 -05:00
// SetReduceIdle tells the connection to reduce it's tunnels during extended idle time.
2019-12-07 22:06:50 -05:00
func SetReduceIdle(b bool) func(*SAMEmit) error {
return func(c *SAMEmit) error {
if b {
c.I2PConfig.ReduceIdle = "true"
return nil
}
c.I2PConfig.ReduceIdle = "false"
return nil
}
}
2024-01-07 12:09:13 -05:00
// SetReduceIdleTime sets the time to wait before reducing tunnels to idle levels
2019-12-07 22:06:50 -05:00
func SetReduceIdleTime(u int) func(*SAMEmit) error {
return func(c *SAMEmit) error {
c.I2PConfig.ReduceIdleTime = "300000"
if u >= 6 {
c.I2PConfig.ReduceIdleTime = strconv.Itoa((u * 60) * 1000)
return nil
}
return fmt.Errorf("Invalid reduce idle timeout(Measured in minutes) %v", u)
}
}
2024-01-07 12:09:13 -05:00
// SetReduceIdleTimeMs sets the time to wait before reducing tunnels to idle levels in milliseconds
2019-12-07 22:06:50 -05:00
func SetReduceIdleTimeMs(u int) func(*SAMEmit) error {
return func(c *SAMEmit) error {
c.I2PConfig.ReduceIdleTime = "300000"
if u >= 300000 {
c.I2PConfig.ReduceIdleTime = strconv.Itoa(u)
return nil
}
return fmt.Errorf("Invalid reduce idle timeout(Measured in milliseconds) %v", u)
}
}
2024-01-07 12:09:13 -05:00
// SetReduceIdleQuantity sets minimum number of tunnels to reduce to during idle time
2019-12-07 22:06:50 -05:00
func SetReduceIdleQuantity(u int) func(*SAMEmit) error {
return func(c *SAMEmit) error {
if u < 5 {
c.I2PConfig.ReduceIdleQuantity = strconv.Itoa(u)
return nil
}
return fmt.Errorf("Invalid reduce tunnel quantity")
}
}
2024-01-07 12:09:13 -05:00
// SetCloseIdle tells the connection to close it's tunnels during extended idle time.
2019-12-07 22:06:50 -05:00
func SetCloseIdle(b bool) func(*SAMEmit) error {
return func(c *SAMEmit) error {
if b {
c.I2PConfig.CloseIdle = "true"
return nil
}
c.I2PConfig.CloseIdle = "false"
return nil
}
}
2024-01-07 12:09:13 -05:00
// SetCloseIdleTime sets the time to wait before closing tunnels to idle levels
2019-12-07 22:06:50 -05:00
func SetCloseIdleTime(u int) func(*SAMEmit) error {
return func(c *SAMEmit) error {
c.I2PConfig.CloseIdleTime = "300000"
if u >= 6 {
c.I2PConfig.CloseIdleTime = strconv.Itoa((u * 60) * 1000)
return nil
}
return fmt.Errorf("Invalid close idle timeout(Measured in minutes) %v", u)
}
}
2024-01-07 12:09:13 -05:00
// SetCloseIdleTimeMs sets the time to wait before closing tunnels to idle levels in milliseconds
2019-12-07 22:06:50 -05:00
func SetCloseIdleTimeMs(u int) func(*SAMEmit) error {
return func(c *SAMEmit) error {
c.I2PConfig.CloseIdleTime = "300000"
if u >= 300000 {
c.I2PConfig.CloseIdleTime = strconv.Itoa(u)
return nil
}
return fmt.Errorf("Invalid close idle timeout(Measured in milliseconds) %v", u)
}
}
2024-01-07 12:09:13 -05:00
// SetAccessListType tells the system to treat the AccessList as a whitelist
2019-12-07 22:06:50 -05:00
func SetAccessListType(s string) func(*SAMEmit) error {
return func(c *SAMEmit) error {
if s == "whitelist" {
c.I2PConfig.AccessListType = "whitelist"
return nil
} else if s == "blacklist" {
c.I2PConfig.AccessListType = "blacklist"
return nil
} else if s == "none" {
c.I2PConfig.AccessListType = ""
return nil
} else if s == "" {
c.I2PConfig.AccessListType = ""
return nil
}
return fmt.Errorf("Invalid Access list type(whitelist, blacklist, none)")
}
}
2024-01-07 12:09:13 -05:00
// SetAccessList tells the system to treat the AccessList as a whitelist
2019-12-07 22:06:50 -05:00
func SetAccessList(s []string) func(*SAMEmit) error {
return func(c *SAMEmit) error {
if len(s) > 0 {
for _, a := range s {
c.I2PConfig.AccessList = append(c.I2PConfig.AccessList, a)
}
return nil
}
return nil
}
}