Skip to content

Commit

Permalink
exec stream WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
slavaGanzin committed Jan 29, 2023
1 parent f8dcd54 commit 6dddd19
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 47 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ chromium http://test.callmemaybe
#### Launch proxies only when you need it
```yaml
zerotier.host:
ip: [hostip]
ip: [host ip]
start: systemctl start zerotier-one
openvpn.host:
ip: [hostip]
ip: [host ip]
start: openvpn /path/to/config.ovpn
wireguard.host:
ip: [hostip]
ip: [host ip]
start: systemctl start wg-quick@wg0
```
Expand Down
139 changes: 96 additions & 43 deletions callmemaybe.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#!/usr/bin/env node

global.config = {}
Error.stackTraceLimit = Infinity

for (const f in require('ramda'))
global[f] = require('ramda')[f]

const {readFile, writeFile} = require('fs').promises
const dns2 = require('dns2');
const {promisify} = require('util')
Expand All @@ -11,36 +14,101 @@ const http = require('http')
const daemonizeProcess = require('daemonize-process')
const yaml = require('yaml')
const { program } = require('commander')
const execa = require('execa')

const running = {}

setInterval(() => {
console.log({running: Object.keys(running)})
for (const name in running) {
// console.log(name, running[name])
if (running[name].exitCode != null) {
// console.log(name)
delete running[name]
}
}
}, 300)

const run = (command, name, opts) => {

console.log(`${name} run ${command}`)
// pp({run: command, name})

const r = execa('bash', ['-c', command], opts)
if (name) {
// console.log(running[name].killed, running[name].closed)
console.log(running[name] && running[name].exitCode)
if (running[name] && running[name].exitCode == null) {
console.log('alraeedy runnint ' + name)
return running[name]
}
running[name] = r
}
// r.stdout.pipe(process.stdout)
// r.stderr.pipe(process.stderr)
return r
}

const healthcheck = (c, name) => {

console.log(`${name} health ${c.healthcheck}`)
// if (!c.healthcheck ) return Promise.reject()
const r = running[name]
if (r && r.exitCode == null) return r

if (r) {
delete running[name]
}

const healthcheck = running[`healthcheck ${name}`]
if (healthcheck) return healthcheck

if (c.healthcheck)
return run(c.healthcheck, `healthcheck ${name}`, {cwd: c.folder || '~'})

return Promise.reject({})
}


const onetimeServer = (title) => {
// daemonizeProcess()
let message = ''
let closed = false
process.stdin.on("data", data => {
message += data.toString()
process.stdout.write(message)
})
process.stdin.on('close', () => closed = true)

const onetimeServer = ({message, title}) => {
try {
const server = http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.write(`callmemaybe: ${title}\n\n${message}`)
res.end()
setTimeout(() => {
console.log('one time server down')
server.close()
}, 100)
if (closed) setTimeout(() => {
console.log('one time server down')
process.exit()
}, 300)

}).listen({port: 80, host: '0.0.0.0'}, () => console.log('one time server up'))
} catch(e) {
console.error(e)
}
}

program
.option('--test')
.option('--server')
.parse()
const options = program.opts();

if (options.test) {
onetimeServer({message: `Hello, is it me you're looking for?`, title: 'test'})
// daemonizeProcess();
return
if (options.server) {
return onetimeServer('')
}

const pp = x =>
process.stdout.write(yaml.stringify(x || {}))
x
// console.log(x)
// process.stdout.write(yaml.stringify(x || {}))

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

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

let running = []

// setInterval(() => {
// pp({running})
// }, 1000)

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

if (c) {
pp({matched: c, running})
pp({matched: c, running: keys(running)})
response.answers.push({
name: question.name,
type: dns2.Packet.TYPE.A,
Expand All @@ -68,36 +139,18 @@ const server = dns2.createServer({
address: c.ip || '127.0.0.1'
});

if (includes(question.name, running)) return send(response)
running.push(question.name)
setTimeout(() => {
running = without(question.name, running)
pp({running})
}, 1000) //should be healthcheck start interval

await (c.healthcheck ? exec(c.healthcheck, {cwd: c.folder || '~', stdio: 'inherit'}) : Promise.reject({}))
.then(({stdout, stderr}) => {
running.push(question.name)
send(response)
pp({healthcheck: 'ok', stdout, stderr})
await healthcheck(c, question.name)
.catch(async x => {
await run(c.start, question.name, {cwd: c.folder})
.catch(({stderr, stdout}) => {
console.log('failed to start')
run(`/home/vganzin/work/callmemaybe/callmemaybe.js --server`, 'error-server', {input: stderr+stdout})
.catch(pp)
})
})
.catch(async ({stdout, stderr}) => {
pp({healthcheck: 'fail', stdout, stderr})
running = without(question.name, running)
if (c.start) {
pp({starting: c.start})

return await exec(c.start, {cwd: c.folder, stdio: 'inherit'})
.then(pp)
.catch(({stderr}) => {
pp({stderr})
onetimeServer({message: stderr, title: question.name + ' ' + c.start + ' error'})
}).then(() => {
send(response)
})
}

})
send(response)
return
}

if (blocklist.has(question.name)) {
Expand Down Expand Up @@ -130,8 +183,8 @@ const server = dns2.createServer({
})

// server.on('request', (request, response, rinfo) => {
// console.log(request.header.id, request.questions[0]);
// });
// console.log(request.header.id, request.questions[0])
// })

.on('requestError', (error) => {
console.log('Client sent an invalid request', error)
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "callmemaybe",
"version": "0.4.0",
"version": "0.5.0",
"description": "",
"main": "index.js",
"scripts": {
Expand All @@ -21,6 +21,7 @@
"daemonize-process": "^3.0.0",
"deep-diff": "^1.0.2",
"dns2": "^2.0.5",
"execa": "^5.1.1",
"got": "^11.8.3",
"ramda": "^0.28.0",
"yaml": "^2.2.1"
Expand Down

0 comments on commit 6dddd19

Please sign in to comment.