Class: Rex::Proto::Proxy::Socks4a

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

Overview

A Socks4a proxy server.

Defined Under Namespace

Classes: Client

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Socks4a

Create a new Socks4a server.


355
356
357
358
359
360
361
362
# File 'lib/rex/proto/proxy/socks4a.rb', line 355

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


436
437
438
# File 'lib/rex/proto/proxy/socks4a.rb', line 436

def opts
  @opts
end

Instance Method Details

#add_client(client) ⇒ Object


428
429
430
# File 'lib/rex/proto/proxy/socks4a.rb', line 428

def add_client( client )
  @clients << client
end

#is_running?Boolean

Check if the server is running.


367
368
369
# File 'lib/rex/proto/proxy/socks4a.rb', line 367

def is_running?
  return @running
end

#joinObject

Block while the server is running.


403
404
405
# File 'lib/rex/proto/proxy/socks4a.rb', line 403

def join
  @server_thread.join if @server_thread
end

#remove_client(client) ⇒ Object


432
433
434
# File 'lib/rex/proto/proxy/socks4a.rb', line 432

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

#startObject

Start the Socks4a server.


374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/rex/proto/proxy/socks4a.rb', line 374

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

#stopObject

Stop the Socks4a server.


410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/rex/proto/proxy/socks4a.rb', line 410

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