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);
23 // apply patch from @nervuri:matrix.org to stop supporting out of spec versions of TLS
24 $cryptoMethod = STREAM_CRYPTO_METHOD_TLS_SERVER
25 & ~ STREAM_CRYPTO_METHOD_TLSv1_0_SERVER
26 & ~ STREAM_CRYPTO_METHOD_TLSv1_1_SERVER;
29 $forkedSocket = stream_socket_accept($socket, "-1", $remoteIP);
31 stream_set_blocking($forkedSocket, true);
32 stream_socket_enable_crypto($forkedSocket, true, $cryptoMethod);
33 $line = fread($forkedSocket, 1024);
34 stream_set_blocking($forkedSocket, false);
36 $parsed_url = $g->parse_request($line);
38 $filepath = $g->get_filepath($parsed_url);
40 $status_code = $g->get_status_code($filepath);
45 if($status_code == "20") {
46 $meta = $g->get_mime_type($filepath);
47 $content = file_get_contents($filepath);
48 $filesize = filesize($filepath);
53 $status_line = $status_code." ".$meta;
55 $g->log_to_file($remoteIP,$status_code, $meta, $filepath, $filesize);
56 $status_line .= "\r\n";
57 fwrite($forkedSocket, $status_line);
59 if($status_code == "20") {
60 fwrite($forkedSocket,$content);
63 fclose($forkedSocket);