Browse Source

Ask ssh user from the standard input and force ssh connection

pull/226/head
Janos Kasza 5 years ago
parent
commit
eb54210dae
  1. 13
      README.md
  2. 9
      bin/ssh-with-user
  3. 2
      docs/API.md
  4. 12
      index.js
  5. 4
      src/server/cli/index.ts
  6. 2
      src/server/cli/options.ts
  7. 4
      src/server/cli/parseArgs.ts
  8. 10
      src/server/command/index.ts
  9. 9
      src/server/command/ssh.ts
  10. 1
      src/server/interfaces.ts
  11. 5
      src/server/wetty/index.ts

13
README.md

@ -1,7 +1,9 @@
# WeTTY = Web + TTY. # WeTTY = Web + TTY.
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section --> <!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
![All Contributors](https://img.shields.io/badge/all_contributors-33-orange.svg?style=flat-square) <!-- ALL-CONTRIBUTORS-BADGE:END -->
![All Contributors](https://img.shields.io/badge/all_contributors-33-orange.svg?style=flat-square)
<!-- ALL-CONTRIBUTORS-BADGE:END -->
![Version](https://img.shields.io/badge/version-1.1.7-blue.svg?cacheSeconds=2592000) ![Version](https://img.shields.io/badge/version-1.1.7-blue.svg?cacheSeconds=2592000)
![Node Version](https://img.shields.io/badge/node-%3E%3D6.9-blue.svg) ![Node Version](https://img.shields.io/badge/node-%3E%3D6.9-blue.svg)
[![Documentation](https://img.shields.io/badge/documentation-yes-brightgreen.svg)](https://github.com/butlerx/wetty/tree/master/docs) [![Documentation](https://img.shields.io/badge/documentation-yes-brightgreen.svg)](https://github.com/butlerx/wetty/tree/master/docs)
@ -31,7 +33,7 @@ yarn global add wetty
## Usage ## Usage
```sh ```sh
wetty [-h] [--port PORT] [--base BASE] [--sshhost SSH_HOST] [--sshport SSH_PORT] [--sshuser SSH_USER] [--host HOST] [--command COMMAND] [--bypasshelmet] [--title TITLE] [--sslkey SSL_KEY_PATH] [--sslcert SSL_CERT_PATH] wetty [-h] [--port PORT] [--base BASE] [--sshhost SSH_HOST] [--sshport SSH_PORT] [--sshuser SSH_USER] [--sshaskuser] [--host HOST] [--command COMMAND] [--forcessh] [--bypasshelmet] [--title TITLE] [--sslkey SSL_KEY_PATH] [--sslcert SSL_CERT_PATH]
``` ```
Open your browser on `http://yourserver:3000/wetty` and you will prompted to Open your browser on `http://yourserver:3000/wetty` and you will prompted to
@ -39,11 +41,14 @@ login. Or go to `http://yourserver:3000/wetty/ssh/<username>` to specify the
user before hand. user before hand.
If you run it as root it will launch `/bin/login` (where you can specify the If you run it as root it will launch `/bin/login` (where you can specify the
user name), else it will launch `ssh` and connect by default to `localhost`. user name), else it will launch `ssh` and connect by default to `localhost`. The
SSH connection can be forced using the `--forcessh` option.
If instead you wish to connect to a remote host you can specify the `--sshhost` If instead you wish to connect to a remote host you can specify the `--sshhost`
option, the SSH port using the `--sshport` option and the SSH user using the option, the SSH port using the `--sshport` option and the SSH user using the
`--sshuser` option. `--sshuser` option. Alternatively you can ask the user from the stard input of
the terminal later (and ignoring `--sshuser`) if using the `--sshaskuser`
option.
Check out the Check out the
[Flags docs](https://github.com/butlerx/wetty/blob/master/docs/flags.md) for a [Flags docs](https://github.com/butlerx/wetty/blob/master/docs/flags.md) for a

9
bin/ssh-with-user

@ -0,0 +1,9 @@
#!/bin/bash
set -e
while [ -z "${username}" ]; do
echo -n "localhost login: "
read username
done
ssh -l "${username}" $@

2
docs/API.md

@ -21,6 +21,7 @@ Starts WeTTy Server
| :------------------------ | --------- | ------------- | ---------------------------------------------------------------------------------------------------------------------- | | :------------------------ | --------- | ------------- | ---------------------------------------------------------------------------------------------------------------------- |
| [ssh] | `Object` | | SSH settings | | [ssh] | `Object` | | SSH settings |
| [ssh.user] | `string` | `"''"` | default user for ssh | | [ssh.user] | `string` | `"''"` | default user for ssh |
| [ssh.askuser] | `boolean` | `false` | ask ssh user from the standard input |
| [ssh.host] | `string` | `"localhost"` | machine to ssh too | | [ssh.host] | `string` | `"localhost"` | machine to ssh too |
| [ssh.auth] | `string` | `"password"` | authtype to use | | [ssh.auth] | `string` | `"password"` | authtype to use |
| [ssh.port] | `number` | `22` | port to connect to over ssh | | [ssh.port] | `number` | `22` | port to connect to over ssh |
@ -33,6 +34,7 @@ Starts WeTTy Server
| [serverConf.title] | `string` | `'WeTTy'` | Title of the server | | [serverConf.title] | `string` | `'WeTTy'` | Title of the server |
| [serverConf.bypasshelmet] | `boolean` | `false` | if helmet should be disabled on the sever | | [serverConf.bypasshelmet] | `boolean` | `false` | if helmet should be disabled on the sever |
| [command] | `string` | `"''"` | The command to execute. If running as root and no host specified this will be login if a host is specified will be ssh | | [command] | `string` | `"''"` | The command to execute. If running as root and no host specified this will be login if a host is specified will be ssh |
| [forcessh] | `boolean` | `false` | Connecting through ssh even if running as root |
| [ssl] | `Object` | | SSL settings | | [ssl] | `Object` | | SSL settings |
| [ssl.key] | `string` | | Path to ssl key | | [ssl.key] | `string` | | Path to ssl key |
| [ssl.cert] | `string` | | Path to ssl cert | | [ssl.cert] | `string` | | Path to ssl cert |

12
index.js

@ -41,6 +41,12 @@ if (require.main === module) {
type: 'string', type: 'string',
default: process.env.SSHUSER || '', default: process.env.SSHUSER || '',
}, },
sshaskuser: {
demand: false,
description: 'ask ssh user from the standard input',
type: 'boolean',
default: process.env.SSHASKUSER || false
},
title: { title: {
demand: false, demand: false,
description: 'window title', description: 'window title',
@ -67,6 +73,12 @@ if (require.main === module) {
type: 'string', type: 'string',
default: process.env.SSHKEY || undefined, default: process.env.SSHKEY || undefined,
}, },
forcessh: {
demand: false,
description: 'Connecting through ssh even if running as root',
type: 'boolean',
default: process.env.FORCESSH || false
},
base: { base: {
demand: false, demand: false,
alias: 'b', alias: 'b',

4
src/server/cli/index.ts

@ -6,8 +6,8 @@ import { unWrapArgs } from './parseArgs';
export default function init(opts: CLI): void { export default function init(opts: CLI): void {
if (!opts.help) { if (!opts.help) {
const { ssh, server, command, ssl } = unWrapArgs(opts); const { ssh, server, command, forcessh, ssl } = unWrapArgs(opts);
WeTTy(ssh, server, command, ssl).catch(err => { WeTTy(ssh, server, command, forcessh, ssl).catch(err => {
logger.error(err); logger.error(err);
process.exitCode = 1; process.exitCode = 1;
}); });

2
src/server/cli/options.ts

@ -2,6 +2,7 @@ export interface Options {
sshhost: string; sshhost: string;
sshport: number; sshport: number;
sshuser: string; sshuser: string;
sshaskuser: boolean;
sshauth: string; sshauth: string;
sshkey?: string; sshkey?: string;
sshpass?: string; sshpass?: string;
@ -12,6 +13,7 @@ export interface Options {
port: number; port: number;
title: string; title: string;
command?: string; command?: string;
forcessh?: boolean;
bypasshelmet?: boolean; bypasshelmet?: boolean;
} }

4
src/server/cli/parseArgs.ts

@ -4,10 +4,11 @@ import { Options } from './options';
export function unWrapArgs( export function unWrapArgs(
args: Options args: Options
): { ssh: SSH; server: Server; command?: string; ssl?: SSL } { ): { ssh: SSH; server: Server; command?: string; forcessh?: boolean; ssl?: SSL } {
return { return {
ssh: { ssh: {
user: args.sshuser, user: args.sshuser,
askuser: args.sshaskuser,
host: args.sshhost, host: args.sshhost,
auth: args.sshauth, auth: args.sshauth,
port: args.sshport, port: args.sshport,
@ -22,6 +23,7 @@ export function unWrapArgs(
bypasshelmet: args.bypasshelmet || false, bypasshelmet: args.bypasshelmet || false,
}, },
command: args.command, command: args.command,
forcessh: args.forcessh,
ssl: ssl:
isUndefined(args.sslkey) || isUndefined(args.sslcert) isUndefined(args.sslkey) || isUndefined(args.sslcert)
? undefined ? undefined

10
src/server/command/index.ts

@ -24,14 +24,16 @@ export default (
conn: { remoteAddress }, conn: { remoteAddress },
}, },
}: Socket, }: Socket,
{ user, host, port, auth, pass, key }: SSH, { user, askuser, host, port, auth, pass, key }: SSH,
command: string command: string,
forcessh: boolean
): { args: string[]; user: boolean } => ({ ): { args: string[]; user: boolean } => ({
args: localhost(host) args: !forcessh && localhost(host)
? loginOptions(command, remoteAddress) ? loginOptions(command, remoteAddress)
: sshOptions( : sshOptions(
urlArgs(referer, { urlArgs(referer, {
host: address(referer, user, host), sshcommand: askuser ? './bin/ssh-with-user' : 'ssh',
host: askuser ? host : address(referer, user, host),
port: `${port}`, port: `${port}`,
pass: pass || '', pass: pass || '',
command, command,

9
src/server/command/ssh.ts

@ -3,18 +3,23 @@ import parseCommand from './parse';
import logger from '../utils/logger'; import logger from '../utils/logger';
export default function sshOptions( export default function sshOptions(
{ pass, path, command, host, port, auth }: { [s: string]: string }, { sshcommand, pass, path, command, host, port, auth }: { [s: string]: string },
key?: string key?: string
): string[] { ): string[] {
const cmd = parseCommand(command, path); const cmd = parseCommand(command, path);
logger.info(`ssh command: ${sshcommand}`);
const sshRemoteOptsBase = [ const sshRemoteOptsBase = [
'ssh', sshcommand,
host, host,
'-t', '-t',
'-p', '-p',
port, port,
'-o', '-o',
`PreferredAuthentications=${auth}`, `PreferredAuthentications=${auth}`,
'-o',
'UserKnownHostsFile=/dev/null',
'-o',
'StrictHostKeyChecking=no',
]; ];
logger.info(`Authentication Type: ${auth}`); logger.info(`Authentication Type: ${auth}`);
if (!isUndefined(key)) { if (!isUndefined(key)) {

1
src/server/interfaces.ts

@ -1,5 +1,6 @@
export interface SSH { export interface SSH {
user: string; user: string;
askuser: boolean;
host: string; host: string;
auth: string; auth: string;
port: number; port: number;

5
src/server/wetty/index.ts

@ -13,7 +13,7 @@ import { SSH, SSL, SSLBuffer, Server } from '../interfaces';
* @name startWeTTy * @name startWeTTy
*/ */
export default function startWeTTy( export default function startWeTTy(
ssh: SSH = { user: '', host: 'localhost', auth: 'password', port: 22 }, ssh: SSH = { user: '', askuser: false, host: 'localhost', auth: 'password', port: 22 },
serverConf: Server = { serverConf: Server = {
base: '/wetty/', base: '/wetty/',
port: 3000, port: 3000,
@ -22,6 +22,7 @@ export default function startWeTTy(
bypasshelmet: false, bypasshelmet: false,
}, },
command = '', command = '',
forcessh = false,
ssl?: SSL ssl?: SSL
): Promise<void> { ): Promise<void> {
return loadSSL(ssl).then((sslBuffer: SSLBuffer) => { return loadSSL(ssl).then((sslBuffer: SSLBuffer) => {
@ -44,7 +45,7 @@ export default function startWeTTy(
* @name connection * @name connection
*/ */
logger.info('Connection accepted.'); logger.info('Connection accepted.');
const { args, user: sshUser } = getCommand(socket, ssh, command); const { args, user: sshUser } = getCommand(socket, ssh, command, forcessh);
logger.debug('Command Generated', { logger.debug('Command Generated', {
user: sshUser, user: sshUser,
cmd: args.join(' '), cmd: args.join(' '),

Loading…
Cancel
Save