Valhalla Legends Forums Archive | Battle.net Bot Development | Finished PHP Socket for Battle.net!

AuthorMessageTime
LockesRabb
Topic resolved.
March 29, 2004, 8:36 AM
Null
this has been covered here before , use the search feature provided to look for it.
March 29, 2004, 9:13 AM
LockesRabb
Did. I ain't that stupid. Found some posts, but none of then answered my question about using sockets in php to connect to battle.net. If you actually found one, I'd be impressed since I searched the forum archives twice using different keywords. once, using PHP as keyword, twice using PHP socket. Useless results either way. PHP has WAY massive results, and i've waded thru it, only to find useless results and even more useless results, while PHP socket brought up only one post of relevance:

<<Post by Tuberload in response to PrivateWilly's post>>
You are either going to have to interface the PHP script with a bot made in another programming language, or make a bot of sort out of PHP that will connect, logon, join the specified channel, and gather information.

I would go with the first suggestion, but either should work. Keep in mind the second solution assumes PHP supports sockets, and I don't know whether it does or not.

Edit: PHP does have socket support, so code on!
<<End of post>>

As you can see, doesn't give info on how php sockets should be used in order to connect to battle.net and login via the telnet gateway. Trust me, I've used planetsourcecode.com's search, google.com's search, yahoo.com's search. Even asked two fellow php coders. they got hostile and didn't want to tell me how sockets should be used to connect to battle.net because they wanted to be "the only ones with working scripts that connected to battle.net and used it".

So as a result, I'm a tad bit quite sick of searching everywhere for something that doesn't exist or doesn't want to be found, and I'm tired of trying to pry this information out of people.

Forgive me if I've offended you, as that was quite unintentional.
March 29, 2004, 9:24 AM
tA-Kane
[quote author=LockesRabb link=board=17;threadid=6055;start=0#msg52396 date=1080549411][code]<?
   $cfgServer = "useast.battle.net";
   $cfgPort = 6112;
   $cfgTimeOut = 10;
   $cfgUser = "something";
   $cfgPassword = "something";

   echo "Configuration set.\n";

   // open a socket
   if(!$cfgTimeOut){
    // without timeout
      echo "Connecting...\n";
   $usenet_handle = fsockopen($cfgServer, $cfgPort);
   } else {
    // with timeout
      echo "Connecting...\n";
   $usenet_handle = fsockopen($cfgServer, $cfgPort, &$errno, &$errstr, $cfgTimeOut);
   }
   if(!$usenet_handle) {
   echo "Connexion failed\n";
    exit();
   } else {
    echo "Connected!\n";
   $tmp = fgets($usenet_handle, 1024);
   }
   echo "Attempting to login...\n";
   echo $tmp."\n";
   fputs($usenet_handle, $cfgUser."\n");
   $tmp = fgets($usenet_handle, 1024);
   echo $tmp."\n";
   fputs($usenet_handle, $cfgPassword."\n");
   $tmp = fgets($usenet_handle, 1024);
   echo $tmp."\n";
?>[/code][/quote]It does not appear as though you are identifying which type of connection you are, but are heading straight to sending your username and password.

Assuming you're trying to logon a CHAT client, you need to send 3 as a byte to identify yourself as a CHAT connection (rather than BnFTP, or game client, or perhaps others). Also, if you want to disable the server's echoing of what you sent, you need to also send 4 as a byte (which is only relevant with CHAT connections).

Lastly, please make use of the [ code ] tags.
March 29, 2004, 6:02 PM
LockesRabb
Post cleared for security reasons.
March 29, 2004, 11:39 PM
LockesRabb
Post cleared for security reasons.
March 30, 2004, 12:04 AM
Kp
[quote author=LockesRabb link=board=17;threadid=6055;start=0#msg52517 date=1080605066]but it seems to still be hanging right about where it attempts login... ideas?[/quote]

If you haven't already, consider having your script just parrot whatever battle.net sends it to the end client. That'll determine whether your parser is broken or whether the BNCS really isn't sending any other data.
March 30, 2004, 12:56 AM
Null
https://davnit.net/bnet/vL/phpbbs/index.php?board=17;action=display;threadid=3661
March 30, 2004, 1:03 AM
LockesRabb
E-mail me for the solution.
March 30, 2004, 7:20 AM
Maddox
If you want to get started on a Binary Bot, then here is a class that should help.

[code]
class buffer
{
   var $buffer   = "";
   var $size   = 0;

   function addSTRING($data)
   {
      $this->buffer .= $data . chr(0);
      $this->size += strlen($data) + 1;
   }

   function addDWORD($data)
   {
      $this->buffer .= $this->getDWORD($data);

      $this->size += 4;
   }

   function addWORD($data)
   {
      $this->buffer .= $this->getWORD($data);

      $this->size += 2;
   }

   function addBYTE($data)
   {
      $this->buffer .= chr($data);

      $this->size++;
   }

   function getDWORD($data)
   {
      $buf = chr((($data & 0x000000FF) >> 0));
      $buf .= chr((($data & 0x0000FF00) >> 8));
      $buf .= chr((($data & 0x00FF0000) >> 16));
      $buf .= chr((($data & 0xFF000000) >> 24));

      return $buf;
   }

   function getWORD($data)
   {
      $buf = chr((($data & 0x00FF) >> 0));
      $buf .= chr((($data & 0xFF00) >> 8));

      return $buf;
   }

   function output()
   {
      return $this->buffer;
   }
}

class bnetbuffer extends buffer
{
   function output($packetid)
   {
      $header = chr(0xff) . chr($packetid) . getWORD($this->size);

      return $header.$this->buffer;
   }
}[/code]
March 31, 2004, 3:57 AM
SKiLLs
You know when you connect with Chat you got to send C.. Try it your self on Telnet. Make sur e u send C..

Link:
http://www.blizzard.com/support/?id=mall0578p

Don't know if you had this in your code or not, just trying to help out.
I like PHP and would love to hear someone did something like this!
April 1, 2004, 3:26 AM
LockesRabb
[quote author=SKiLLs link=board=17;threadid=6055;start=0#msg52971 date=1080789963]
You know when you connect with Chat you got to send C.. Try it your self on Telnet. Make sur e u send C..

Link:
http://www.blizzard.com/support/?id=mall0578p

Don't know if you had this in your code or not, just trying to help out.
I like PHP and would love to hear someone did something like this!
[/quote]

Nah, don't need c to be in code- the script already sends the neccessary data to establish a connection. And the script is fully functional. Try it out for yourself! :-)
April 1, 2004, 5:08 AM
LockesRabb
[quote author=Maddox link=board=17;threadid=6055;start=0#msg52792 date=1080705467]
If you want to get started on a Binary Bot, then here is a class that should help.

[code]
class buffer
{
   var $buffer   = "";
   var $size   = 0;

   function addSTRING($data)
   {
      $this->buffer .= $data . chr(0);
      $this->size += strlen($data) + 1;
   }

   function addDWORD($data)
   {
      $this->buffer .= $this->getDWORD($data);

      $this->size += 4;
   }

   function addWORD($data)
   {
      $this->buffer .= $this->getWORD($data);

      $this->size += 2;
   }

   function addBYTE($data)
   {
      $this->buffer .= chr($data);

      $this->size++;
   }

   function getDWORD($data)
   {
      $buf = chr((($data & 0x000000FF) >> 0));
      $buf .= chr((($data & 0x0000FF00) >> 8));
      $buf .= chr((($data & 0x00FF0000) >> 16));
      $buf .= chr((($data & 0xFF000000) >> 24));

      return $buf;
   }

   function getWORD($data)
   {
      $buf = chr((($data & 0x00FF) >> 0));
      $buf .= chr((($data & 0xFF00) >> 8));

      return $buf;
   }

   function output()
   {
      return $this->buffer;
   }
}

class bnetbuffer extends buffer
{
   function output($packetid)
   {
      $header = chr(0xff) . chr($packetid) . getWORD($this->size);

      return $header.$this->buffer;
   }
}[/code]
[/quote]

Thanks for the info! But I won't be developing a binary bot in PHP since PHP sucks for that. If I was going to develop one, I'd probably do it in C. But I'll keep that info in storage for if I ever do decide to take on the project.
April 1, 2004, 5:12 AM
AC_Drkan
Can't you just use telnet?

telnet useast.battle.net 6112
then type 'c' when you see the cursor
May 6, 2004, 9:43 AM
Forged
Yeah but then it wouldn't be a script on his site now woud it...
May 6, 2004, 12:55 PM
AC_Drkan
[quote author=Forged link=board=17;threadid=6055;start=0#msg58788 date=1083848115]
Yeah but then it wouldn't be a script on his site now woud it...
[/quote]
True true true
May 11, 2004, 9:25 AM
AC_Drkan
So is there any way to keep a socket open?

like if i wanted to run a bot off of a server would their be any way?
June 8, 2004, 5:18 PM
eurolan-StorM
[code][/code][code]
<?

/*   
Project   AdminCraft
Module      -
File      /classes/bot.class.php
Aim      Class of the Bot to Battle.Net.
*/

require_once "C:\Program Files\EasyPHP1-7\php\pear\go-pear-bundle\PEAR.php" ;

define ('BNET_DISCONNECTED', 0) ;
define ('BNET_CONNECTED', 1) ;
define ('BNET_LOGGEDIN', 2) ;

class Bot {
   var $server ;
   var $port ;
   var $flood ;

   
   var $fp ;
   
   var $sbuff ;
   var $rbuff ;
   var $hbuff ;
   
   var $status ;
   
   var $historySize ;
   var $handlers ;
   
   function Bot (
      $server,
      $port,
      $flood = 5
      ) {
      
      $this->server = $server ;
      $this->port = $port ;
      $this->flood = $flood ;
      
      $this->sbuff = $this->rbuff = $this->hbuff = array() ;
      $this->status = BNET_DISCONNECTED ;
      $this->historySize = 30 ;
      
      $this->handlers = array() ;
   
   }

   function setHistory($size) {
      $this->historySize = $size ;
   }
   
   function connect() {
      $fp = fsockopen($this->server, $this->port, $errno, $errstr, 30);
      if(!$fp) {
         echo "$errstr, $errno" ;
      }
      socket_set_blocking($fp, FALSE) ;
      $this->fp = $fp ;
      $this->status = BNET_CONNECTED ;
   }
   
   function disconnect() {
      echo "Fermeture en cours...\n" ;
      fclose($this->fp) ;
      exit ();
      $this->status = BNET_DISCONNECTED ;
   }
   
   function login($login, $pass = false) {
      $l = chr(3).chr(4).$login."\r\n" ;
      if ($pass) $l .= $pass."\r\n" ;
      $this->send($l) ;
      
      $GLOBALS['_loggedin'] = $this->status == BNET_LOGGEDIN ;
      while ($this->status == BNET_CONNECTED) {
         $this->register('NAME', '_login_handler') ;
         $this->idle() ;
         sleep(1) ;
         if ($GLOBALS['_loggedin']) $this->status = BNET_LOGGEDIN ;
      }
      if ($this->status == BNET_DISCONNECTED) {
         echo 'Connexion perdue au login' ;
      }
      $this->status = BNET_LOGGEDIN ;
   }

   
   function idle() {
      $this->_commit() ;
      $this->_read() ;
      while ($this->rQueueCount()) {
         $data = $this->get() ;
         if (trim($data)) {
            list($actionNo, $actionName, $text) = explode(' ', $data, 3) ;
            if (array_key_exists($actionName, $this->handlers)) {
               $func = $this->handlers[$actionName] ;
               $func($actionNo, $text) ;
            }
         }
      }
   }
   
   function tempo() {
      static $last = 0 ;
      $now = mktime() ;
      if ($now-$last > $this->flood) {
         $last = $now ;
         return true ;
      }
      
      return FALSE ;
   }
   
   function _commit() {
       if ($this->tempo() and $this->sQueueCount()) {
         $data = array_shift($this->sbuff) ;
         fputs($this->fp, $data) ;
         $this->_historize('> '.$data) ;
      }
   }
   
   function send($data) {
      array_push($this->sbuff, $data."\r\n") ;
      $this->idle();
      echo $this->getHistory('/ERROR|WHISPER/'); // Pour le débugage : echo $bot->getHistory('/ERROR|WHISPER|\>/') ;
      sleep(1);

   }
   
   function _read() {
      while (!feof($this->fp) and $data = fgets($this->fp, 512)) {
         array_push($this->rbuff, $data) ;
         $this->_historize('< '.$data) ;
      }
      if (feof($this->fp)) {
         echo 'Connexion perdue' ;
         $this->status = "BNET_DISCONNECTED" ;
      }
      
   }
   
   function sQueueCount() {
      return count($this->sbuff) ;
   }
   function rQueueCount() {
      return count($this->rbuff) ;
   }
   
   function get() {
      if (!$this->rQueueCount()) echo 'File vide' ;
      return array_shift($this->rbuff) ;
   }
   
   function _historize($data) {
      array_push($this->hbuff, $data) ;
      if (count($this->hbuff) > $this->historySize) $void = array_shift($this->hbuff) ;
   }
   
   function getHistory($filter = '/.*/') {
      $hist = '' ;
      while ($data = array_shift($this->hbuff)) {
         if (preg_match($filter, $data)) $hist .= $data ;
      }
      return $hist ;
   }
   
   function register($type, $function) {
      $this->handlers[$type] = $function ;
   }
   
   function getStatus() {
      return $this->status ;
   }
   
   function whisp($aka, $message) {
   $this->send("/w $aka $message") ;
   }
   
   function ann($aka, $message) {
   $this->send("/ann $aka $message") ;
   }
   
   function ping($server) {
   $server = $server;
      if (eregi('windows', $_SERVER["HTTP_USER_AGENT"])) {
      $arg = '-n 1';
      }
      else {
      $arg = '-c 1';
      }
   $list = exec('ping '.$server.' '.$arg);
   $ping = explode (' ',$list);
      foreach ($ping as $valeur) {
         if (eregi('ms', $valeur)) {
         static $i = 1;
         ++$i;      
            if ($i == 3) {
            echo "Votre ping est de $valeur sur le serveur\n";
            }
         }
      }
   }
}
   function _login_handler($no, $txt) {
   $GLOBALS['_loggedin'] = true ;
   }

?>
[/code]
June 8, 2004, 7:04 PM
LockesRabb
mmmm that scripts superior to mine. 0.o
June 9, 2004, 12:44 AM
eurolan-StorM
I create other class with this to have a powerfull bot : many commands, database gestion (xml or mysql), tournament management.
Do you have an idea how to create a bot how can log himself as warcraft client ?
June 9, 2004, 6:29 AM
LockesRabb
ummm if ur using the chat protocol for the script, then i'd say no way really- because when you login, you're set up as a chat user, meaning if you wanted to reterive ur own stats, ud have to specify what client.

However if you truly want to login as a warcraft client using PHP, that's a tad bit complicated because then it'd be a binary bot- it'd involve the use of hashes, check revision, packet handling, and et centra, which would be a toughie if done via PHP.

But then don't take my word for it, since I've not succeeded at coding a binary bot yet in VB. :-P
June 10, 2004, 12:06 AM
eurolan-StorM
Yes it will be complicated to re write hash for php :-/
June 10, 2004, 11:49 AM
tA-Kane
It's cases like this which BNLS because extremely useful. It decreases coding time as well as execution time (being that PHP is a script, all of the hashing functions... CheckRevision in particular, will run very slowly).
June 12, 2004, 5:57 PM

Search