Files
sam3/emit-options.go

431 lines
13 KiB
Go
Raw Normal View History

2019-12-07 22:06:50 -05:00
package sam3
import (
"fmt"
2024-10-15 17:01:43 -04:00
"github.com/sirupsen/logrus"
2019-12-07 22:06:50 -05:00
"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
2024-10-15 17:01:43 -04:00
log.WithField("style", s).Debug("Set session style")
2019-12-07 22:06:50 -05:00
return nil
} else if s == "DATAGRAM" {
c.Style = s
2024-10-15 17:01:43 -04:00
log.WithField("style", s).Debug("Set session style")
2019-12-07 22:06:50 -05:00
return nil
2019-12-07 22:10:55 -05:00
} else if s == "RAW" {
c.Style = s
2024-10-15 17:01:43 -04:00
log.WithField("style", s).Debug("Set session style")
2019-12-07 22:10:55 -05:00
return nil
}
2024-10-15 17:01:43 -04:00
log.WithField("style", s).Error("Invalid session style")
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 {
2024-10-15 17:01:43 -04:00
log.WithField("address", s).Error("Invalid SAM address")
2019-12-07 22:06:50 -05:00
return fmt.Errorf("Invalid address string: %s", sp)
}
if len(sp) == 2 {
c.I2PConfig.SamPort = sp[1]
}
c.I2PConfig.SamHost = sp[0]
2024-10-15 17:01:43 -04:00
log.WithFields(logrus.Fields{
"host": c.I2PConfig.SamHost,
"port": c.I2PConfig.SamPort,
}).Debug("Set SAM address")
2019-12-07 22:06:50 -05:00
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
2024-10-15 17:01:43 -04:00
log.WithField("host", s).Debug("Set SAM host")
2019-12-07 22:06:50 -05:00
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 {
2024-10-15 17:01:43 -04:00
log.WithField("port", s).Error("Invalid SAM port: non-number")
2019-12-07 22:06:50 -05:00
return fmt.Errorf("Invalid SAM Port %s; non-number", s)
}
if port < 65536 && port > -1 {
c.I2PConfig.SamPort = s
2024-10-15 17:01:43 -04:00
log.WithField("port", s).Debug("Set SAM port")
2019-12-07 22:06:50 -05:00
return nil
}
2024-10-15 17:01:43 -04:00
log.WithField("port", port).Error("Invalid SAM port")
2019-12-07 22:06:50 -05:00
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
2024-10-15 17:01:43 -04:00
log.WithField("name", s).Debug("Set tunnel name")
2019-12-07 22:06:50 -05:00
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)
2024-10-15 17:01:43 -04:00
log.WithField("inLength", u).Debug("Set inbound tunnel length")
2019-12-07 22:06:50 -05:00
return nil
}
2024-10-15 17:01:43 -04:00
log.WithField("inLength", u).Error("Invalid inbound tunnel length")
2019-12-07 22:06:50 -05:00
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)
2024-10-15 17:01:43 -04:00
log.WithField("outLength", u).Debug("Set outbound tunnel length")
2019-12-07 22:06:50 -05:00
return nil
}
2024-10-15 17:01:43 -04:00
log.WithField("outLength", u).Error("Invalid outbound tunnel length")
2019-12-07 22:06:50 -05:00
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)
2024-10-15 17:01:43 -04:00
log.WithField("inVariance", i).Debug("Set inbound tunnel variance")
2019-12-07 22:06:50 -05:00
return nil
}
2024-10-15 17:01:43 -04:00
log.WithField("inVariance", i).Error("Invalid inbound tunnel variance")
2019-12-07 22:06:50 -05:00
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)
2024-10-15 17:01:43 -04:00
log.WithField("outVariance", i).Debug("Set outbound tunnel variance")
2019-12-07 22:06:50 -05:00
return nil
}
2024-10-15 17:01:43 -04:00
log.WithField("outVariance", i).Error("Invalid outbound tunnel variance")
2019-12-07 22:06:50 -05:00
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)
2024-10-15 17:01:43 -04:00
log.WithField("inQuantity", u).Debug("Set inbound tunnel quantity")
2019-12-07 22:06:50 -05:00
return nil
}
2024-10-15 17:01:43 -04:00
log.WithField("inQuantity", u).Error("Invalid inbound tunnel quantity")
2019-12-07 22:06:50 -05:00
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)
2024-10-15 17:01:43 -04:00
log.WithField("outQuantity", u).Debug("Set outbound tunnel quantity")
2019-12-07 22:06:50 -05:00
return nil
}
2024-10-15 17:01:43 -04:00
log.WithField("outQuantity", u).Error("Invalid outbound tunnel quantity")
2019-12-07 22:06:50 -05:00
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)
2024-10-15 17:01:43 -04:00
log.WithField("inBackups", u).Debug("Set inbound tunnel backups")
2019-12-07 22:06:50 -05:00
return nil
}
2024-10-15 17:01:43 -04:00
log.WithField("inBackups", u).Error("Invalid inbound tunnel backup quantity")
2019-12-07 22:06:50 -05:00
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)
2024-10-15 17:01:43 -04:00
log.WithField("outBackups", u).Debug("Set outbound tunnel backups")
2019-12-07 22:06:50 -05:00
return nil
}
2024-10-15 17:01:43 -04:00
log.WithField("outBackups", u).Error("Invalid outbound tunnel backup quantity")
2019-12-07 22:06:50 -05:00
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"
2024-10-15 17:01:43 -04:00
log.WithField("encrypt", b).Debug("Set lease set encryption")
2019-12-07 22:06:50 -05:00
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
2024-10-15 17:01:43 -04:00
log.WithField("leaseSetKey", s).Debug("Set lease set key")
2019-12-07 22:06:50 -05:00
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
2024-10-15 17:01:43 -04:00
log.WithField("leaseSetPrivateKey", s).Debug("Set lease set private key")
2019-12-07 22:06:50 -05:00
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
2024-10-15 17:01:43 -04:00
log.WithField("leaseSetPrivateSigningKey", s).Debug("Set lease set private signing key")
2019-12-07 22:06:50 -05:00
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
2024-10-15 17:01:43 -04:00
log.WithField("messageReliability", s).Debug("Set message reliability")
2019-12-07 22:06:50 -05:00
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"
2024-10-15 17:01:43 -04:00
log.WithField("allowZeroIn", b).Debug("Set allow zero-hop inbound")
2019-12-07 22:06:50 -05:00
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"
2024-10-15 17:01:43 -04:00
log.WithField("allowZeroOut", b).Debug("Set allow zero-hop outbound")
2019-12-07 22:06:50 -05:00
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"
2024-10-15 17:01:43 -04:00
log.WithField("compress", b).Debug("Set compression")
2019-12-07 22:06:50 -05:00
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"
2024-10-15 17:01:43 -04:00
log.WithField("fastReceive", b).Debug("Set fast receive")
2019-12-07 22:06:50 -05:00
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"
2024-10-15 17:01:43 -04:00
log.WithField("reduceIdle", b).Debug("Set reduce idle")
2019-12-07 22:06:50 -05:00
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 {
2024-10-15 17:01:43 -04:00
idleTime := strconv.Itoa((u * 60) * 1000)
c.I2PConfig.ReduceIdleTime = idleTime
log.WithField("reduceIdleTime", idleTime).Debug("Set reduce idle time")
2019-12-07 22:06:50 -05:00
return nil
}
2024-10-15 17:01:43 -04:00
log.WithField("minutes", u).Error("Invalid reduce idle timeout")
2019-12-07 22:06:50 -05:00
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)
2024-10-15 17:01:43 -04:00
log.WithField("reduceIdleTimeMs", u).Debug("Set reduce idle time in milliseconds")
2019-12-07 22:06:50 -05:00
return nil
}
2024-10-15 17:01:43 -04:00
log.WithField("milliseconds", u).Error("Invalid reduce idle timeout")
2019-12-07 22:06:50 -05:00
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)
2024-10-15 17:01:43 -04:00
log.WithField("reduceIdleQuantity", u).Debug("Set reduce idle quantity")
2019-12-07 22:06:50 -05:00
return nil
}
2024-10-15 17:01:43 -04:00
log.WithField("quantity", u).Error("Invalid reduce tunnel quantity")
2019-12-07 22:06:50 -05:00
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 {
2024-10-15 17:01:43 -04:00
idleTime := strconv.Itoa((u * 60) * 1000)
c.I2PConfig.CloseIdleTime = idleTime
log.WithFields(logrus.Fields{
"minutes": u,
"milliseconds": idleTime,
}).Debug("Set close idle time")
2019-12-07 22:06:50 -05:00
return nil
}
2024-10-15 17:01:43 -04:00
log.WithField("minutes", u).Error("Invalid close idle timeout")
2019-12-07 22:06:50 -05:00
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)
2024-10-15 17:01:43 -04:00
log.WithField("closeIdleTimeMs", u).Debug("Set close idle time in milliseconds")
2019-12-07 22:06:50 -05:00
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"
2024-10-15 17:01:43 -04:00
log.Debug("Set access list type to whitelist")
2019-12-07 22:06:50 -05:00
return nil
} else if s == "blacklist" {
c.I2PConfig.AccessListType = "blacklist"
2024-10-15 17:01:43 -04:00
log.Debug("Set access list type to blacklist")
2019-12-07 22:06:50 -05:00
return nil
} else if s == "none" {
c.I2PConfig.AccessListType = ""
2024-10-15 17:01:43 -04:00
log.Debug("Set access list type to none")
2019-12-07 22:06:50 -05:00
return nil
} else if s == "" {
c.I2PConfig.AccessListType = ""
2024-10-15 17:01:43 -04:00
log.Debug("Set access list type to none")
2019-12-07 22:06:50 -05:00
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)
}
2024-10-15 17:01:43 -04:00
log.WithField("accessList", s).Debug("Set access list")
2019-12-07 22:06:50 -05:00
return nil
}
2024-10-15 17:01:43 -04:00
log.Debug("No access list set (empty list provided)")
2019-12-07 22:06:50 -05:00
return nil
}
}