@@ -12,61 +12,6 @@ module Proxy
1212# A client connected to the proxy server.
1313#
1414module Socks5
15- #
16- # A mixin for a socket to perform a relay to another socket.
17- #
18- module TcpRelay
19- #
20- # TcpRelay data coming in from relay_sock to this socket.
21- #
22- def relay ( relay_client , relay_sock )
23- @relay_client = relay_client
24- @relay_sock = relay_sock
25- # start the relay thread (modified from Rex::IO::StreamAbstraction)
26- @relay_thread = Rex ::ThreadFactory . spawn ( "SOCKS5ProxyServerTcpRelay" , false ) do
27- loop do
28- closed = false
29- buf = nil
30-
31- begin
32- s = Rex ::ThreadSafe . select ( [ @relay_sock ] , nil , nil , 0.2 )
33- next if s . nil? || s [ 0 ] . nil?
34- rescue
35- closed = true
36- end
37-
38- unless closed
39- begin
40- buf = @relay_sock . sysread ( 32768 )
41- closed = buf . nil?
42- rescue
43- closed = true
44- end
45- end
46-
47- unless closed
48- total_sent = 0
49- total_length = buf . length
50- while total_sent < total_length
51- begin
52- data = buf [ total_sent , buf . length ]
53- sent = self . write ( data )
54- total_sent += sent if sent > 0
55- rescue
56- closed = true
57- break
58- end
59- end
60- end
61-
62- if closed
63- @relay_client . stop
64- ::Thread . exit
65- end
66- end
67- end
68- end
69- end
7015
7116 #
7217 # A client connected to the SOCKS5 server.
@@ -122,13 +67,13 @@ def start
12267
12368 # handle the request
12469 handle_command
125- rescue => exception
70+ rescue StndardError => e
12671 # respond with a general failure to the client
12772 response = ResponsePacket . new
12873 response . command = REPLY_GENERAL_FAILURE
12974 @lsock . put ( response . to_binary_s )
13075
131- wlog ( " Client.start - #{ $! } " )
76+ elog ( ' Client.start - encountered a problem while processing the client connection' , error : e )
13277 self . stop
13378 end
13479 end
@@ -259,15 +204,11 @@ def handle_command_udp_associate(request)
259204 end
260205
261206 #
262- # Setup the TcpRelay between lsock and rsock.
207+ # Setup the relay between lsock and rsock.
263208 #
264209 def setup_tcp_relay
265- # setup the two way relay for full duplex io
266- @lsock . extend ( TcpRelay )
267- @rsock . extend ( TcpRelay )
268- # start the socket relays...
269- @lsock . relay ( self , @rsock )
270- @rsock . relay ( self , @lsock )
210+ @server . relay_manager . add_relay ( @rsock , sink : @lsock , name : 'SOCKS5ProxyRelay-Remote' , on_exit : method ( :stop ) )
211+ @server . relay_manager . add_relay ( @lsock , sink : @rsock , name : 'SOCKS5ProxyRelay-Local' , on_exit : method ( :stop ) )
271212 end
272213
273214 #
@@ -286,7 +227,6 @@ def stop
286227 rescue
287228 end
288229
289- @client_thread . kill if @client_thread and @client_thread . alive?
290230 @server . remove_client ( self )
291231 @closed = true
292232 end
0 commit comments