Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/rex/proto/proxy/socks5/server.rb
Views: 11766
# -*- coding: binary -*-12require 'thread'3require 'rex/socket'45module Rex6module Proto7module Proxy89module Socks510#11# A SOCKS5 proxy server.12#13class Server14#15# Create a new SOCKS5 server.16#17def initialize(opts={})18@opts = { 'ServerHost' => '0.0.0.0', 'ServerPort' => 1080 }19@opts = @opts.merge(opts)20@server = nil21@clients = ::Array.new22@running = false23@server_thread = nil24end2526#27# Check if the server is running.28#29def is_running?30return @running31end3233#34# Start the SOCKS5 server.35#36def start37begin38# create the servers main socket (ignore the context here because we don't want a remote bind)39@server = Rex::Socket::TcpServer.create(40'LocalHost' => @opts['ServerHost'],41'LocalPort' => @opts['ServerPort'],42'Comm' => @opts['Comm']43)44# signal we are now running45@running = true46# start the servers main thread to pick up new clients47@server_thread = Rex::ThreadFactory.spawn("SOCKS5ProxyServer", false) do48while @running49begin50# accept the client connection51sock = @server.accept52# and fire off a new client instance to handle it53ServerClient.new(self, sock, @opts).start54rescue55wlog("SOCKS5.start - server_thread - #{$!}")56end57end58end59rescue60wlog("SOCKS5.start - #{$!}")61return false62end63return true64end6566#67# Block while the server is running.68#69def join70@server_thread.join if @server_thread71end7273#74# Stop the SOCKS5 server.75#76def stop77if @running78# signal we are no longer running79@running = false80# stop any clients we have (create a new client array as client.stop will delete from @clients)81clients = @clients.dup82clients.each do | client |83client.stop84end85# close the server socket86@server.close if @server87# if the server thread did not terminate gracefully, kill it.88@server_thread.kill if @server_thread and @server_thread.alive?89end90return !@running91end9293def add_client(client)94@clients << client95end9697def remove_client(client)98@clients.delete(client)99end100101attr_reader :opts102end103end104end105end106end107108109