summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjake <jake@jakes-mail.top>2022-01-24 23:30:11 -0500
committerjake <jake@jakes-mail.top>2022-01-24 23:30:11 -0500
commit0b8665b5371d696e9a447eacfccfca0b434ef354 (patch)
tree865e30cf936c30fb44746b8ce3bd97a4f4c335dc
Initial commit
-rw-r--r--.gitignore2
-rwxr-xr-xgmi.pl73
-rw-r--r--index.gmi3
3 files changed, 78 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..0efd2ee
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+# make your own certs
+*.pem
diff --git a/gmi.pl b/gmi.pl
new file mode 100755
index 0000000..36a9cd1
--- /dev/null
+++ b/gmi.pl
@@ -0,0 +1,73 @@
+#!/usr/bin/perl
+
+# this is a gemini server
+
+use strict;
+use warnings;
+#use diagnostics;
+
+our $VERSION = 'v0.0.1';
+
+# Modules
+use IO::Socket::SSL; # CPAN
+use URL::XS qw(parse_url split_url_path parse_url_query); # CPAN
+#use Term::ANSIColor; # Core
+#use Path::Naive qw(normalize_path); # CPAN
+use Smart::Comments; # CPAN
+#use URI::Encode qw(uri_encode); # CPAN
+use IO::Select;
+
+# sudo cpanm IO::Socket::SSL URL::XS Text::Wraper Path::Naive Term::ReadLine Smart::Comments URI::Encode
+
+my $srv = IO::Socket::SSL->new(
+ #SSL_server => 1,
+ LocalAddr => '0.0.0.0',
+ LocalPort => 1965,
+ Listen => 10,
+
+ SSL_cert_file => './cert.pem',
+ SSL_key_file => './key.pem',
+
+ SSL_fast_shutdown => 1,
+
+) or die "error=$!, ssl_error=$SSL_ERROR";
+
+while (1) {
+ my $cl = $srv->accept();
+
+ sysread($cl,my $data,1024);
+
+ my $clhost = $cl->peerhost();
+ my $clport = $cl->peerport();
+
+ print "Connection from $clhost:$clport - $data";
+ my $url;
+ my $path;
+ substr($data,-2,2,''); # removing \r\n
+ eval {
+ $url = parse_url($data);
+ };
+ $path = $url->{path};
+ my $doc;
+ ### $data
+ ### $url
+ ### $path
+ if ($path) {
+ # Note: this will serve ANYTHING including /etc/passwd and other sensitive files
+ open (my $FH ,'<', "./$path") or syswrite($cl,"51 Not Found\r\n") and $cl->close('SHUT_WR') and next;
+ while (<$FH>) {
+ $doc .= $_;
+ }
+ close ($FH);
+
+ syswrite($cl,"20 text/gemini\r\n$doc",0);
+ }
+ else {
+ syswrite($cl,"50 Failure\r\n");
+ $cl->close('SHUT_WR');
+ }
+
+ $cl->close('SHUT_WR');
+}
+
+$srv->close();
diff --git a/index.gmi b/index.gmi
new file mode 100644
index 0000000..58a92a0
--- /dev/null
+++ b/index.gmi
@@ -0,0 +1,3 @@
+# Hello world!
+
+This is a proper index.gmi file which my server should serve if it has been configured correctly.