diff --git a/config.php.sample b/config.php.sample new file mode 100644 index 0000000..3faf586 --- /dev/null +++ b/config.php.sample @@ -0,0 +1,43 @@ +> certs/yourdomain.com.pem + + * Enter the passphrase (if you used one) below. + * + */ + +$config['certificate_file'] = ''; +$config['certificate_passphrase'] = ''; + +// IP address to listen to (leave commented out to listen on all interfaces) +//$config['ip'] = "127.0.0.1"; + +// Port to listen on (1965 is the default) +//$config['port'] = "1965"; + +// This is the location files are served from +// The path is constructed based on the hostname in the client request +// i.e. gemini://domain1.com/file1 is retrieved from hosts/domain1.com/file1 +//$config['data_dir'] = "hosts/"; +//$config['default_host_dir'] = "default/"; + +// Default index file. If a path isn't specified then the server will +// default to an index file (like index.html on a web server). +//$config['default_index_file'] = "index.gemini"; + +// Logging, setting this to false will disable logging (default is on/true); +//$config['logging'] = true; +//$config['log_file'] = "logs/gemini-php.log"; + +?> diff --git a/server.php b/server.php new file mode 100644 index 0000000..dda1c70 --- /dev/null +++ b/server.php @@ -0,0 +1,61 @@ +certificate_file); +stream_context_set_option($context, 'ssl', 'passphrase', $g->certificate_passphrase); +stream_context_set_option($context, 'ssl', 'allow_self_signed', true); +stream_context_set_option($context, 'ssl', 'verify_peer', false); + +$socket = stream_socket_server("tcp://{$g->ip}:{$g->port}", $errno, $errstr, STREAM_SERVER_BIND|STREAM_SERVER_LISTEN, $context); + +stream_socket_enable_crypto($socket, false); + +while(true) { + $forkedSocket = stream_socket_accept($socket, "-1", $remoteIP); + + stream_set_blocking($forkedSocket, true); + stream_socket_enable_crypto($forkedSocket, true, STREAM_CRYPTO_METHOD_TLS_SERVER); + $line = fread($forkedSocket, 1024); + stream_set_blocking($forkedSocket, false); + + $parsed_url = $g->parse_request($line); + + $filepath = $g->get_filepath($parsed_url); + + $status_code = $g->get_status_code($filepath); + + $meta = ""; + $filesize = 0; + + if($status_code == "20") { + $meta = $g->get_mime_type($filepath); + $content = file_get_contents($filepath); + $filesize = filesize($filepath); + } else { + $meta = "Not found"; + } + + $status_line = $status_code." ".$meta; + if($g->logging) + $g->log_to_file($remoteIP,$status_code, $meta, $filepath, $filesize); + $status_line .= "\r\n"; + fwrite($forkedSocket, $status_line); + + if($status_code == "20") { + fwrite($forkedSocket,$content); + } + + fclose($forkedSocket); +} + +?>