Skip to content

Commit 6dddd19

Browse files
committed
exec stream WIP
1 parent f8dcd54 commit 6dddd19

File tree

3 files changed

+101
-47
lines changed

3 files changed

+101
-47
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ chromium http://test.callmemaybe
5555
#### Launch proxies only when you need it
5656
```yaml
5757
zerotier.host:
58-
ip: [hostip]
58+
ip: [host ip]
5959
start: systemctl start zerotier-one
6060
openvpn.host:
61-
ip: [hostip]
61+
ip: [host ip]
6262
start: openvpn /path/to/config.ovpn
6363
wireguard.host:
64-
ip: [hostip]
64+
ip: [host ip]
6565
start: systemctl start wg-quick@wg0
6666
```
6767

callmemaybe.js

Lines changed: 96 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#!/usr/bin/env node
22

33
global.config = {}
4+
Error.stackTraceLimit = Infinity
5+
46
for (const f in require('ramda'))
57
global[f] = require('ramda')[f]
8+
69
const {readFile, writeFile} = require('fs').promises
710
const dns2 = require('dns2');
811
const {promisify} = require('util')
@@ -11,36 +14,101 @@ const http = require('http')
1114
const daemonizeProcess = require('daemonize-process')
1215
const yaml = require('yaml')
1316
const { program } = require('commander')
17+
const execa = require('execa')
18+
19+
const running = {}
20+
21+
setInterval(() => {
22+
console.log({running: Object.keys(running)})
23+
for (const name in running) {
24+
// console.log(name, running[name])
25+
if (running[name].exitCode != null) {
26+
// console.log(name)
27+
delete running[name]
28+
}
29+
}
30+
}, 300)
31+
32+
const run = (command, name, opts) => {
33+
34+
console.log(`${name} run ${command}`)
35+
// pp({run: command, name})
36+
37+
const r = execa('bash', ['-c', command], opts)
38+
if (name) {
39+
// console.log(running[name].killed, running[name].closed)
40+
console.log(running[name] && running[name].exitCode)
41+
if (running[name] && running[name].exitCode == null) {
42+
console.log('alraeedy runnint ' + name)
43+
return running[name]
44+
}
45+
running[name] = r
46+
}
47+
// r.stdout.pipe(process.stdout)
48+
// r.stderr.pipe(process.stderr)
49+
return r
50+
}
51+
52+
const healthcheck = (c, name) => {
53+
54+
console.log(`${name} health ${c.healthcheck}`)
55+
// if (!c.healthcheck ) return Promise.reject()
56+
const r = running[name]
57+
if (r && r.exitCode == null) return r
58+
59+
if (r) {
60+
delete running[name]
61+
}
62+
63+
const healthcheck = running[`healthcheck ${name}`]
64+
if (healthcheck) return healthcheck
65+
66+
if (c.healthcheck)
67+
return run(c.healthcheck, `healthcheck ${name}`, {cwd: c.folder || '~'})
68+
69+
return Promise.reject({})
70+
}
71+
72+
73+
const onetimeServer = (title) => {
74+
// daemonizeProcess()
75+
let message = ''
76+
let closed = false
77+
process.stdin.on("data", data => {
78+
message += data.toString()
79+
process.stdout.write(message)
80+
})
81+
process.stdin.on('close', () => closed = true)
1482

15-
const onetimeServer = ({message, title}) => {
1683
try {
1784
const server = http.createServer(function (req, res) {
1885
res.writeHead(200, { 'Content-Type': 'text/plain' })
1986
res.write(`callmemaybe: ${title}\n\n${message}`)
2087
res.end()
21-
setTimeout(() => {
22-
console.log('one time server down')
23-
server.close()
24-
}, 100)
88+
if (closed) setTimeout(() => {
89+
console.log('one time server down')
90+
process.exit()
91+
}, 300)
92+
2593
}).listen({port: 80, host: '0.0.0.0'}, () => console.log('one time server up'))
2694
} catch(e) {
2795
console.error(e)
2896
}
2997
}
3098

3199
program
32-
.option('--test')
100+
.option('--server')
33101
.parse()
34102
const options = program.opts();
35103

36-
if (options.test) {
37-
onetimeServer({message: `Hello, is it me you're looking for?`, title: 'test'})
38-
// daemonizeProcess();
39-
return
104+
if (options.server) {
105+
return onetimeServer('')
40106
}
41107

42108
const pp = x =>
43-
process.stdout.write(yaml.stringify(x || {}))
109+
x
110+
// console.log(x)
111+
// process.stdout.write(yaml.stringify(x || {}))
44112

45113
const pe = x =>
46114
process.stderr.write(yaml.stringify(x || {}))
@@ -49,7 +117,10 @@ dns2.pp = (x, comment='') => console.log(comment + join('',values(mapObjIndexed(
49117

50118
require('./config').then(() => {
51119

52-
let running = []
120+
121+
// setInterval(() => {
122+
// pp({running})
123+
// }, 1000)
53124

54125
const server = dns2.createServer({
55126
udp: true,
@@ -59,7 +130,7 @@ const server = dns2.createServer({
59130
const c = config[question.name]
60131

61132
if (c) {
62-
pp({matched: c, running})
133+
pp({matched: c, running: keys(running)})
63134
response.answers.push({
64135
name: question.name,
65136
type: dns2.Packet.TYPE.A,
@@ -68,36 +139,18 @@ const server = dns2.createServer({
68139
address: c.ip || '127.0.0.1'
69140
});
70141

71-
if (includes(question.name, running)) return send(response)
72-
running.push(question.name)
73-
setTimeout(() => {
74-
running = without(question.name, running)
75-
pp({running})
76-
}, 1000) //should be healthcheck start interval
77-
78-
await (c.healthcheck ? exec(c.healthcheck, {cwd: c.folder || '~', stdio: 'inherit'}) : Promise.reject({}))
79-
.then(({stdout, stderr}) => {
80-
running.push(question.name)
81-
send(response)
82-
pp({healthcheck: 'ok', stdout, stderr})
142+
await healthcheck(c, question.name)
143+
.catch(async x => {
144+
await run(c.start, question.name, {cwd: c.folder})
145+
.catch(({stderr, stdout}) => {
146+
console.log('failed to start')
147+
run(`/home/vganzin/work/callmemaybe/callmemaybe.js --server`, 'error-server', {input: stderr+stdout})
148+
.catch(pp)
149+
})
83150
})
84-
.catch(async ({stdout, stderr}) => {
85-
pp({healthcheck: 'fail', stdout, stderr})
86-
running = without(question.name, running)
87-
if (c.start) {
88-
pp({starting: c.start})
89-
90-
return await exec(c.start, {cwd: c.folder, stdio: 'inherit'})
91-
.then(pp)
92-
.catch(({stderr}) => {
93-
pp({stderr})
94-
onetimeServer({message: stderr, title: question.name + ' ' + c.start + ' error'})
95-
}).then(() => {
96-
send(response)
97-
})
98-
}
99151

100-
})
152+
send(response)
153+
return
101154
}
102155

103156
if (blocklist.has(question.name)) {
@@ -130,8 +183,8 @@ const server = dns2.createServer({
130183
})
131184

132185
// server.on('request', (request, response, rinfo) => {
133-
// console.log(request.header.id, request.questions[0]);
134-
// });
186+
// console.log(request.header.id, request.questions[0])
187+
// })
135188

136189
.on('requestError', (error) => {
137190
console.log('Client sent an invalid request', error)

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "callmemaybe",
3-
"version": "0.4.0",
3+
"version": "0.5.0",
44
"description": "",
55
"main": "index.js",
66
"scripts": {
@@ -21,6 +21,7 @@
2121
"daemonize-process": "^3.0.0",
2222
"deep-diff": "^1.0.2",
2323
"dns2": "^2.0.5",
24+
"execa": "^5.1.1",
2425
"got": "^11.8.3",
2526
"ramda": "^0.28.0",
2627
"yaml": "^2.2.1"

0 commit comments

Comments
 (0)