diff --git a/core/core.js b/core/core.js index af7ea016..cfd32135 100644 --- a/core/core.js +++ b/core/core.js @@ -380,6 +380,17 @@ async function perfettoHandler(request, response, uri) { } } +async function wellKnownHandler(request, response, path) { + var data = await File.readFile("data/global/.well-known/" + path); + if (data) { + response.writeHead(200, {"Content-Type": "text/plain", "Content-Length": data.length}); + response.end(data); + } else { + response.writeHead(404, {"Content-Type": "text/plain", "Content-Length": "File not found".length}); + response.end("File not found"); + } +} + function sendData(response, data, type) { if (data) { if (startsWithBytes(data, [0xff, 0xd8, 0xff, 0xdb]) || @@ -555,14 +566,7 @@ loadSettings().then(function() { } else if (request.uri == "/robots.txt") { return blobHandler(request, response, null, request.uri); } else if ((match = /^\/.well-known\/(.*)/.exec(request.uri)) && request.uri.indexOf("..") == -1) { - var data = File.readFile("data/global/.well-known/" + match[1]); - if (data) { - response.writeHead(200, {"Content-Type": "text/plain", "Content-Length": data.length}); - response.end(data); - } else { - response.writeHead(404, {"Content-Type": "text/plain", "Content-Length": "File not found".length}); - response.end("File not found"); - } + return wellKnownHandler(request, response, match[1]); } else { var data = "File not found."; response.writeHead(404, {"Content-Type": "text/plain; charset=utf-8", "Content-Length": data.length.toString()}); diff --git a/core/httpd.js b/core/httpd.js index 2cce1e08..c30eaf8b 100644 --- a/core/httpd.js +++ b/core/httpd.js @@ -468,38 +468,37 @@ if (tildefriends.https_port) { var tls = {}; var secureSocket = new Socket(); secureSocket.bind(kHost, tildefriends.https_port).then(function() { - return secureSocket.listen(kBacklog, function() { - return secureSocket.accept().then(function(client) { - handleConnection(client); - + return secureSocket.listen(kBacklog, async function() { + try { + var client = await secureSocket.accept(); const kCertificatePath = "data/httpd/certificate.pem"; const kPrivateKeyPath = "data/httpd/privatekey.pem"; - return Promise.all([ - File.stat(kCertificatePath), - File.stat(kPrivateKeyPath), - ]).then(function(stat) { - if (!tls.context || - tls.certStat.mtime != stat[0].mtime || - tls.certStat.size != stat[0].size || - tls.keyStat.mtime != stat[1].mtime || - tls.keyStat.size != stat[1].size) { - print("Reloading " + kCertificatePath + " and " + kPrivateKeyPath); - var privateKey = new TextDecoder("ASCII").decode(File.readFile(kPrivateKeyPath)); - var certificate = new TextDecoder("ASCII").decode(File.readFile(kCertificatePath)); + var stat = await Promise.all([ + await File.stat(kCertificatePath), + await File.stat(kPrivateKeyPath), + ]); + if (!tls.context || + tls.certStat.mtime != stat[0].mtime || + tls.certStat.size != stat[0].size || + tls.keyStat.mtime != stat[1].mtime || + tls.keyStat.size != stat[1].size) { + print("Reloading " + kCertificatePath + " and " + kPrivateKeyPath); + var privateKey = new TextDecoder("ASCII").decode(await File.readFile(kPrivateKeyPath)); + var certificate = new TextDecoder("ASCII").decode(await File.readFile(kCertificatePath)); - tls.context = new TlsContext(); - tls.context.setPrivateKey(privateKey); - tls.context.setCertificate(certificate); - tls.certStat = stat[0]; - tls.keyStat = stat[1]; - } + tls.context = new TlsContext(); + tls.context.setPrivateKey(privateKey); + tls.context.setCertificate(certificate); + tls.certStat = stat[0]; + tls.keyStat = stat[1]; + } - return client.startTls(tls.context); - }).catch(function(error) { - logError("[" + new Date() + "] [" + client.peerName + "] " + error); - }); - }); + handleConnection(client); + return client.startTls(tls.context); + } catch (error) { + logError("[" + new Date() + "] [" + client.peerName + "] " + error); + } }); }).catch(function(error) { logError("[" + new Date() + "] bind error " + error); diff --git a/src/main.c b/src/main.c index 4cb58a98..1a31182b 100644 --- a/src/main.c +++ b/src/main.c @@ -376,6 +376,7 @@ static int _tf_command_run(const char* file, int argc, char* argv[]) .count = 1, .script = "core/core.js", .http_port = 12345, + .https_port = 12346, .ssb_port = 8009, .db_path = "db.sqlite", .secrets_path = "/.config/tildefriends/secret",