Skip to content

Commit

Permalink
feat: add 'GatewayConnectTimeout' (fix #289)
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Sep 6, 2018
1 parent c2535da commit 9d97a92
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 22 deletions.
1 change: 0 additions & 1 deletion .gometalinter.json
Expand Up @@ -24,7 +24,6 @@
"ineffassign",
"vet",
"vetshadow",
"gas",
"megacheck"
]
}
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -134,6 +134,7 @@ hosts:

hostd:
Hostname: 13.14.15.16
GatewayConnectTimeout: 2
Gateways:
- direct
- hosta
Expand Down Expand Up @@ -833,6 +834,7 @@ Host *

* Remove invalid warn ([#283](https://github.com/moul/advanced-ssh-config/issues/283))
* Avoid double-connection when having chained gateways ([#285](https://github.com/moul/advanced-ssh-config/pull/285)) by [@4wrxb](https://github.com/4wrxb)
* Add `GatewayConnectTimeout` option ([#289](https://github.com/moul/advanced-ssh-config/issues/289))

[Full commits list](https://github.com/moul/advanced-ssh-config/compare/v2.8.0...master)

Expand Down
10 changes: 10 additions & 0 deletions examples/test-289/assh.yml
@@ -0,0 +1,10 @@
hosts:

hosta:
HostName: 1.2.3.4 # homerouter

hostb:
HostName: 192.168.1.3 # my raspi
User: pi
Gateways: [ direct, hosta ]
GatewayConnectTimeout: 1
18 changes: 14 additions & 4 deletions pkg/commands/proxy.go
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"math"
"net"
"os"
Expand Down Expand Up @@ -219,13 +220,13 @@ func proxy(host *config.Host, conf *config.Config, dryRun bool) error {
if len(host.Gateways) > 0 {
logger.Logger.Debugf("Trying gateways: %s", host.Gateways)
for _, gateway := range host.Gateways {
log.Println(gateway)
if gateway == "direct" {
err = proxyDirect(host, dryRun)
if err != nil {
logger.Logger.Errorf("Failed to use 'direct' connection: %v", err)
} else {
if err == nil {
return nil
}
logger.Logger.Errorf("Failed to use 'direct' connection: %v", err)
} else {
hostCopy := host.Clone()
gatewayHost := conf.GetGatewaySafe(gateway)
Expand Down Expand Up @@ -388,7 +389,16 @@ func proxyGo(host *config.Host, dryRun bool) error {
defer beforeConnectDrivers.Close()

logger.Logger.Debugf("Connecting to %s:%s", host.HostName, host.Port)
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%s", host.HostName, host.Port), time.Duration(host.ConnectTimeout)*time.Second)

// use GatewayConnectTimeout, fallback on ConnectTimeout
timeout := host.GatewayConnectTimeout
if host.ConnectTimeout != 0 {
timeout = host.ConnectTimeout
}
if timeout < 0 { // set to 0 to disable
timeout = 0
}
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%s", host.HostName, host.Port), time.Duration(timeout)*time.Second)
if err != nil {
// OnConnectError hook
connectHookArgs.Error = err
Expand Down
42 changes: 25 additions & 17 deletions pkg/config/host.go
Expand Up @@ -112,25 +112,26 @@ type Host struct {
ProxyCommand string `yaml:"proxycommand,omitempty,flow" json:"ProxyCommand,omitempty"`

// exposed assh fields
noAutomaticRewrite bool
Inherits composeyaml.Stringorslice `yaml:"inherits,omitempty,flow" json:"Inherits,omitempty"`
Gateways composeyaml.Stringorslice `yaml:"gateways,omitempty,flow" json:"Gateways,omitempty"`
ResolveNameservers composeyaml.Stringorslice `yaml:"resolvenameservers,omitempty,flow" json:"ResolveNameservers,omitempty"`
ResolveCommand string `yaml:"resolvecommand,omitempty,flow" json:"ResolveCommand,omitempty"`
ControlMasterMkdir string `yaml:"controlmastermkdir,omitempty,flow" json:"ControlMasterMkdir,omitempty"`
Aliases composeyaml.Stringorslice `yaml:"aliases,omitempty,flow" json:"Aliases,omitempty"`
Hooks *HostHooks `yaml:"hooks,omitempty,flow" json:"Hooks,omitempty"`
Comment composeyaml.Stringorslice `yaml:"comment,omitempty,flow" json:"Comment,omitempty"`
RateLimit string `yaml:"ratelimit,omitempty,flow" json:"RateLimit,omitempty"`
Inherits composeyaml.Stringorslice `yaml:"inherits,omitempty,flow" json:"Inherits,omitempty"`
Gateways composeyaml.Stringorslice `yaml:"gateways,omitempty,flow" json:"Gateways,omitempty"`
ResolveNameservers composeyaml.Stringorslice `yaml:"resolvenameservers,omitempty,flow" json:"ResolveNameservers,omitempty"`
ResolveCommand string `yaml:"resolvecommand,omitempty,flow" json:"ResolveCommand,omitempty"`
ControlMasterMkdir string `yaml:"controlmastermkdir,omitempty,flow" json:"ControlMasterMkdir,omitempty"`
Aliases composeyaml.Stringorslice `yaml:"aliases,omitempty,flow" json:"Aliases,omitempty"`
Hooks *HostHooks `yaml:"hooks,omitempty,flow" json:"Hooks,omitempty"`
Comment composeyaml.Stringorslice `yaml:"comment,omitempty,flow" json:"Comment,omitempty"`
RateLimit string `yaml:"ratelimit,omitempty,flow" json:"RateLimit,omitempty"`
GatewayConnectTimeout int `yaml:"gatewayconnecttimeout,omitempty,flow" json:"GatewayConnectTimeout,omitempty"`

// private assh fields
knownHosts []string
pattern string
name string
inputName string
isDefault bool
isTemplate bool
inherited map[string]bool
noAutomaticRewrite bool
knownHosts []string
pattern string
name string
inputName string
isDefault bool
isTemplate bool
inherited map[string]bool
}

// NewHost returns a host with name
Expand Down Expand Up @@ -1054,6 +1055,10 @@ func (h *Host) ApplyDefaults(defaults *Host) {
h.RateLimit = defaults.RateLimit
}

if h.GatewayConnectTimeout == 0 {
h.GatewayConnectTimeout = defaults.GatewayConnectTimeout
}

if h.Hooks == nil {
h.Hooks = defaults.Hooks
if h.Hooks == nil {
Expand Down Expand Up @@ -1405,6 +1410,9 @@ func (h *Host) WriteSSHConfigTo(w io.Writer) error {
if len(h.Comment) > 0 {
fmt.Fprint(w, sliceComment("Comment", h.Comment))
}
if h.GatewayConnectTimeout > 0 {
fmt.Fprint(w, stringComment("GatewayConnectTimeout", fmt.Sprintf("%d", h.GatewayConnectTimeout)))
}
if len(h.Aliases) > 0 {
if aliasIdx == 0 {
fmt.Fprint(w, sliceComment("Aliases", h.Aliases))
Expand Down

0 comments on commit 9d97a92

Please sign in to comment.