From 5e1b136d979719e17cf72ae92c88b208915d6f0e Mon Sep 17 00:00:00 2001 From: Adam Pippin Date: Thu, 30 Dec 2021 21:04:19 -0800 Subject: [PATCH] Sample server --- pyxis.helloworld/Program.cs | 70 ++++++++++++++++++++++++ pyxis.helloworld/pyxis.helloworld.csproj | 12 ++++ 2 files changed, 82 insertions(+) create mode 100644 pyxis.helloworld/Program.cs create mode 100644 pyxis.helloworld/pyxis.helloworld.csproj diff --git a/pyxis.helloworld/Program.cs b/pyxis.helloworld/Program.cs new file mode 100644 index 0000000..cf8811d --- /dev/null +++ b/pyxis.helloworld/Program.cs @@ -0,0 +1,70 @@ +using System; +using System.Net; +using System.Security.Cryptography; +using System.Security.Cryptography.X509Certificates; +using System.IO; + +namespace pyxis.helloworld +{ + class Program + { + static void Main(string[] args) + { + Program p = new Program(); + p.run(); + } + + public void run() + { + X509Certificate2 cert_default = this.readKeyAndCert("gemini.key", "gemini.crt"); + X509Certificate2 cert_alt = this.readKeyAndCert("alt.key", "alt.crt"); + + gemini.Server.Server server = new gemini.Server.Server( + new IPEndPoint(IPAddress.Loopback, 1965), + cert_default + ); + server.AddCertificate("test.l.nucleardog.org", cert_alt); + + server.OnProcessRequest += this.processRequest; + + server.Start(); + + Console.WriteLine("[Press Any Key to Continue]"); + Console.ReadKey(); + + server.Stop(); + } + + public gemini.Protocol.Response processRequest(gemini.Server.Server server, gemini.Protocol.Request request) + { + gemini.Protocol.Response resp = new gemini.Protocol.Response(); + resp.setStatus(20); + resp.setMeta("text/gemini; charset=utf-8"); + resp.setBody(System.Text.Encoding.UTF8.GetBytes("Hello, world!")); + return resp; + } + + private X509Certificate2 readKeyAndCert(string keypath, string certpath) + { + // Read cert + X509Certificate2 cert = new X509Certificate2(certpath); + + // Read key + string[] lines = File.ReadAllLines(keypath); + string privateKeyString = ""; + foreach (string line in lines) + { + privateKeyString += line; + } + byte[] privateKeyData = Convert.FromBase64String(privateKeyString); + + RSA rsa = RSA.Create(); + rsa.ImportPkcs8PrivateKey(privateKeyData, out _); + + cert = cert.CopyWithPrivateKey(rsa); + cert = new X509Certificate2(cert.Export(X509ContentType.Pfx)); + + return cert; + } + } +} diff --git a/pyxis.helloworld/pyxis.helloworld.csproj b/pyxis.helloworld/pyxis.helloworld.csproj new file mode 100644 index 0000000..f781f25 --- /dev/null +++ b/pyxis.helloworld/pyxis.helloworld.csproj @@ -0,0 +1,12 @@ + + + + Exe + net5.0 + + + + + + +