3 * Gemini server written in PHP by seven@0xm.net
4 * Version 0.1, Oct 2020
7 if(!require("config.php"))
8 die("config.php is missing. Copy config.php.sample to config.php and customise your settings");
9 require("gemini.class.php");
10 $g = new Gemini($config);
12 $context = stream_context_create();
14 stream_context_set_option($context, 'ssl', 'local_cert', $g->certificate_file);
15 stream_context_set_option($context, 'ssl', 'passphrase', $g->certificate_passphrase);
16 stream_context_set_option($context, 'ssl', 'allow_self_signed', true);
17 stream_context_set_option($context, 'ssl', 'verify_peer', false);
19 $socket = stream_socket_server("tcp://{$g->ip}:{$g->port}", $errno, $errstr, STREAM_SERVER_BIND|STREAM_SERVER_LISTEN, $context);
21 stream_socket_enable_crypto($socket, false);
24 $forkedSocket = stream_socket_accept($socket, "-1", $remoteIP);
26 stream_set_blocking($forkedSocket, true);
27 stream_socket_enable_crypto($forkedSocket, true, STREAM_CRYPTO_METHOD_TLS_SERVER);
28 $line = fread($forkedSocket, 1024);
29 stream_set_blocking($forkedSocket, false);
31 $parsed_url = $g->parse_request($line);
33 $filepath = $g->get_filepath($parsed_url);
35 $status_code = $g->get_status_code($filepath);
40 if($status_code == "20") {
41 $meta = $g->get_mime_type($filepath);
42 $content = file_get_contents($filepath);
43 $filesize = filesize($filepath);
48 $status_line = $status_code." ".$meta;
50 $g->log_to_file($remoteIP,$status_code, $meta, $filepath, $filesize);
51 $status_line .= "\r\n";
52 fwrite($forkedSocket, $status_line);
54 if($status_code == "20") {
55 fwrite($forkedSocket,$content);
58 fclose($forkedSocket);