aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


commit 81c39de5ee15b11f9e72a5d9fb1a0c0bd3b56358
parent 7ff5a23307dfb771c72714fad0dcaa1ede8e5c7c
Author: anon <anon@example.com>
Date:   Sat, 17 Oct 2026 06:23:20 +0000

Serve all interfaces with persistent local HTTPS certificate

Diffstat:
MREADME.md | 10+++++++++-
Aserver/https.go | 76++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aserver/https_test.go | 91+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mserver/main.go | 22++++++++++++++--------
4 files changed, 190 insertions(+), 9 deletions(-)

diff --git a/README.md b/README.md @@ -25,7 +25,15 @@ commands below. Creating a character in server controls also works. ### Server (Windows or Linux) -Requires Go 1.24 or newer. Default ports are login 7500, world 7501 and HTTP 8080. +Requires Go 1.24 or newer. The server listens on all network interfaces. Default +ports are login 7500, world 7501, HTTP 8080 and HTTPS 8443. From another machine, +open `https://<server-address>:8443/wasm/` or `/control`. On first startup, Go +creates a self-signed certificate in `local/https/cert.pem` and a private key in +`local/https/key.pem`; both stay out of Git. Trust the certificate on the client +machine before playing: shared WebAssembly memory requires trusted HTTPS. +The certificate covers the server hostname and current IP addresses for one year. +If those change, remove both files and restart to generate a new certificate. +HTTP remains available, including `http://<server-address>:8080/control`. ```sh go run ./server diff --git a/server/https.go b/server/https.go @@ -0,0 +1,76 @@ +package main + +import ( + "crypto/ecdsa" + "crypto/elliptic" + "crypto/rand" + "crypto/tls" + "crypto/x509" + "crypto/x509/pkix" + "encoding/pem" + "math/big" + "net" + "os" + "time" +) + +func prepareHTTPS() error { + // Reuse the certificate so clients can trust it across server restarts. + if _, err := os.Stat("local/https/cert.pem"); err == nil { + _, err := tls.LoadX509KeyPair("local/https/cert.pem", "local/https/key.pem") + return err + } else if !os.IsNotExist(err) { + return err + } + if err := os.MkdirAll("local/https", 0700); err != nil { + return err + } + key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + if err != nil { + return err + } + serial, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128)) + if err != nil { + return err + } + hostname, err := os.Hostname() + if err != nil { + return err + } + + // Cover localhost and this machine's current LAN addresses. + certificate := &x509.Certificate{ + SerialNumber: serial, + Subject: pkix.Name{CommonName: hostname}, + NotBefore: time.Now().Add(-5 * time.Minute), + NotAfter: time.Now().AddDate(1, 0, 0), + DNSNames: []string{"localhost", hostname}, + KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign, + ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, + BasicConstraintsValid: true, + IsCA: true, + } + addresses, err := net.InterfaceAddrs() + if err != nil { + return err + } + for _, address := range addresses { + ip, _, err := net.ParseCIDR(address.String()) + if err != nil { + return err + } + certificate.IPAddresses = append(certificate.IPAddresses, ip) + } + der, err := x509.CreateCertificate(rand.Reader, certificate, certificate, &key.PublicKey, key) + if err != nil { + return err + } + privateKey, err := x509.MarshalPKCS8PrivateKey(key) + if err != nil { + return err + } + if err := os.WriteFile("local/https/key.pem", pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: privateKey}), 0600); err != nil { + return err + } + return os.WriteFile("local/https/cert.pem", pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der}), 0644) +} diff --git a/server/https_test.go b/server/https_test.go @@ -0,0 +1,91 @@ +package main + +import ( + "bytes" + "crypto/tls" + "crypto/x509" + "io" + "net/http" + "net/http/httptest" + "os" + "testing" +) + +func TestHTTPSCertificateSupportsTrustedLocalConnection(t *testing.T) { + t.Chdir(t.TempDir()) + if err := prepareHTTPS(); err != nil { + t.Fatal(err) + } + certificate, err := tls.LoadX509KeyPair("local/https/cert.pem", "local/https/key.pem") + if err != nil { + t.Fatal(err) + } + leaf, err := x509.ParseCertificate(certificate.Certificate[0]) + if err != nil { + t.Fatal(err) + } + roots := x509.NewCertPool() + roots.AddCert(leaf) + server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + io.WriteString(w, "AO HTTPS") + })) + server.TLS = &tls.Config{Certificates: []tls.Certificate{certificate}} + server.StartTLS() + defer server.Close() + transport := &http.Transport{TLSClientConfig: &tls.Config{RootCAs: roots}} + defer transport.CloseIdleConnections() + client := &http.Client{Transport: transport} + + response, err := client.Get(server.URL) + if err != nil { + t.Fatal(err) + } + defer response.Body.Close() + body, err := io.ReadAll(response.Body) + if err != nil { + t.Fatal(err) + } + + if response.StatusCode != http.StatusOK || string(body) != "AO HTTPS" { + t.Fatalf("HTTPS response: %d %q", response.StatusCode, body) + } + if err := leaf.VerifyHostname("localhost"); err != nil { + t.Fatal(err) + } + hostname, err := os.Hostname() + if err != nil { + t.Fatal(err) + } + if err := leaf.VerifyHostname(hostname); err != nil { + t.Fatal(err) + } +} + +func TestHTTPSCertificatePersistsAcrossStarts(t *testing.T) { + t.Chdir(t.TempDir()) + if err := prepareHTTPS(); err != nil { + t.Fatal(err) + } + certificate, err := os.ReadFile("local/https/cert.pem") + if err != nil { + t.Fatal(err) + } + key, err := os.ReadFile("local/https/key.pem") + if err != nil { + t.Fatal(err) + } + + if err := prepareHTTPS(); err != nil { + t.Fatal(err) + } + + for path, expected := range map[string][]byte{"local/https/cert.pem": certificate, "local/https/key.pem": key} { + actual, err := os.ReadFile(path) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(actual, expected) { + t.Fatalf("server replaced %s", path) + } + } +} diff --git a/server/main.go b/server/main.go @@ -17,9 +17,9 @@ import ( ) func main() { - loginPort := flag.Int("login-port", 7500, "local login port") - worldPort := flag.Int("world-port", 7501, "local world port") - httpPort := flag.Int("http-port", 8080, "local HTTP and control panel port") + loginPort := flag.Int("login-port", 7500, "login port") + worldPort := flag.Int("world-port", 7501, "world port") + httpPort := flag.Int("http-port", 8080, "HTTP and control panel port") flag.Parse() for _, port := range []int{*loginPort, *worldPort, *httpPort} { if port < 1 || port > 65535 { @@ -29,9 +29,12 @@ func main() { if err := prepareRuntime(); err != nil { log.Fatal(err) } + if err := prepareHTTPS(); err != nil { + log.Fatal(err) + } server := newServer() server.loginPort, server.worldPort, server.httpPort = *loginPort, *worldPort, *httpPort - httpAddress := fmt.Sprintf("127.0.0.1:%d", server.httpPort) + httpAddress := fmt.Sprintf(":%d", server.httpPort) http.HandleFunc("GET /control", server.controlPage) http.HandleFunc("GET /users/{character}", server.userPage) http.HandleFunc("POST /users", server.createUser) @@ -74,16 +77,19 @@ func main() { go func() { log.Fatal(http.ListenAndServe(httpAddress, nil)) }() + go func() { + log.Fatal(http.ListenAndServeTLS(":8443", "local/https/cert.pem", "local/https/key.pem", nil)) + }() go server.serveWorld() go server.roamNPCs() go server.checkZoneCrossings() - // Keep development connections confined to this computer. - listener, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", server.loginPort)) + // Accept connections on every network interface. + listener, err := net.Listen("tcp", fmt.Sprintf(":%d", server.loginPort)) if err != nil { log.Fatal(err) } - log.Printf("Control: http://%s/control; login: 127.0.0.1:%d; world: 127.0.0.1:%d", httpAddress, server.loginPort, server.worldPort) + log.Printf("Listening on all interfaces: HTTPS 8443, HTTP %d, login %d, world %d; local controls: http://127.0.0.1:%d/control", server.httpPort, server.loginPort, server.worldPort, server.httpPort) for { conn, err := listener.Accept() if err != nil { @@ -213,7 +219,7 @@ func (server *server) serveLogin(conn net.Conn) error { } func (server *server) serveWorld() { - listener, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", server.worldPort)) + listener, err := net.Listen("tcp", fmt.Sprintf(":%d", server.worldPort)) if err != nil { log.Fatal(err) }