duante
20.06.2006, 06:01
This is a multi socket server for tcp the original idea was to develop a login server using it however it would seem blowfish encryption does not sit well with php as far as correct decryption. If anybody was able to get a php blowfish class to decrypt login packets from a real l2 client correctly then making this act as the login server would be a piece of cake.
this is not the newest version of this class but if you are a decent php programmer it wont be hard for you to add your own functionality
oh this script was designed to be run from a console not from a browser :)
classSocket.php
[php:1:019ea06b56]
<?
//socket class for tcp server php script (runs on any web server with php & sockets installed!
class Socket {
//server vars
var $address = "";
var $port = "";
var $maxconn = 1;
//connections
var $count = 0;
var $sock = "";
var $socket = array();
var $read = array();
var $timeout = 10;
//premade packets
var $packets = array();
//starts a listening server
function serverStart($address = "", $port = "") {
if (!$address)
$address = $this->address;
if (!$port)
$port = $this->port;
//mark read with maxconn
for ($i = 0; $i < $this->maxconn; $i++) {
$this->read[$i] = "";
}
//mark socket with maxconn
for ($i = 0; $i < $this->maxconn; $i++) {
$this->socket[$i] = array('sock' => null, 'buffer' => null, 'time' => null);
}
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
//$sock = socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($sock, $address, $port);
socket_listen($sock);
return $sock;
}
function serverStop($sock) {
if (!sock)
$sock = $this->sock;
socket_close($sock);
}
function makeReadArray() {
$read[0] = $this->sock;
for ($i = 0; $i < $this->maxconn; $i++) {
if ($this->socket[$i]['sock'] != null)
$read[] = $this->socket[$i]['sock'];
}
$this->read = $read;
}
function getClientInput() {
for ($i = 0; $i < $this->maxconn; $i++) {
if (in_array($this->socket[$i]['sock'], $this->read)) {
//print "Found some input: $i\n";
$bytes = socket_read($this->socket[$i]['sock'], 1024);
$this->socket[$i]['buffer'] .= $bytes;
$this->socket[$i]['time'] = strtotime("now");
if ($bytes == null) $this->socket[$i]['time'] = 0; }
}
}
function checkTimeouts() {
for ($i = 0; $i < $this->maxconn; $i++) {
//print "Socket: $i - ".(strtotime("now") - strtotime("now", $this->socket[$i]['time']))."\n";
if ($this->socket[$i]['sock'] != null && $this->socket[$i]['time'] < strtotime("-".$this->timeout." seconds"))
$this->close($i);
}
}
function printBuffers() {
for ($i = 0; $i < $this->maxconn; $i++) {
print $this->socket[$i]['buffer']."\n";
}
}
function checkNewConn() {
if (in_array($this->sock, $this->read) || !$this->count)
$this->accept();
}
//accept socket connection
function accept($sock = "") {
if (!$sock)
$sock = $this->sock;
for ($i = 0; $i < $this->maxconn; $i++) {
if ($this->socket[$i]['sock'] == null) {
$this->socket[$i]['sock'] = socket_accept($sock);
$this->socket[$i]['time'] = strtotime("now");
$this->count++;
//print "New Connection Accepted! total(".$this->count.")\n";
break;
}
}
}
//create new packet
function newPacket($packet) {
$this->packets[$packet] = "";
}
//write byte
function addBytes($packet, $byte) {
$this->packets[$packet] .= chr($byte);
}
//write string
function addString($packet, $string) {
$this->packets[$packet] .= $string;
}
function send($socket, $packet) {
//print "Sent Packet($packet): ".$this->packets[$packet]."\n";
socket_write($this->socket[$socket]['sock'], $this->packets[$packet]);
}
function close($socket) {
socket_close($this->socket[$socket]['sock']);
$this->socket[$socket]['sock'] = null;
$this->socket[$socket]['buffer'] = null;
$this->socket[$socket]['time'] = null;
$this->count--;
//print "Connection Closed!\n";
}
}
?>
[/php:1:019ea06b56]
This example packets created were for a smtp server but you basically will get a feel for the socket class
exampleserver.php
[php:1:019ea06b56]
<?
set_time_limit(0);
ob_implicit_flush();
error_reporting(E_ALL);
print "Loading Class Socket\n";
include ("classSocket.php");
$socket = new Socket();
$socket->address = "127.0.0.1";
$socket->port = 7999;
$socket->maxconn = 10;
$socket->timeout = 5;
print "Starting Server\n";
$socket->sock = $socket->serverStart();
print "Making basic packets\n";
//create basic packets
$socket->newPacket("220");
$socket->addString("220", "220 Welcome to XXX Server... Please ensure you are not sending spam mail as we will blacklist your ip have a nice day. ".strtotime("now")."\r\n");
$socket->newPacket("221");
$socket->addString("221", "221 Connection Terminated");
$socket->newPacket("250");
$socket->addString("250", "250 OK");
$socket->newPacket("354");
$socket->addString("354", "354 Enter mail, end with '.' on a line by itself");
$socket->newPacket("500");
$socket->addString("500", "500 Unrecognized command or out of order command!");
print "Waiting for Connections!\n";
while (true) {
//make socket read array
print "loop start... total (".$socket->count.")\n";
print "make read array\n";
$socket->makeReadArray();
//wait for any connection to push data!
print "watch read array\n";
$ready = socket_select($socket->read, $write = null, $except = null, $tm = null);
//if a new connection is incoming add to clients
print "check new con\n";
$socket->checkNewConn();
//get all client input into thier buffers
print "get client input\n";
$socket->getClientInput();
//check socket timeouts
print "check socket timeouts\n";
$socket->checkTimeouts();
//debug just print buffers out!
print "print buffers debug\n";
$socket->printBuffers();
print "loop finish...\n";
}
?>
[/php:1:019ea06b56]
this is not the newest version of this class but if you are a decent php programmer it wont be hard for you to add your own functionality
oh this script was designed to be run from a console not from a browser :)
classSocket.php
[php:1:019ea06b56]
<?
//socket class for tcp server php script (runs on any web server with php & sockets installed!
class Socket {
//server vars
var $address = "";
var $port = "";
var $maxconn = 1;
//connections
var $count = 0;
var $sock = "";
var $socket = array();
var $read = array();
var $timeout = 10;
//premade packets
var $packets = array();
//starts a listening server
function serverStart($address = "", $port = "") {
if (!$address)
$address = $this->address;
if (!$port)
$port = $this->port;
//mark read with maxconn
for ($i = 0; $i < $this->maxconn; $i++) {
$this->read[$i] = "";
}
//mark socket with maxconn
for ($i = 0; $i < $this->maxconn; $i++) {
$this->socket[$i] = array('sock' => null, 'buffer' => null, 'time' => null);
}
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
//$sock = socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($sock, $address, $port);
socket_listen($sock);
return $sock;
}
function serverStop($sock) {
if (!sock)
$sock = $this->sock;
socket_close($sock);
}
function makeReadArray() {
$read[0] = $this->sock;
for ($i = 0; $i < $this->maxconn; $i++) {
if ($this->socket[$i]['sock'] != null)
$read[] = $this->socket[$i]['sock'];
}
$this->read = $read;
}
function getClientInput() {
for ($i = 0; $i < $this->maxconn; $i++) {
if (in_array($this->socket[$i]['sock'], $this->read)) {
//print "Found some input: $i\n";
$bytes = socket_read($this->socket[$i]['sock'], 1024);
$this->socket[$i]['buffer'] .= $bytes;
$this->socket[$i]['time'] = strtotime("now");
if ($bytes == null) $this->socket[$i]['time'] = 0; }
}
}
function checkTimeouts() {
for ($i = 0; $i < $this->maxconn; $i++) {
//print "Socket: $i - ".(strtotime("now") - strtotime("now", $this->socket[$i]['time']))."\n";
if ($this->socket[$i]['sock'] != null && $this->socket[$i]['time'] < strtotime("-".$this->timeout." seconds"))
$this->close($i);
}
}
function printBuffers() {
for ($i = 0; $i < $this->maxconn; $i++) {
print $this->socket[$i]['buffer']."\n";
}
}
function checkNewConn() {
if (in_array($this->sock, $this->read) || !$this->count)
$this->accept();
}
//accept socket connection
function accept($sock = "") {
if (!$sock)
$sock = $this->sock;
for ($i = 0; $i < $this->maxconn; $i++) {
if ($this->socket[$i]['sock'] == null) {
$this->socket[$i]['sock'] = socket_accept($sock);
$this->socket[$i]['time'] = strtotime("now");
$this->count++;
//print "New Connection Accepted! total(".$this->count.")\n";
break;
}
}
}
//create new packet
function newPacket($packet) {
$this->packets[$packet] = "";
}
//write byte
function addBytes($packet, $byte) {
$this->packets[$packet] .= chr($byte);
}
//write string
function addString($packet, $string) {
$this->packets[$packet] .= $string;
}
function send($socket, $packet) {
//print "Sent Packet($packet): ".$this->packets[$packet]."\n";
socket_write($this->socket[$socket]['sock'], $this->packets[$packet]);
}
function close($socket) {
socket_close($this->socket[$socket]['sock']);
$this->socket[$socket]['sock'] = null;
$this->socket[$socket]['buffer'] = null;
$this->socket[$socket]['time'] = null;
$this->count--;
//print "Connection Closed!\n";
}
}
?>
[/php:1:019ea06b56]
This example packets created were for a smtp server but you basically will get a feel for the socket class
exampleserver.php
[php:1:019ea06b56]
<?
set_time_limit(0);
ob_implicit_flush();
error_reporting(E_ALL);
print "Loading Class Socket\n";
include ("classSocket.php");
$socket = new Socket();
$socket->address = "127.0.0.1";
$socket->port = 7999;
$socket->maxconn = 10;
$socket->timeout = 5;
print "Starting Server\n";
$socket->sock = $socket->serverStart();
print "Making basic packets\n";
//create basic packets
$socket->newPacket("220");
$socket->addString("220", "220 Welcome to XXX Server... Please ensure you are not sending spam mail as we will blacklist your ip have a nice day. ".strtotime("now")."\r\n");
$socket->newPacket("221");
$socket->addString("221", "221 Connection Terminated");
$socket->newPacket("250");
$socket->addString("250", "250 OK");
$socket->newPacket("354");
$socket->addString("354", "354 Enter mail, end with '.' on a line by itself");
$socket->newPacket("500");
$socket->addString("500", "500 Unrecognized command or out of order command!");
print "Waiting for Connections!\n";
while (true) {
//make socket read array
print "loop start... total (".$socket->count.")\n";
print "make read array\n";
$socket->makeReadArray();
//wait for any connection to push data!
print "watch read array\n";
$ready = socket_select($socket->read, $write = null, $except = null, $tm = null);
//if a new connection is incoming add to clients
print "check new con\n";
$socket->checkNewConn();
//get all client input into thier buffers
print "get client input\n";
$socket->getClientInput();
//check socket timeouts
print "check socket timeouts\n";
$socket->checkTimeouts();
//debug just print buffers out!
print "print buffers debug\n";
$socket->printBuffers();
print "loop finish...\n";
}
?>
[/php:1:019ea06b56]