Skip to content

Commit

Permalink
Add group support for tunneling.
Browse files Browse the repository at this point in the history
  • Loading branch information
alinex committed Nov 17, 2016
1 parent c64a7cb commit 8818482
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,32 @@ ssh.tunnel 'db', (err, conn) ->
, 10000
```

#### Cluster/Group

Like in the use of connections you may use cluster or group names within the tunneling,
too. This means that the tunnel will be made through the best working host.

``` coffee
ssh = require 'alinex-ssh'
ssh.tunnel
group: 'dmz'
tunnel:
host: '172.30.0.11'
port: 80
retry:
times: 3
intervall: 200
, (err, conn) ->
console.log "ssh connection #{conn.name} opened"
# wait 10 seconds, then close the tunnel
setTimeout ->
tunnel.close()
, 10000
```

And if you use a preconfigured tunnel you may use the group reference name within
the tunnel's remote setting like the server name.

#### Configuration files

To use configuration files you also need to setup and initialize this before using it:
Expand All @@ -323,8 +349,9 @@ ssh.setup (err) ->
See the {@link src/configSchema.coffee} for a detailed information about it's possibilities.
And then put your own settings in external files like described at {@link alinex-config}:

/ssh.yaml - contains named setup of ssh connections
/tunnel.yaml - set the tunnel configuration with name
/ssh/server.yaml - contains named setup of ssh connections
/ssh/group.yaml - cluster/group definition
/ssh/tunnel.yaml - set the tunnel configuration with name

But you may also directly give your setup to the methods above.

Expand Down
35 changes: 29 additions & 6 deletions src/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,36 @@ If the `host` and `port` setting is not given a socks5 proxy tunnel will be open
or the tunnel information on success
###
exports.tunnel = (setup, cb) ->
# resolve string only value
if typeof setup is 'string'
setup =
if config.get "/ssh/tunnel/#{setup}"
tunnel: config.get "/ssh/tunnel/#{setup}"
else
server: config.get "/ssh/server/#{setup}"
setup.server = setup.tunnel.remote if setup.tunnel?.remote
if conf = config.get "/ssh/tunnel/#{setup}"
setup =
tunnel: conf
else if conf = config.get "/ssh/group/#{setup}"
setup =
group: conf
else if conf = config.get "/ssh/server/#{setup}"
setup =
server: conf
else
return cb new Error "Could not find tunnel, group or server in ssh configuration
with name '#{setup}'"
# resolve tunnel setting
if setup.tunnel? and typeof setup.tunnel is 'string'
if conf = config.get "/ssh/tunnel/#{setup}"
setup.tunnel = conf
else
return cb new Error "Could not find tunnel in ssh configuration with name '#{setup}'"
# resolve remote setting in tunnel
if setup.tunnel?.remote
if conf = config.get "/ssh/group/#{setup.tunnel.remote}"
setup.group = conf
else if conf = config.get "/ssh/server/#{setup.tunnel.remote}"
setup.server = conf
else
return cb new Error "Could not find group or server in ssh configuration
with name '#{setup.tunnel.remote}'"
# connect
exports.connect setup, (err, conn) ->
setup.tunnel ?= {}
setup.tunnel.remote ?= setup.server
Expand Down

0 comments on commit 8818482

Please sign in to comment.