-
Notifications
You must be signed in to change notification settings - Fork 1
/
stream.go
202 lines (189 loc) · 5.6 KB
/
stream.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
package sam3
import (
"bufio"
"bytes"
"errors"
"net"
"strconv"
"strings"
)
// Represents a streaming session.
type StreamSession struct {
samAddr string // address to the sam bridge (ipv4:port)
id string // tunnel name
conn net.Conn // connection to sam bridge
keys I2PKeys // i2p destination keys
}
// Returns the local tunnel name of the I2P tunnel used for the stream session
func (ss StreamSession) ID() string {
return ss.id
}
// Returns the I2P destination (the address) of the stream session
func (ss StreamSession) Addr() I2PAddr {
return ss.keys.Addr()
}
// Returns the keys associated with the stream session
func (ss StreamSession) Keys() I2PKeys {
return ss.keys
}
// Creates a new StreamSession with the I2CP- and streaminglib options as
// specified. See the I2P documentation for a full list of options.
func (sam *SAM) NewStreamSession(id string, keys I2PKeys, options []string) (*StreamSession, error) {
conn, err := sam.newGenericSession("STREAM", id, keys, options, []string{})
if err != nil {
return nil, err
}
return &StreamSession{sam.address, id, conn, keys}, nil
}
// Dials to an I2P destination and returns a SAMConn, which implements a net.Conn.
func (s *StreamSession) DialI2P(addr I2PAddr) (*SAMConn, error) {
sam, err := NewSAM(s.samAddr)
if err != nil {
return nil, err
}
conn := sam.conn
_, err = conn.Write([]byte("STREAM CONNECT ID=" + s.id + " DESTINATION=" + addr.Base64() + " SILENT=false\n"))
if err != nil {
return nil, err
}
buf := make([]byte, 4096)
n, err := conn.Read(buf)
if err != nil {
return nil, err
}
scanner := bufio.NewScanner(bytes.NewReader(buf[:n]))
scanner.Split(bufio.ScanWords)
for scanner.Scan() {
switch scanner.Text() {
case "STREAM":
continue
case "STATUS":
continue
case "RESULT=OK":
return &SAMConn{s.keys.addr, addr, conn}, nil
case "RESULT=CANT_REACH_PEER":
return nil, errors.New("Can not reach peer")
case "RESULT=I2P_ERROR":
return nil, errors.New("I2P internal error")
case "RESULT=INVALID_KEY":
return nil, errors.New("Invalid key")
case "RESULT=INVALID_ID":
return nil, errors.New("Invalid tunnel ID")
case "RESULT=TIMEOUT":
return nil, errors.New("Timeout")
default:
return nil, errors.New("Unknown error: " + scanner.Text() + " : " + string(buf[:n]))
}
}
panic("sam3 go library error in StreamSession.DialI2P()")
}
// Returns a listener for the I2P destination (I2PAddr) associated with the
// StreamSession.
func (s *StreamSession) Listen() (*StreamListener, error) {
sam, err := NewSAM(s.conn.RemoteAddr().String())
if err != nil {
return nil, err
}
lhost, _, err := net.SplitHostPort(s.conn.LocalAddr().String())
if err != nil {
sam.Close()
return nil, err
}
listener, err := net.Listen("tcp4", lhost+":0")
_, lport, err := net.SplitHostPort(listener.Addr().String())
if err != nil {
sam.Close()
return nil, err
}
conn := sam.conn
_, err = conn.Write([]byte("STREAM FORWARD ID=" + s.id + " PORT=" + lport + " SILENT=false\n"))
if err != nil {
conn.Close()
return nil, err
}
buf := make([]byte, 512)
n, err := conn.Read(buf)
if err != nil {
conn.Close()
return nil, err
}
scanner := bufio.NewScanner(bytes.NewReader(buf[:n]))
scanner.Split(bufio.ScanWords)
for scanner.Scan() {
switch strings.TrimSpace(scanner.Text()) {
case "STREAM":
continue
case "STATUS":
continue
case "RESULT=OK":
port, _ := strconv.Atoi(lport)
return &StreamListener{conn, listener, port, s.keys.Addr()}, nil
case "RESULT=I2P_ERROR":
conn.Close()
return nil, errors.New("I2P internal error")
case "RESULT=INVALID_ID":
conn.Close()
return nil, errors.New("Invalid tunnel ID")
default:
conn.Close()
return nil, errors.New("Unknown error: " + scanner.Text() + " : " + string(buf[:n]))
}
}
panic("sam3 go library error in StreamSession.Listener()")
}
// Implements net.Listener for I2P streaming sessions
type StreamListener struct {
conn net.Conn
listener net.Listener
lport int
laddr I2PAddr
}
const defaultListenReadLen = 516
// Accepts incomming connections to your StreamSession tunnel. Implements net.Listener
func (l *StreamListener) Accept() (*SAMConn, error) {
conn, err := l.listener.Accept()
if err != nil {
return nil, err
}
buf := make([]byte, defaultListenReadLen)
n, err := conn.Read(buf)
if n < defaultListenReadLen {
return nil, errors.New("Unknown destination type: " + string(buf[:n]))
}
// I2P inserts the I2P address ("destination") of the connecting peer into the datastream, followed by
// a \n. Since the length of a destination may vary, this reads until a newline is found. At the time
// of writing, the length is never less then, and almost always equals 516 bytes, which is why
// defaultListenReadLen is 516.
if rune(buf[defaultListenReadLen-1]) != '\n' {
abuf := make([]byte, 1)
for {
n, err := conn.Read(abuf)
if n != 1 || err != nil {
return nil, errors.New("Failed to decode connecting peers I2P destination.")
}
buf = append(buf, abuf[0])
if rune(abuf[0]) == '\n' {
break
}
}
}
rAddr, err := NewI2PAddrFromString(string(buf[:len(buf)-1])) // the address minus the trailing newline
if err != nil {
conn.Close()
return nil, errors.New("Could not determine connecting tunnels address.")
}
return &SAMConn{l.laddr, rAddr, conn}, nil
}
// Closes the stream session. Implements net.Listener
func (l *StreamListener) Close() error {
err := l.listener.Close()
err2 := l.conn.Close()
if err2 != nil {
return err2
}
return err
}
// Returns the I2P destination (address) of the stream session. Implements net.Listener
func (l *StreamListener) Addr() net.Addr {
return l.laddr
}