Created
March 23, 2018 07:16
-
-
Save Muscipular/c965d8e9dff85d37fab1f25c6a3d9419 to your computer and use it in GitHub Desktop.
redis-cli-ssl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/node | |
const { execSync, exec, spawn, spawnSync } = require('child_process'); | |
const { writeFileSync, unlinkSync } = require('fs'); | |
process.on('uncaughtException', e => LogError(e)); | |
const config = `/tmp/stunnel-${Date.now()}${Math.random()}.conf`; | |
const port = parseInt(Math.random() * 20000) + 30000; | |
let stunnel = null; | |
const quiet = process.argv.indexOf("---Q") >= 0; | |
const debug = !quiet && process.argv.indexOf("---D") >= 0; | |
const args = process.argv.slice(2).filter(c => ['---D', '---Q'].indexOf(c) < 0); | |
let host = args[args.indexOf('-h') + 1]; | |
let hostPort = args[args.indexOf('-p') + 1]; | |
args[args.indexOf('-h') + 1] = '127.0.0.1'; | |
args[args.indexOf('-p') + 1] = port; | |
LogDebug(args, port, host, hostPort, config); | |
const configData = ` | |
foreground = yes | |
[redis-ssl] | |
client = yes | |
accept = 127.0.0.1:${port} | |
connect = ${host}:${hostPort} | |
verify = 0 | |
CApath = @sysconfdir/ssl/certs | |
` | |
writeFileSync(config, configData); | |
LogDebug('config file: \n', execSync(`cat ${config}`).toString('utf8')) | |
LogInfo(`start stunnel@${port}...`); | |
stunnel = spawn(`stunnel`, [config], { stdio: 'pipe' }); | |
stunnel.stdout.on('data', (data) => { | |
LogDebug(`stunnel stdout:\n ${data}`); | |
}); | |
stunnel.stderr.on('data', (data) => { | |
LogDebug(`stunnel stdout:\n ${data}`); | |
}); | |
LogInfo(`stunnel[${stunnel.pid}] started.`) | |
setTimeout(() => { | |
LogInfo("start redis..."); | |
LogDebug("redis args:", ...args); | |
spawnSync("redis-cli", args, { | |
stdio: "inherit" | |
}) | |
LogInfo("kill stunnel.") | |
stunnel.kill(); | |
unlinkSync(config); | |
LogInfo("clean up, bye.") | |
process.exit(); | |
}, 300); | |
function LogDebug(...args) { | |
if (debug) | |
console.log('\x1b[31m[DEBUG]\x1b[39m', ...args); | |
} | |
function LogInfo(...args) { | |
if (!quiet) | |
console.log('\x1b[32m[INFO]\x1b[39m', ...args); | |
} | |
function LogError(...args) { | |
if (!quiet) | |
console.error('\x1b[31m[Error]\x1b[39m', ...args); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment