3

I am trying to create a simple https server in c# in a Unity3D game, to be accessed through a web browser. I've created a server cert and key with openssl, but I can't find a multi-platform way to pass the cert to the server without any additional configuration outside the code.

Most of the info I've been able to find falls into these categories:

  • Uses SslStream, but that seems to only be relevant for TcpListener (and I want something higher level that can serve webpages)
  • Requires external Windows-only tools like httpcfg that I'd prefer not to use
  • Programmatically or manually installing certs in certificate stores, which seems to require either the program or the user to have admin/root privileges

I know in python you do something like:

ssl.wrap_socket (httpd.socket, certfile='./server-crt.pem', keyfile='./server-key.pem', server_side=True)

...but there doesn't seem to be an equivalent in c# for httplistener, or in system.security.securitymanager or anything. I assume/hope that I'm just missing something obvious here.

For what it's worth, here's what I have so far, which is just the MSDN httplistener example put in a Unity script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Net;

public class SimpleListenerExample : MonoBehaviour {

    // This example requires the System and System.Net namespaces.
    public static void StartServer(string[] prefixes)
    {
        if (!HttpListener.IsSupported)
        {
            Console.WriteLine("Windows XP SP2 or Server 2003 is required to use the HttpListener class.");
            return;
        }
        // URI prefixes are required,
        // for example "http://contoso.com:8080/index/".
        if (prefixes == null || prefixes.Length == 0)
            throw new ArgumentException("prefixes");

        // Create a listener.
        HttpListener listener = new HttpListener();
        // Add the prefixes.
        foreach (string s in prefixes)
        {
            listener.Prefixes.Add(s);
        }

        /* and here's the part where I would load the server certificate ...somehow */

        listener.Start();
        Console.WriteLine("Listening...");
        // Note: The GetContext method blocks while waiting for a request. 
        HttpListenerContext context = listener.GetContext();
        HttpListenerRequest request = context.Request;
        // Obtain a response object.
        HttpListenerResponse response = context.Response;
        // Construct a response.
        string responseString = "<HTML><BODY> Hello world!</BODY></HTML>";
        byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
        // Get a response stream and write the response to it.
        response.ContentLength64 = buffer.Length;
        System.IO.Stream output = response.OutputStream;
        output.Write(buffer, 0, buffer.Length);
        // You must close the output stream.
        output.Close();
        listener.Stop();
    }

    // Use this for initialization
    void Start () {
        String[] prefixes = { "http://*:8089/", "https://*:8443/" };
        StartServer(prefixes);
    }

    // Update is called once per frame
    void Update () {

    }
}
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
user2686455
  • 97
  • 2
  • 7
  • 1
    The comment above shows how to register a certificate on Windows. If you need to support Mac or Linux, then you have to install the certificate to a Mono folder. I have some sample code in Jexus Manager Remote Services, https://github.com/jexuswebserver/jxmgr/blob/master/RemoteServices/Program.cs#L69 – Lex Li May 27 '18 at 14:28
  • @LexLi Can you write an answer for how to install this? That would be helpful since this is Unity and I don't think there is an existing solution for Mac and Linux. – Programmer May 27 '18 at 15:19
  • Thanks very much for the help. However, the linked question that this is supposedly a duplicate of has no accepted answer and all but one of the solutions uses httpcfg and/or netsh. The other answer uses a Windows only API. I've changed the title of my question to clarify that I'm really looking for a Mono solution here. – user2686455 May 27 '18 at 17:25
  • I have recently implement HTTPS using HTTPListener and allow communication over LAN as well by add firewall rule. There is no any input required, everything is handled by C# code. I think it can help you out. I have shared my solution here: https://stackoverflow.com/a/58149405/983548 – Habib Sheikh Sep 28 '19 at 21:06

1 Answers1

1

If you came here from Google trying to find information on HttpListener in Mono and SSL, you will find more pertinent information on this related question:

Mono HttpListener client certificate

The OPs originally desire is a simple web server without any platform-specific configuration. So far, the only library I've found that supports an approach that avoids platform configuration is Ceen HTTPd.

There was a discussion about Ceen from a poster with a similar need here: https://softwarerecs.stackexchange.com/questions/52304/net-library-for-running-an-embedded-selfhosted-light-web-server-from-c-with-ss

JamesHoux
  • 2,999
  • 3
  • 32
  • 50