Files
sam-forwarder/config/tunconf.go

302 lines
8.0 KiB
Go
Raw Normal View History

package i2ptunconf
import (
"io/ioutil"
2018-07-30 02:44:36 -04:00
"log"
"os"
"path/filepath"
2018-08-09 15:19:50 -04:00
"strconv"
2018-07-29 00:52:36 -04:00
"strings"
)
2018-08-16 15:27:29 -04:00
import (
"github.com/zieckey/goini"
)
// Conf is a tructure containing an ini config, with some functions to help
// when you use it for in conjunction with command-line flags
2018-07-29 00:52:36 -04:00
type Conf struct {
2018-11-29 19:05:19 -05:00
Config *goini.INI
FilePath string
KeyFilePath string
Labels []string
Client bool
ClientDest string
2019-04-25 22:58:49 -04:00
SigType string
Type string
SaveDirectory string
SaveFile bool
TargetHost string
TargetPort string
SamHost string
SamPort string
TunnelHost string
ControlHost string
ControlPort string
TargetForPort443 string
TunName string
EncryptLeaseSet bool
LeaseSetKey string
LeaseSetPrivateKey string
LeaseSetPrivateSigningKey string
InAllowZeroHop bool
OutAllowZeroHop bool
InLength int
OutLength int
InQuantity int
OutQuantity int
InVariance int
OutVariance int
InBackupQuantity int
OutBackupQuantity int
UseCompression bool
FastRecieve bool
ReduceIdle bool
ReduceIdleTime int
ReduceIdleQuantity int
CloseIdle bool
CloseIdleTime int
AccessListType string
AccessList []string
2018-09-14 00:16:22 -04:00
MessageReliability string
2018-09-18 23:24:48 -04:00
exists bool
2019-05-11 00:26:24 -04:00
UserName string
Password string
2018-07-29 00:52:36 -04:00
}
2018-08-09 15:19:07 -04:00
// Print returns and prints a formatted list of configured tunnel settings.
func (c *Conf) Print() []string {
2018-08-09 15:19:50 -04:00
confstring := []string{
2019-04-26 00:27:43 -04:00
c.SignatureType(),
2018-08-09 15:19:50 -04:00
"inbound.length=" + strconv.Itoa(c.InLength),
"outbound.length=" + strconv.Itoa(c.OutLength),
"inbound.lengthVariance=" + strconv.Itoa(c.InVariance),
"outbound.lengthVariance=" + strconv.Itoa(c.OutVariance),
"inbound.backupQuantity=" + strconv.Itoa(c.InBackupQuantity),
"outbound.backupQuantity=" + strconv.Itoa(c.OutBackupQuantity),
"inbound.quantity=" + strconv.Itoa(c.InQuantity),
"outbound.quantity=" + strconv.Itoa(c.OutQuantity),
"inbound.allowZeroHop=" + strconv.FormatBool(c.InAllowZeroHop),
"outbound.allowZeroHop=" + strconv.FormatBool(c.OutAllowZeroHop),
"i2cp.encryptLeaseSet=" + strconv.FormatBool(c.EncryptLeaseSet),
"i2cp.gzip=" + strconv.FormatBool(c.UseCompression),
"i2cp.reduceOnIdle=" + strconv.FormatBool(c.ReduceIdle),
"i2cp.reduceIdleTime=" + strconv.Itoa(c.ReduceIdleTime),
"i2cp.reduceQuantity=" + strconv.Itoa(c.ReduceIdleQuantity),
"i2cp.closeOnIdle=" + strconv.FormatBool(c.CloseIdle),
"i2cp.closeIdleTime=" + strconv.Itoa(c.CloseIdleTime),
2019-04-26 00:27:43 -04:00
"i2cp.fastRecieve=" + strconv.FormatBool(c.FastRecieve),
c.reliability(),
2018-08-09 15:19:50 -04:00
c.accesslisttype(),
c.accesslist(),
2019-04-26 00:27:43 -04:00
c.lsk(),
c.lspk(),
c.lsspk(),
2018-08-09 15:19:50 -04:00
}
2018-08-09 15:19:07 -04:00
log.Println(confstring)
2018-08-09 15:19:50 -04:00
return confstring
2018-07-29 00:52:36 -04:00
}
2019-04-26 00:27:43 -04:00
func (c *Conf) lsk() string {
if c.LeaseSetKey != "" {
return "i2cp.leaseSetKey=" + c.LeaseSetKey
}
return ""
}
func (c *Conf) lspk() string {
if c.LeaseSetPrivateKey != "" {
return "i2cp.leaseSetPrivateKey=" + c.LeaseSetPrivateKey
}
return ""
}
func (c *Conf) lsspk() string {
if c.LeaseSetPrivateSigningKey != "" {
return "i2cp.leaseSetSigningPrivateKey=" + c.LeaseSetPrivateSigningKey
}
return ""
}
2019-04-26 00:00:19 -04:00
func (c *Conf) SignatureType() string {
2019-04-26 00:27:43 -04:00
if c.SigType == "" {
return ""
}
return "SIGNATURE_TYPE=" + c.SigType
2019-04-26 00:00:19 -04:00
}
// Get passes directly through to goini.Get
2018-09-12 14:35:59 -04:00
func (c *Conf) Get(key string, label ...string) (string, bool) {
if len(c.Labels) > 0 {
if len(label) > 0 {
2018-11-29 19:05:19 -05:00
return c.Config.SectionGet(label[0], key)
2018-09-12 18:23:18 -04:00
}
2018-11-29 19:05:19 -05:00
return c.Config.SectionGet(c.Labels[0], key)
} else {
2018-11-29 19:05:19 -05:00
if c.Config != nil {
return c.Config.Get(key)
} else {
return "", false
}
}
return "", false
2018-07-29 00:52:36 -04:00
}
// GetBool passes directly through to goini.GetBool
2018-09-12 14:35:59 -04:00
func (c *Conf) GetBool(key string, label ...string) (bool, bool) {
if len(c.Labels) > 0 {
if len(label) > 0 {
2018-11-29 19:05:19 -05:00
return c.Config.SectionGetBool(label[0], key)
2018-09-12 18:23:18 -04:00
}
2018-11-29 19:05:19 -05:00
return c.Config.SectionGetBool(c.Labels[0], key)
} else {
2018-11-29 19:05:19 -05:00
if c.Config != nil {
return c.Config.GetBool(key)
} else {
return false, false
}
}
return false, false
2018-07-29 00:52:36 -04:00
}
// GetInt passes directly through to goini.GetInt
2018-09-12 14:35:59 -04:00
func (c *Conf) GetInt(key string, label ...string) (int, bool) {
if len(c.Labels) > 0 {
if len(label) > 0 {
2018-11-29 19:05:19 -05:00
return c.Config.SectionGetInt(label[0], key)
2018-09-12 18:23:18 -04:00
}
2018-11-29 19:05:19 -05:00
return c.Config.SectionGetInt(c.Labels[0], key)
} else {
2018-11-29 19:05:19 -05:00
if c.Config != nil {
return c.Config.GetInt(key)
} else {
return -1, false
}
}
return -1, false
2018-07-29 00:52:36 -04:00
}
func (c *Conf) Write() error {
if file, err := os.Open(filepath.Join(c.FilePath, c.TunName+".ini")); err != nil {
defer file.Close()
return err
} else {
defer file.Close()
2018-11-29 19:05:19 -05:00
return c.Config.Write(file)
}
}
/*
// Get takes an argument and a default. If the argument differs from the
// default, the argument is always returned. If the argument and default are
// the same and the key exists, the key is returned. If the key is absent, the
// default is returned.
2018-09-12 14:35:59 -04:00
func (c *Conf) Get(arg, def int, label ...string) int {
return 0
}
*/
// SetLabels
func (c *Conf) SetLabels(iniFile string) {
if tempfile, temperr := ioutil.ReadFile(iniFile); temperr == nil {
tempstring := string(tempfile)
tempslice := strings.Split(tempstring, "\n")
for _, element := range tempslice {
trimmedelement := strings.Trim(element, "\t\n ")
if strings.HasPrefix(trimmedelement, "[") && strings.HasSuffix(trimmedelement, "]") {
c.Labels = append(c.Labels, strings.Trim(trimmedelement, "[]"))
}
}
}
log.Println("Found Labels:", c.Labels)
}
2018-08-16 13:46:22 -04:00
/*
// Set
func (c *Conf) Set(label ...string) {
2018-08-16 13:46:22 -04:00
}
*/
// I2PINILoad loads variables from an ini file into the Conf data structure.
2018-09-12 14:35:59 -04:00
func (c *Conf) I2PINILoad(iniFile string, label ...string) error {
2018-08-16 13:46:22 -04:00
var err error
2018-09-18 23:34:35 -04:00
c.exists = true
2018-11-29 19:05:19 -05:00
c.Config = goini.New()
2018-09-12 19:44:05 -04:00
if iniFile != "none" && iniFile != "" {
c.FilePath = iniFile
2018-11-29 19:05:19 -05:00
err = c.Config.ParseFile(iniFile)
2018-08-16 13:46:22 -04:00
if err != nil {
return err
2018-07-30 02:36:03 -04:00
}
c.SetLabels(iniFile)
2018-09-12 14:35:59 -04:00
c.SetDir(label...)
c.SetType(label...)
c.SetKeys(label...)
c.SetHost(label...)
c.SetPort(label...)
c.SetSAMHost(label...)
c.SetSAMPort(label...)
c.SetEndpointHost(label...)
2018-09-12 14:35:59 -04:00
c.SetTunName(label...)
2019-04-26 00:27:43 -04:00
c.SetSigType(label...)
2018-09-12 14:35:59 -04:00
c.SetEncryptLease(label...)
c.SetLeasesetKey(label...)
c.SetLeasesetPrivateKey(label...)
c.SetLeasesetPrivateSigningKey(label...)
2018-09-12 14:35:59 -04:00
c.SetAllowZeroHopIn(label...)
c.SetAllowZeroHopOut(label...)
c.SetInLength(label...)
c.SetOutLength(label...)
c.SetInQuantity(label...)
c.SetOutQuantity(label...)
c.SetInVariance(label...)
c.SetOutVariance(label...)
c.SetInBackups(label...)
c.SetOutBackups(label...)
2018-09-13 21:20:18 -04:00
c.SetFastRecieve(label...)
2018-09-12 14:35:59 -04:00
c.SetCompressed(label...)
c.SetReduceIdle(label...)
c.SetReduceIdleTime(label...)
c.SetReduceIdleQuantity(label...)
c.SetCloseIdle(label...)
c.SetCloseIdleTime(label...)
c.SetAccessListType(label...)
c.SetTargetPort443(label...)
2018-09-14 00:16:22 -04:00
c.SetMessageReliability(label...)
c.SetClientDest(label...)
c.SetKeyFile(label...)
2019-05-11 00:26:24 -04:00
c.SetUserName(label...)
c.SetPassword(label...)
c.SetControlHost(label...)
c.SetControlPort(label...)
if v, ok := c.Get("i2cp.accessList", label...); 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
}
}
}
2018-08-14 22:22:01 -04:00
return nil
}
2018-08-14 20:34:22 -04:00
// NewI2PBlankTunConf returns an empty but intialized tunconf
func NewI2PBlankTunConf() *Conf {
2018-08-14 22:22:01 -04:00
var c Conf
2019-05-12 21:37:42 -04:00
c.Config = &goini.INI{}
2018-11-29 19:05:19 -05:00
c.Config = goini.New()
2019-05-12 21:37:42 -04:00
c.Config.Parse([]byte(""), "\n", "=")
2018-08-14 20:34:22 -04:00
return &c
}
// NewI2PTunConf returns a Conf structure from an ini file, for modification
// before starting the tunnel
2018-09-12 18:23:18 -04:00
func NewI2PTunConf(iniFile string, label ...string) (*Conf, error) {
2018-09-18 23:24:48 -04:00
c := NewI2PBlankTunConf()
if err := c.I2PINILoad(iniFile, label...); err != nil {
return nil, err
2018-07-29 00:52:36 -04:00
}
2018-09-18 23:24:48 -04:00
return c, nil
2018-07-29 00:52:36 -04:00
}