/
tunconf.go
316 lines (295 loc) · 9.57 KB
/
tunconf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package i2ptunconf
import (
// "crypto/tls"
"io/ioutil"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/eyedeekay/i2pkeys"
"github.com/zieckey/goini"
)
// "github.com/eyedeekay/sam3"
// Conf is a tructure containing an ini config, with some functions to help
// when you use it for in conjunction with command-line flags
type Conf struct {
//Config *goini.INI `default:&goini.INI{}`
*goini.INI
FilePath string `default:"./"`
KeyFilePath string `default:"./"`
Labels []string `default:"{''}"`
Client bool `default:"true"`
ClientDest string `default:"idk.i2p"`
SigType string `default:"SIGNATURE_TYPE=EdDSA_SHA512_Ed25519"`
Type string `default:"client"`
SaveDirectory string `default:"./"`
ServeDirectory string `default:"./www"`
SaveFile bool `default:"false"`
TargetHost string `default:"127.0.0.1"`
TargetPort string `default:"7778"`
SamHost string `default:"127.0.0.1"`
SamPort string `default:"7656"`
TunnelHost string `default:"127.0.0.1"`
ControlHost string `default:"127.0.0.1"`
ControlPort string `default:"7951"`
TargetForPort443 string `default:""`
TunName string `default:"goi2ptunnel"`
EncryptLeaseSet bool `default:"false"`
LeaseSetKey string `default:""`
LeaseSetEncType string `default:"4,0"`
LeaseSetPrivateKey string `default:""`
LeaseSetPrivateSigningKey string `default:""`
InAllowZeroHop bool `default:"false"`
OutAllowZeroHop bool `default:"false"`
InLength int `default:"3"`
OutLength int `default:"3"`
InQuantity int `default:"1"`
OutQuantity int `default:"1"`
InVariance int `default:"0"`
OutVariance int `default:"0"`
InBackupQuantity int `default:"1"`
OutBackupQuantity int `default:"1"`
UseCompression bool `default:"true"`
FastRecieve bool `default:"true"`
ReduceIdle bool `default:"false"`
ReduceIdleTime int `default:"36000000"`
ReduceIdleQuantity int `default:"1"`
CloseIdle bool `default:"false"`
CloseIdleTime int `default:"36000000"`
AccessListType string `default:"none"`
AccessList []string `default:"{''}"`
MessageReliability string `default:""`
exists bool `default:"false"`
UserName string `default:""`
Password string `default:""`
UseTLS bool `default:"false"`
Cert string `default:""`
Pem string `default:""`
HostName string `default:""`
//TLSConf *tls.Config
LoadedKeys i2pkeys.I2PKeys
}
// PrintSlice returns and prints a formatted list of configured tunnel settings.
func (c *Conf) PrintSlice() []string {
confstring := []string{
c.SignatureType(),
"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),
"i2cp.fastRecieve=" + strconv.FormatBool(c.FastRecieve),
c.reliability(),
c.accesslisttype(),
c.accesslist(),
c.lsk(),
c.lspk(),
c.lsspk(),
}
log.Println("Tunnel:", c.TunName, "using config:", confstring)
return confstring
}
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 ""
}
func (c *Conf) SignatureType() string {
if c.SigType == "" {
return ""
}
return "SIGNATURE_TYPE=" + c.SigType
}
// Get passes directly through to goini.Get
func (c *Conf) Get(key string, label ...string) (string, bool) {
if len(c.Labels) > 0 {
if len(label) > 0 {
return c.SectionGet(label[0], key)
}
return c.SectionGet(c.Labels[0], key)
}
if &c.INI != nil {
return c.INI.Get(key)
}
return "", false
}
// GetBool passes directly through to goini.GetBool
func (c *Conf) GetBool(key string, label ...string) (bool, bool) {
if len(c.Labels) > 0 {
if len(label) > 0 {
return c.SectionGetBool(label[0], key)
}
return c.SectionGetBool(c.Labels[0], key)
}
if &c.INI != nil {
return c.INI.GetBool(key)
}
return false, false
}
// GetInt passes directly through to goini.GetInt
func (c *Conf) GetInt(key string, label ...string) (int, bool) {
if len(c.Labels) > 0 {
if len(label) > 0 {
return c.SectionGetInt(label[0], key)
}
return c.SectionGetInt(c.Labels[0], key)
}
if &c.INI != nil {
return c.INI.GetInt(key)
}
return -1, false
}
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()
return c.INI.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.
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)
}
/*
// Set
func (c *Conf) Set(label ...string) {
}
*/
// I2PINILoad loads variables from an ini file into the Conf data structure.
func (c *Conf) I2PINILoad(iniFile string, label ...string) error {
var err error
c.exists = true
c.INI = goini.New()
if iniFile != "none" && iniFile != "" {
c.FilePath = iniFile
err = c.INI.ParseFile(iniFile)
if err != nil {
return err
}
c.SetLabels(iniFile)
c.SetDir(label...)
c.SetType(label...)
c.SetKeys(label...)
c.SetHost(label...)
c.SetPort(label...)
c.SetSAMHost(label...)
c.SetSAMPort(label...)
c.SetEndpointHost(label...)
c.SetTunName(label...)
c.SetSigType(label...)
c.SetEncryptLease(label...)
c.SetLeaseSetEncType(label...)
c.SetLeasesetKey(label...)
c.SetLeasesetPrivateKey(label...)
c.SetLeasesetPrivateSigningKey(label...)
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...)
c.SetFastRecieve(label...)
c.SetCompressed(label...)
c.SetReduceIdle(label...)
c.SetReduceIdleTime(label...)
c.SetReduceIdleQuantity(label...)
c.SetCloseIdle(label...)
c.SetCloseIdleTime(label...)
c.SetAccessListType(label...)
c.SetTargetPort443(label...)
c.SetMessageReliability(label...)
c.SetClientDest(label...)
c.SetKeyFile(label...)
c.SetUserName(label...)
c.SetPassword(label...)
c.SetControlHost(label...)
c.SetControlPort(label...)
c.SetWWWDir(label...)
c.SetUseTLS(label...)
c.SetTLSConfigCertPem(label...)
c.SetTLSConfigKeyPem(label...)
if v, ok := c.Get("i2cp.accessList", label...); ok {
csv := strings.Split(v, ",")
for _, z := range csv {
c.AccessList = append(c.AccessList, z)
}
}
}
return nil
}
// NewI2PBlankTunConf returns an empty but intialized tunconf
func NewI2PBlankTunConf() *Conf {
// var c Conf
c := new(Conf)
c.SamHost = "127.0.0.1"
c.SamPort = "7656"
c.TunName = "unksam"
c.TargetHost = "127.0.0.1"
c.TargetPort = "0"
c.ClientDest = "idk.i2p"
c.LeaseSetEncType = "4,0"
//c.INI = &goini.INI{}
c.INI = goini.New()
c.INI.Parse([]byte("[client]\nsamhost=\"127.0.0.1\"\nsamport=\"7656\"\n"), "\n", "=")
return c
}
// NewI2PTunConf returns a Conf structure from an ini file, for modification
// before starting the tunnel
func NewI2PTunConf(iniFile string, label ...string) (*Conf, error) {
c := NewI2PBlankTunConf()
if err := c.I2PINILoad(iniFile, label...); err != nil {
return nil, err
}
return c, nil
}