Skip to content

Commit 045aee0

Browse files
authored
Merge pull request #159 from cookielab/cookielab-fixes
2 parents a409de2 + f6864f2 commit 045aee0

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

tunnel/tunnel.go

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ func NewServer(user, address, key, sshAgent, cfgPath string) (*Server, error) {
4444
var host string
4545
var hostname string
4646
var port string
47+
var c *SSHConfigFile
48+
var err error
4749

4850
host = address
4951
if strings.Contains(host, ":") {
@@ -52,16 +54,17 @@ func NewServer(user, address, key, sshAgent, cfgPath string) (*Server, error) {
5254
port = args[1]
5355
}
5456

55-
c, err := NewSSHConfigFile(cfgPath)
56-
if err != nil {
57-
if !errors.Is(err, os.ErrNotExist) {
58-
return nil, fmt.Errorf("error accessing %s: %v", host, err)
59-
}
60-
}
61-
62-
// If ssh config file doesnt exists, create an empty ssh config struct to avoid nil pointer deference
63-
if errors.Is(err, os.ErrNotExist) {
57+
if cfgPath == "" {
6458
c = NewEmptySSHConfigStruct()
59+
} else {
60+
c, err = NewSSHConfigFile(cfgPath)
61+
if err != nil {
62+
if !errors.Is(err, os.ErrNotExist) {
63+
return nil, fmt.Errorf("error accessing %s: %v", host, err)
64+
} else {
65+
c = NewEmptySSHConfigStruct()
66+
}
67+
}
6568
}
6669

6770
h := c.Get(host)
@@ -471,11 +474,16 @@ func (t *Tunnel) Channels() []*SSHChannel {
471474
func sshClientConfig(server Server) (*ssh.ClientConfig, error) {
472475
var signers []ssh.Signer
473476

474-
signer, err := server.Key.Parse()
475-
if err != nil {
476-
return nil, err
477+
if server.Key == nil && server.SSHAgent == "" {
478+
return nil, fmt.Errorf("at least one authentication method (key or ssh agent) must be present.")
479+
}
480+
481+
if server.Key != nil {
482+
signer, err := server.Key.Parse()
483+
if err == nil {
484+
signers = append(signers, signer)
485+
}
477486
}
478-
signers = append(signers, signer)
479487

480488
if server.SSHAgent != "" {
481489
if _, err := os.Stat(server.SSHAgent); err == nil {
@@ -489,6 +497,10 @@ func sshClientConfig(server Server) (*ssh.ClientConfig, error) {
489497
}
490498
}
491499

500+
if len(signers) == 0 {
501+
return nil, fmt.Errorf("at least one working authentication method (key or ssh agent) must be present.")
502+
}
503+
492504
clb, err := knownHostsCallback(server.Insecure)
493505
if err != nil {
494506
return nil, err
@@ -506,6 +518,8 @@ func sshClientConfig(server Server) (*ssh.ClientConfig, error) {
506518

507519
func copyConn(writer, reader net.Conn) {
508520
_, err := io.Copy(writer, reader)
521+
defer writer.Close()
522+
defer reader.Close()
509523
if err != nil {
510524
log.Errorf("%v", err)
511525
}

0 commit comments

Comments
 (0)