2018-07-29 00:31:23 -04:00
|
|
|
package i2ptunconf
|
|
|
|
|
|
|
|
import (
|
2018-09-12 12:52:44 -04:00
|
|
|
"io/ioutil"
|
2018-07-30 02:44:36 -04:00
|
|
|
"log"
|
2018-11-29 19:01:10 -05:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-08-09 15:19:50 -04:00
|
|
|
"strconv"
|
2018-07-29 00:52:36 -04:00
|
|
|
"strings"
|
2018-07-29 00:31:23 -04:00
|
|
|
)
|
|
|
|
|
2018-08-16 15:27:29 -04:00
|
|
|
import (
|
2019-08-31 00:13:10 -04:00
|
|
|
"github.com/eyedeekay/sam3/i2pkeys"
|
2018-08-16 15:27:29 -04:00
|
|
|
"github.com/zieckey/goini"
|
|
|
|
)
|
|
|
|
|
2018-08-09 13:23:11 -04:00
|
|
|
// 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
|
2018-09-13 22:13:44 -04:00
|
|
|
FilePath string
|
2018-09-17 23:40:09 -04:00
|
|
|
KeyFilePath string
|
2018-09-13 22:13:44 -04:00
|
|
|
Labels []string
|
|
|
|
Client bool
|
2018-09-19 12:08:20 -04:00
|
|
|
ClientDest string
|
2019-04-25 22:58:49 -04:00
|
|
|
SigType string
|
2018-09-13 22:13:44 -04:00
|
|
|
Type string
|
|
|
|
SaveDirectory string
|
|
|
|
SaveFile bool
|
|
|
|
TargetHost string
|
|
|
|
TargetPort string
|
|
|
|
SamHost string
|
|
|
|
SamPort string
|
2019-06-12 09:16:13 -04:00
|
|
|
TunnelHost string
|
2019-06-14 01:09:55 -04:00
|
|
|
ControlHost string
|
|
|
|
ControlPort string
|
2018-09-13 22:13:44 -04:00
|
|
|
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
|
2019-08-31 00:13:10 -04:00
|
|
|
LoadedKeys i2pkeys.I2PKeys
|
2018-07-29 00:52:36 -04:00
|
|
|
}
|
2018-07-29 00:31:23 -04:00
|
|
|
|
2019-08-31 00:13:10 -04:00
|
|
|
// PrintSlice returns and prints a formatted list of configured tunnel settings.
|
|
|
|
func (c *Conf) PrintSlice() []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
|
|
|
}
|
|
|
|
|
2018-07-29 01:07:17 -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) {
|
2018-09-19 10:34:29 -04:00
|
|
|
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)
|
2018-09-19 10:34:29 -04:00
|
|
|
} else {
|
2018-11-29 19:05:19 -05:00
|
|
|
if c.Config != nil {
|
|
|
|
return c.Config.Get(key)
|
2018-09-19 11:26:47 -04:00
|
|
|
} else {
|
|
|
|
return "", false
|
|
|
|
}
|
2018-09-19 10:34:29 -04:00
|
|
|
}
|
|
|
|
return "", false
|
2018-07-29 00:52:36 -04:00
|
|
|
}
|
|
|
|
|
2018-07-29 01:07:17 -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) {
|
2018-09-19 10:34:29 -04:00
|
|
|
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)
|
2018-09-19 10:34:29 -04:00
|
|
|
} else {
|
2018-11-29 19:05:19 -05:00
|
|
|
if c.Config != nil {
|
|
|
|
return c.Config.GetBool(key)
|
2018-09-19 11:26:47 -04:00
|
|
|
} else {
|
|
|
|
return false, false
|
|
|
|
}
|
2018-09-19 10:34:29 -04:00
|
|
|
}
|
|
|
|
return false, false
|
2018-07-29 00:52:36 -04:00
|
|
|
}
|
|
|
|
|
2018-07-29 01:07:17 -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) {
|
2018-09-19 10:34:29 -04:00
|
|
|
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)
|
2018-09-19 10:34:29 -04:00
|
|
|
} else {
|
2018-11-29 19:05:19 -05:00
|
|
|
if c.Config != nil {
|
|
|
|
return c.Config.GetInt(key)
|
2018-09-19 11:26:47 -04:00
|
|
|
} else {
|
|
|
|
return -1, false
|
|
|
|
}
|
2018-09-19 10:34:29 -04:00
|
|
|
}
|
|
|
|
return -1, false
|
2018-07-29 00:52:36 -04:00
|
|
|
}
|
|
|
|
|
2018-11-29 19:01:10 -05: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)
|
2018-11-29 19:01:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-17 11:08:26 -04:00
|
|
|
/*
|
|
|
|
// 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 {
|
2018-08-17 11:08:26 -04:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2018-09-12 12:52:44 -04:00
|
|
|
// 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, "]") {
|
2018-09-12 13:43:18 -04:00
|
|
|
c.Labels = append(c.Labels, strings.Trim(trimmedelement, "[]"))
|
2018-09-12 12:52:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Println("Found Labels:", c.Labels)
|
|
|
|
}
|
|
|
|
|
2018-08-16 13:46:22 -04:00
|
|
|
/*
|
|
|
|
// Set
|
2018-09-12 13:43:18 -04:00
|
|
|
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
|
|
|
}
|
2018-09-12 12:52:44 -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...)
|
2019-06-12 09:16:13 -04:00
|
|
|
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...)
|
2018-09-13 23:30:54 -04:00
|
|
|
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...)
|
2018-09-19 12:08:20 -04:00
|
|
|
c.SetClientDest(label...)
|
2018-09-17 23:40:09 -04:00
|
|
|
c.SetKeyFile(label...)
|
2019-05-11 00:26:24 -04:00
|
|
|
c.SetUserName(label...)
|
|
|
|
c.SetPassword(label...)
|
2019-06-25 11:41:22 -04:00
|
|
|
c.SetControlHost(label...)
|
|
|
|
c.SetControlPort(label...)
|
2018-09-12 22:33:19 -04:00
|
|
|
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-11 01:16:00 -04:00
|
|
|
}
|
2018-08-14 22:22:01 -04:00
|
|
|
return nil
|
2018-08-11 01:16:00 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-08-11 01:16:00 -04:00
|
|
|
// 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 {
|
2018-08-11 01:16:00 -04:00
|
|
|
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
|
|
|
}
|