This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 161
/
methods.go
169 lines (164 loc) · 3.98 KB
/
methods.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
package main
import (
"crypto/rand"
"errors"
"fmt"
"net"
"strings"
"sync"
"time"
"github.com/ethereum/go-ethereum/rlp"
"github.com/geph-official/geph2/libs/bdclient"
"github.com/geph-official/geph2/libs/kcp-go"
"github.com/geph-official/geph2/libs/niaucchi4"
log "github.com/sirupsen/logrus"
"github.com/xtaci/smux"
)
func getDirect(greeting [2][]byte, host string, pk []byte) (ss *smux.Session, err error) {
var conn net.Conn
if useTCP {
conn, err = net.Dial("tcp", host+":2389")
if err != nil {
return
}
ss, err = negotiateSmux(greeting, conn, pk)
return
}
conn, err = niaucchi4.DialKCP(host+":2389", make([]byte, 32))
if err != nil {
err = fmt.Errorf("plain TCP failed: %w", err)
return
}
ss, err = negotiateSmux(greeting, conn, pk)
return
}
func getSinglepath(bridges []bdclient.BridgeInfo) (conn net.Conn, err error) {
bridgeRace := make(chan net.Conn)
bridgeDeadWait := new(sync.WaitGroup)
bridgeDeadWait.Add(len(bridges))
go func() {
bridgeDeadWait.Wait()
close(bridgeRace)
}()
for _, bi := range bridges {
bi := bi
go func() {
defer bridgeDeadWait.Done()
bridgeConn, err := dialBridge(bi.Host, bi.Cookie)
if err != nil {
log.Debugln("dialing to", bi.Host, "failed!", err)
return
}
bridgeConn.SetDeadline(time.Now().Add(time.Second * 30))
rlp.Encode(bridgeConn, "conn/feedback")
rlp.Encode(bridgeConn, exitName)
_, err = bridgeConn.Read(make([]byte, 1))
if err != nil {
bridgeConn.Close()
log.Debugln("conn in", bi.Host, "failed!", err)
return
}
select {
case bridgeRace <- bridgeConn:
log.Infoln("Selected bridge", bridgeConn.RemoteAddr())
default:
bridgeConn.Close()
}
}()
}
zz, ok := <-bridgeRace
if !ok {
err = errors.New("singlepath timed out")
return
}
useStats(func(sc *stats) {
sc.bridgeThunk = func() []niaucchi4.LinkInfo {
sessions := make([]niaucchi4.LinkInfo, 1)
sessions[0].RemoteIP = strings.Split(zz.RemoteAddr().String(), ":")[0]
sessions[0].RecvCnt = -1
return sessions
}
})
conn = zz
return
}
func getMultipath(bridges []bdclient.BridgeInfo) (conn net.Conn, err error) {
bridgeRace := make(chan bool)
bridgeDeadWait := new(sync.WaitGroup)
bridgeDeadWait.Add(len(bridges))
usocket := niaucchi4.Wrap(func() net.PacketConn {
us, err := net.ListenPacket("udp", ":")
if err != nil {
panic(err)
}
return us
})
cookie := make([]byte, 32)
rand.Read(cookie)
osocket := niaucchi4.ObfsListen(cookie, usocket)
e2esid := niaucchi4.NewSessAddr()
e2e := niaucchi4.NewE2EConn(osocket)
go func() {
bridgeDeadWait.Wait()
close(bridgeRace)
}()
useStats(func(sc *stats) {
sc.bridgeThunk = func() []niaucchi4.LinkInfo {
sessions := e2e.DebugInfo()
if len(sessions) < 1 {
return nil
}
return sessions[0]
}
})
for _, bi := range bridges {
bi := bi
go func() {
defer bridgeDeadWait.Done()
bridgeConn, err := dialBridge(bi.Host, bi.Cookie)
if err != nil {
log.Debugln("dialing to", bi.Host, "failed!", err)
return
}
defer bridgeConn.Close()
bridgeConn.SetDeadline(time.Now().Add(time.Second * 30))
rlp.Encode(bridgeConn, "conn/e2e")
rlp.Encode(bridgeConn, exitName)
rlp.Encode(bridgeConn, cookie)
var port uint
e := rlp.Decode(bridgeConn, &port)
if e != nil {
log.Warnln("conn/e2e to", bi.Host, "failed:", e)
return
}
complete := fmt.Sprintf("%v:%v", strings.Split(bi.Host, ":")[0], port)
compudp, e := net.ResolveUDPAddr("udp", complete)
if e != nil {
log.Println("cannot resolve udp for", complete, err)
return
}
log.Debugln("adding", complete, "to our e2e")
e2e.SetSessPath(e2esid, compudp)
select {
case bridgeRace <- true:
default:
}
}()
}
// get the bridge
_, ok := <-bridgeRace
if !ok {
err = errors.New("e2e did not work")
return
}
toret, err := kcp.NewConn2(e2esid, nil, 0, 0, e2e)
if err != nil {
panic(err)
}
toret.SetWindowSize(1000, 10000)
toret.SetNoDelay(0, 50, 3, 0)
toret.SetStreamMode(true)
toret.SetMtu(1300)
conn = toret
return
}