forked from coder/code-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoder-cloud.ts
More file actions
72 lines (61 loc) · 1.67 KB
/
Copy pathcoder-cloud.ts
File metadata and controls
72 lines (61 loc) · 1.67 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
import { logger } from "@coder/logger"
import { spawn } from "child_process"
import path from "path"
import split2 from "split2"
const coderCloudAgent = path.resolve(__dirname, "../../lib/coder-cloud-agent")
export async function coderCloudBind(serverName: string): Promise<void> {
const agent = spawn(coderCloudAgent, ["link", serverName], {
stdio: ["inherit", "inherit", "pipe"],
})
agent.stderr.pipe(split2()).on("data", (line) => {
line = line.replace(/^[0-9-]+ [0-9:]+ [^ ]+\t/, "")
logger.info(line)
})
return new Promise((res, rej) => {
agent.on("error", rej)
agent.on("close", (code) => {
if (code !== 0) {
rej({
message: `coder cloud agent exited with ${code}`,
})
return
}
res()
})
})
}
export function coderCloudProxy(addr: string) {
// addr needs to be in host:port format.
// So we trim the protocol.
addr = addr.replace(/^https?:\/\//, "")
const _proxy = async () => {
const agent = spawn(coderCloudAgent, ["proxy", "--code-server-addr", addr], {
stdio: ["inherit", "inherit", "pipe"],
})
agent.stderr.pipe(split2()).on("data", (line) => {
line = line.replace(/^[0-9-]+ [0-9:]+ [^ ]+\t/, "")
logger.info(line)
})
return new Promise((res, rej) => {
agent.on("error", rej)
agent.on("close", (code) => {
if (code !== 0) {
rej({
message: `coder cloud agent exited with ${code}`,
})
return
}
res()
})
})
}
const proxy = async () => {
try {
await _proxy()
} catch (err) {
logger.error(err.message)
}
setTimeout(proxy, 3000)
}
proxy()
}