/
AssClientDirect.go
224 lines (181 loc) · 6.09 KB
/
AssClientDirect.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
package udptlssctp
import (
"bytes"
"context"
"errors"
"fmt"
"github.com/lunixbochs/struc"
"github.com/mustafaturan/bus"
"github.com/xiaokangwang/VLite/interfaces"
"github.com/xiaokangwang/VLite/interfaces/ibus"
"github.com/xiaokangwang/VLite/proto"
"github.com/xiaokangwang/VLite/transport"
"github.com/xiaokangwang/VLite/transport/http/httpClient"
udpsctpserver "github.com/xiaokangwang/VLite/transport/packetsctp/sctprelay"
"github.com/xiaokangwang/VLite/transport/packetuni/puniClient"
"github.com/xiaokangwang/VLite/transport/packetuni/puniCommon"
"github.com/xiaokangwang/VLite/transport/udp/udpClient"
"github.com/xiaokangwang/VLite/transport/udp/udpuni/udpunic"
"github.com/xiaokangwang/VLite/transport/uni/uniclient"
client2 "github.com/xiaokangwang/VLite/workers/client"
"github.com/xiaokangwang/VLite/workers/tcp/tcpClient"
"io"
"log"
"net"
"strings"
)
func NewUdptlsSctpClientDirect(remoteAddress string, password string, ctx context.Context) *UdptlsSctpClientDirect {
utsc := &UdptlsSctpClientDirect{}
utsc.Address = remoteAddress
utsc.password = []byte(password)
utsc.msgbus = ibus.NewMessageBus()
ctxwbus := context.WithValue(ctx, interfaces.ExtraOptionsMessageBus, utsc.msgbus)
utsc.ctx = ctxwbus
useUniConn := false
if strings.HasPrefix(remoteAddress, "uni+") {
useUniConn = true
remoteAddress = remoteAddress[4:]
}
useWs := false
if strings.HasPrefix(remoteAddress, "ws+") {
useWs = true
remoteAddress = remoteAddress[3:]
}
if strings.HasPrefix(remoteAddress, "http") {
if useWs {
utsc.ctx = context.WithValue(utsc.ctx, interfaces.ExtraOptionsUseWebSocketInsteadOfHTTP, useWs)
}
utsc.udpdialer = httpClient.NewProviderClientCreator(remoteAddress, 2, 2, password, utsc.ctx)
} else {
if strings.HasPrefix(remoteAddress, "fec+") {
remoteAddress = remoteAddress[4:]
utsc.ctx = context.WithValue(utsc.ctx, interfaces.ExtraOptionsUDPFECEnabled, true)
}
utsc.ctx = context.WithValue(utsc.ctx, interfaces.ExtraOptionsUDPMask, string(utsc.password))
utsc.udpdialer = udpClient.NewUdpClient(remoteAddress, utsc.ctx)
if useUniConn {
utsc.udpdialer = udpunic.NewUdpUniClient(string(utsc.password), utsc.ctx, utsc.udpdialer)
}
}
if useUniConn {
unis := uniclient.NewUnifiedConnectionClient(utsc.udpdialer, utsc.ctx)
utsc.udpdialer = unis
utsc.uni = unis
}
return utsc
}
type UdptlsSctpClientDirect struct {
Address string
ctx context.Context
udpdialer transport.UnderlayTransportDialer
udprelay *udpsctpserver.PacketSCTPRelay
udpserver *client2.UDPClientContext
uni *uniclient.UnifiedConnectionClient
puni *puniClient.PacketUniClient
password []byte
TunnelTxToTun chan interfaces.UDPPacket
TunnelRxFromTun chan interfaces.UDPPacket
msgbus *bus.Bus
connCtx context.Context
httpac *httpClient.ProviderClient
}
func (s *UdptlsSctpClientDirect) Dial(network, address string, port uint16, ctx context.Context) (net.Conn, error) {
return s.DialDirect(address, port)
}
func (s *UdptlsSctpClientDirect) DialDirect(address string, port uint16) (net.Conn, error) {
var Stream io.ReadWriteCloser
if s.httpac != nil {
Stream = s.httpac.DialWsAlternativeChannelConnection(s.ctx)
} else if s.uni != nil && UsePuni {
Stream = s.puni.ClientOpenStream()
} else {
Stream = s.udprelay.ClientOpenStream()
}
var w = &bytes.Buffer{}
tcpClient.WriteTcpDialHeader(w, address, port)
lw := &bytes.Buffer{}
lenb := &proto.StreamConnectDomainHeaderLen{}
lenb.Length = uint16(w.Len())
struc.Pack(lw, lenb)
_, _ = Stream.Write(lw.Bytes())
len2, err := Stream.Write(w.Bytes())
if err != nil {
fmt.Println(err)
return nil, err
}
if len2 != w.Len() {
fmt.Println("cannot write exact length")
return nil, errors.New("cannot write exact length")
}
//fmt.Println("Connected")
return &UdptlsSctpClientStramToNetConnAdp{rwc: Stream}, nil
}
func (s *UdptlsSctpClientDirect) NotifyMeltdown(reason error) {
panic("implement me")
}
func (s *UdptlsSctpClientDirect) Up() {
//Open Connection
conn, err, connctx := s.udpdialer.Connect(s.ctx)
if err != nil {
log.Println(err)
}
C_C2STraffic := make(chan client2.UDPClientTxToServerTraffic, 8)
C_C2SDataTraffic := make(chan client2.UDPClientTxToServerDataTraffic, 8)
C_S2CTraffic := make(chan client2.UDPClientRxFromServerTraffic, 8)
C_C2STraffic2 := make(chan interfaces.TrafficWithChannelTag, 8)
C_C2SDataTraffic2 := make(chan interfaces.TrafficWithChannelTag, 8)
C_S2CTraffic2 := make(chan interfaces.TrafficWithChannelTag, 8)
go func(ctx context.Context) {
for {
select {
case data := <-C_C2STraffic:
C_C2STraffic2 <- interfaces.TrafficWithChannelTag(data)
case <-ctx.Done():
return
}
}
}(connctx)
go func(ctx context.Context) {
for {
select {
case data := <-C_C2SDataTraffic:
C_C2SDataTraffic2 <- interfaces.TrafficWithChannelTag(data)
case <-ctx.Done():
return
}
}
}(connctx)
go func(ctx context.Context) {
for {
select {
case data := <-C_S2CTraffic2:
C_S2CTraffic <- client2.UDPClientRxFromServerTraffic(data)
case <-ctx.Done():
return
}
}
}(connctx)
TunnelTxToTun := make(chan interfaces.UDPPacket)
TunnelRxFromTun := make(chan interfaces.UDPPacket)
s.TunnelTxToTun = TunnelTxToTun
s.TunnelRxFromTun = TunnelRxFromTun
if s.uni != nil && UsePuni {
s.puni = puniClient.NewPacketUniClient(C_C2STraffic2, C_C2SDataTraffic2, C_S2CTraffic2, s.password, connctx)
s.puni.OnAutoCarrier(conn, connctx)
s.udpserver = client2.UDPClient(connctx, C_C2STraffic, C_C2SDataTraffic, C_S2CTraffic, TunnelTxToTun, TunnelRxFromTun, s.puni)
} else {
s.udprelay = udpsctpserver.NewPacketRelayClient(conn, C_C2STraffic2, C_C2SDataTraffic2, C_S2CTraffic2, s.password, connctx)
s.udpserver = client2.UDPClient(connctx, C_C2STraffic, C_C2SDataTraffic, C_S2CTraffic, TunnelTxToTun, TunnelRxFromTun, s.udprelay)
}
s.connCtx = connctx
}
func (s *UdptlsSctpClientDirect) Reconnect() {
if UsePuni {
puniCommon.ReHandshake2(s.connCtx, true)
} else {
s.uni.ReconnectUnder(s.connCtx)
}
}
func (s *UdptlsSctpClientDirect) AlternativeChannel(url string) {
s.httpac = httpClient.NewProviderClientCreator(url, 0, 0, string(s.password), s.ctx).ConnectL(s.ctx)
}