Fixed https.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3734 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2022-01-05 01:58:12 +00:00
parent 24cf18651a
commit c02a3d3659
3 changed files with 39 additions and 35 deletions

View File

@ -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) { function sendData(response, data, type) {
if (data) { if (data) {
if (startsWithBytes(data, [0xff, 0xd8, 0xff, 0xdb]) || if (startsWithBytes(data, [0xff, 0xd8, 0xff, 0xdb]) ||
@ -555,14 +566,7 @@ loadSettings().then(function() {
} else if (request.uri == "/robots.txt") { } else if (request.uri == "/robots.txt") {
return blobHandler(request, response, null, request.uri); return blobHandler(request, response, null, request.uri);
} else if ((match = /^\/.well-known\/(.*)/.exec(request.uri)) && request.uri.indexOf("..") == -1) { } else if ((match = /^\/.well-known\/(.*)/.exec(request.uri)) && request.uri.indexOf("..") == -1) {
var data = File.readFile("data/global/.well-known/" + match[1]); return wellKnownHandler(request, response, 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");
}
} else { } else {
var data = "File not found."; var data = "File not found.";
response.writeHead(404, {"Content-Type": "text/plain; charset=utf-8", "Content-Length": data.length.toString()}); response.writeHead(404, {"Content-Type": "text/plain; charset=utf-8", "Content-Length": data.length.toString()});

View File

@ -468,38 +468,37 @@ if (tildefriends.https_port) {
var tls = {}; var tls = {};
var secureSocket = new Socket(); var secureSocket = new Socket();
secureSocket.bind(kHost, tildefriends.https_port).then(function() { secureSocket.bind(kHost, tildefriends.https_port).then(function() {
return secureSocket.listen(kBacklog, function() { return secureSocket.listen(kBacklog, async function() {
return secureSocket.accept().then(function(client) { try {
handleConnection(client); var client = await secureSocket.accept();
const kCertificatePath = "data/httpd/certificate.pem"; const kCertificatePath = "data/httpd/certificate.pem";
const kPrivateKeyPath = "data/httpd/privatekey.pem"; const kPrivateKeyPath = "data/httpd/privatekey.pem";
return Promise.all([ var stat = await Promise.all([
File.stat(kCertificatePath), await File.stat(kCertificatePath),
File.stat(kPrivateKeyPath), await File.stat(kPrivateKeyPath),
]).then(function(stat) { ]);
if (!tls.context || if (!tls.context ||
tls.certStat.mtime != stat[0].mtime || tls.certStat.mtime != stat[0].mtime ||
tls.certStat.size != stat[0].size || tls.certStat.size != stat[0].size ||
tls.keyStat.mtime != stat[1].mtime || tls.keyStat.mtime != stat[1].mtime ||
tls.keyStat.size != stat[1].size) { tls.keyStat.size != stat[1].size) {
print("Reloading " + kCertificatePath + " and " + kPrivateKeyPath); print("Reloading " + kCertificatePath + " and " + kPrivateKeyPath);
var privateKey = new TextDecoder("ASCII").decode(File.readFile(kPrivateKeyPath)); var privateKey = new TextDecoder("ASCII").decode(await File.readFile(kPrivateKeyPath));
var certificate = new TextDecoder("ASCII").decode(File.readFile(kCertificatePath)); var certificate = new TextDecoder("ASCII").decode(await File.readFile(kCertificatePath));
tls.context = new TlsContext(); tls.context = new TlsContext();
tls.context.setPrivateKey(privateKey); tls.context.setPrivateKey(privateKey);
tls.context.setCertificate(certificate); tls.context.setCertificate(certificate);
tls.certStat = stat[0]; tls.certStat = stat[0];
tls.keyStat = stat[1]; tls.keyStat = stat[1];
} }
return client.startTls(tls.context); handleConnection(client);
}).catch(function(error) { return client.startTls(tls.context);
logError("[" + new Date() + "] [" + client.peerName + "] " + error); } catch (error) {
}); logError("[" + new Date() + "] [" + client.peerName + "] " + error);
}); }
}); });
}).catch(function(error) { }).catch(function(error) {
logError("[" + new Date() + "] bind error " + error); logError("[" + new Date() + "] bind error " + error);

View File

@ -376,6 +376,7 @@ static int _tf_command_run(const char* file, int argc, char* argv[])
.count = 1, .count = 1,
.script = "core/core.js", .script = "core/core.js",
.http_port = 12345, .http_port = 12345,
.https_port = 12346,
.ssb_port = 8009, .ssb_port = 8009,
.db_path = "db.sqlite", .db_path = "db.sqlite",
.secrets_path = "/.config/tildefriends/secret", .secrets_path = "/.config/tildefriends/secret",