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

@@ -27,6 +27,12 @@ import (
"github.com/niruix/sshwifty/application/rw"
)
// CommandConfiguration contains configuration data needed to run command
type CommandConfiguration struct {
Dial network.Dial
DialTimeout time.Duration
}
// Commander command control
type Commander struct {
commands Commands
@@ -41,7 +47,7 @@ func New(cs Commands) Commander {
// New Adds a new client
func (c Commander) New(
dialer network.Dial,
cfg CommandConfiguration,
receiver rw.FetchReader,
sender io.Writer,
senderLock *sync.Mutex,
@@ -50,7 +56,7 @@ func (c Commander) New(
l log.Logger,
) (Handler, error) {
return newHandler(
dialer,
cfg,
&c.commands,
receiver,
sender,

View File

@@ -22,7 +22,6 @@ import (
"fmt"
"github.com/niruix/sshwifty/application/log"
"github.com/niruix/sshwifty/application/network"
)
// Consts
@@ -37,7 +36,11 @@ var (
)
// Command represents a command handler machine builder
type Command func(l log.Logger, w StreamResponder, d network.Dial) FSMMachine
type Command func(
l log.Logger,
w StreamResponder,
cfg CommandConfiguration,
) FSMMachine
// Commands contains data of all commands
type Commands [MaxCommandID + 1]Command
@@ -57,7 +60,10 @@ func (c *Commands) Register(id byte, cb Command) {
// Run creates command executer
func (c Commands) Run(
id byte, l log.Logger, w StreamResponder, dial network.Dial) (FSM, error) {
id byte,
l log.Logger,
w StreamResponder,
cfg CommandConfiguration) (FSM, error) {
if id > MaxCommandID {
return FSM{}, ErrCommandRunUndefinedCommand
}
@@ -68,5 +74,5 @@ func (c Commands) Run(
return FSM{}, ErrCommandRunUndefinedCommand
}
return newFSM(cc(l, w, dial)), nil
return newFSM(cc(l, w, cfg)), nil
}

View File

@@ -25,7 +25,6 @@ import (
"time"
"github.com/niruix/sshwifty/application/log"
"github.com/niruix/sshwifty/application/network"
"github.com/niruix/sshwifty/application/rw"
)
@@ -105,7 +104,7 @@ func (h streamHandlerSender) Write(b []byte) (int, error) {
// Handler client stream control
type Handler struct {
dialer network.Dial
cfg CommandConfiguration
commands *Commands
receiver rw.FetchReader
sender handlerSender
@@ -118,7 +117,7 @@ type Handler struct {
}
func newHandler(
dialer network.Dial,
cfg CommandConfiguration,
commands *Commands,
receiver rw.FetchReader,
sender io.Writer,
@@ -128,7 +127,7 @@ func newHandler(
l log.Logger,
) Handler {
return Handler{
dialer: dialer,
cfg: cfg,
commands: commands,
receiver: receiver,
sender: handlerSender{writer: sender, lock: senderLock},
@@ -235,7 +234,7 @@ func (e *Handler) handleStream(h Header, d byte, l log.Logger) error {
return st.reinit(h, &e.receiver, streamHandlerSender{
handlerSender: &e.sender,
sendDelay: e.sendDelay,
}, l, e.commands, e.dialer, e.rBuf[:])
}, l, e.commands, e.cfg, e.rBuf[:])
}
func (e *Handler) handleClose(h Header, d byte, l log.Logger) error {

View File

@@ -78,7 +78,7 @@ func TestHandlerHandleEcho(t *testing.T) {
}
lock := sync.Mutex{}
handler := newHandler(
nil,
CommandConfiguration{},
nil,
rw.NewFetchReader(testDummyFetchGen(s)),
&w,

View File

@@ -25,7 +25,6 @@ import (
"testing"
"github.com/niruix/sshwifty/application/log"
"github.com/niruix/sshwifty/application/network"
"github.com/niruix/sshwifty/application/rw"
)
@@ -58,7 +57,7 @@ func testDummyFetchChainGen(dd <-chan []byte) rw.FetchReaderFetcher {
}
type dummyStreamCommand struct {
lock sync.Mutex
lock sync.Mutex
l log.Logger
w StreamResponder
downWait sync.WaitGroup
@@ -67,9 +66,12 @@ type dummyStreamCommand struct {
}
func newDummyStreamCommand(
l log.Logger, w StreamResponder, d network.Dial) FSMMachine {
l log.Logger,
w StreamResponder,
cfg CommandConfiguration,
) FSMMachine {
return &dummyStreamCommand{
lock:sync.Mutex{},
lock: sync.Mutex{},
l: l,
w: w,
downWait: sync.WaitGroup{},
@@ -84,7 +86,7 @@ func (d *dummyStreamCommand) Bootup(
) (FSMState, FSMError) {
d.downWait.Add(1)
echoTrans:=d.echoTrans
echoTrans := d.echoTrans
go func() {
defer func() {
@@ -178,7 +180,7 @@ func TestHandlerHandleStream(t *testing.T) {
lock := sync.Mutex{}
hhd := newHandler(
nil,
CommandConfiguration{},
&cmds,
rw.NewFetchReader(readerSource),
wBuffer,

View File

@@ -22,7 +22,6 @@ import (
"io"
"github.com/niruix/sshwifty/application/log"
"github.com/niruix/sshwifty/application/network"
"github.com/niruix/sshwifty/application/rw"
)
@@ -342,7 +341,7 @@ func (c *stream) reinit(
w streamHandlerSender,
l log.Logger,
cc *Commands,
dialer network.Dial,
cfg CommandConfiguration,
b []byte,
) error {
hd := streamInitialHeader{}
@@ -355,7 +354,8 @@ func (c *stream) reinit(
l = l.Context("Command (%d)", hd.command())
ccc, cccErr := cc.Run(hd.command(), l, newStreamResponder(w, h), dialer)
ccc, cccErr := cc.Run(
hd.command(), l, newStreamResponder(w, h), cfg)
if cccErr != nil {
hd.set(0, uint16(StreamErrorCommandUndefined), false)