Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if(!isset($argv[2]))
- {
- while(1)
- {
- for($port=9051;$port<=9060;$port++)
- {
- execInBackground("php run.php $port 1");
- }
- $port=9051;
- sleep(1);
- }
- }
- else
- {
- $tbot = TorBot::getInst();
- $tbot->config = array(
- 'serv' => 'localhost',
- 'port' => 6667,
- 'nick' => 'lol'.substr(mt_rand(), 0, strlen(mt_rand())/2),
- 'name' => mt_rand(),
- 'chan' => array('#bots'),
- 'tor_addr' => 'localhost',
- 'tor_port' => isset($argv[1])?$argv[1]:9050
- );
- $tbot->Init();
- }
- ###########################################################
- function execInBackground($cmd)
- {
- if (substr(php_uname(), 0, 7) == "Windows")
- {
- pclose(popen("start /B ". $cmd, "r"));
- }
- else
- {
- exec($cmd . " > /dev/null &");
- }
- }
- ###########################################################
- class TorBot
- {
- private $is_CLI = false;
- private $socket = null;
- private $ex = null;
- private $tstamp = '0000-00-00 00:00:00';
- private $nicks = array();
- private $chans = array();
- private $curruser = null;
- private $currchan = null;
- private $userwhois = null;
- public $config = array(
- 'serv' => 'please.change.me', # Bot Serv
- 'port' => 6667, # Bot Port
- 'nick' => 'tbot', # Bot Nick
- 'name' => 'TorBot', # Bot Name
- 'chan' => array('#chan1','#chan2'), # Bot Chan
- 'tor_serv' => 'localhost', # Tor Serv
- 'tor_port' => 9050, # Tor Port
- );
- public static function getInst()
- {
- static $inst = null;
- if ($inst === null)
- {
- $inst = new self;
- }
- return $inst;
- }
- // Initital functions
- public function Init()
- {
- // Timestamps
- $this->tstamp = (new \DateTime())->format('ymd.H:i:s');
- // Start connection to IRC
- //$this->socket = fsockopen($this->config['serv'], $this->config['port']);
- $this->socket = new Socks($this->config['serv'], $this->config['port'], $this->config['tor_addr'], $this->config['tor_port']);
- // Send USER/NICK to IRC
- $this->sendData('USER', "{$this->config['nick']} irc.land {$this->config['nick']} :{$this->config['name']}\r\n");
- $this->sendData('NICK', "{$this->config['nick']}\r\n");
- while(true)
- {
- // Bot Core
- $this->ex = $this->recvData();
- // Play Ping-Pong
- if($this->ex[0] == 'PING')
- {
- $this->sendData('PONG', "{$this->ex[1]}");
- }
- // Set Current User/Chan
- $this->curruser = substr(explode('!', $this->ex[0])[0], 1);
- $this->currfunc = @$this->ex[1];
- $this->currchan = @$this->ex[2];
- // Switch Functions
- $this->preSwitches();
- } // While(true)
- }
- // Initital Core functions
- private function preSwitches()
- {
- // preSwitches include Pre commands, and post nick/join/kick/part/quit
- switch(@$this->ex[1])
- {
- case '001': // Check for time to join
- if(is_array($this->config['chan']))
- {
- foreach($this->config['chan'] as $chan)
- {
- $this->sendData('JOIN', "$chan\r\n");
- $this->sendData("PRIVMSG $chan :", "TrollololTrollololTrollololTrollololTrollolol\r\n");
- $this->socket->ph_close();
- }
- }
- else
- {
- $this->sendData('JOIN', "{$this->config['chan']}\r\n");
- }
- break;
- #####################
- case '311': // Fetch Whois Info
- $ex = $this->ex;
- unset($ex[0], $ex[1], $ex[2], $ex[6], $ex[7]);
- $this->userwhois = $ex;
- break;
- #####################
- case '353': // Fetch Nicks from Channel
- $ex = $this->ex;
- $chan = $ex[4];
- unset($ex[0], $ex[1], $ex[2], $ex[3], $ex[4]);
- $nicks = implode(' ', $ex);
- $nicks = str_replace(':', '', $nicks);
- $nicks = str_replace('@', '', $nicks);
- $this->nicks[$chan] = explode(' ', ucwords($nicks));
- break;
- #####################
- case '433': // Set random nick, if Nick inuse
- $randId = substr(mt_rand(), 0, strlen(mt_rand())/2);
- $this->sendData('NICK', "{$this->config['nick']}$randId\r\n");
- break;
- #####################
- case 'NICK': // Update Nicks array on Nick Change
- $olduser = trim(ucwords(str_replace(':', '', explode("!", substr($this->ex[0], 1))[0])));
- $newuser = trim(ucwords($this->ex[2]));
- if($olduser == ucwords($this->config['nick']))
- {
- $this->sendData('NICK', "{$this->config['nick']}\r\n");
- }
- foreach($this->chans as $chan)
- {
- foreach($this->nicks[$chan] as $nickId => $nick)
- {
- if($olduser == $nick)
- {
- $this->nicks[$chan][$nickId] = $newuser;
- }
- }
- }
- break;
- #####################
- case 'JOIN':
- $chan = trim(strtolower(substr($this->ex[2], 1)));
- $user = trim(ucwords(explode("!", substr($this->ex[0], 1))[0]));
- $this->chans[$chan] = $chan;
- $this->nicks[$chan][] = $user;
- break;
- #####################
- case 'KICK':
- $this->sendData('JOIN', "{$this->ex[2]}\r\n");
- break;
- #####################
- case 'PART':
- $olduser = trim(ucwords(str_replace(':', '', explode("!", substr($this->ex[0], 1))[0])));
- $this->currchan = trim($this->currchan);
- foreach($this->nicks[$this->currchan] as $nickId => $nick)
- {
- if($olduser == $nick)
- {
- unset($this->nicks[$this->currchan][$nickId]);
- }
- }
- break;
- #####################
- case 'QUIT':
- $olduser = trim(ucwords(str_replace(':', '', explode("!", substr($this->ex[0], 1))[0])));
- foreach($this->chans as $chan)
- {
- foreach($this->nicks[$chan] as $nickId => $nick)
- {
- if($olduser == $nick)
- {
- unset($this->nicks[$chan][$nickId]);
- }
- }
- }
- break;
- }
- }
- // Send/Recv/Log Functions
- private function sendData($command, $message = null)
- {
- echo 'SND-MSG: ';
- if($message == null)
- {
- echo "$command" . PHP_EOL;
- //fputs($this->socket, (string)"$command\r\n");
- $this->socket->ph_write((string)"$command\r\n");
- }
- else
- {
- echo "$command -> $message" . PHP_EOL;
- //fputs($this->socket, (string)"$command $message\r\n");
- $this->socket->ph_write((string)"$command $message\r\n");
- }
- }
- private function recvData()
- {
- //$data = fgets($this->socket, 1024);
- $data = $this->socket->ph_read(8192);
- if($data != '')
- {
- echo "RCV-MSG: $data" . PHP_EOL;
- }
- return explode(' ', $data);
- }
- }
- class Socks
- {
- function __construct($ph_desthost,$ph_destport,$ph_proxyhost="localhost",$ph_proxyport="9150")
- {
- //Set some default attributes.
- $this->ready = false;
- //Establish connection to SOCKS proxy server and perform handshake.
- //Initiate class variables for error handling using $this->errno and $this->errbuf
- $this->socket = @fsockopen($ph_proxyhost,$ph_proxyport,$this->errno,$this->errbuf);
- if ( !$this->socket ) {
- //Can't connect to the local proxy.
- $this->ph_error("Connection to proxy server could not be established on ".$ph_proxyhost.":".$ph_proxyport." (Connection refused)");
- $this->socket_status = -1;
- return false;
- }
- //Connection has been established.
- $this->socket_status = 1;
- //Initiate the SOCKS handshake sequence.
- //Write our version an method to the server.
- //Version 5, 1 authentication method, no authentication. (For now)
- @fwrite($this->socket, pack("C3", 0x05, 0x01, 0x00) );
- //Wait for a reply from the SOCKS server.
- $this->server_status = @fread($this->socket,8192);
- //Check if server status is okay.
- //Throw error if required.
- if( $this->server_status == pack("C2", 0x05, 0x00) )
- {
- $this->socket_status = 2;
- }
- else
- {
- $this->ph_error("SOCKS Server does not support this version and/or authentication method of SOCKS.");
- $this->socket_status = -2;
- return false;
- }
- //At this stage, our SOCKS socket should be open and ready to make its remote connection.
- //Send the connection request.
- @fwrite( $this->socket, pack("C5", 0x05 , 0x01 , 0x00 , 0x03, strlen($ph_desthost) ) . $ph_desthost . pack("n", $ph_destport) );
- //Wait for a reply from the SOCKS server.
- $this->server_buffer = fread($this->socket,8192);
- //Check is the connection succeeded.
- if( $this->server_buffer == pack("C10", 0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ) )
- {
- //Connection succeeded.
- $this->socket_status = 3;
- }
- else
- {
- //Connection failed.
- $this->ph_error("The SOCKS server failed to connect to the specificed host and port. ( ".$ph_desthost.":".$ph_destport." )");
- $this->socket_status = -3;
- return false;
- }
- //Our socket is now ready to write data.
- if ($this->socket_status == 3)
- {
- $this->ready = true;
- }
- }
- public function __destruct()
- {
- //Close the socket connection if it's open
- if ( $this->socket )
- {
- @fclose( $this->socket );
- }
- }
- public function ph_write($ph_data)
- {
- //Write the data to the socket.
- @fwrite($this->socket,$ph_data);
- }
- public function ph_read($ph_buffersize=8192)
- {
- //Read the data from the socket.
- return @fread($this->socket,$ph_buffersize);
- }
- public function ph_close()
- {
- fclose($this->socket);
- }
- public function ph_error($ph_errormsg)
- {
- print($ph_errormsg."\n");
- }
- public function ph_debug_dump()
- {
- print_r($this);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment