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.


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

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


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

def opts
  @opts
end

Instance Method Details

#add_client(client) ⇒ Object


92
93
94
# File 'lib/rex/proto/proxy/socks5/server.rb', line 92

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.


68
69
70
# File 'lib/rex/proto/proxy/socks5/server.rb', line 68

def join
  @server_thread.join if @server_thread
end

#remove_client(client) ⇒ Object


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

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
# 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'])
    # 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.


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

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