feat: auto-detect Tailscale cert for HTTPS
This commit is contained in:
parent
8f5c205edd
commit
f38b76a9e9
|
|
@ -3,14 +3,30 @@ const express = require('express');
|
||||||
const { WebSocketServer } = require('ws');
|
const { WebSocketServer } = require('ws');
|
||||||
const pty = require('node-pty');
|
const pty = require('node-pty');
|
||||||
const http = require('http');
|
const http = require('http');
|
||||||
|
const https = require('https');
|
||||||
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
const os = require('os');
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
const PORT = process.env.PORT || 3333;
|
const PORT = process.env.PORT || 3333;
|
||||||
|
|
||||||
app.use(express.static(path.join(__dirname)));
|
app.use(express.static(path.join(__dirname)));
|
||||||
|
|
||||||
const server = http.createServer(app);
|
// ホームディレクトリのTailscale証明書を自動検出
|
||||||
|
function findCert() {
|
||||||
|
const home = os.homedir();
|
||||||
|
const crt = fs.readdirSync(home).find((f) => f.endsWith('.crt'));
|
||||||
|
if (!crt) return null;
|
||||||
|
const key = crt.replace('.crt', '.key');
|
||||||
|
if (!fs.existsSync(path.join(home, key))) return null;
|
||||||
|
return { cert: fs.readFileSync(path.join(home, crt)), key: fs.readFileSync(path.join(home, key)) };
|
||||||
|
}
|
||||||
|
|
||||||
|
const tlsOpts = findCert();
|
||||||
|
const server = tlsOpts ? https.createServer(tlsOpts, app) : http.createServer(app);
|
||||||
|
const proto = tlsOpts ? 'https' : 'http';
|
||||||
|
|
||||||
const wss = new WebSocketServer({ server, path: '/terminal' });
|
const wss = new WebSocketServer({ server, path: '/terminal' });
|
||||||
|
|
||||||
wss.on('connection', (ws) => {
|
wss.on('connection', (ws) => {
|
||||||
|
|
@ -42,5 +58,5 @@ wss.on('connection', (ws) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
server.listen(PORT, '0.0.0.0', () => {
|
server.listen(PORT, '0.0.0.0', () => {
|
||||||
console.log(`posimai-dev running on http://0.0.0.0:${PORT}`);
|
console.log(`posimai-dev running on ${proto}://0.0.0.0:${PORT}`);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue