diff --git a/posimai-dev/server.js b/posimai-dev/server.js index 81a0bffa..c58b51e2 100644 --- a/posimai-dev/server.js +++ b/posimai-dev/server.js @@ -3,14 +3,30 @@ const express = require('express'); const { WebSocketServer } = require('ws'); const pty = require('node-pty'); const http = require('http'); +const https = require('https'); +const fs = require('fs'); const path = require('path'); +const os = require('os'); const app = express(); const PORT = process.env.PORT || 3333; 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' }); wss.on('connection', (ws) => { @@ -42,5 +58,5 @@ wss.on('connection', (ws) => { }); 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}`); });