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.



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

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
end

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



102
103
104
# File 'lib/rex/proto/proxy/socks5/server.rb', line 102

def opts
  @opts
end

Instance Method Details

#add_client(client) ⇒ Object



94
95
96
# File 'lib/rex/proto/proxy/socks5/server.rb', line 94

def add_client(client)
  @clients << client
end

#is_running?Boolean

Check if the server is running.

Returns:

  • (Boolean)


30
31
32
# File 'lib/rex/proto/proxy/socks5/server.rb', line 30

def is_running?
  return @running
end

#joinObject

Block while the server is running.



70
71
72
# File 'lib/rex/proto/proxy/socks5/server.rb', line 70

def join
  @server_thread.join if @server_thread
end

#remove_client(client) ⇒ Object



98
99
100
# File 'lib/rex/proto/proxy/socks5/server.rb', line 98

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

#startObject

Start the SOCKS5 server.



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

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.



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

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