-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlistener.go
149 lines (133 loc) · 3.09 KB
/
listener.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
package tor
import (
"context"
"net"
"strconv"
"sync"
"github.com/cretz/bine/tor"
tpt "github.com/libp2p/go-libp2p-core/transport"
tptu "github.com/libp2p/go-libp2p-transport-upgrader"
ma "github.com/multiformats/go-multiaddr"
"github.com/joomcode/errorx"
)
type listener struct {
service *tor.OnionService
ctx context.Context
cancel func()
closer sync.Once
upgrader *tptu.Upgrader
t *transport
lAddrStore *listenStore
lAddrCur *listenHolder
}
func (l *listener) Multiaddr() ma.Multiaddr {
var base string
if l.service.Version3 {
base = "/onion3/"
} else {
base = "/onion/"
}
m, err := ma.NewMultiaddr(base + l.service.ID + ":" + strconv.Itoa(l.service.RemotePorts[0]))
checkError(err)
return m
}
func (l *listener) Addr() net.Addr {
return addr(l.service.ID + ":" + strconv.Itoa(l.service.RemotePorts[0]))
}
func (l *listener) Close() error {
var err error
l.closer.Do(func() {
// Remove the listener from the store.
l.lAddrStore.Lock()
cur := l.lAddrStore.cur
if cur == l.lAddrCur {
l.lAddrStore.cur = l.lAddrCur.next
goto FinishRemovingLAddr
}
// No need to check for nil, we must hit our current first.
for cur.next != l.lAddrCur {
cur = cur.next
}
cur.next = l.lAddrCur.next
FinishRemovingLAddr:
l.lAddrStore.Unlock()
l.cancel()
err = l.service.Close()
})
return err
}
func (l *listener) Accept() (tpt.CapableConn, error) {
c, err := l.service.Accept()
if err != nil {
return nil, errorx.Decorate(err, "Can't accept connection")
}
maConn := &listConn{
netConnWithoutAddr: c,
l: l,
raddr: NopMaddr2,
}
conn, err := l.upgrader.UpgradeInbound(
l.ctx,
l.t,
maConn,
)
if err != nil {
return nil, errorx.Decorate(err, "Can't upgrade raddr exchange connection")
}
stream, err := conn.AcceptStream()
if err != nil {
conn.Close()
return nil, errorx.Decorate(err, "Can't accept raddr exchange conn")
}
// mbuf Maddr BUFfer
var mbuf []byte
buf := make([]byte, 40)
var n, leftToRead int
for {
n, err = stream.Read(buf)
if err != nil {
// In this case terminate because there should be any reason this wouldn't
// work.
conn.Close()
return nil, errorx.Decorate(err, "Can't read raddr exchange stream")
}
if n != 0 {
break
}
}
mbuf = buf[1:n]
leftToRead -= n
switch buf[0] {
case encodeOnion:
leftToRead += 39
case encodeOnion3:
leftToRead += 14
default:
// In case of default do nothing, it's not because we can't dial him back we
// will not use this conn.
goto EndLAddrExchange
}
for leftToRead > 0 {
n, err = stream.Read(buf)
if err != nil {
// In this case terminate because there should be any reason this wouldn't
// work.
conn.Close()
return nil, errorx.Decorate(err, "Can't read raddr exchange stream")
}
mbuf = append(mbuf, buf[:n]...)
leftToRead -= n
}
{
raddr, err := ma.NewMultiaddrBytes(mbuf)
if err != nil {
// In case of error do nothing, it's not because we can't dial him back we
// will not use this conn.
goto EndLAddrExchange
}
maConn.raddr = raddr
}
EndLAddrExchange:
stream.Close()
return conn, nil
}