@@ -25,15 +25,15 @@ type UDPPacket struct {
25
25
26
26
type Server struct {
27
27
PasswordHashes []string
28
- underlying net.Interface
28
+ Underlying net.Interface
29
29
30
30
initOnce sync.Once
31
31
}
32
32
33
33
func (s * Server ) Handle (conn * tcp.Conn ) tcp.SerRet {
34
34
s .initOnce .Do (func () {
35
- if s .underlying == nil {
36
- s .underlying = net .DefaultRouteTable
35
+ if s .Underlying == nil {
36
+ s .Underlying = net .DefaultRouteTable
37
37
}
38
38
})
39
39
@@ -66,15 +66,17 @@ func (s *Server) Handle(conn *tcp.Conn) tcp.SerRet {
66
66
switch metadata .Command {
67
67
case 0x1 : // Connect
68
68
var target net.Conn
69
+ var address string
69
70
switch metadata .Address .AddressType {
70
71
case IPv4 , IPv6 :
71
- target , err = s .underlying .Dial ("tcp" , gonet .JoinHostPort (metadata .Address .IP .String (), strconv .Itoa (metadata .Address .Port )))
72
- log .Verboseln ("[TROJAN]" , "Dialed TCP" , metadata .Address .IP .String (), metadata .Address .Port )
72
+ address = gonet .JoinHostPort (metadata .Address .IP .String (), strconv .Itoa (metadata .Address .Port ))
73
73
case DomainName :
74
- target , err = s .underlying .Dial ("tcp" , gonet .JoinHostPort (metadata .Address .DomainName , strconv .Itoa (metadata .Address .Port )))
75
- log .Verboseln ("[TROJAN]" , "Dialed TCP" , metadata .Address .DomainName , metadata .Address .Port )
74
+ address = gonet .JoinHostPort (metadata .Address .DomainName , strconv .Itoa (metadata .Address .Port ))
76
75
}
77
76
77
+ target , err = s .Underlying .Dial ("tcp" , address )
78
+ log .Verboseln ("[TROJAN]" , "Dialed TCP" , address )
79
+
78
80
if err != nil {
79
81
log .Verboseln ("[TROJAN]" , "Dial failed" , err )
80
82
return tcp .Close
@@ -160,43 +162,30 @@ func (s *Server) Handle(conn *tcp.Conn) tcp.SerRet {
160
162
}
161
163
162
164
// Determine target address
163
- var targetAddr * gonet.UDPAddr
164
- var addrKey string
165
+ var address string
165
166
166
167
switch addr .AddressType {
167
168
case IPv4 , IPv6 :
168
- targetAddr = & gonet.UDPAddr {
169
- IP : addr .IP ,
170
- Port : addr .Port ,
171
- }
172
- addrKey = targetAddr .String ()
169
+ address = gonet .JoinHostPort (addr .IP .String (), strconv .Itoa (addr .Port ))
173
170
case DomainName :
174
- // Resolve domain name
175
- resolvedAddr , err := gonet .ResolveUDPAddr ("udp" , gonet .JoinHostPort (addr .DomainName , strconv .Itoa (addr .Port )))
176
- if err != nil {
177
- log .Verboseln ("[TROJAN]" , "failed to resolve domain" , addr .DomainName , err )
178
- continue
179
- }
180
- targetAddr = resolvedAddr
181
- addrKey = gonet .JoinHostPort (addr .DomainName , strconv .Itoa (addr .Port ))
171
+ address = gonet .JoinHostPort (addr .DomainName , strconv .Itoa (addr .Port ))
182
172
}
183
173
184
- // Get or create UDP connection for this target
185
- target , exists := connTable [addrKey ]
174
+ target , exists := connTable [address ]
186
175
if ! exists {
187
- _target , err := s .underlying .Dial ("udp" , gonet . JoinHostPort ( targetAddr . IP . String (), strconv . Itoa ( targetAddr . Port )) )
176
+ _target , err := s .Underlying .Dial ("udp" , address )
188
177
189
178
if err != nil {
190
- log .Verboseln ("[TROJAN]" , "DialUDP failed for" , addrKey , err )
179
+ log .Verboseln ("[TROJAN]" , "DialUDP failed for" , address , err )
191
180
continue
192
181
}
193
182
194
183
target = _target .(* gonet.UDPConn )
195
184
196
- connTable [addrKey ] = target
185
+ connTable [address ] = target
197
186
go downlink (target )
198
187
199
- log .Verboseln ("[TROJAN]" , "Created new UDP connection to" , addrKey )
188
+ log .Verboseln ("[TROJAN]" , "new UDP connection to" , address )
200
189
}
201
190
202
191
n , err := io .ReadFull (r , buf [:length ])
@@ -210,7 +199,7 @@ func (s *Server) Handle(conn *tcp.Conn) tcp.SerRet {
210
199
211
200
if err != nil {
212
201
target .Close ()
213
- delete (connTable , addrKey )
202
+ delete (connTable , address )
214
203
continue
215
204
}
216
205
0 commit comments