diff --git a/app.js b/app.js index d786c14..b2f296c 100644 --- a/app.js +++ b/app.js @@ -1,4 +1,3 @@ #! /usr/bin/env node require('@std/esm'); require('./cli'); -module.exports = require('./wetty').default; diff --git a/index.js b/index.js new file mode 100644 index 0000000..fb8ef13 --- /dev/null +++ b/index.js @@ -0,0 +1,2 @@ +require('@std/esm'); +module.exports = require('./wetty'); diff --git a/package.json b/package.json index ffb845c..e1e50cf 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,7 @@ }, "homepage": "https://github.com/butlerx/wetty", "preferGlobal": "true", - "main": "app.js", + "main": "index.js", "bin": { "wetty": "./app.js" }, @@ -64,7 +64,10 @@ }, "files": [ "bin", - "public" + "public", + "app.js", + "cli.js", + "wetty.js" ], "scripts": { "lint": "eslint .", diff --git a/wetty.js b/wetty.js index c9a338d..554315b 100644 --- a/wetty.js +++ b/wetty.js @@ -36,21 +36,32 @@ function createServer(port, sslopts) { }); } -export default (port, globalsshuser, sshhost, sshport, sshauth, sslopts) => { +function getCommand(socket, sshuser, sshhost, sshport, sshauth) { + const request = socket.request; + const match = request.headers.referer.match('.+/ssh/.+$'); + const sshAddress = sshuser ? `${sshuser}@${sshhost}` : sshhost; + const ssh = match ? `${match[0].split('/ssh/').pop()}@${sshhost}` : sshAddress; + + let cmd; + let args; + if (process.getuid() === 0 && sshhost === 'localhost') { + cmd = '/usr/bin/login'; + args = ['-h', socket.client.conn.remoteAddress.split(':')[3]]; + } else { + cmd = path.join(__dirname, 'bin/ssh'); + args = [ssh, '-p', sshport, '-o', `PreferredAuthentications=${sshauth}`]; + } + return [cmd, args, ssh]; +} + +export default function start(port, sshuser, sshhost, sshport, sshauth, sslopts) { const httpserv = createServer(port, sslopts); const events = new EventEmitter(); const io = server(httpserv, { path: '/wetty/socket.io' }); io.on('connection', socket => { - const request = socket.request; console.log(`${new Date()} Connection accepted.`); - const match = request.headers.referer.match('.+/ssh/.+$'); - const sshAddress = globalsshuser ? `${globalsshuser}@${sshhost}` : sshhost; - const ssh = match ? `${match[0].split('/ssh/').pop()}@${sshhost}` : sshAddress; - const cmd = process.getuid() === 0 && sshhost === 'localhost' ? '/usr/bin/login' : './bin/ssh'; - const args = - cmd === './bin/ssh' - ? [ssh, '-p', sshport, '-o', `PreferredAuthentications=${sshauth}`] - : ['-h', socket.client.conn.remoteAddress.split(':')[3]]; + + const [cmd, args, ssh] = getCommand(socket, sshuser, sshhost, sshport, sshauth); const term = pty.spawn(cmd, args, { name: 'xterm-256color', cols: 80, @@ -77,4 +88,4 @@ export default (port, globalsshuser, sshhost, sshport, sshauth, sslopts) => { }); }); return events; -}; +}