Adam Pippin
2 years ago
2 changed files with 82 additions and 0 deletions
@ -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; |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,12 @@ |
|||||
|
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
|
||||
|
<PropertyGroup> |
||||
|
<OutputType>Exe</OutputType> |
||||
|
<TargetFramework>net5.0</TargetFramework> |
||||
|
</PropertyGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<ProjectReference Include="..\pyxis.gemini\pyxis.gemini.csproj" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
</Project> |
Loading…
Reference in new issue