Module: Rex::Proto::Proxy::Socks5::TcpRelay

Defined in:
lib/rex/proto/proxy/socks5/server_client.rb

Overview

A mixin for a socket to perform a relay to another socket.

Instance Method Summary collapse

Instance Method Details

#relay(relay_client, relay_sock) ⇒ Object

TcpRelay data coming in from relay_sock to this socket.


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
# File 'lib/rex/proto/proxy/socks5/server_client.rb', line 22

def relay(relay_client, relay_sock)
  @relay_client = relay_client
  @relay_sock   = relay_sock
  # start the relay thread (modified from Rex::IO::StreamAbstraction)
  @relay_thread = Rex::ThreadFactory.spawn("SOCKS5ProxyServerTcpRelay", false) do
    loop do
      closed = false
      buf    = nil

      begin
        s = Rex::ThreadSafe.select([@relay_sock], nil, nil, 0.2)
        next if s.nil? || s[0].nil?
      rescue
        closed = true
      end

      unless closed
        begin
          buf = @relay_sock.sysread( 32768 )
          closed = buf.nil?
        rescue
          closed = true
        end
      end

      unless closed
        total_sent   = 0
        total_length = buf.length
        while total_sent < total_length
          begin
            data = buf[total_sent, buf.length]
            sent = self.write(data)
            total_sent += sent if sent > 0
          rescue
            closed = true
            break
          end
        end
      end

      if closed
        @relay_client.stop
        ::Thread.exit
      end
    end
  end
end