Created
January 18, 2015 14:55
-
-
Save boenrobot/f636eba79043f7303fb4 to your computer and use it in GitHub Desktop.
EchoService
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
ini_set('memory_limit', -1); | |
//sleep(20); | |
$protocol = isset($argv[3]) ? $argv[3] : 'tcp'; | |
$conn = stream_socket_client( | |
"{$protocol}://[{$argv[1]}]:{$argv[2]}/", | |
$errno, | |
$errstr, | |
10, | |
STREAM_CLIENT_CONNECT, | |
stream_context_create( | |
array( | |
'ssl' => array( | |
'verify_peer' => true, | |
'verify_peer_name' => false, | |
'cafile' => __DIR__ . DIRECTORY_SEPARATOR . 'selfSigned.cer', | |
'allow_self_signed' => true | |
) | |
) | |
) | |
); | |
echo "Client running...\n"; | |
while (!feof($conn)) { | |
$line = ''; | |
$r = $e = null; | |
$w = array($conn); | |
if (1 === stream_select($r, $w, $e, null)) { | |
$line = trim(fgets(STDIN)); | |
$line = str_repeat('t', $line) . PHP_EOL; | |
$r = $e = null; | |
$w = array($conn); | |
while ('' != $line) { | |
if (!feof($conn) && 1 === stream_select($r, $w, $e, null)) { | |
$w = array($conn); | |
$line = substr($line, fwrite($conn, $line)); | |
} else { | |
continue 2; | |
} | |
} | |
fflush($conn); | |
echo "Line sent\n"; | |
$readBuff = ''; | |
$bytesRead = 0; | |
$w = $e = null; | |
$r = array($conn); | |
while (!feof($conn) && 1 === stream_select($r, $w, $e, null)) { | |
$r = array($conn); | |
$readBuff .= fread($conn, 1); | |
echo str_repeat("\x8", strlen((string)$bytesRead)) . ++$bytesRead; | |
$lines = explode(PHP_EOL, $readBuff); | |
$readBuff = array_pop($lines); | |
if (!empty($lines)) { | |
break; | |
} | |
} | |
echo "\n"; | |
$bytesRead = 0; | |
foreach ($lines as $line) { | |
fwrite(STDOUT, '(' . strlen($line) . ' received) ' . $line . PHP_EOL); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
ini_set('memory_limit', -1); | |
$protocol = isset($argv[2]) ? $argv[2] : 'tcp'; | |
$server = stream_socket_server( | |
"{$protocol}://0.0.0.0:{$argv[1]}", | |
$errorno, | |
$errstr, | |
STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, | |
stream_context_create( | |
array( | |
'ssl' => array( | |
'verify_peer' => false, | |
'verify_peer_name' => false, | |
'local_cert' => __DIR__ . DIRECTORY_SEPARATOR . 'selfSigned.cer', | |
'cafile' => __DIR__ . DIRECTORY_SEPARATOR . 'selfSigned.cer', | |
'allow_self_signed' => true | |
) | |
) | |
) | |
); | |
echo "Server running...\n"; | |
while (true) { | |
$conn = stream_socket_accept($server, -1, $peerName); | |
if ($conn === false) { | |
continue; | |
} | |
echo "Connection started...\n"; | |
$bytesRead = 0; | |
$readBuff = ''; | |
while (!feof($conn)) { | |
$w = $e = null; | |
$r = array($conn); | |
while (!feof($conn) && 1 === stream_select($r, $w, $e, null)) { | |
$r = array($conn); | |
$readBuff .= fread($conn, 1); | |
fflush($conn); | |
echo str_repeat("\x8", strlen((string)$bytesRead)) . ++$bytesRead; | |
$lines = explode(PHP_EOL, $readBuff); | |
$readBuff = array_pop($lines); | |
if (!empty($lines)) { | |
break; | |
} | |
} | |
$r = $e = null; | |
$w = array($conn); | |
foreach ($lines as $line) { | |
echo "\nEchoing back " . strlen($line) . ' bytes...'; | |
$line .= PHP_EOL; | |
while ('' != $line) { | |
if (!feof($conn) && 1 === stream_select($r, $w, $e, null)) { | |
$w = array($conn); | |
$line = substr($line, fwrite($conn, $line)); | |
} else { | |
continue 3; | |
} | |
} | |
fflush($conn); | |
echo "Done\n"; | |
} | |
$bytesRead = 0; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define('CERTIFICATE_FILE', 'selfSigned'); | |
//Prepare a self signed certificate | |
$configargs = array(); | |
if (strpos(PHP_OS, 'WIN') === 0) { | |
$phpbin = defined('PHP_BINARY') | |
? PHP_BINARY | |
: getenv('PHP_PEAR_PHP_BIN'); | |
$configargs['config'] = dirname($phpbin) . '/extras/ssl/openssl.cnf'; | |
} | |
$privkey = openssl_pkey_new($configargs); | |
$cert = openssl_csr_sign( | |
openssl_csr_new( | |
array( | |
'countryName' => 'US', | |
'stateOrProvinceName' => 'IRRELEVANT', | |
'localityName' => 'IRRELEVANT', | |
'organizationName' => 'PHP', | |
'organizationalUnitName' => 'PHP', | |
'commonName' => 'IRRELEVANT', | |
'emailAddress' => 'IRRELEVANT@example.com' | |
), | |
$privkey, | |
$configargs | |
), | |
null, | |
$privkey, | |
2, | |
$configargs | |
); | |
$pem = array(); | |
openssl_x509_export($cert, $pem[0]); | |
openssl_pkey_export($privkey, $pem[1], null, $configargs); | |
openssl_pkcs12_export_to_file( | |
$pem[0], | |
__DIR__ . DIRECTORY_SEPARATOR . CERTIFICATE_FILE . '.p12', | |
$pem[1], | |
'123456' | |
); | |
file_put_contents( | |
__DIR__ . DIRECTORY_SEPARATOR . CERTIFICATE_FILE . '.cer', | |
implode('', $pem) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment