[PHP] Make Sock5 Server

Transcribe
by Transcribe · 2 posts
12 years ago in PHP
Posted 12 years ago · Author
Code
#!/usr/bin/php -q
<?php [/size][/color][/font]

[font="verdana, arial, sans-serif"][color="#000000"][size=2]array($dest);
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush(); [/size][/color][/font]
[font="verdana, arial, sans-serif"][color="#000000"][size=2]$address = '127.0.0.1';
$port = 5554; [/size][/color][/font]
[font="verdana, arial, sans-serif"][color="#000000"][size=2]if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) {
echo "socket_create() fallito: motivo: " . socket_strerror($sock) . "\n";
}
if (($ret = socket_bind($sock, $address, $port)) < 0) {
echo "socket_bind() fallito: motivo: " . socket_strerror($ret) . "\n";
}
if (($ret = socket_listen($sock, 5)) < 0) {
echo "socket_listen() fallito: motivo: " . socket_strerror($ret) . "\n";
}
do {
if (($msgsock = socket_accept($sock)) < 0) {
echo "socket_accept() fallito: motivo: " . socket_strerror ($msgsock) . "\n";
break;
} [/size][/color][/font]
[font="verdana, arial, sans-serif"][color="#000000"][size=2]/** Initial Socks5 HandShake **/
$buf = socket_read($msgsock, 128, PHP_BINARY_READ);
if((implode('',unpack('C3cmd',$buf))) == 510){
$ans = pack('C2',0x05,0x00);
socket_write($msgsock, $ans, strlen($ans));
} [/size][/color][/font]
[font="verdana, arial, sans-serif"][color="#000000"][size=2]$buf = socket_read($msgsock, 4, PHP_BINARY_READ);
$buf = unpack('C4cmd',$buf);
$at = $buf['cmd4'];
switch($at){ [/size][/color][/font]
[font="verdana, arial, sans-serif"][color="#000000"][size=2]case 3 : /** Client gave us Domain Name **/
$buf = socket_read($msgsock, 1, PHP_BINARY_READ);
$buf = unpack('C1len',$buf);
$dest['host'] = socket_read($msgsock, $buf['len'], PHP_BINARY_READ);
break; [/size][/color][/font]
[font="verdana, arial, sans-serif"][color="#000000"][size=2]case 1: /** Client gave us IP **/
$buf = socket_read($msgsock, 4, PHP_BINARY_READ);
$hip = $buf;
$buf = unpack('C*',$buf);
$dest['host'] = implode('.',$buf);
break;
}
$buf = socket_read($msgsock, 2, PHP_BINARY_READ);
$buf = unpack('nport',$buf);
$dest['port'] = $buf['port']; [/size][/color][/font]
[font="verdana, arial, sans-serif"][color="#000000"][size=2]/** Set Up Socket for Remote Host **/
$dsocket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$conn = socket_connect($dsocket, $dest['host'], $dest['port']); [/size][/color][/font]
[font="verdana, arial, sans-serif"][color="#000000"][size=2]switch($at){ [/size][/color][/font]
[font="verdana, arial, sans-serif"][color="#000000"][size=2]case 3: /** Domain Name Address Reply **/
$buf = pack('C5',0x05,0x00,0x00,0x03,strlen($dest['host'])).$dest['host'].pack("n", $dest['port']);
socket_write($msgsock, $buf, strlen($buf));
break; [/size][/color][/font]
[font="verdana, arial, sans-serif"][color="#000000"][size=2]case 1: /** Ip Address reply **/
$buf = pack('C4',0x05,0x00,0x00,0x01).$hip.pack("n", $dest['port']);
socket_write($msgsock, $buf, strlen($buf));
break;
} [/size][/color][/font]
[font="verdana, arial, sans-serif"][color="#000000"][size=2]/** Start data transfer loop **/
do {
if (FALSE === ($req = socket_read($msgsock, 2048, PHP_BINARY_READ))) {
echo "socket_read() fallito: motivo: " . socket_strerror($ret) . "\n";
break 2;
} [/size][/color][/font]
[font="verdana, arial, sans-serif"][color="#000000"][size=2]$ans = '' ; [/size][/color][/font]
[font="verdana, arial, sans-serif"][color="#000000"][size=2]echo $req;
socket_write($dsocket,$req,strlen($req));
$ans = socket_read($dsocket,2048,PHP_BINARY_READ);
echo $ans;
socket_write($msgsock,$ans,strlen($ans)); [/size][/color][/font]

[font="verdana, arial, sans-serif"][color="#000000"][size=2]echo "----r/w completed----\n";
}while (true);
socket_close($msgsock);
socket_close($dsocket);
} while(true); [/size][/color][/font]

[font="verdana, arial, sans-serif"][color="#000000"][size=2]?>

Create an account or sign in to comment

You need to be a member in order to leave a comment

Sign in

Already have an account? Sign in here

SIGN IN NOW

Create an account

Sign up for a new account in our community. It's easy!

REGISTER A NEW ACCOUNT