@@ -169,7 +169,8 @@ func (s *udpService) Serve(clientConn net.PacketConn) error {
169
169
}
170
170
171
171
cipherData := cipherBuf [:clientProxyBytes ]
172
- var textData []byte
172
+ var payload []byte
173
+ var tgtUDPAddr * net.UDPAddr
173
174
targetConn := nm .Get (clientAddr .String ())
174
175
if targetConn == nil {
175
176
clientLocation , locErr := s .m .GetLocation (clientAddr )
@@ -179,6 +180,7 @@ func (s *udpService) Serve(clientConn net.PacketConn) error {
179
180
debugUDPAddr (clientAddr , "Got location \" %s\" " , clientLocation )
180
181
181
182
ip := clientAddr .(* net.UDPAddr ).IP
183
+ var textData []byte
182
184
var cipher * ss.Cipher
183
185
unpackStart := time .Now ()
184
186
textData , keyID , cipher , err = findAccessKeyUDP (ip , textBuf , cipherData , s .ciphers )
@@ -188,36 +190,32 @@ func (s *udpService) Serve(clientConn net.PacketConn) error {
188
190
return onet .NewConnectionError ("ERR_CIPHER" , "Failed to unpack initial packet" , err )
189
191
}
190
192
193
+ var onetErr * onet.ConnectionError
194
+ if payload , tgtUDPAddr , onetErr = s .parsePacket (textData ); onetErr != nil {
195
+ return onetErr
196
+ }
197
+
191
198
udpConn , err := net .ListenPacket ("udp" , "" )
192
199
if err != nil {
193
200
return onet .NewConnectionError ("ERR_CREATE_SOCKET" , "Failed to create UDP socket" , err )
194
201
}
195
202
targetConn = nm .Add (clientAddr , clientConn , cipher , udpConn , clientLocation , keyID )
196
203
} else {
197
204
unpackStart := time .Now ()
198
- textData , err = ss .Unpack (nil , cipherData , targetConn .cipher )
205
+ textData , err : = ss .Unpack (nil , cipherData , targetConn .cipher )
199
206
timeToCipher = time .Now ().Sub (unpackStart )
200
207
if err != nil {
201
208
return onet .NewConnectionError ("ERR_CIPHER" , "Failed to unpack data from client" , err )
202
209
}
203
- }
204
- clientLocation = targetConn .clientLocation
205
210
206
- tgtAddr := socks .SplitAddr (textData )
207
- if tgtAddr == nil {
208
- return onet .NewConnectionError ("ERR_READ_ADDRESS" , "Failed to get target address" , nil )
209
- }
210
-
211
- tgtUDPAddr , err := net .ResolveUDPAddr ("udp" , tgtAddr .String ())
212
- if err != nil {
213
- return onet .NewConnectionError ("ERR_RESOLVE_ADDRESS" , fmt .Sprintf ("Failed to resolve target address %v" , tgtAddr ), err )
214
- }
215
- if err := s .targetIPValidator (tgtUDPAddr .IP ); err != nil {
216
- return err
211
+ var onetErr * onet.ConnectionError
212
+ if payload , tgtUDPAddr , onetErr = s .parsePacket (textData ); onetErr != nil {
213
+ return onetErr
214
+ }
217
215
}
216
+ clientLocation = targetConn .clientLocation
218
217
219
218
debugUDPAddr (clientAddr , "Proxy exit %v" , targetConn .LocalAddr ())
220
- payload := textData [len (tgtAddr ):]
221
219
proxyTargetBytes , err = targetConn .WriteTo (payload , tgtUDPAddr ) // accept only UDPAddr despite the signature
222
220
if err != nil {
223
221
return onet .NewConnectionError ("ERR_WRITE" , "Failed to write to target" , err )
@@ -228,6 +226,27 @@ func (s *udpService) Serve(clientConn net.PacketConn) error {
228
226
return nil
229
227
}
230
228
229
+ // Given the decrypted contents of a UDP packet, return
230
+ // the payload and the destination address, or an error if
231
+ // this packet cannot or should not be forwarded.
232
+ func (s * udpService ) parsePacket (textData []byte ) ([]byte , * net.UDPAddr , * onet.ConnectionError ) {
233
+ tgtAddr := socks .SplitAddr (textData )
234
+ if tgtAddr == nil {
235
+ return nil , nil , onet .NewConnectionError ("ERR_READ_ADDRESS" , "Failed to get target address" , nil )
236
+ }
237
+
238
+ tgtUDPAddr , err := net .ResolveUDPAddr ("udp" , tgtAddr .String ())
239
+ if err != nil {
240
+ return nil , nil , onet .NewConnectionError ("ERR_RESOLVE_ADDRESS" , fmt .Sprintf ("Failed to resolve target address %v" , tgtAddr ), err )
241
+ }
242
+ if err := s .targetIPValidator (tgtUDPAddr .IP ); err != nil {
243
+ return nil , nil , err
244
+ }
245
+
246
+ payload := textData [len (tgtAddr ):]
247
+ return payload , tgtUDPAddr , nil
248
+ }
249
+
231
250
func (s * udpService ) Stop () error {
232
251
s .mu .Lock ()
233
252
defer s .mu .Unlock ()
0 commit comments