nodejs code to enable zmNinga to establish a ngrok connection - generated by codex!
Posted: Sun Jan 15, 2023 12:11 am
So this is really trippy - I don't know if people are familiar with openai and it's code generation capabilities. After writing a simple python script to establish a ngrok connection, I pasted the code into openai and asked it to review my code. It seemed to like my code but suggested using a more robust library for SSH connections like paramiko or fabric for prolonged sessions.
Then I took the natural language interpretation of the code generated by openai and pasted it into the codex javascript sandbox and out popped the following nodejs code. It even includes the asynchronous function to connect to ssh once the host port has been obtained from ngrok api. I could have just generated the code by typing in a few natural language lines describing the function. I forgot to ask it about establishing local forwards and asked it to add the localforwards to ssh -- see below
I have a zmNinja build environment setup and will figure out where to insert this function:
Then I took the natural language interpretation of the code generated by openai and pasted it into the codex javascript sandbox and out popped the following nodejs code. It even includes the asynchronous function to connect to ssh once the host port has been obtained from ngrok api. I could have just generated the code by typing in a few natural language lines describing the function. I forgot to ask it about establishing local forwards and asked it to add the localforwards to ssh -- see below
I have a zmNinja build environment setup and will figure out where to insert this function:
Code: Select all
/*
Can you add the following:
Obtain a list of local port forwards from the config file. The format is port:remote_hostname:remote_port.
Then when opening the ssh connection, establish the localforward ports on localhost
*/
var fs = require('fs');
var ngrok = require('ngrok');
var config = JSON.parse(fs.readFileSync('config.json', 'utf8'));
var apiKey = config.apiKey;
var username = config.username;
var password = config.password;
var port = config.port;
var localForwards = config.localForwards;
ngrok.authtoken(apiKey, function (err, token) {
if (err) {
console.log(err);
} else {
while (true) {
ngrok.connect({
proto: 'tcp',
addr: port,
auth: username + ':' + password
}, function (err, url) {
if (err) {
console.log(err);
} else {
var hostname = url.split(':')[0];
var hostport = url.split(':')[1];
console.log(hostname);
console.log(hostport);
var ssh = require('ssh2').Client();
ssh.on('ready', function () {
console.log('Client :: ready');
ssh.exec('uptime', function (err, stream) {
if (err) throw err;
stream.on('close', function (code, signal) {
console.log('Stream :: close :: code: ' + code + ', signal: ' + signal);
ssh.end();
}).on('data', function (data) {
console.log('STDOUT: ' + data);
}).stderr.on('data', function (data) {
console.log('STDERR: ' + data);
});
});
}).connect({
host: hostname,
port: hostport,
username: username,
password: password,
localForwards: localForwards
});
}
});
sleep(2000);
}
}
});