Files
sam3/config.go

292 lines
8.6 KiB
Go
Raw Normal View History

2015-12-14 09:00:11 -05:00
package sam3
import (
2019-12-07 22:10:55 -05:00
"math/rand"
2019-12-07 22:09:40 -05:00
"strings"
"time"
"github.com/sirupsen/logrus"
2024-11-09 11:54:54 -05:00
"github.com/go-i2p/i2pkeys"
"github.com/go-i2p/sam3/common"
"github.com/go-i2p/sam3/config"
2015-12-14 09:00:11 -05:00
)
const DEFAULT_LEASESET_TYPE = "i2cp.leaseSetEncType=4"
2019-12-07 22:09:40 -05:00
// I2PConfig is a struct which manages I2P configuration options
type I2PConfig struct {
common.SAMFormatter
config.SessionOptions
config.TransportOptions
config.TunnelOptions
config.EncryptedLeaseSetOptions
2019-12-07 22:10:55 -05:00
DestinationKeys i2pkeys.I2PKeys
2019-12-07 22:09:40 -05:00
// Streaming Library options
2019-12-07 22:09:40 -05:00
AccessListType string
AccessList []string
}
2024-11-22 20:35:39 -05:00
// Sam returns the SAM address in the form of "host:port"
2019-12-07 22:09:40 -05:00
func (f *I2PConfig) Sam() string {
host := "127.0.0.1"
port := "7656"
if f.SamHost != "" {
host = f.SamHost
}
if f.SamPort != "" {
port = f.SamPort
}
2024-10-15 12:23:24 -04:00
log.WithFields(logrus.Fields{
"host": host,
"port": port,
}).Debug("SAM address constructed")
2019-12-07 22:09:40 -05:00
return host + ":" + port
}
2024-11-22 20:35:39 -05:00
// SetSAMAddress sets the SAM address from a string in the form of "host:port"
2019-12-07 22:09:40 -05:00
func (f *I2PConfig) SetSAMAddress(addr string) {
hp := strings.Split(addr, ":")
if len(hp) == 1 {
f.SamHost = hp[0]
} else if len(hp) == 2 {
f.SamPort = hp[1]
f.SamHost = hp[0]
}
f.SamPort = "7656"
f.SamHost = "127.0.0.1"
2024-10-15 12:23:24 -04:00
log.WithFields(logrus.Fields{
"host": f.SamHost,
"port": f.SamPort,
}).Debug("SAM address set")
i2pkeys.DefaultSAMAddress = f.Sam()
2019-12-07 22:09:40 -05:00
}
2024-11-22 20:35:39 -05:00
// ID returns the tunnel name in the form of "ID=name"
2019-12-07 22:09:40 -05:00
func (f *I2PConfig) ID() string {
if f.NickName == "" {
2019-12-07 22:09:40 -05:00
b := make([]byte, 12)
2019-12-07 22:10:55 -05:00
for i := range b {
b[i] = "abcdefghijklmnopqrstuvwxyz"[rand.Intn(len("abcdefghijklmnopqrstuvwxyz"))]
}
f.NickName = string(b)
log.WithField("NickName", f.NickName).Debug("Generated random tunnel name")
2019-12-07 22:09:40 -05:00
}
return " ID=" + f.NickName + " "
2019-12-07 22:09:40 -05:00
}
2024-11-22 20:35:39 -05:00
// Leasesetsettings returns the lease set settings in the form of "i2cp.leaseSetKey=key i2cp.leaseSetPrivateKey=key i2cp.leaseSetPrivateSigningKey=key"
2019-12-07 22:09:40 -05:00
func (f *I2PConfig) Leasesetsettings() (string, string, string) {
var r, s, t string
if f.LeaseSetKey != "" {
r = " i2cp.leaseSetKey=" + f.LeaseSetKey + " "
}
if f.LeaseSetPrivateKey != "" {
s = " i2cp.leaseSetPrivateKey=" + f.LeaseSetPrivateKey + " "
}
if f.LeaseSetPrivateSigningKey != "" {
t = " i2cp.leaseSetPrivateSigningKey=" + f.LeaseSetPrivateSigningKey + " "
}
2024-10-15 12:23:24 -04:00
log.WithFields(logrus.Fields{
"leaseSetKey": r,
"leaseSetPrivateKey": s,
"leaseSetPrivateSigningKey": t,
}).Debug("Lease set settings constructed")
2019-12-07 22:09:40 -05:00
return r, s, t
}
2024-11-22 20:35:39 -05:00
// SessionStyle returns the session style setting in the form of "STYLE=style"
2019-12-07 22:09:40 -05:00
func (f *I2PConfig) SessionStyle() string {
if f.SessionOptions.Style != "" {
log.WithField("style", f.SessionOptions.Style).Debug("Session style set")
return " STYLE=" + f.SessionOptions.Style + " "
2019-12-07 22:10:55 -05:00
}
2024-10-15 12:23:24 -04:00
log.Debug("Using default STREAM style")
2019-12-07 22:10:55 -05:00
return " STYLE=STREAM "
2019-12-07 22:09:40 -05:00
}
2024-11-22 20:35:39 -05:00
// MinSAM returns the minimum SAM version required in major.minor form
2019-12-07 22:09:40 -05:00
func (f *I2PConfig) MinSAM() string {
min, _ := f.GetVersions()
return string(min)
2019-12-07 22:09:40 -05:00
}
2024-11-22 20:35:39 -05:00
// MaxSAM returns the maximum SAM version required in major.minor form
2019-12-07 22:09:40 -05:00
func (f *I2PConfig) MaxSAM() string {
_, 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)
}
2019-12-07 22:09:40 -05:00
if f.SamMax == "" {
max = common.SAM33Version.String
log.Debug("Using default MaxSAM: 3.3")
} else {
max = common.ProtocolVersion(f.SamMax)
2019-12-07 22:09:40 -05:00
}
return min, max
2019-12-07 22:09:40 -05:00
}
2024-11-22 20:35:39 -05:00
// DestinationKey returns the destination key setting in the form of "DESTINATION=key"
2019-12-07 22:09:40 -05:00
func (f *I2PConfig) DestinationKey() string {
if &f.DestinationKeys != nil {
2024-10-15 12:23:24 -04:00
log.WithField("destinationKey", f.DestinationKeys.String()).Debug("Destination key set")
2019-12-07 22:09:40 -05:00
return " DESTINATION=" + f.DestinationKeys.String() + " "
}
2024-10-15 12:23:24 -04:00
log.Debug("Using TRANSIENT destination")
2019-12-07 22:09:40 -05:00
return " DESTINATION=TRANSIENT "
}
2024-11-22 20:35:39 -05:00
// Reliability returns the message reliability setting in the form of "i2cp.messageReliability=reliability"
2019-12-07 22:09:40 -05:00
func (f *I2PConfig) Reliability() string {
if f.TransportOptions.Reliability != "" {
log.WithField("reliability", f.TransportOptions.Reliability).Debug("Message reliability set")
return " i2cp.messageReliability=" + f.TransportOptions.Reliability + " "
2019-12-07 22:10:55 -05:00
}
2024-10-15 12:23:24 -04:00
log.Debug("Message reliability not set")
2019-12-07 22:10:55 -05:00
return ""
2019-12-07 22:09:40 -05:00
}
2024-11-22 20:35:39 -05:00
// Reduce returns the reduce idle settings in the form of "i2cp.reduceOnIdle=true i2cp.reduceIdleTime=time i2cp.reduceQuantity=quantity"
2019-12-07 22:09:40 -05:00
func (f *I2PConfig) Reduce() string {
if f.ReduceIdle {
2024-10-15 12:23:24 -04:00
log.WithFields(logrus.Fields{
"reduceIdle": f.ReduceIdle,
"reduceIdleTime": f.TransportOptions.ReduceIdleTimeout.String(),
"reduceIdleQuantity": f.TransportOptions.ReduceIdleQuantity,
2024-10-15 12:23:24 -04:00
}).Debug("Reduce idle settings applied")
return "i2cp.reduceOnIdle=" + f.ReduceOnIdle() + "i2cp.reduceIdleTime=" + f.TransportOptions.ReduceIdleTimeout.String() + "i2cp.reduceQuantity=" + f.ReduceQuantity()
2019-12-07 22:10:55 -05:00
}
2024-10-15 12:23:24 -04:00
log.Debug("Reduce idle settings not applied")
2019-12-07 22:10:55 -05:00
return ""
2019-12-07 22:09:40 -05:00
}
2024-11-22 20:35:39 -05:00
// Close returns the close idle settings in the form of "i2cp.closeOnIdle=true i2cp.closeIdleTime=time"
2019-12-07 22:09:40 -05:00
func (f *I2PConfig) Close() string {
if f.CloseIdle {
2024-10-15 12:23:24 -04:00
log.WithFields(logrus.Fields{
"closeIdle": f.CloseIdle,
"closeIdleTime": f.TransportOptions.CloseIdleTimeout.String(),
2024-10-15 12:23:24 -04:00
}).Debug("Close idle settings applied")
return "i2cp.closeOnIdle=" + f.CloseOnIdle() + "i2cp.closeIdleTime=" + f.TransportOptions.CloseIdleTimeout.String()
2019-12-07 22:10:55 -05:00
}
2024-10-15 12:23:24 -04:00
log.Debug("Close idle settings not applied")
2019-12-07 22:10:55 -05:00
return ""
2019-12-07 22:09:40 -05:00
}
2024-11-22 20:35:39 -05:00
// Print returns the full config as a string
2019-12-07 22:09:40 -05:00
func (f *I2PConfig) Print() []string {
lsk, lspk, lspsk := f.Leasesetsettings()
return []string{
// f.targetForPort443(),
"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(),
2019-12-07 22:09:40 -05:00
f.DoZero(),
//"i2cp.fastRecieve=" + f.FastRecieve,
"i2cp.gzip=" + f.TransportOptions.UseCompression,
2019-12-07 22:10:55 -05:00
f.Reduce(),
f.Close(),
2019-12-07 22:09:40 -05:00
f.Reliability(),
f.EncryptLease(),
lsk, lspk, lspsk,
f.Accesslisttype(),
f.Accesslist(),
2024-01-07 12:09:13 -05:00
f.LeaseSetEncryptionType(),
2019-12-07 22:09:40 -05:00
}
}
2024-11-22 20:35:39 -05:00
// Accesslisttype returns the access list type
2019-12-07 22:09:40 -05:00
func (f *I2PConfig) Accesslisttype() string {
if f.AccessListType == "whitelist" {
2024-10-15 12:23:24 -04:00
log.Debug("Access list type set to whitelist")
2019-12-07 22:09:40 -05:00
return "i2cp.enableAccessList=true"
} else if f.AccessListType == "blacklist" {
2024-10-15 12:23:24 -04:00
log.Debug("Access list type set to blacklist")
2019-12-07 22:09:40 -05:00
return "i2cp.enableBlackList=true"
} else if f.AccessListType == "none" {
2024-10-15 12:23:24 -04:00
log.Debug("Access list type set to none")
2019-12-07 22:09:40 -05:00
return ""
}
2024-10-15 12:23:24 -04:00
log.Debug("Access list type not set")
2019-12-07 22:09:40 -05:00
return ""
}
2024-11-22 20:35:39 -05:00
// Accesslist returns the access list in the form of "i2cp.accessList=list"
2019-12-07 22:09:40 -05:00
func (f *I2PConfig) Accesslist() string {
if f.AccessListType != "" && len(f.AccessList) > 0 {
r := ""
for _, s := range f.AccessList {
r += s + ","
}
2024-10-15 12:23:24 -04:00
log.WithField("accessList", r).Debug("Access list generated")
2019-12-07 22:09:40 -05:00
return "i2cp.accessList=" + strings.TrimSuffix(r, ",")
}
2024-10-15 12:23:24 -04:00
log.Debug("Access list not set")
2019-12-07 22:09:40 -05:00
return ""
}
2024-11-22 20:35:39 -05:00
// NewConfig returns a new config with default values or updates them with functional arguments
2019-12-07 22:09:40 -05:00
func NewConfig(opts ...func(*I2PConfig) error) (*I2PConfig, error) {
config := I2PConfig{
EncryptedLeaseSetOptions: config.EncryptedLeaseSetOptions{
EncryptLeaseSet: false,
LeaseSetKey: "",
LeaseSetPrivateKey: "",
LeaseSetPrivateSigningKey: "",
LeaseSetEncryption: DEFAULT_LEASESET_TYPE,
},
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,
},
}
2019-12-07 22:09:40 -05:00
for _, o := range opts {
if err := o(&config); err != nil {
return nil, err
}
}
return &config, nil
}