forked from eyedeekay/goSam
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptions.go
589 lines (528 loc) · 13.3 KB
/
options.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
package gosam
import (
"fmt"
"strconv"
"strings"
)
// Option is a client Option
type Option func(*Client) error
// SetAddr sets a clients's address in the form host:port or host, port
func SetAddr(s ...string) func(*Client) error {
return func(c *Client) error {
if len(s) == 1 {
split := strings.SplitN(s[0], ":", 2)
if len(split) == 2 {
if i, err := strconv.Atoi(split[1]); err == nil {
if i < 65536 {
c.host = split[0]
c.port = split[1]
return nil
}
return fmt.Errorf("Invalid port")
}
return fmt.Errorf("Invalid port; non-number")
}
return fmt.Errorf("Invalid address; use host:port %s", split)
} else if len(s) == 2 {
if i, err := strconv.Atoi(s[1]); err == nil {
if i < 65536 {
c.host = s[0]
c.port = s[1]
return nil
}
return fmt.Errorf("Invalid port")
}
return fmt.Errorf("Invalid port; non-number")
} else {
return fmt.Errorf("Invalid address")
}
}
}
// SetAddrMixed sets a clients's address in the form host, port(int)
func SetAddrMixed(s string, i int) func(*Client) error {
return func(c *Client) error {
if i < 65536 && i > 0 {
c.host = s
c.port = strconv.Itoa(i)
return nil
}
return fmt.Errorf("Invalid port")
}
}
// SetHost sets the host of the client's SAM bridge
func SetHost(s string) func(*Client) error {
return func(c *Client) error {
c.host = s
return nil
}
}
// SetUser sets the username for authentication during the SAM HELLO phase
func SetUser(s string) func(*Client) error {
return func(c *Client) error {
c.user = s
return nil
}
}
// SetUser sets the password for authentication during the SAM HELLO phase
func SetPass(s string) func(*Client) error {
return func(c *Client) error {
c.pass = s
return nil
}
}
func SetSAMMinVersion(i int) func(*Client) error {
return func(c *Client) error {
if i < 0 {
return fmt.Errorf("SAM version must be greater than or equal to 0")
}
if i > 3 {
return fmt.Errorf("SAM version must be less than or equal to 3")
}
c.sammin = i
return nil
}
}
func SetSAMMaxVersion(i int) func(*Client) error {
return func(c *Client) error {
if i < 0 {
return fmt.Errorf("SAM version must be greater than or equal to 0")
}
if i > 3 {
return fmt.Errorf("SAM version must be less than or equal to 3")
}
c.sammin = i
return nil
}
}
// SetLocalDestination sets the local destination of the tunnel from a private
// key
func SetLocalDestination(s string) func(*Client) error {
return func(c *Client) error {
c.destination = s
return nil
}
}
func setid(s int32) func(*Client) error {
return func(c *Client) error {
c.id = s
return nil
}
}
// SetPort sets the port of the client's SAM bridge using a string
func SetPort(s string) func(*Client) error {
return func(c *Client) error {
port, err := strconv.Atoi(s)
if err != nil {
return fmt.Errorf("Invalid port; non-number")
}
if port < 65536 && port > -1 {
c.port = s
return nil
}
return fmt.Errorf("Invalid port")
}
}
// SetPortInt sets the port of the client's SAM bridge using a string
func SetPortInt(i int) func(*Client) error {
return func(c *Client) error {
if i < 65536 && i > -1 {
c.port = strconv.Itoa(i)
return nil
}
return fmt.Errorf("Invalid port")
}
}
// SetFromPort sets the port of the client's SAM bridge using a string
func SetFromPort(s string) func(*Client) error {
return func(c *Client) error {
port, err := strconv.Atoi(s)
if err != nil {
return fmt.Errorf("Invalid port; non-number")
}
if port < 65536 && port > -1 {
c.fromport = s
return nil
}
return fmt.Errorf("Invalid port")
}
}
// SetFromPortInt sets the port of the client's SAM bridge using a string
func SetFromPortInt(i int) func(*Client) error {
return func(c *Client) error {
if i < 65536 && i > -1 {
c.fromport = strconv.Itoa(i)
return nil
}
return fmt.Errorf("Invalid port")
}
}
// SetToPort sets the port of the client's SAM bridge using a string
func SetToPort(s string) func(*Client) error {
return func(c *Client) error {
port, err := strconv.Atoi(s)
if err != nil {
return fmt.Errorf("Invalid port; non-number")
}
if port < 65536 && port > -1 {
c.toport = s
return nil
}
return fmt.Errorf("Invalid port")
}
}
// SetToPortInt sets the port of the client's SAM bridge using a string
func SetToPortInt(i int) func(*Client) error {
return func(c *Client) error {
if i < 65536 && i > -1 {
c.fromport = strconv.Itoa(i)
return nil
}
return fmt.Errorf("Invalid port")
}
}
// SetDebug enables debugging messages
func SetDebug(b bool) func(*Client) error {
return func(c *Client) error {
//c.debug = b
c.debug = true
return nil
}
}
// SetInLength sets the number of hops inbound
func SetInLength(u uint) func(*Client) error {
return func(c *Client) error {
if u < 7 {
c.inLength = u
return nil
}
return fmt.Errorf("Invalid inbound tunnel length")
}
}
// SetOutLength sets the number of hops outbound
func SetOutLength(u uint) func(*Client) error {
return func(c *Client) error {
if u < 7 {
c.outLength = u
return nil
}
return fmt.Errorf("Invalid outbound tunnel length")
}
}
// SetInVariance sets the variance of a number of hops inbound
func SetInVariance(i int) func(*Client) error {
return func(c *Client) error {
if i < 7 && i > -7 {
c.inVariance = i
return nil
}
return fmt.Errorf("Invalid inbound tunnel length")
}
}
// SetOutVariance sets the variance of a number of hops outbound
func SetOutVariance(i int) func(*Client) error {
return func(c *Client) error {
if i < 7 && i > -7 {
c.outVariance = i
return nil
}
return fmt.Errorf("Invalid outbound tunnel variance")
}
}
// SetInQuantity sets the inbound tunnel quantity
func SetInQuantity(u uint) func(*Client) error {
return func(c *Client) error {
if u <= 16 {
c.inQuantity = u
return nil
}
return fmt.Errorf("Invalid inbound tunnel quantity")
}
}
// SetOutQuantity sets the outbound tunnel quantity
func SetOutQuantity(u uint) func(*Client) error {
return func(c *Client) error {
if u <= 16 {
c.outQuantity = u
return nil
}
return fmt.Errorf("Invalid outbound tunnel quantity")
}
}
// SetInBackups sets the inbound tunnel backups
func SetInBackups(u uint) func(*Client) error {
return func(c *Client) error {
if u < 6 {
c.inBackups = u
return nil
}
return fmt.Errorf("Invalid inbound tunnel backup quantity")
}
}
// SetOutBackups sets the inbound tunnel backups
func SetOutBackups(u uint) func(*Client) error {
return func(c *Client) error {
if u < 6 {
c.outBackups = u
return nil
}
return fmt.Errorf("Invalid outbound tunnel backup quantity")
}
}
// SetUnpublished tells the router to not publish the client leaseset
func SetUnpublished(b bool) func(*Client) error {
return func(c *Client) error {
c.dontPublishLease = b
return nil
}
}
// SetEncrypt tells the router to use an encrypted leaseset
func SetEncrypt(b bool) func(*Client) error {
return func(c *Client) error {
c.encryptLease = b
return nil
}
}
// SetLeaseSetEncType tells the router to use an encrypted leaseset of a specific type.
// defaults to 4,0
func SetLeaseSetEncType(b string) func(*Client) error {
return func(c *Client) error {
c.leaseSetEncType = b
return nil
}
}
// SetReduceIdle sets the created tunnels to be reduced during extended idle time to avoid excessive resource usage
func SetReduceIdle(b bool) func(*Client) error {
return func(c *Client) error {
c.reduceIdle = b
return nil
}
}
// SetReduceIdleTime sets time to wait before the tunnel quantity is reduced
func SetReduceIdleTime(u uint) func(*Client) error {
return func(c *Client) error {
if u > 299999 {
c.reduceIdleTime = u
return nil
}
return fmt.Errorf("Invalid reduce idle time %v", u)
}
}
// SetReduceIdleQuantity sets number of tunnels to keep alive during an extended idle period
func SetReduceIdleQuantity(u uint) func(*Client) error {
return func(c *Client) error {
if u < 5 {
c.reduceIdleQuantity = u
return nil
}
return fmt.Errorf("Invalid reduced tunnel quantity %v", u)
}
}
// SetCloseIdle sets the tunnels to close after a specific amount of time
func SetCloseIdle(b bool) func(*Client) error {
return func(c *Client) error {
c.closeIdle = b
return nil
}
}
// SetCloseIdleTime sets the time in milliseconds to wait before closing tunnels
func SetCloseIdleTime(u uint) func(*Client) error {
return func(c *Client) error {
if u > 299999 {
c.closeIdleTime = u
return nil
}
return fmt.Errorf("Invalid close idle time %v", u)
}
}
// SetCompression sets the tunnels to close after a specific amount of time
func SetCompression(b bool) func(*Client) error {
return func(c *Client) error {
c.compress = b
return nil
}
}
/* SAM v 3.1 Options*/
// SetSignatureType tells gosam to pass SAM a signature_type parameter with one
// of the following values:
//
// "SIGNATURE_TYPE=DSA_SHA1",
// "SIGNATURE_TYPE=ECDSA_SHA256_P256",
// "SIGNATURE_TYPE=ECDSA_SHA384_P384",
// "SIGNATURE_TYPE=ECDSA_SHA512_P521",
// "SIGNATURE_TYPE=EdDSA_SHA512_Ed25519",
//
// or an empty string
func SetSignatureType(s string) func(*Client) error {
return func(c *Client) error {
if s == "" {
c.sigType = ""
return nil
}
for _, valid := range SAMsigTypes {
if s == valid {
c.sigType = valid
return nil
}
}
return fmt.Errorf("Invalid signature type specified at construction time")
}
}
// return the from port as a string.
func (c *Client) from() string {
if c.fromport == "FROM_PORT=0" {
return ""
}
if c.fromport == "0" {
return ""
}
if c.fromport == "" {
return ""
}
return fmt.Sprintf(" FROM_PORT=%v ", c.fromport)
}
// return the to port as a string.
func (c *Client) to() string {
if c.fromport == "TO_PORT=0" {
return ""
}
if c.fromport == "0" {
return ""
}
if c.toport == "" {
return ""
}
return fmt.Sprintf(" TO_PORT=%v ", c.toport)
}
// return the signature type as a string.
func (c *Client) sigtype() string {
return fmt.Sprintf(" %s ", c.sigType)
}
// return the inbound length as a string.
func (c *Client) inlength() string {
return fmt.Sprintf(" inbound.length=%d ", c.inLength)
}
// return the outbound length as a string.
func (c *Client) outlength() string {
return fmt.Sprintf(" outbound.length=%d ", c.outLength)
}
// return the inbound length variance as a string.
func (c *Client) invariance() string {
return fmt.Sprintf(" inbound.lengthVariance=%d ", c.inVariance)
}
// return the outbound length variance as a string.
func (c *Client) outvariance() string {
return fmt.Sprintf(" outbound.lengthVariance=%d ", c.outVariance)
}
// return the inbound tunnel quantity as a string.
func (c *Client) inquantity() string {
return fmt.Sprintf(" inbound.quantity=%d ", c.inQuantity)
}
// return the outbound tunnel quantity as a string.
func (c *Client) outquantity() string {
return fmt.Sprintf(" outbound.quantity=%d ", c.outQuantity)
}
// return the inbound tunnel quantity as a string.
func (c *Client) inbackups() string {
return fmt.Sprintf(" inbound.backupQuantity=%d ", c.inQuantity)
}
// return the outbound tunnel quantity as a string.
func (c *Client) outbackups() string {
return fmt.Sprintf(" outbound.backupQuantity=%d ", c.outQuantity)
}
func (c *Client) encryptlease() string {
if c.encryptLease {
return " i2cp.encryptLeaseSet=true "
}
return " i2cp.encryptLeaseSet=false "
}
func (c *Client) leasesetenctype() string {
if c.encryptLease {
return fmt.Sprintf(" i2cp.leaseSetEncType=%s ", c.leaseSetEncType)
}
return " i2cp.leaseSetEncType=4,0 "
}
func (c *Client) dontpublishlease() string {
if c.dontPublishLease {
return " i2cp.dontPublishLeaseSet=true "
}
return " i2cp.dontPublishLeaseSet=false "
}
func (c *Client) closeonidle() string {
if c.closeIdle {
return " i2cp.closeOnIdle=true "
}
return " i2cp.closeOnIdle=false "
}
func (c *Client) closeidletime() string {
return fmt.Sprintf(" i2cp.closeIdleTime=%d ", c.closeIdleTime)
}
func (c *Client) reduceonidle() string {
if c.reduceIdle {
return " i2cp.reduceOnIdle=true "
}
return " i2cp.reduceOnIdle=false "
}
func (c *Client) reduceidletime() string {
return fmt.Sprintf(" i2cp.reduceIdleTime=%d ", c.reduceIdleTime)
}
func (c *Client) reduceidlecount() string {
return fmt.Sprintf(" i2cp.reduceIdleQuantity=%d ", c.reduceIdleQuantity)
}
func (c *Client) compression() string {
if c.compress {
return " i2cp.gzip=true "
}
return " i2cp.gzip=false "
}
// return all options as string ready for passing to sendcmd
func (c *Client) allOptions() string {
return c.inlength() +
c.outlength() +
c.invariance() +
c.outvariance() +
c.inquantity() +
c.outquantity() +
c.inbackups() +
c.outbackups() +
c.dontpublishlease() +
c.encryptlease() +
c.leasesetenctype() +
c.reduceonidle() +
c.reduceidletime() +
c.reduceidlecount() +
c.closeonidle() +
c.closeidletime() +
c.compression()
}
// Print return all options as string
func (c *Client) Print() string {
return c.inlength() +
c.outlength() +
c.invariance() +
c.outvariance() +
c.inquantity() +
c.outquantity() +
c.inbackups() +
c.outbackups() +
c.dontpublishlease() +
c.encryptlease() +
c.leasesetenctype() +
c.reduceonidle() +
c.reduceidletime() +
c.reduceidlecount() +
c.closeonidle() +
c.closeidletime() +
c.compression()
}
func (c *Client) getUser() string {
if c.user == "" {
return ""
}
return fmt.Sprintf("USER=%s", c.user)
}
func (c *Client) getPass() string {
if c.pass == "" {
return ""
}
return fmt.Sprintf("PASSWORD=%s", c.pass)
}