forked from cryptix/goSam
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathclient.go
286 lines (247 loc) · 6.44 KB
/
client.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
package gosam
import (
"bufio"
"crypto/sha256"
"encoding/base32"
"encoding/base64"
"encoding/binary"
"fmt"
"math"
"math/rand"
"net"
"strings"
"sync"
"time"
"github.com/go-i2p/i2pkeys"
//samkeys "github.com/go-i2p/gosam/compat"
)
// A Client represents a single Connection to the SAM bridge
type Client struct {
host string
port string
fromport string
toport string
user string
pass string
SamConn net.Conn // Control socket
SamDGConn net.PacketConn // Datagram socket
rd *bufio.Reader
// d *Client
sigType string
destination string
inLength uint
inVariance int
inQuantity uint
inBackups uint
outLength uint
outVariance int
outQuantity uint
outBackups uint
dontPublishLease bool
encryptLease bool
leaseSetEncType string
reduceIdle bool
reduceIdleTime uint
reduceIdleQuantity uint
closeIdle bool
closeIdleTime uint
compress bool
debug bool
mutex sync.Mutex
//NEVER, EVER modify lastaddr or id yourself. They are used internally only.
id int32
sammin int
sammax int
}
// SAMsigTypes is a slice of the available signature types
var SAMsigTypes = []string{
"SIGNATURE_TYPE=DSA_SHA1",
"SIGNATURE_TYPE=ECDSA_SHA256_P256",
"SIGNATURE_TYPE=ECDSA_SHA384_P384",
"SIGNATURE_TYPE=ECDSA_SHA512_P521",
"SIGNATURE_TYPE=EdDSA_SHA512_Ed25519",
}
var ValidSAMCommands = []string{
"HELLO",
"SESSION",
"STREAM",
}
var (
i2pB64enc *base64.Encoding = base64.NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-~")
i2pB32enc *base32.Encoding = base32.NewEncoding("abcdefghijklmnopqrstuvwxyz234567")
)
// NewDefaultClient creates a new client, connecting to the default host:port at localhost:7656
func NewDefaultClient() (*Client, error) {
return NewClient("localhost:7656")
}
// NewClient creates a new client, connecting to a specified port
func NewClient(addr string) (*Client, error) {
return NewClientFromOptions(SetAddr(addr))
}
func NewID() int32 {
id := rand.Int31n(math.MaxInt32)
fmt.Printf("Initializing new ID: %d\n", id)
return id
}
// NewID generates a random number to use as an tunnel name
func (c *Client) NewID() int32 {
if c.id == 0 {
c.id = NewID()
}
return c.id
}
// Destination returns the full destination of the local tunnel
func (c *Client) Destination() string {
return c.destination
}
// Base32 returns the base32 of the local tunnel
func (c *Client) Base32() string {
// hash := sha256.New()
b64, err := i2pB64enc.DecodeString(c.Base64())
if err != nil {
return ""
}
//hash.Write([]byte(b64))
var s []byte
for _, e := range sha256.Sum256(b64) {
s = append(s, e)
}
return strings.ToLower(strings.Replace(i2pB32enc.EncodeToString(s), "=", "", -1))
}
func (c *Client) base64() []byte {
if c.destination != "" {
s, _ := i2pB64enc.DecodeString(c.destination)
alen := binary.BigEndian.Uint16(s[385:387])
return s[:387+alen]
}
return []byte("")
}
// Base64 returns the base64 of the local tunnel
func (c *Client) Base64() string {
return i2pB64enc.EncodeToString(c.base64())
}
// NewClientFromOptions creates a new client, connecting to a specified port
func NewClientFromOptions(opts ...func(*Client) error) (*Client, error) {
var c Client
c.host = "127.0.0.1"
c.port = "7656"
c.inLength = 3
c.inVariance = 0
c.inQuantity = 3
c.inBackups = 1
c.outLength = 3
c.outVariance = 0
c.outQuantity = 3
c.outBackups = 1
c.dontPublishLease = true
c.encryptLease = false
c.reduceIdle = false
c.reduceIdleTime = 300000
c.reduceIdleQuantity = 1
c.closeIdle = true
c.closeIdleTime = 600000
c.debug = false
c.sigType = SAMsigTypes[4]
c.id = 0
c.destination = ""
c.leaseSetEncType = "4,0"
c.fromport = ""
c.toport = ""
c.sammin = 0
c.sammax = 1
for _, o := range opts {
if err := o(&c); err != nil {
return nil, err
}
}
c.id = c.NewID()
conn, err := net.DialTimeout("tcp", c.samaddr(), 15*time.Minute)
if err != nil {
return nil, err
}
if c.debug {
conn = WrapConn(conn)
}
c.SamConn = conn
c.rd = bufio.NewReader(conn)
return &c, c.hello()
}
// ID returns a the current ID of the client as a string
func (p *Client) ID() string {
return fmt.Sprintf("%d", p.NewID())
}
// Addr returns the address of the client as a net.Addr
func (p *Client) Addr() net.Addr {
keys := i2pkeys.I2PAddr(p.Destination())
return keys
}
func (p *Client) LocalAddr() net.Addr {
return p.Addr()
}
// LocalKeys returns the local keys of the client as a fully-fledged i2pkeys.I2PKeys
func (p *Client) PrivateAddr() i2pkeys.I2PKeys {
//keys := i2pkeys.I2PAddr(p.Destination())
keys := i2pkeys.NewKeys(i2pkeys.I2PAddr(p.base64()), p.Destination())
return keys
}
// return the combined host:port of the SAM bridge
func (c *Client) samaddr() string {
return fmt.Sprintf("%s:%s", c.host, c.port)
}
// send the initial handshake command and check that the reply is ok
func (c *Client) hello() error {
r, err := c.sendCmd("HELLO VERSION MIN=3.%d MAX=3.%d %s %s\n", c.sammin, c.sammax, c.getUser(), c.getPass())
if err != nil {
return err
}
if r.Topic != "HELLO" {
return fmt.Errorf("Client Hello Unknown Reply: %+v\n", r)
}
if r.Pairs["RESULT"] != "OK" {
return fmt.Errorf("Handshake did not succeed\nReply:%+v\n", r)
}
return nil
}
// helper to send one command and parse the reply by sam
func (c *Client) sendCmd(str string, args ...interface{}) (*Reply, error) {
if _, err := fmt.Fprintf(c.SamConn, str, args...); err != nil {
return nil, err
}
line, err := c.rd.ReadString('\n')
if err != nil {
return nil, err
}
return parseReply(line)
}
// Close the underlying socket to SAM
func (c *Client) Close() error {
c.rd = nil
return c.SamConn.Close()
}
// NewClient generates an exact copy of the client with the same options, but
// re-does all the handshaky business so that Dial can pick up right where it
// left off, should the need arise.
func (c *Client) NewClient(id int32) (*Client, error) {
return NewClientFromOptions(
SetHost(c.host),
SetPort(c.port),
SetDebug(c.debug),
SetInLength(c.inLength),
SetOutLength(c.outLength),
SetInVariance(c.inVariance),
SetOutVariance(c.outVariance),
SetInQuantity(c.inQuantity),
SetOutQuantity(c.outQuantity),
SetInBackups(c.inBackups),
SetOutBackups(c.outBackups),
SetUnpublished(c.dontPublishLease),
SetEncrypt(c.encryptLease),
SetReduceIdle(c.reduceIdle),
SetReduceIdleTime(c.reduceIdleTime),
SetReduceIdleQuantity(c.reduceIdleQuantity),
SetCloseIdle(c.closeIdle),
SetCloseIdleTime(c.closeIdleTime),
SetCompression(c.compress),
setid(id),
)
}