Add initial proof-of-concept
This commit is contained in:
parent
1a1385471c
commit
fc00fdbd63
|
@ -0,0 +1 @@
|
|||
vendor
|
|
@ -0,0 +1,105 @@
|
|||
#!/usr/bin/env php
|
||||
<?php
|
||||
|
||||
// Certificate data:
|
||||
$dn = array(
|
||||
"countryName" => "UK",
|
||||
"stateOrProvinceName" => "Somerset",
|
||||
"localityName" => "Glastonbury",
|
||||
"organizationName" => "The Brain Room Limited",
|
||||
"organizationalUnitName" => "PHP Documentation Team",
|
||||
"commonName" => "Wez Furlong",
|
||||
"emailAddress" => "wez@example.com"
|
||||
);
|
||||
|
||||
// Generate certificate
|
||||
$privkey = openssl_pkey_new();
|
||||
$cert = openssl_csr_new($dn, $privkey);
|
||||
$cert = openssl_csr_sign($cert, null, $privkey, 365);
|
||||
|
||||
// Generate PEM file
|
||||
# Optionally change the passphrase from 'comet' to whatever you want, or leave it empty for no passphrase
|
||||
$pem_passphrase = 'comet';
|
||||
$pem = array();
|
||||
openssl_x509_export($cert, $pem[0]);
|
||||
openssl_pkey_export($privkey, $pem[1], $pem_passphrase);
|
||||
$pem = implode($pem);
|
||||
|
||||
// Save PEM file
|
||||
$pemfile = './server.pem';
|
||||
file_put_contents($pemfile, $pem);
|
||||
|
||||
$context = stream_context_create();
|
||||
|
||||
// local_cert must be in PEM format
|
||||
stream_context_set_option($context, 'ssl', 'local_cert', $pemfile);
|
||||
// Pass Phrase (password) of private key
|
||||
stream_context_set_option($context, 'ssl', 'passphrase', $pem_passphrase);
|
||||
|
||||
stream_context_set_option($context, 'ssl', 'allow_self_signed', true);
|
||||
stream_context_set_option($context, 'ssl', 'verify_peer', false);
|
||||
|
||||
// Create the server socket
|
||||
$server = stream_socket_server('ssl://0.0.0.0:1965', $errno, $errstr, STREAM_SERVER_BIND|STREAM_SERVER_LISTEN, $context);
|
||||
|
||||
while (true) {
|
||||
//$buffer = '';
|
||||
print "waiting...";
|
||||
$client = @stream_socket_accept($server);
|
||||
|
||||
if ($client) {
|
||||
print "accepted " . stream_socket_get_name($client, true) . "\n";
|
||||
$request = stream_get_line($client, 1024, "\r\n");
|
||||
// Read until CRLF
|
||||
//while(!preg_match('/\r\n/', $buffer) )
|
||||
// $buffer .= fread($client, 2046);
|
||||
print(View::hexView($request));
|
||||
|
||||
// Respond to client
|
||||
$response = implode("\r\n", ['20 text/gemini', 'Thanks for all the fish']);
|
||||
fwrite($client, $response);
|
||||
fclose($client);
|
||||
} else {
|
||||
print "error.\n";
|
||||
}
|
||||
}
|
||||
|
||||
class View
|
||||
{
|
||||
/**
|
||||
* View hex chars of string
|
||||
*
|
||||
* Outputs a listing of hexidecimal values in 16 byte rows
|
||||
*
|
||||
* @param mixed $text Input text
|
||||
* @return string
|
||||
*/
|
||||
public static function hexView($text)
|
||||
{
|
||||
$num = 16;
|
||||
$outStr = '';
|
||||
$printableChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
||||
. 'abcdefghijklmnopqrstuvwxyz'
|
||||
. '0123456789~!@#$%^&*()_+-={}|[]\:";\'<>?,./';
|
||||
|
||||
$charCount = strlen($text);
|
||||
for ($i = 0; $i < $charCount; $i += $num) {
|
||||
$printStr = '';
|
||||
for ($j = 0; $j < $num; $j++) {
|
||||
$char = substr($text, $i+$j, 1);
|
||||
|
||||
$outStr .= sprintf("%02X", ord($char)) . " ";
|
||||
|
||||
if (ord($char) >= 32 && ord($char) < 127) {
|
||||
$printStr .= $char;
|
||||
} else {
|
||||
$printStr .= ".";
|
||||
}
|
||||
}
|
||||
|
||||
$outStr .= " | " . $printStr . "\n";
|
||||
}
|
||||
|
||||
return $outStr;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue