Move just a spectacular amount of config stuff into a config package because

This commit is contained in:
eyedeekay
2024-11-30 18:34:01 -05:00
parent 1f27cb656e
commit 1c1652f234
10 changed files with 364 additions and 284 deletions

292
config.go
View File

@ -2,7 +2,6 @@ package sam3
import (
"math/rand"
"strconv"
"strings"
"time"
@ -10,63 +9,20 @@ import (
"github.com/go-i2p/i2pkeys"
"github.com/go-i2p/sam3/common"
"github.com/go-i2p/sam3/config"
)
const DEFAULT_LEASESET_TYPE = "i2cp.leaseSetEncType=4"
type SessionOptions struct {
Style string
SignatureType string
FromPort string
ToPort string
Protocol string
UDPPort int
}
// Add transport options
type TransportOptions struct {
UseCompression string
FastReceive string
Reliability string
IdleTimeout time.Duration
}
// I2PConfig is a struct which manages I2P configuration options
type I2PConfig struct {
SamHost string
SamPort string
TunName string
SamMin string
SamMax string
SessionOptions
TransportOptions
common.SAMFormatter
config.SessionOptions
config.TransportOptions
config.TunnelOptions
config.EncryptedLeaseSetOptions
DestinationKeys i2pkeys.I2PKeys
SigType string
EncryptLeaseSet string
LeaseSetKey string
LeaseSetPrivateKey string
LeaseSetPrivateSigningKey string
LeaseSetKeys i2pkeys.I2PKeys
InAllowZeroHop string
OutAllowZeroHop string
InLength string
OutLength string
InQuantity string
OutQuantity string
InVariance string
OutVariance string
InBackupQuantity string
OutBackupQuantity string
CloseIdle string
ReduceIdle string
ReduceIdleTime string
ReduceIdleQuantity string
LeaseSetEncryption string
// Streaming Library options
AccessListType string
AccessList []string
@ -109,15 +65,15 @@ func (f *I2PConfig) SetSAMAddress(addr string) {
// ID returns the tunnel name in the form of "ID=name"
func (f *I2PConfig) ID() string {
if f.TunName == "" {
if f.NickName == "" {
b := make([]byte, 12)
for i := range b {
b[i] = "abcdefghijklmnopqrstuvwxyz"[rand.Intn(len("abcdefghijklmnopqrstuvwxyz"))]
}
f.TunName = string(b)
log.WithField("TunName", f.TunName).Debug("Generated random tunnel name")
f.NickName = string(b)
log.WithField("NickName", f.NickName).Debug("Generated random tunnel name")
}
return " ID=" + f.TunName + " "
return " ID=" + f.NickName + " "
}
// Leasesetsettings returns the lease set settings in the form of "i2cp.leaseSetKey=key i2cp.leaseSetPrivateKey=key i2cp.leaseSetPrivateSigningKey=key"
@ -140,34 +96,6 @@ func (f *I2PConfig) Leasesetsettings() (string, string, string) {
return r, s, t
}
// FromPort returns the from port setting in the form of "FROM_PORT=port"
func (f *I2PConfig) FromPort() string {
if f.samMax() < common.SAM31Version.Number {
log.Debug("SAM version < 3.1, FromPort not applicable")
return ""
}
if f.SessionOptions.FromPort != "0" {
log.WithField("fromPort", f.SessionOptions.FromPort).Debug("FromPort set")
return " FROM_PORT=" + f.SessionOptions.FromPort + " "
}
log.Debug("FromPort not set")
return ""
}
// ToPort returns the to port setting in the form of "TO_PORT=port"
func (f *I2PConfig) ToPort() string {
if f.samMax() < common.SAM31Version.Number {
log.Debug("SAM version < 3.1, ToPort not applicable")
return ""
}
if f.SessionOptions.ToPort != "0" {
log.WithField("toPort", f.SessionOptions.ToPort).Debug("ToPort set")
return " TO_PORT=" + f.SessionOptions.ToPort + " "
}
log.Debug("ToPort not set")
return ""
}
// SessionStyle returns the session style setting in the form of "STYLE=style"
func (f *I2PConfig) SessionStyle() string {
if f.SessionOptions.Style != "" {
@ -178,34 +106,31 @@ func (f *I2PConfig) SessionStyle() string {
return " STYLE=STREAM "
}
func (f *I2PConfig) samMax() float64 {
i, err := strconv.Atoi(f.SamMax)
if err != nil {
log.WithError(err).Warn("Failed to parse SamMax, using default 3.1")
return 3.1
}
log.WithField("samMax", float64(i)).Debug("SAM max version parsed")
return float64(i)
}
// MinSAM returns the minimum SAM version required in major.minor form
func (f *I2PConfig) MinSAM() string {
if f.SamMin == "" {
log.Debug("Using default MinSAM: 3.1")
return "3.1"
}
log.WithField("minSAM", f.SamMin).Debug("MinSAM set")
return f.SamMin
min, _ := f.GetVersions()
return string(min)
}
// MaxSAM returns the maximum SAM version required in major.minor form
func (f *I2PConfig) MaxSAM() string {
if f.SamMax == "" {
log.Debug("Using default MaxSAM: 3.3")
return "3.3"
_, max := f.GetVersions()
return string(max)
}
func (f *I2PConfig) GetVersions() (min, max common.ProtocolVersion) {
if f.SamMin == "" {
min = common.SAM31Version.String
} else {
min = common.ProtocolVersion(f.SamMin)
}
log.WithField("maxSAM", f.SamMax).Debug("MaxSAM set")
return f.SamMax
if f.SamMax == "" {
max = common.SAM33Version.String
log.Debug("Using default MaxSAM: 3.3")
} else {
max = common.ProtocolVersion(f.SamMax)
}
return min, max
}
// DestinationKey returns the destination key setting in the form of "DESTINATION=key"
@ -218,30 +143,6 @@ func (f *I2PConfig) DestinationKey() string {
return " DESTINATION=TRANSIENT "
}
// SignatureType returns the signature type setting in the form of "SIGNATURE_TYPE=type"
func (f *I2PConfig) SignatureType() string {
if f.samMax() < common.SAM31Version.Number {
log.Debug("SAM version < 3.1, SignatureType not applicable")
return ""
}
if f.SigType != "" {
log.WithField("sigType", f.SigType).Debug("Signature type set")
return " SIGNATURE_TYPE=" + f.SigType + " "
}
log.Debug("Signature type not set")
return ""
}
// EncryptLease returns the lease set encryption setting in the form of "i2cp.encryptLeaseSet=true"
func (f *I2PConfig) EncryptLease() string {
if f.EncryptLeaseSet == "true" {
log.Debug("Lease set encryption enabled")
return " i2cp.encryptLeaseSet=true "
}
log.Debug("Lease set encryption not enabled")
return ""
}
// Reliability returns the message reliability setting in the form of "i2cp.messageReliability=reliability"
func (f *I2PConfig) Reliability() string {
if f.TransportOptions.Reliability != "" {
@ -254,13 +155,13 @@ func (f *I2PConfig) Reliability() string {
// Reduce returns the reduce idle settings in the form of "i2cp.reduceOnIdle=true i2cp.reduceIdleTime=time i2cp.reduceQuantity=quantity"
func (f *I2PConfig) Reduce() string {
if f.ReduceIdle == "true" {
if f.ReduceIdle {
log.WithFields(logrus.Fields{
"reduceIdle": f.ReduceIdle,
"reduceIdleTime": f.ReduceIdleTime,
"reduceIdleQuantity": f.ReduceIdleQuantity,
"reduceIdleTime": f.TransportOptions.ReduceIdleTimeout.String(),
"reduceIdleQuantity": f.TransportOptions.ReduceIdleQuantity,
}).Debug("Reduce idle settings applied")
return "i2cp.reduceOnIdle=" + f.ReduceIdle + "i2cp.reduceIdleTime=" + f.ReduceIdleTime + "i2cp.reduceQuantity=" + f.ReduceIdleQuantity
return "i2cp.reduceOnIdle=" + f.ReduceOnIdle() + "i2cp.reduceIdleTime=" + f.TransportOptions.ReduceIdleTimeout.String() + "i2cp.reduceQuantity=" + f.ReduceQuantity()
}
log.Debug("Reduce idle settings not applied")
return ""
@ -268,46 +169,30 @@ func (f *I2PConfig) Reduce() string {
// Close returns the close idle settings in the form of "i2cp.closeOnIdle=true i2cp.closeIdleTime=time"
func (f *I2PConfig) Close() string {
if f.CloseIdle == "true" {
if f.CloseIdle {
log.WithFields(logrus.Fields{
"closeIdle": f.CloseIdle,
"closeIdleTime": f.TransportOptions.IdleTimeout.String(),
"closeIdleTime": f.TransportOptions.CloseIdleTimeout.String(),
}).Debug("Close idle settings applied")
return "i2cp.closeOnIdle=" + f.CloseIdle + "i2cp.closeIdleTime=" + f.TransportOptions.IdleTimeout.String()
return "i2cp.closeOnIdle=" + f.CloseOnIdle() + "i2cp.closeIdleTime=" + f.TransportOptions.CloseIdleTimeout.String()
}
log.Debug("Close idle settings not applied")
return ""
}
// DoZero returns the zero hop settings in the form of "inbound.allowZeroHop=true outbound.allowZeroHop=true fastRecieve=true"
func (f *I2PConfig) DoZero() string {
r := ""
if f.InAllowZeroHop == "true" {
r += " inbound.allowZeroHop=" + f.InAllowZeroHop + " "
}
if f.OutAllowZeroHop == "true" {
r += " outbound.allowZeroHop= " + f.OutAllowZeroHop + " "
}
if f.TransportOptions.FastReceive == "true" {
r += " " + f.TransportOptions.FastReceive + " "
}
log.WithField("zeroHopSettings", r).Debug("Zero hop settings applied")
return r
}
// Print returns the full config as a string
func (f *I2PConfig) Print() []string {
lsk, lspk, lspsk := f.Leasesetsettings()
return []string{
// f.targetForPort443(),
"inbound.length=" + f.InLength,
"outbound.length=" + f.OutLength,
"inbound.lengthVariance=" + f.InVariance,
"outbound.lengthVariance=" + f.OutVariance,
"inbound.backupQuantity=" + f.InBackupQuantity,
"outbound.backupQuantity=" + f.OutBackupQuantity,
"inbound.quantity=" + f.InQuantity,
"outbound.quantity=" + f.OutQuantity,
"inbound.length=" + f.InboundLength(),
"outbound.length=" + f.OutboundLength(),
"inbound.lengthVariance=" + f.InboundVariance(),
"outbound.lengthVariance=" + f.OutboundVariance(),
"inbound.backupQuantity=" + f.InboundBackupQuantity(),
"outbound.backupQuantity=" + f.OutboundBackupQuantity(),
"inbound.quantity=" + f.InboundQuantity(),
"outbound.quantity=" + f.OutboundQuantity(),
f.DoZero(),
//"i2cp.fastRecieve=" + f.FastRecieve,
"i2cp.gzip=" + f.TransportOptions.UseCompression,
@ -352,62 +237,49 @@ func (f *I2PConfig) Accesslist() string {
return ""
}
// LeaseSetEncryptionType returns the lease set encryption type in the form of "i2cp.leaseSetEncType=type"
func (f *I2PConfig) LeaseSetEncryptionType() string {
if f.LeaseSetEncryption == "" {
log.Debug("Using default lease set encryption type: 4,0")
return "i2cp.leaseSetEncType=4,0"
}
for _, s := range strings.Split(f.LeaseSetEncryption, ",") {
if _, err := strconv.Atoi(s); err != nil {
log.WithField("invalidType", s).Panic("Invalid encrypted leaseSet type")
// panic("Invalid encrypted leaseSet type: " + s)
}
}
log.WithField("leaseSetEncType", f.LeaseSetEncryption).Debug("Lease set encryption type set")
return "i2cp.leaseSetEncType=" + f.LeaseSetEncryption
}
// NewConfig returns a new config with default values or updates them with functional arguments
func NewConfig(opts ...func(*I2PConfig) error) (*I2PConfig, error) {
config := I2PConfig{
SamHost: "127.0.0.1",
SamPort: "7656",
SamMin: "3.0",
SamMax: "3.3",
TunName: "",
InLength: "3",
OutLength: "3",
InQuantity: "2",
OutQuantity: "2",
InVariance: "1",
OutVariance: "1",
InBackupQuantity: "3",
OutBackupQuantity: "3",
InAllowZeroHop: "false",
OutAllowZeroHop: "false",
EncryptLeaseSet: "false",
LeaseSetKey: "",
LeaseSetPrivateKey: "",
LeaseSetPrivateSigningKey: "",
ReduceIdle: "false",
ReduceIdleTime: "15",
ReduceIdleQuantity: "1",
CloseIdle: "false",
LeaseSetEncryption: DEFAULT_LEASESET_TYPE,
SessionOptions: SessionOptions{
Style: "STREAM",
SignatureType: "EdDSA_SHA512_Ed25519",
FromPort: "",
ToPort: "",
Protocol: "",
UDPPort: 0,
EncryptedLeaseSetOptions: config.EncryptedLeaseSetOptions{
EncryptLeaseSet: false,
LeaseSetKey: "",
LeaseSetPrivateKey: "",
LeaseSetPrivateSigningKey: "",
LeaseSetEncryption: DEFAULT_LEASESET_TYPE,
},
TransportOptions: TransportOptions{
UseCompression: "true",
FastReceive: "false",
Reliability: "none",
IdleTimeout: 5 * time.Minute,
TunnelOptions: config.TunnelOptions{
InAllowZeroHop: false,
OutAllowZeroHop: false,
InLength: 3,
OutLength: 3,
InQuantity: 2,
OutQuantity: 2,
InVariance: 1,
OutVariance: 1,
InBackupQuantity: 3,
OutBackupQuantity: 3,
},
SessionOptions: config.SessionOptions{
NickName: "",
Style: "STREAM",
SigType: "EdDSA_SHA512_Ed25519",
InFromPort: "",
OutToPort: "",
Protocol: "",
UDPPort: 0,
SamHost: "127.0.0.1",
SamPort: "7656",
SamMin: string(common.SAM31Version.String),
SamMax: string(common.SAM33Version.String),
},
TransportOptions: config.TransportOptions{
UseCompression: "true",
FastReceive: "false",
Reliability: "none",
CloseIdleTimeout: 5 * time.Minute,
ReduceIdleQuantity: 1,
ReduceIdle: false,
CloseIdle: false,
},
}
for _, o := range opts {

44
config/leaseset.go Normal file
View File

@ -0,0 +1,44 @@
package config
import (
"strconv"
"strings"
"github.com/go-i2p/i2pkeys"
)
type EncryptedLeaseSetOptions struct {
// SigType string
EncryptLeaseSet bool
LeaseSetKey string
LeaseSetPrivateKey string
LeaseSetPrivateSigningKey string
LeaseSetKeys i2pkeys.I2PKeys
LeaseSetEncryption string
}
// EncryptLease returns the lease set encryption setting in the form of "i2cp.encryptLeaseSet=true"
func (f *EncryptedLeaseSetOptions) EncryptLease() string {
if f.EncryptLeaseSet {
log.Debug("Lease set encryption enabled")
return " i2cp.encryptLeaseSet=true "
}
log.Debug("Lease set encryption not enabled")
return ""
}
// LeaseSetEncryptionType returns the lease set encryption type in the form of "i2cp.leaseSetEncType=type"
func (f *EncryptedLeaseSetOptions) LeaseSetEncryptionType() string {
if f.LeaseSetEncryption == "" {
log.Debug("Using default lease set encryption type: 4,0")
return "i2cp.leaseSetEncType=4,0"
}
for _, s := range strings.Split(f.LeaseSetEncryption, ",") {
if _, err := strconv.Atoi(s); err != nil {
log.WithField("invalidType", s).Panic("Invalid encrypted leaseSet type")
// panic("Invalid encrypted leaseSet type: " + s)
}
}
log.WithField("leaseSetEncType", f.LeaseSetEncryption).Debug("Lease set encryption type set")
return "i2cp.leaseSetEncType=" + f.LeaseSetEncryption
}

5
config/log.go Normal file
View File

@ -0,0 +1,5 @@
package config
import "github.com/go-i2p/sam3/common"
var log = common.GetSAM3Logger()

73
config/session.go Normal file
View File

@ -0,0 +1,73 @@
package config
import (
"strconv"
"github.com/go-i2p/sam3/common"
)
type SessionOptions struct {
NickName string
Style string
SigType string
InFromPort string
OutToPort string
Protocol string
UDPPort int
SamHost string
SamPort string
SamMin string
SamMax string
}
func (f *SessionOptions) samMax() float64 {
i, err := strconv.Atoi(f.SamMax)
if err != nil {
log.WithError(err).Warn("Failed to parse SamMax, using default 3.1")
return 3.1
}
log.WithField("samMax", float64(i)).Debug("SAM max version parsed")
return float64(i)
}
// SignatureType returns the signature type setting in the form of "SIGNATURE_TYPE=type"
func (f *SessionOptions) SignatureType() string {
if f.samMax() < common.SAM31Version.Number {
log.Debug("SAM version < 3.1, SignatureType not applicable")
return ""
}
if f.SigType != "" {
log.WithField("sigType", f.SigType).Debug("Signature type set")
return " SIGNATURE_TYPE=" + f.SigType + " "
}
log.Debug("Signature type not set")
return ""
}
// FromPort returns the from port setting in the form of "FROM_PORT=port"
func (f *SessionOptions) FromPort() string {
if f.samMax() < common.SAM31Version.Number {
log.Debug("SAM version < 3.1, FromPort not applicable")
return ""
}
if f.InFromPort != "0" {
log.WithField("fromPort", f.InFromPort).Debug("FromPort set")
return " FROM_PORT=" + f.InFromPort + " "
}
log.Debug("FromPort not set")
return ""
}
// ToPort returns the to port setting in the form of "TO_PORT=port"
func (f *SessionOptions) ToPort() string {
if f.samMax() < common.SAM31Version.Number {
log.Debug("SAM version < 3.1, ToPort not applicable")
return ""
}
if f.OutToPort != "0" {
log.WithField("toPort", f.OutToPort).Debug("ToPort set")
return " TO_PORT=" + f.OutToPort + " "
}
log.Debug("ToPort not set")
return ""
}

44
config/transport.go Normal file
View File

@ -0,0 +1,44 @@
package config
import (
"strconv"
"time"
)
func boolToStr(b bool) string {
if b {
return "true"
}
return "false"
}
// Add transport options
type TransportOptions struct {
UseCompression string
FastReceive string
Reliability string
CloseIdleTimeout time.Duration
CloseIdle bool
ReduceIdleTimeout time.Duration
ReduceIdle bool
ReduceIdleQuantity int
}
func (f *TransportOptions) ReduceOnIdle() string {
return boolToStr(f.ReduceIdle)
}
func (f *TransportOptions) ReduceQuantity() string {
return strconv.Itoa(f.ReduceIdleQuantity)
}
func (f *TransportOptions) CloseOnIdle() string {
return boolToStr(f.CloseIdle)
}
func (f *TransportOptions) DoFastReceive() string {
if f.FastReceive == "true" {
return " " + f.FastReceive + " "
}
return ""
}

70
config/tunnel.go Normal file
View File

@ -0,0 +1,70 @@
package config
import "strconv"
type TunnelOptions struct {
InAllowZeroHop bool
OutAllowZeroHop bool
InLength int
OutLength int
InQuantity int
OutQuantity int
InVariance int
OutVariance int
InBackupQuantity int
OutBackupQuantity int
}
func (f *TunnelOptions) InboundDoZero() string {
return boolToStr(f.InAllowZeroHop)
}
func (f *TunnelOptions) OutboundDoZero() string {
return boolToStr(f.OutAllowZeroHop)
}
func (f *TunnelOptions) InboundLength() string {
return strconv.Itoa(f.InLength)
}
func (f *TunnelOptions) OutboundLength() string {
return strconv.Itoa(f.OutLength)
}
func (f *TunnelOptions) InboundQuantity() string {
return strconv.Itoa(f.InQuantity)
}
func (f *TunnelOptions) OutboundQuantity() string {
return strconv.Itoa(f.OutQuantity)
}
func (f *TunnelOptions) InboundVariance() string {
return strconv.Itoa(f.InVariance)
}
func (f *TunnelOptions) OutboundVariance() string {
return strconv.Itoa(f.OutVariance)
}
func (f *TunnelOptions) InboundBackupQuantity() string {
return strconv.Itoa(f.InBackupQuantity)
}
func (f *TunnelOptions) OutboundBackupQuantity() string {
return strconv.Itoa(f.OutBackupQuantity)
}
// DoZero returns the zero hop settings in the form of "inbound.allowZeroHop=true outbound.allowZeroHop=true fastRecieve=true"
func (f *TunnelOptions) DoZero() string {
r := ""
if f.InAllowZeroHop {
r += " inbound.allowZeroHop=" + f.InboundDoZero() + " "
}
if f.OutAllowZeroHop {
r += " outbound.allowZeroHop= " + f.OutboundDoZero() + " "
}
log.WithField("zeroHopSettings", r).Debug("Zero hop settings applied")
return r
}

View File

@ -83,7 +83,7 @@ func SetSAMPort(s string) func(*SAMEmit) error {
// SetName sets the host of the SAMEmit's SAM bridge
func SetName(s string) func(*SAMEmit) error {
return func(c *SAMEmit) error {
c.I2PConfig.TunName = s
c.I2PConfig.SessionOptions.NickName = s
log.WithField("name", s).Debug("Set tunnel name")
return nil
}
@ -93,7 +93,7 @@ func SetName(s string) func(*SAMEmit) error {
func SetInLength(u int) func(*SAMEmit) error {
return func(c *SAMEmit) error {
if u < 7 && u >= 0 {
c.I2PConfig.InLength = strconv.Itoa(u)
c.I2PConfig.InLength = u
log.WithField("inLength", u).Debug("Set inbound tunnel length")
return nil
}
@ -106,7 +106,7 @@ func SetInLength(u int) func(*SAMEmit) error {
func SetOutLength(u int) func(*SAMEmit) error {
return func(c *SAMEmit) error {
if u < 7 && u >= 0 {
c.I2PConfig.OutLength = strconv.Itoa(u)
c.I2PConfig.OutLength = u
log.WithField("outLength", u).Debug("Set outbound tunnel length")
return nil
}
@ -119,7 +119,7 @@ func SetOutLength(u int) func(*SAMEmit) error {
func SetInVariance(i int) func(*SAMEmit) error {
return func(c *SAMEmit) error {
if i < 7 && i > -7 {
c.I2PConfig.InVariance = strconv.Itoa(i)
c.I2PConfig.InVariance = i
log.WithField("inVariance", i).Debug("Set inbound tunnel variance")
return nil
}
@ -132,7 +132,7 @@ func SetInVariance(i int) func(*SAMEmit) error {
func SetOutVariance(i int) func(*SAMEmit) error {
return func(c *SAMEmit) error {
if i < 7 && i > -7 {
c.I2PConfig.OutVariance = strconv.Itoa(i)
c.I2PConfig.OutVariance = i
log.WithField("outVariance", i).Debug("Set outbound tunnel variance")
return nil
}
@ -145,7 +145,7 @@ func SetOutVariance(i int) func(*SAMEmit) error {
func SetInQuantity(u int) func(*SAMEmit) error {
return func(c *SAMEmit) error {
if u <= 16 && u > 0 {
c.I2PConfig.InQuantity = strconv.Itoa(u)
c.I2PConfig.InQuantity = u
log.WithField("inQuantity", u).Debug("Set inbound tunnel quantity")
return nil
}
@ -158,7 +158,7 @@ func SetInQuantity(u int) func(*SAMEmit) error {
func SetOutQuantity(u int) func(*SAMEmit) error {
return func(c *SAMEmit) error {
if u <= 16 && u > 0 {
c.I2PConfig.OutQuantity = strconv.Itoa(u)
c.I2PConfig.OutQuantity = u
log.WithField("outQuantity", u).Debug("Set outbound tunnel quantity")
return nil
}
@ -171,7 +171,7 @@ func SetOutQuantity(u int) func(*SAMEmit) error {
func SetInBackups(u int) func(*SAMEmit) error {
return func(c *SAMEmit) error {
if u < 6 && u >= 0 {
c.I2PConfig.InBackupQuantity = strconv.Itoa(u)
c.I2PConfig.InBackupQuantity = u
log.WithField("inBackups", u).Debug("Set inbound tunnel backups")
return nil
}
@ -184,7 +184,7 @@ func SetInBackups(u int) func(*SAMEmit) error {
func SetOutBackups(u int) func(*SAMEmit) error {
return func(c *SAMEmit) error {
if u < 6 && u >= 0 {
c.I2PConfig.OutBackupQuantity = strconv.Itoa(u)
c.I2PConfig.OutBackupQuantity = u
log.WithField("outBackups", u).Debug("Set outbound tunnel backups")
return nil
}
@ -196,11 +196,7 @@ func SetOutBackups(u int) func(*SAMEmit) error {
// SetEncrypt tells the router to use an encrypted leaseset
func SetEncrypt(b bool) func(*SAMEmit) error {
return func(c *SAMEmit) error {
if b {
c.I2PConfig.EncryptLeaseSet = "true"
return nil
}
c.I2PConfig.EncryptLeaseSet = "false"
c.I2PConfig.EncryptLeaseSet = b
log.WithField("encrypt", b).Debug("Set lease set encryption")
return nil
}
@ -245,11 +241,7 @@ func SetMessageReliability(s string) func(*SAMEmit) error {
// SetAllowZeroIn tells the tunnel to accept zero-hop peers
func SetAllowZeroIn(b bool) func(*SAMEmit) error {
return func(c *SAMEmit) error {
if b {
c.I2PConfig.InAllowZeroHop = "true"
return nil
}
c.I2PConfig.InAllowZeroHop = "false"
c.I2PConfig.InAllowZeroHop = b
log.WithField("allowZeroIn", b).Debug("Set allow zero-hop inbound")
return nil
}
@ -258,11 +250,7 @@ func SetAllowZeroIn(b bool) func(*SAMEmit) error {
// SetAllowZeroOut tells the tunnel to accept zero-hop peers
func SetAllowZeroOut(b bool) func(*SAMEmit) error {
return func(c *SAMEmit) error {
if b {
c.I2PConfig.OutAllowZeroHop = "true"
return nil
}
c.I2PConfig.OutAllowZeroHop = "false"
c.I2PConfig.OutAllowZeroHop = b
log.WithField("allowZeroOut", b).Debug("Set allow zero-hop outbound")
return nil
}
@ -297,11 +285,7 @@ func SetFastRecieve(b bool) func(*SAMEmit) error {
// SetReduceIdle tells the connection to reduce it's tunnels during extended idle time.
func SetReduceIdle(b bool) func(*SAMEmit) error {
return func(c *SAMEmit) error {
if b {
c.I2PConfig.ReduceIdle = "true"
return nil
}
c.I2PConfig.ReduceIdle = "false"
c.I2PConfig.ReduceIdle = b
log.WithField("reduceIdle", b).Debug("Set reduce idle")
return nil
}
@ -310,11 +294,14 @@ func SetReduceIdle(b bool) func(*SAMEmit) error {
// SetReduceIdleTime sets the time to wait before reducing tunnels to idle levels
func SetReduceIdleTime(u int) func(*SAMEmit) error {
return func(c *SAMEmit) error {
c.I2PConfig.ReduceIdleTime = "300000"
c.I2PConfig.TransportOptions.ReduceIdleTimeout = 300000
if u >= 6 {
idleTime := strconv.Itoa((u * 60) * 1000)
c.I2PConfig.ReduceIdleTime = idleTime
log.WithField("reduceIdleTime", idleTime).Debug("Set reduce idle time")
idleTime := (u * 60) * 1000
c.I2PConfig.TransportOptions.ReduceIdleTimeout = time.Duration(idleTime)
log.WithFields(logrus.Fields{
"minutes": u,
"milliseconds": idleTime,
}).Debug("Set reduce idle time")
return nil
}
log.WithField("minutes", u).Error("Invalid reduce idle timeout")
@ -325,13 +312,12 @@ func SetReduceIdleTime(u int) func(*SAMEmit) error {
// SetReduceIdleTimeMs sets the time to wait before reducing tunnels to idle levels in milliseconds
func SetReduceIdleTimeMs(u int) func(*SAMEmit) error {
return func(c *SAMEmit) error {
c.I2PConfig.ReduceIdleTime = "300000"
c.I2PConfig.TransportOptions.ReduceIdleTimeout = 300000
if u >= 300000 {
c.I2PConfig.ReduceIdleTime = strconv.Itoa(u)
c.I2PConfig.TransportOptions.ReduceIdleTimeout = time.Duration(u)
log.WithField("reduceIdleTimeMs", u).Debug("Set reduce idle time in milliseconds")
return nil
}
log.WithField("milliseconds", u).Error("Invalid reduce idle timeout")
return fmt.Errorf("Invalid reduce idle timeout(Measured in milliseconds) %v", u)
}
}
@ -340,7 +326,7 @@ func SetReduceIdleTimeMs(u int) func(*SAMEmit) error {
func SetReduceIdleQuantity(u int) func(*SAMEmit) error {
return func(c *SAMEmit) error {
if u < 5 {
c.I2PConfig.ReduceIdleQuantity = strconv.Itoa(u)
c.I2PConfig.ReduceIdleQuantity = u
log.WithField("reduceIdleQuantity", u).Debug("Set reduce idle quantity")
return nil
}
@ -352,11 +338,7 @@ func SetReduceIdleQuantity(u int) func(*SAMEmit) error {
// SetCloseIdle tells the connection to close it's tunnels during extended idle time.
func SetCloseIdle(b bool) func(*SAMEmit) error {
return func(c *SAMEmit) error {
if b {
c.I2PConfig.CloseIdle = "true"
return nil
}
c.I2PConfig.CloseIdle = "false"
c.I2PConfig.CloseIdle = b
return nil
}
}
@ -364,10 +346,10 @@ func SetCloseIdle(b bool) func(*SAMEmit) error {
// SetCloseIdleTime sets the time to wait before closing tunnels to idle levels
func SetCloseIdleTime(u int) func(*SAMEmit) error {
return func(c *SAMEmit) error {
c.I2PConfig.TransportOptions.IdleTimeout = 300000
c.I2PConfig.TransportOptions.CloseIdleTimeout = 300000
if u >= 6 {
idleTime := (u * 60) * 1000
c.I2PConfig.TransportOptions.IdleTimeout = time.Duration(idleTime)
c.I2PConfig.TransportOptions.CloseIdleTimeout = time.Duration(idleTime)
log.WithFields(logrus.Fields{
"minutes": u,
"milliseconds": idleTime,
@ -382,9 +364,9 @@ func SetCloseIdleTime(u int) func(*SAMEmit) error {
// SetCloseIdleTimeMs sets the time to wait before closing tunnels to idle levels in milliseconds
func SetCloseIdleTimeMs(u int) func(*SAMEmit) error {
return func(c *SAMEmit) error {
c.I2PConfig.TransportOptions.IdleTimeout = 300000
c.I2PConfig.TransportOptions.CloseIdleTimeout = 300000
if u >= 300000 {
c.I2PConfig.TransportOptions.IdleTimeout = time.Duration(u)
c.I2PConfig.TransportOptions.CloseIdleTimeout = time.Duration(u)
log.WithField("closeIdleTimeMs", u).Debug("Set close idle time in milliseconds")
return nil
}

20
log.go
View File

@ -1,21 +1,5 @@
package sam3
import (
logger "github.com/go-i2p/logger"
)
import "github.com/go-i2p/sam3/common"
var log *logger.Logger
func InitializeSAM3Logger() {
logger.InitializeGoI2PLogger()
log = GetSAM3Logger()
}
// GetSAM3Logger returns the initialized logger
func GetSAM3Logger() *logger.Logger {
return logger.GetGoI2PLogger()
}
func init() {
InitializeSAM3Logger()
}
var log = common.GetSAM3Logger()

23
sam3.go
View File

@ -15,21 +15,22 @@ import (
"github.com/sirupsen/logrus"
"github.com/go-i2p/i2pkeys"
. "github.com/go-i2p/i2pkeys"
"github.com/go-i2p/sam3/common"
)
func init() {
InitializeSAM3Logger()
common.InitializeSAM3Logger()
}
// Used for controlling I2Ps SAMv3.
// This implements the "Control Socket" for all connections.
type SAM struct {
address string
conn net.Conn
keys *i2pkeys.I2PKeys
sigType int
address string
conn net.Conn
keys *i2pkeys.I2PKeys
sigType int
formatter *common.SAMFormatter
version common.Version
SAMEmit
*SAMResolver
}
@ -66,7 +67,11 @@ func RandString() string {
// Creates a new controller for the I2P routers SAM bridge.
func NewSAM(address string) (*SAM, error) {
log.WithField("address", address).Debug("Creating new SAM instance")
var s SAM
s := SAM{
address: address,
version: common.SAM31Version,
formatter: common.NewSAMFormatter(common.SAM31Version.String),
}
// TODO: clean this up
conn, err := net.Dial("tcp", address)
if err != nil {
@ -223,7 +228,7 @@ func (sam *SAM) NewKeys(sigType ...string) (i2pkeys.I2PKeys, error) {
}
}
log.Debug("Successfully generated new keys")
return NewKeys(I2PAddr(pub), priv), nil
return i2pkeys.NewKeys(i2pkeys.I2PAddr(pub), priv), nil
}
// Performs a lookup, probably this order: 1) routers known addresses, cached

View File

@ -6,6 +6,7 @@ import (
"os"
"strings"
"github.com/go-i2p/sam3/common"
"github.com/sirupsen/logrus"
)
@ -114,7 +115,7 @@ func PrimarySessionString() string {
var PrimarySessionSwitch string = PrimarySessionString()
func getEnv(key, fallback string) string {
InitializeSAM3Logger()
common.InitializeSAM3Logger()
value, ok := os.LookupEnv(key)
if !ok {
log.WithFields(logrus.Fields{