Make DialTimeout configurable.

This commit is contained in:
NI
2019-08-12 23:50:24 +08:00
parent db633f59bd
commit f3336d61a0
17 changed files with 173 additions and 119 deletions

View File

@@ -22,13 +22,11 @@ import (
"io"
"net"
"sync"
"time"
"golang.org/x/crypto/ssh"
"github.com/niruix/sshwifty/application/command"
"github.com/niruix/sshwifty/application/log"
"github.com/niruix/sshwifty/application/network"
"github.com/niruix/sshwifty/application/rw"
)
@@ -116,8 +114,7 @@ func (s sshRemoteConn) isValid() bool {
type sshClient struct {
w command.StreamResponder
l log.Logger
dial network.Dial
dialTimeout time.Duration
cfg command.CommandConfiguration
remoteCloseWait sync.WaitGroup
credentialReceive chan []byte
credentialProcessed bool
@@ -132,13 +129,12 @@ type sshClient struct {
func newSSH(
l log.Logger,
w command.StreamResponder,
dial network.Dial,
cfg command.CommandConfiguration,
) command.FSMMachine {
return &sshClient{
w: w,
l: l,
dial: dial,
dialTimeout: 10 * time.Second,
cfg: cfg,
remoteCloseWait: sync.WaitGroup{},
credentialReceive: make(chan []byte, 1),
credentialProcessed: false,
@@ -300,7 +296,7 @@ func (d *sshClient) comfirmRemoteFingerprint(
func (d *sshClient) dialRemote(
network, addr string, config *ssh.ClientConfig) (*ssh.Client, error) {
conn, err := d.dial(network, addr, config.Timeout)
conn, err := d.cfg.Dial(network, addr, config.Timeout)
if err != nil {
return nil, err
@@ -332,7 +328,7 @@ func (d *sshClient) remote(
HostKeyCallback: func(h string, r net.Addr, k ssh.PublicKey) error {
return d.comfirmRemoteFingerprint(h, r, k, buf[:])
},
Timeout: d.dialTimeout,
Timeout: d.cfg.DialTimeout,
})
if dErr != nil {

View File

@@ -21,11 +21,9 @@ import (
"errors"
"io"
"sync"
"time"
"github.com/niruix/sshwifty/application/command"
"github.com/niruix/sshwifty/application/log"
"github.com/niruix/sshwifty/application/network"
"github.com/niruix/sshwifty/application/rw"
)
@@ -48,28 +46,26 @@ const (
)
type telnetClient struct {
l log.Logger
w command.StreamResponder
dial network.Dial
remoteChan chan io.WriteCloser
remoteConn io.WriteCloser
closeWait sync.WaitGroup
dialTimeout time.Duration
l log.Logger
w command.StreamResponder
cfg command.CommandConfiguration
remoteChan chan io.WriteCloser
remoteConn io.WriteCloser
closeWait sync.WaitGroup
}
func newTelnet(
l log.Logger,
w command.StreamResponder,
dial network.Dial,
cfg command.CommandConfiguration,
) command.FSMMachine {
return &telnetClient{
l: l,
w: w,
dial: dial,
remoteChan: make(chan io.WriteCloser, 1),
remoteConn: nil,
closeWait: sync.WaitGroup{},
dialTimeout: 10 * time.Second,
l: l,
w: w,
cfg: cfg,
remoteChan: make(chan io.WriteCloser, 1),
remoteConn: nil,
closeWait: sync.WaitGroup{},
}
}
@@ -101,7 +97,7 @@ func (d *telnetClient) remote(addr string) {
buf := [4096]byte{}
clientConn, clientConnErr := d.dial("tcp", addr, d.dialTimeout)
clientConn, clientConnErr := d.cfg.Dial("tcp", addr, d.cfg.DialTimeout)
if clientConnErr != nil {
errLen := copy(