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.



354
355
356
357
358
359
360
361
# 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

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



439
440
441
# File 'lib/rex/proto/proxy/socks4a.rb', line 439

def opts
  @opts
end

Instance Method Details

#add_client(client) ⇒ Object



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

def add_client( client )
  @clients << client
end

#is_running?Boolean

Check if the server is running.

Returns:

  • (Boolean)


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

def is_running?
  return @running
end

#joinObject

Block while the server is running.



406
407
408
# File 'lib/rex/proto/proxy/socks4a.rb', line 406

def join
  @server_thread.join if @server_thread
end

#remove_client(client) ⇒ Object



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

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

#startObject

Start the Socks4a server.



373
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
399
400
401
# 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'],
        '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("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.



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

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