-
Notifications
You must be signed in to change notification settings - Fork 629
Expand file tree
/
Copy pathserver-entry.js
More file actions
253 lines (226 loc) · 7.74 KB
/
server-entry.js
File metadata and controls
253 lines (226 loc) · 7.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import { createServer } from 'node:http'
import { readFile, stat } from 'node:fs/promises'
import { join, extname } from 'node:path'
import { fileURLToPath } from 'node:url'
import server from './dist/server/server.js'
const __dirname = fileURLToPath(new URL('.', import.meta.url))
const CLIENT_DIR = join(__dirname, 'dist', 'client')
const port = parseInt(process.env.PORT || '3000', 10)
// Default HOST to localhost-only. Operators who want the workspace reachable
// on a LAN / Tailscale / public surface must opt in explicitly with
// HOST=0.0.0.0 *and* set CLAUDE_PASSWORD (enforced below). See #122.
const host = process.env.HOST || '127.0.0.1'
function isNonLoopbackHost(h) {
if (!h) return false
const norm = h.trim().toLowerCase()
if (norm === '127.0.0.1' || norm === '::1' || norm === 'localhost') {
return false
}
return true
}
if (isNonLoopbackHost(host)) {
// Honor HERMES_PASSWORD (current name) with CLAUDE_PASSWORD as a back-compat
// fallback for deployments configured pre-rename.
const password = (
process.env.HERMES_PASSWORD ||
process.env.CLAUDE_PASSWORD ||
''
).trim()
if (!password) {
console.error(
'\n[workspace] refusing to start.\n' +
` HOST is set to "${host}" (non-loopback), but HERMES_PASSWORD is unset.\n` +
' This would expose a high-privilege control plane (terminals, files, agents)\n' +
' to anyone who can reach the port. Either:\n' +
' • set HOST=127.0.0.1 for local-only access, or\n' +
' • set HERMES_PASSWORD=<strong-secret> to enable workspace auth, or\n' +
' • set HERMES_ALLOW_INSECURE_REMOTE=1 to bypass this check (not recommended).\n' +
' See #122 for context.\n',
)
const allowInsecure = (
process.env.HERMES_ALLOW_INSECURE_REMOTE ||
process.env.CLAUDE_ALLOW_INSECURE_REMOTE ||
''
)
.trim()
.toLowerCase()
if (
allowInsecure !== '1' &&
allowInsecure !== 'true' &&
allowInsecure !== 'yes'
) {
process.exit(1)
}
console.warn(
'[workspace] HERMES_ALLOW_INSECURE_REMOTE is set — starting anyway.',
)
}
// Warn when serving over plain HTTP with a password: NODE_ENV=production
// sets the Secure flag on session cookies, which browsers silently drop
// over http://. Operators must set COOKIE_SECURE=0 for plain-HTTP LAN
// deployments. See #149.
const cookieSecureOverride = (process.env.COOKIE_SECURE || '')
.trim()
.toLowerCase()
const cookieSecureExplicit =
cookieSecureOverride === '0' ||
cookieSecureOverride === 'false' ||
cookieSecureOverride === 'no'
if (!cookieSecureExplicit && process.env.NODE_ENV === 'production') {
console.warn(
'\n[workspace] warning: plain-HTTP LAN deployment detected.\n' +
' NODE_ENV=production enables the Secure flag on session cookies.\n' +
' Browsers silently drop Secure cookies over http://, so login will fail.\n' +
' Add COOKIE_SECURE=0 to your .env to fix this. See #149.\n',
)
}
}
const MIME_TYPES = {
'.js': 'application/javascript',
'.mjs': 'application/javascript',
'.css': 'text/css',
'.html': 'text/html',
'.json': 'application/json',
'.svg': 'image/svg+xml',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.webp': 'image/webp',
'.gif': 'image/gif',
'.ico': 'image/x-icon',
'.woff': 'font/woff',
'.woff2': 'font/woff2',
'.ttf': 'font/ttf',
'.eot': 'application/vnd.ms-fontobject',
'.map': 'application/json',
'.txt': 'text/plain',
'.xml': 'application/xml',
'.webmanifest': 'application/manifest+json',
}
async function tryServeStatic(req, res) {
const url = new URL(
req.url || '/',
`http://${req.headers.host || 'localhost'}`,
)
const pathname = decodeURIComponent(url.pathname)
// Prevent directory traversal
if (pathname.includes('..')) return false
// Asset requests should never fall through to the SSR handler. If a browser
// asks for a stale hashed JS/CSS chunk after a deploy or branch switch,
// returning the HTML shell with 200 text/html makes the SPA fail as a black
// screen. Return a real 404 instead so clients reload/recover correctly and
// health checks can detect the broken asset reference.
if (pathname.startsWith('/assets/')) {
const filePath = join(CLIENT_DIR, pathname)
if (!filePath.startsWith(CLIENT_DIR)) return false
try {
const fileStat = await stat(filePath)
if (!fileStat.isFile()) throw new Error('not a file')
} catch {
res.writeHead(404, {
'Content-Type': 'text/plain; charset=utf-8',
'Cache-Control': 'no-store, no-cache, must-revalidate, proxy-revalidate',
})
res.end('Asset not found')
return true
}
}
const filePath = join(CLIENT_DIR, pathname)
// Make sure the resolved path is within CLIENT_DIR
if (!filePath.startsWith(CLIENT_DIR)) return false
try {
const fileStat = await stat(filePath)
if (!fileStat.isFile()) return false
const ext = extname(filePath).toLowerCase()
const contentType = MIME_TYPES[ext] || 'application/octet-stream'
const data = await readFile(filePath)
const headers = {
'Content-Type': contentType,
'Content-Length': data.length,
}
// Cache hashed assets aggressively (they have content hashes in filenames)
if (pathname.startsWith('/assets/')) {
headers['Cache-Control'] = 'public, max-age=31536000, immutable'
}
res.writeHead(200, headers)
res.end(data)
return true
} catch {
return false
}
}
async function requestHandler(req, res) {
// Try static files first (client assets)
if (req.method === 'GET' || req.method === 'HEAD') {
const served = await tryServeStatic(req, res)
if (served) return
}
// Fall through to SSR handler
const url = new URL(
req.url || '/',
`http://${req.headers.host || 'localhost'}`,
)
const headers = new Headers()
for (const [key, value] of Object.entries(req.headers)) {
if (value) headers.set(key, Array.isArray(value) ? value.join(', ') : value)
}
let body = null
if (req.method !== 'GET' && req.method !== 'HEAD') {
body = await new Promise((resolve) => {
const chunks = []
req.on('data', (chunk) => chunks.push(chunk))
req.on('end', () => resolve(Buffer.concat(chunks)))
})
}
const request = new Request(url.toString(), {
method: req.method,
headers,
body,
duplex: 'half',
})
try {
const response = await server.fetch(request)
res.writeHead(
response.status,
Object.fromEntries(response.headers.entries()),
)
if (response.body) {
const reader = response.body.getReader()
const pump = async () => {
while (true) {
const { done, value } = await reader.read()
if (done) break
res.write(value)
}
res.end()
}
pump().catch((err) => {
console.error('Stream error:', err)
res.end()
})
} else {
const text = await response.text()
res.end(text)
}
} catch (err) {
console.error('Request error:', err)
res.writeHead(500)
res.end('Internal Server Error')
}
}
function listenOn(bindHost) {
const httpServer = createServer(requestHandler)
httpServer.listen(port, bindHost, () => {
console.log(`Hermes Workspace running at http://${bindHost}:${port}`)
})
return httpServer
}
listenOn(host)
// Cloudflared remote-managed ingress currently points at http://localhost:10280.
// On macOS, localhost may resolve to ::1 before 127.0.0.1; if Workspace only
// listens on IPv4 loopback, tunneled requests intermittently fail with
// `dial tcp [::1]:10280: connect: connection refused`. Keep the default
// local-only security posture while also serving IPv6 loopback.
if (host === '127.0.0.1') {
listenOn('::1')
}