Class: Rex::Proto::Proxy::Socks5::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/rex/proto/proxy/socks5/server.rb

Overview

A SOCKS5 proxy server.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Server

Create a new SOCKS5 server.



19
20
21
22
23
24
25
26
27
# File 'lib/rex/proto/proxy/socks5/server.rb', line 19

def initialize(opts={})
  @opts          = { 'ServerHost' => '0.0.0.0', 'ServerPort' => 1080 }
  @opts          = @opts.merge(opts)
  @server        = nil
  @clients       = ::Array.new
  @running       = false
  @server_thread = nil
  @relay_manager = Rex::IO::RelayManager.new
end

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



104
105
106
# File 'lib/rex/proto/proxy/socks5/server.rb', line 104

def opts
  @opts
end

#relay_managerObject (readonly)

Returns the value of attribute relay_manager.



105
106
107
# File 'lib/rex/proto/proxy/socks5/server.rb', line 105

def relay_manager
  @relay_manager
end

Instance Method Details

#add_client(client) ⇒ Object



96
97
98
# File 'lib/rex/proto/proxy/socks5/server.rb', line 96

def add_client(client)
  @clients << client
end

#is_running?Boolean

Check if the server is running.

Returns:

  • (Boolean)


32
33
34
# File 'lib/rex/proto/proxy/socks5/server.rb', line 32

def is_running?
  return @running
end

#joinObject

Block while the server is running.



72
73
74
# File 'lib/rex/proto/proxy/socks5/server.rb', line 72

def join
  @server_thread.join if @server_thread
end

#remove_client(client) ⇒ Object



100
101
102
# File 'lib/rex/proto/proxy/socks5/server.rb', line 100

def remove_client(client)
  @clients.delete(client)
end

#startObject

Start the SOCKS5 server.



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

def start
  begin
    # create the servers main socket (ignore the context here because we don't want a remote bind)
    @server = Rex::Socket::TcpServer.create(
      'LocalHost' => @opts['ServerHost'],
      'LocalPort' => @opts['ServerPort'],
      'Comm' => @opts['Comm']
    )
    # signal we are now running
    @running = true
    # start the servers main thread to pick up new clients
    @server_thread = Rex::ThreadFactory.spawn("SOCKS5ProxyServer", false) do
      while @running
        begin
          # accept the client connection
          sock = @server.accept
          # and fire off a new client instance to handle it
          ServerClient.new(self, sock, @opts).start
        rescue
          wlog("SOCKS5.start - server_thread - #{$!}")
        end
      end
    end
  rescue
    wlog("SOCKS5.start - #{$!}")
    return false
  end
  return true
end

#stopObject

Stop the SOCKS5 server.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rex/proto/proxy/socks5/server.rb', line 79

def stop
  if @running
    # signal we are no longer running
    @running = false
    # stop any clients we have (create a new client array as client.stop will delete from @clients)
    clients = @clients.dup
    clients.each do | client |
      client.stop
    end
    # close the server socket
    @server.close if @server
    # if the server thread did not terminate gracefully, kill it.
    @server_thread.kill if @server_thread and @server_thread.alive?
  end
  return !@running
end