Files
sam-forwarder/config/tunconf.go

392 lines
12 KiB
Go
Raw Normal View History

package i2ptunconf
import (
2018-07-29 02:13:49 -04:00
"github.com/eyedeekay/sam-forwarder"
2018-07-30 20:36:33 -04:00
"github.com/eyedeekay/sam-forwarder/udp"
2018-07-29 00:52:36 -04:00
"github.com/zieckey/goini"
2018-07-30 02:44:36 -04:00
"log"
2018-07-29 00:52:36 -04:00
"strings"
)
2018-07-29 00:52:36 -04:00
type Conf struct {
config *goini.INI
2018-08-09 02:44:38 -04:00
SaveFile bool
2018-07-29 00:52:36 -04:00
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
2018-08-03 01:17:08 -04:00
closeIdle bool
closeIdleTime int
2018-08-09 02:44:38 -04:00
AccessListType string
AccessList []string
2018-07-29 00:52:36 -04:00
}
2018-07-30 02:44:36 -04:00
func (c *Conf) Print() {
log.Println(
2018-08-09 02:44:38 -04:00
"\n", c.SaveFile,
2018-07-30 03:30:21 -04:00
"\n", c.TargetHost,
"\n", c.TargetPort,
"\n", c.SamHost,
"\n", c.SamPort,
"\n", c.TunName,
"\n", c.encryptLeaseSet,
"\n", c.inAllowZeroHop,
"\n", c.outAllowZeroHop,
"\n", c.inLength,
"\n", c.outLength,
"\n", c.inQuantity,
"\n", c.outQuantity,
"\n", c.inVariance,
"\n", c.outVariance,
"\n", c.inBackupQuantity,
"\n", c.outBackupQuantity,
"\n", c.useCompression,
"\n", c.reduceIdle,
"\n", c.reduceIdleTime,
"\n", c.reduceIdleQuantity,
2018-08-09 02:44:38 -04:00
"\n", c.AccessListType,
"\n", c.AccessList,
2018-07-30 02:44:36 -04:00
)
2018-07-29 00:52:36 -04:00
}
// 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 (c *Conf) AddAccessListMember(key string) {
2018-08-09 02:44:38 -04:00
for _, item := range c.AccessList {
2018-08-03 01:17:08 -04:00
if item == key {
return
}
}
2018-08-09 02:44:38 -04:00
c.AccessList = append(c.AccessList, key)
}
2018-07-29 00:52:36 -04:00
func NewI2PTunConf(iniFile string) (*Conf, error) {
var err error
2018-07-29 02:13:49 -04:00
var c Conf
2018-07-30 02:44:36 -04:00
c.config = goini.New()
2018-07-29 00:52:36 -04:00
if iniFile != "none" {
2018-07-30 02:44:36 -04:00
err = c.config.ParseFile(iniFile)
2018-07-29 00:52:36 -04:00
if err != nil {
return nil, err
}
2018-07-30 03:25:06 -04:00
2018-07-30 03:51:35 -04:00
if _, ok := c.config.Get("keys"); ok {
2018-08-09 02:44:38 -04:00
c.SaveFile = true
2018-07-29 02:13:49 -04:00
} else {
2018-08-09 02:44:38 -04:00
c.SaveFile = false
2018-07-29 00:52:36 -04:00
}
2018-07-29 02:13:49 -04:00
2018-07-30 02:44:36 -04:00
if v, ok := c.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-30 02:44:36 -04:00
if v, ok := c.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:44:36 -04:00
if v, ok := c.config.Get("samhost"); ok {
2018-07-30 02:32:58 -04:00
c.SamHost = strings.Replace(v, ":", "", -1)
2018-07-30 02:17:52 -04:00
} else {
2018-07-30 02:32:58 -04:00
c.SamHost = "127.0.0.1"
2018-07-30 02:17:52 -04:00
}
2018-07-30 02:44:36 -04:00
if v, ok := c.config.Get("samport"); ok {
2018-07-30 02:32:58 -04:00
c.SamPort = strings.Replace(v, ":", "", -1)
2018-07-30 02:17:52 -04:00
} else {
2018-07-30 02:32:58 -04:00
c.SamPort = "7656"
2018-07-30 02:17:52 -04:00
}
2018-07-30 02:44:36 -04:00
if v, ok := c.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-30 02:44:36 -04:00
if v, ok := c.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
2018-07-30 02:44:36 -04:00
if v, ok := c.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-30 02:44:36 -04:00
if v, ok := c.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
2018-07-30 02:44:36 -04:00
if v, ok := c.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-30 02:44:36 -04:00
if v, ok := c.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
2018-07-30 02:44:36 -04:00
if v, ok := c.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-30 02:44:36 -04:00
if v, ok := c.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
2018-07-30 02:44:36 -04:00
if v, ok := c.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-30 02:44:36 -04:00
if v, ok := c.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
2018-07-30 02:44:36 -04:00
if v, ok := c.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-30 02:44:36 -04:00
if v, ok := c.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
2018-07-30 02:44:36 -04:00
if v, ok := c.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
2018-07-30 02:44:36 -04:00
if v, ok := c.config.GetBool("i2cp.reduceOnIdle"); ok {
2018-07-30 03:51:35 -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-30 02:44:36 -04:00
if v, ok := c.config.GetInt("i2cp.reduceIdleTime"); ok {
2018-07-29 02:13:49 -04:00
c.reduceIdleTime = (v / 1000) / 60
} else {
c.reduceIdleTime = (6 * 60) * 1000
2018-07-29 00:52:36 -04:00
}
2018-07-30 02:44:36 -04:00
if v, ok := c.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
2018-08-03 01:17:08 -04:00
if v, ok := c.config.GetBool("i2cp.closeOnIdle"); ok {
c.closeIdle = v
} else {
2018-08-03 01:19:20 -04:00
c.closeIdle = false
2018-08-03 01:17:08 -04:00
}
if v, ok := c.config.GetInt("i2cp.closeIdleTime"); ok {
c.closeIdleTime = (v / 2000) / 60
} else {
c.closeIdleTime = (6 * 60) * 2000
}
2018-07-30 02:44:36 -04:00
if v, ok := c.config.GetBool("i2cp.enableBlackList"); ok {
2018-07-29 00:52:36 -04:00
if v {
2018-08-09 02:44:38 -04:00
c.AccessListType = "blacklist"
2018-07-29 00:52:36 -04:00
}
}
2018-07-30 02:44:36 -04:00
if v, ok := c.config.GetBool("i2cp.enableAccessList"); ok {
2018-07-29 00:52:36 -04:00
if v {
2018-08-09 02:44:38 -04:00
c.AccessListType = "whitelist"
2018-07-29 00:52:36 -04:00
}
}
2018-08-09 02:44:38 -04:00
if c.AccessListType != "whitelist" && c.AccessListType != "blacklist" {
c.AccessListType = "none"
2018-07-30 02:36:03 -04:00
}
2018-08-09 02:44:38 -04:00
if v, ok := c.config.Get("i2cp.AccessList"); ok {
2018-07-29 00:52:36 -04:00
csv := strings.Split(v, ",")
for _, z := range csv {
2018-08-09 02:44:38 -04:00
c.AccessList = append(c.AccessList, z)
2018-07-29 00:52:36 -04:00
}
}
return &c, nil
}
return nil, nil
}
2018-07-29 02:13:49 -04:00
2018-08-02 22:35:41 -04:00
func NewSAMForwarderFromConf(config *Conf) (*samforwarder.SAMForwarder, error) {
if config != nil {
return samforwarder.NewSAMForwarderFromOptions(
2018-08-09 02:44:38 -04:00
samforwarder.SetSaveFile(config.SaveFile),
2018-08-02 22:35:41 -04:00
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),
2018-08-03 01:17:08 -04:00
samforwarder.SetCloseIdle(config.closeIdle),
samforwarder.SetCloseIdleTime(config.closeIdleTime),
2018-08-09 02:44:38 -04:00
samforwarder.SetAccessListType(config.AccessListType),
samforwarder.SetAccessList(config.AccessList),
2018-08-02 22:35:41 -04:00
)
}
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(
2018-08-09 02:44:38 -04:00
samforwarder.SetSaveFile(config.SaveFile),
2018-07-29 02:13:49 -04:00
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),
2018-08-03 01:17:08 -04:00
samforwarder.SetCloseIdle(config.closeIdle),
samforwarder.SetCloseIdleTime(config.closeIdleTime),
2018-08-09 02:44:38 -04:00
samforwarder.SetAccessListType(config.AccessListType),
samforwarder.SetAccessList(config.AccessList),
2018-07-29 02:13:49 -04:00
)
}
return nil, nil
}
2018-07-30 19:26:31 -04:00
func NewSAMSSUForwarderFromConfig(iniFile, SamHost, SamPort string) (*samforwarderudp.SAMSSUForwarder, error) {
if iniFile != "none" {
config, err := NewI2PTunConf(iniFile)
if err != nil {
return nil, err
}
return samforwarderudp.NewSAMSSUForwarderFromOptions(
2018-08-09 02:44:38 -04:00
samforwarderudp.SetSaveFile(config.SaveFile),
2018-07-30 19:26:31 -04:00
samforwarderudp.SetHost(config.TargetHost),
samforwarderudp.SetPort(config.TargetPort),
samforwarderudp.SetSAMHost(config.SamHost),
samforwarderudp.SetSAMPort(config.SamPort),
samforwarderudp.SetName(config.TunName),
2018-08-02 22:35:41 -04:00
samforwarderudp.SetInLength(config.inLength),
samforwarderudp.SetOutLength(config.outLength),
samforwarderudp.SetInVariance(config.inVariance),
samforwarderudp.SetOutVariance(config.outVariance),
samforwarderudp.SetInQuantity(config.inQuantity),
samforwarderudp.SetOutQuantity(config.outQuantity),
samforwarderudp.SetInBackups(config.inBackupQuantity),
samforwarderudp.SetOutBackups(config.outBackupQuantity),
samforwarderudp.SetEncrypt(config.encryptLeaseSet),
samforwarderudp.SetAllowZeroIn(config.inAllowZeroHop),
samforwarderudp.SetAllowZeroOut(config.outAllowZeroHop),
samforwarderudp.SetCompress(config.useCompression),
samforwarderudp.SetReduceIdle(config.reduceIdle),
samforwarderudp.SetReduceIdleTime(config.reduceIdleTime),
samforwarderudp.SetReduceIdleQuantity(config.reduceIdleQuantity),
2018-08-03 01:17:08 -04:00
samforwarderudp.SetCloseIdle(config.closeIdle),
samforwarderudp.SetCloseIdleTime(config.closeIdleTime),
2018-08-09 02:44:38 -04:00
samforwarderudp.SetAccessListType(config.AccessListType),
samforwarderudp.SetAccessList(config.AccessList),
2018-08-02 22:35:41 -04:00
)
}
return nil, nil
}
func NewSAMSSUForwarderFromConf(config *Conf) (*samforwarderudp.SAMSSUForwarder, error) {
if config != nil {
return samforwarderudp.NewSAMSSUForwarderFromOptions(
2018-08-09 02:44:38 -04:00
samforwarderudp.SetSaveFile(config.SaveFile),
2018-08-02 22:35:41 -04:00
samforwarderudp.SetHost(config.TargetHost),
samforwarderudp.SetPort(config.TargetPort),
samforwarderudp.SetSAMHost(config.SamHost),
samforwarderudp.SetSAMPort(config.SamPort),
samforwarderudp.SetName(config.TunName),
2018-07-30 19:26:31 -04:00
samforwarderudp.SetInLength(config.inLength),
samforwarderudp.SetOutLength(config.outLength),
samforwarderudp.SetInVariance(config.inVariance),
samforwarderudp.SetOutVariance(config.outVariance),
samforwarderudp.SetInQuantity(config.inQuantity),
samforwarderudp.SetOutQuantity(config.outQuantity),
samforwarderudp.SetInBackups(config.inBackupQuantity),
samforwarderudp.SetOutBackups(config.outBackupQuantity),
samforwarderudp.SetEncrypt(config.encryptLeaseSet),
samforwarderudp.SetAllowZeroIn(config.inAllowZeroHop),
samforwarderudp.SetAllowZeroOut(config.outAllowZeroHop),
samforwarderudp.SetCompress(config.useCompression),
samforwarderudp.SetReduceIdle(config.reduceIdle),
samforwarderudp.SetReduceIdleTime(config.reduceIdleTime),
samforwarderudp.SetReduceIdleQuantity(config.reduceIdleQuantity),
2018-08-03 01:17:08 -04:00
samforwarderudp.SetCloseIdle(config.closeIdle),
samforwarderudp.SetCloseIdleTime(config.closeIdleTime),
2018-08-09 02:44:38 -04:00
samforwarderudp.SetAccessListType(config.AccessListType),
samforwarderudp.SetAccessList(config.AccessList),
2018-07-30 19:26:31 -04:00
)
}
return nil, nil
}