class Rex::Proto::Proxy::Socks4a

A Socks4a proxy server.

Attributes

opts[R]

Public Class Methods

new( opts={} ) click to toggle source

Create a new Socks4a server.

# File lib/rex/proto/proxy/socks4a.rb, line 354
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

Public Instance Methods

add_client( client ) click to toggle source
# File lib/rex/proto/proxy/socks4a.rb, line 427
def add_client( client )
  @clients << client
end
is_running?() click to toggle source

Check if the server is running.

# File lib/rex/proto/proxy/socks4a.rb, line 366
def is_running?
  return @running
end
join() click to toggle source

Block while the server is running.

# File lib/rex/proto/proxy/socks4a.rb, line 402
def join
  @server_thread.join if @server_thread
end
remove_client( client ) click to toggle source
# File lib/rex/proto/proxy/socks4a.rb, line 431
def remove_client( client )
  @clients.delete( client )
end
start() click to toggle source

Start the Socks4a server.

# File lib/rex/proto/proxy/socks4a.rb, line 373
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("SOCKS4AProxyServer", false) do
        while( @running ) do
          begin
            # accept the client connection
            sock = @server.accept
            # and fire off a new client instance to handle it
            Client.new( self, sock ).start
          rescue
            wlog( "Socks4a.start - server_thread - #{$!}" )
          end
        end
      end
    rescue
      wlog( "Socks4a.start - #{$!}" )
      return false
    end
    return true
end
stop() click to toggle source

Stop the Socks4a server.

# File lib/rex/proto/proxy/socks4a.rb, line 409
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.concat( @clients )
    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