From 8d4fd327acfb275fe7838b41b921e663f55a763b Mon Sep 17 00:00:00 2001 From: nosemeocurrenada Date: Mon, 4 Sep 2017 15:26:15 -0300 Subject: [PATCH] Fix installation from npm (#23) * Moved the command,args choice to a function + Add files to the package.json * Using js export * fix on package.json --- app.js | 1 - index.js | 2 ++ package.json | 7 +++++-- wetty.js | 33 ++++++++++++++++++++++++--------- 4 files changed, 31 insertions(+), 12 deletions(-) create mode 100644 index.js 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 944f5da..a4eee66 100644 --- a/wetty.js +++ b/wetty.js @@ -36,20 +36,35 @@ function createServer(port, sslopts) { }); } -export default (port, globalsshuser, sshhost, sshport, sshauth, sslopts) => { +function getCommand(socket, sshuser, sshhost, sshport, sshauth) { + const { request } = socket; + const match = request.headers.referer.match('.+/ssh/.+$'); + const sshAddress = sshuser ? `${sshuser}@${sshhost}` : sshhost; + const ssh = match ? `${match[0].split('/ssh/').pop()}@${sshhost}` : sshAddress; + + return [ + process.getuid() === 0 && sshhost === 'localhost' + ? ['login', '-h', socket.client.conn.remoteAddress.split(':')[3]] + : [ + path.join(__dirname, 'bin/ssh'), + ssh, + '-p', + sshport, + '-o', + `PreferredAuthentications=${sshauth}`, + ], + 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; 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 args = process.getuid() === 0 && sshhost === 'localhost' - ? ['login', '-h', socket.client.conn.remoteAddress.split(':')[3]] - : [ssh, '-p', sshport, '-o', `PreferredAuthentications=${sshauth}`]; + const [args, ssh] = getCommand(socket, sshuser, sshhost, sshport, sshauth); const term = pty.spawn('/usr/bin/env', args, { name: 'xterm-256color', cols: 80, @@ -76,4 +91,4 @@ export default (port, globalsshuser, sshhost, sshport, sshauth, sslopts) => { }); }); return events; -}; +}