Files
sam-forwarder/config/tunconf.go

241 lines
5.8 KiB
Go
Raw Normal View History

package i2ptunconf
import (
2018-07-29 02:13:49 -04:00
"github.com/eyedeekay/sam-forwarder"
2018-07-29 00:52:36 -04:00
"github.com/zieckey/goini"
"strings"
)
2018-07-29 00:52:36 -04:00
type Conf struct {
config *goini.INI
saveFile bool
TargetHost string
TargetPort string
2018-07-29 02:13:49 -04:00
SamHost string
SamPort string
2018-07-29 00:52:36 -04:00
TunName string
encryptLeaseSet bool
inAllowZeroHop bool
outAllowZeroHop bool
inLength int
outLength int
inQuantity int
outQuantity int
inVariance int
outVariance int
inBackupQuantity int
outBackupQuantity int
useCompression bool
reduceIdle bool
reduceIdleTime int
reduceIdleQuantity int
accessListType string
accessList []string
}
2018-07-29 00:52:36 -04:00
func (c *Conf) configParse(path string) (*goini.INI, error) {
ini := goini.New()
if err := ini.ParseFile(path); err != nil {
return nil, err
}
return ini, nil
}
// Get passes directly through to goini.Get
2018-07-29 00:52:36 -04:00
func (c *Conf) Get(key string) (string, bool) {
return c.config.Get(key)
}
// GetBool passes directly through to goini.GetBool
2018-07-29 00:52:36 -04:00
func (c *Conf) GetBool(key string) (bool, bool) {
return c.config.GetBool(key)
}
// GetInt passes directly through to goini.GetInt
2018-07-29 00:52:36 -04:00
func (c *Conf) GetInt(key string) (int, bool) {
return c.config.GetInt(key)
}
func NewI2PTunConf(iniFile string) (*Conf, error) {
2018-07-29 02:13:49 -04:00
var config *goini.INI
2018-07-29 00:52:36 -04:00
var err error
2018-07-29 02:13:49 -04:00
var c Conf
2018-07-29 00:52:36 -04:00
if iniFile != "none" {
2018-07-29 02:13:49 -04:00
config, err = c.configParse(iniFile)
2018-07-29 00:52:36 -04:00
if err != nil {
return nil, err
}
2018-07-30 02:17:52 -04:00
if v, ok := config.GetBool("keys"); ok {
2018-07-29 00:52:36 -04:00
c.saveFile = v
2018-07-29 02:13:49 -04:00
} else {
c.saveFile = false
2018-07-29 00:52:36 -04:00
}
2018-07-29 02:13:49 -04:00
if v, ok := config.Get("host"); ok {
2018-07-30 02:17:52 -04:00
c.TargetHost = strings.Replace(v, ":", "", -1)
2018-07-29 02:13:49 -04:00
} else {
c.TargetHost = "127.0.0.1"
2018-07-29 00:52:36 -04:00
}
2018-07-29 02:13:49 -04:00
if v, ok := config.Get("port"); ok {
2018-07-30 02:17:52 -04:00
c.TargetPort = strings.Replace(v, ":", "", -1)
2018-07-29 02:13:49 -04:00
} else {
c.TargetPort = "8081"
2018-07-29 00:52:36 -04:00
}
2018-07-29 02:13:49 -04:00
2018-07-30 02:17:52 -04:00
if v, ok := config.Get("samhost"); ok {
c.TargetHost = strings.Replace(v, ":", "", -1)
} else {
c.TargetHost = "127.0.0.1"
}
if v, ok := config.Get("samport"); ok {
c.TargetPort = strings.Replace(v, ":", "", -1)
} else {
c.TargetPort = "7656"
}
2018-07-29 02:13:49 -04:00
if v, ok := config.Get("keys"); ok {
2018-07-29 00:52:36 -04:00
c.TunName = v
2018-07-29 02:13:49 -04:00
} else {
c.TunName = "fowarder"
2018-07-29 00:52:36 -04:00
}
2018-07-29 02:13:49 -04:00
if v, ok := config.GetBool("i2cp.encryptLeaseSet"); ok {
2018-07-29 00:52:36 -04:00
c.encryptLeaseSet = v
2018-07-29 02:13:49 -04:00
} else {
c.encryptLeaseSet = false
2018-07-29 00:52:36 -04:00
}
2018-07-29 02:13:49 -04:00
if v, ok := config.GetBool("inbound.allowZeroHop"); ok {
2018-07-29 00:52:36 -04:00
c.inAllowZeroHop = v
2018-07-29 02:13:49 -04:00
} else {
c.inAllowZeroHop = false
2018-07-29 00:52:36 -04:00
}
2018-07-29 02:13:49 -04:00
if v, ok := config.GetBool("outbound.allowZeroHop"); ok {
2018-07-29 00:52:36 -04:00
c.outAllowZeroHop = v
2018-07-29 02:13:49 -04:00
} else {
c.outAllowZeroHop = false
2018-07-29 00:52:36 -04:00
}
2018-07-29 02:13:49 -04:00
if v, ok := config.GetInt("inbound.length"); ok {
2018-07-29 00:52:36 -04:00
c.inLength = v
2018-07-29 02:13:49 -04:00
} else {
c.inLength = 3
2018-07-29 00:52:36 -04:00
}
2018-07-29 02:13:49 -04:00
if v, ok := config.GetInt("outbound.length"); ok {
2018-07-29 00:52:36 -04:00
c.outLength = v
2018-07-29 02:13:49 -04:00
} else {
c.outLength = 3
2018-07-29 00:52:36 -04:00
}
2018-07-29 02:13:49 -04:00
if v, ok := config.GetInt("inbound.quantity"); ok {
2018-07-29 00:52:36 -04:00
c.inQuantity = v
2018-07-29 02:13:49 -04:00
} else {
c.inQuantity = 5
2018-07-29 00:52:36 -04:00
}
2018-07-29 02:13:49 -04:00
if v, ok := config.GetInt("outbound.quantity"); ok {
2018-07-29 00:52:36 -04:00
c.outQuantity = v
2018-07-29 02:13:49 -04:00
} else {
c.outQuantity = 5
2018-07-29 00:52:36 -04:00
}
2018-07-29 02:13:49 -04:00
if v, ok := config.GetInt("inbound.variance"); ok {
2018-07-29 00:52:36 -04:00
c.inVariance = v
2018-07-29 02:13:49 -04:00
} else {
c.inVariance = 0
2018-07-29 00:52:36 -04:00
}
2018-07-29 02:13:49 -04:00
if v, ok := config.GetInt("outbound.variance"); ok {
2018-07-29 00:52:36 -04:00
c.outVariance = v
2018-07-29 02:13:49 -04:00
} else {
c.outVariance = 0
2018-07-29 00:52:36 -04:00
}
2018-07-29 02:13:49 -04:00
if v, ok := config.GetInt("inbound.backupQuantity"); ok {
2018-07-29 00:52:36 -04:00
c.inBackupQuantity = v
2018-07-29 02:13:49 -04:00
} else {
c.inBackupQuantity = 2
2018-07-29 00:52:36 -04:00
}
2018-07-29 02:13:49 -04:00
if v, ok := config.GetInt("outbound.backupQuantity"); ok {
2018-07-29 00:52:36 -04:00
c.outBackupQuantity = v
2018-07-29 02:13:49 -04:00
} else {
c.outBackupQuantity = 2
2018-07-29 00:52:36 -04:00
}
2018-07-29 02:13:49 -04:00
if v, ok := config.GetBool("gzip"); ok {
2018-07-29 00:52:36 -04:00
c.useCompression = v
2018-07-29 02:13:49 -04:00
} else {
c.useCompression = true
2018-07-29 00:52:36 -04:00
}
2018-07-29 02:13:49 -04:00
if v, ok := config.GetBool("i2cp.reduceOnIdle"); ok {
2018-07-29 00:52:36 -04:00
c.reduceIdle = v
2018-07-29 02:13:49 -04:00
} else {
c.reduceIdle = false
2018-07-29 00:52:36 -04:00
}
2018-07-29 02:13:49 -04:00
if v, ok := config.GetInt("i2cp.reduceIdleTime"); ok {
c.reduceIdleTime = (v / 1000) / 60
} else {
c.reduceIdleTime = (6 * 60) * 1000
2018-07-29 00:52:36 -04:00
}
2018-07-29 02:13:49 -04:00
if v, ok := config.GetInt("i2cp.reduceQuantity"); ok {
2018-07-29 00:52:36 -04:00
c.reduceIdleQuantity = v
2018-07-29 02:13:49 -04:00
} else {
c.reduceIdleQuantity = 3
2018-07-29 00:52:36 -04:00
}
2018-07-29 02:13:49 -04:00
if v, ok := config.GetBool("i2cp.enableBlackList"); ok {
2018-07-29 00:52:36 -04:00
if v {
c.accessListType = "blacklist"
}
}
2018-07-29 02:13:49 -04:00
if v, ok := config.GetBool("i2cp.enableAccessList"); ok {
2018-07-29 00:52:36 -04:00
if v {
c.accessListType = "whitelist"
}
}
2018-07-29 02:13:49 -04:00
if v, ok := config.Get("i2cp.accessList"); ok {
2018-07-29 00:52:36 -04:00
csv := strings.Split(v, ",")
for _, z := range csv {
c.accessList = append(c.accessList, z)
}
}
2018-07-29 02:13:49 -04:00
2018-07-29 00:52:36 -04:00
return &c, nil
}
return nil, nil
}
2018-07-29 02:13:49 -04:00
func NewSAMForwarderFromConfig(iniFile, SamHost, SamPort string) (*samforwarder.SAMForwarder, error) {
if iniFile != "none" {
config, err := NewI2PTunConf(iniFile)
if err != nil {
return nil, err
}
return samforwarder.NewSAMForwarderFromOptions(
samforwarder.SetSaveFile(config.saveFile),
samforwarder.SetHost(config.TargetHost),
samforwarder.SetPort(config.TargetPort),
samforwarder.SetSAMHost(config.SamHost),
samforwarder.SetSAMPort(config.SamPort),
samforwarder.SetName(config.TunName),
samforwarder.SetInLength(config.inLength),
samforwarder.SetOutLength(config.outLength),
samforwarder.SetInVariance(config.inVariance),
samforwarder.SetOutVariance(config.outVariance),
samforwarder.SetInQuantity(config.inQuantity),
samforwarder.SetOutQuantity(config.outQuantity),
samforwarder.SetInBackups(config.inBackupQuantity),
samforwarder.SetOutBackups(config.outBackupQuantity),
samforwarder.SetEncrypt(config.encryptLeaseSet),
samforwarder.SetAllowZeroIn(config.inAllowZeroHop),
samforwarder.SetAllowZeroOut(config.outAllowZeroHop),
samforwarder.SetCompress(config.useCompression),
samforwarder.SetReduceIdle(config.reduceIdle),
samforwarder.SetReduceIdleTime(config.reduceIdleTime),
samforwarder.SetReduceIdleQuantity(config.reduceIdleQuantity),
samforwarder.SetAccessListType(config.accessListType),
samforwarder.SetAccessList(config.accessList),
)
}
return nil, nil
}