Skip to content

Commit

Permalink
refactor: smarter channel sleeps, better byte handling, update crypto
Browse files Browse the repository at this point in the history
  • Loading branch information
carlmontanari committed Aug 19, 2023
1 parent 3689f12 commit 74f5fd2
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 40 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/commit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
- "1.17"
- "1.18"
- "1.19"
- "1.20"
steps:
- name: checkout
uses: actions/checkout@v3
Expand All @@ -31,7 +32,7 @@ jobs:
- name: lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.52
version: v1.54
args: --timeout 5m
- name: install gotestsum
run: go install gotest.tools/gotestsum@latest
Expand All @@ -45,7 +46,7 @@ jobs:
os:
- ubuntu-latest
go-version:
- "1.19"
- "1.20"
runtime:
- "docker"
needs:
Expand Down
4 changes: 2 additions & 2 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ issues:
text: "package-comments"

run:
go: '1.19'
go: '1.20'
skip-dirs:
- private

output:
uniq-by-line: false

service:
golangci-lint-version: 1.52.x
golangci-lint-version: 1.54.x
7 changes: 4 additions & 3 deletions channel/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ const (
// DefaultTimeoutOpsSeconds is the default time value for operations -- 60 seconds.
DefaultTimeoutOpsSeconds = 60
// DefaultReadDelayMicroSeconds is the default value for the delay between reads of the
// transport -- 100 microseconds. Going very low is likely to lead to very high cpu and not
// transport -- 250 microseconds. Going very low is likely to lead to very high cpu and not
// yield any recognizable gains, so be careful changing this!
DefaultReadDelayMicroSeconds = 250
// DefaultReturnChar is the character used to send an "enter" key to the device, "\n".
DefaultReturnChar = "\n"
// DefaultPromptSearchDepth -- is the default depth to search for the prompt in the received
// bytes.
DefaultPromptSearchDepth = 1_000
redacted = "redacted"
readDelayDivisor = 1_000

redacted = "redacted"
readDelayDivisor = 1_000
)

var (
Expand Down
34 changes: 11 additions & 23 deletions channel/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ func (c *Channel) read() {
// afraid to remove it!
b = bytes.ReplaceAll(b, []byte("\r"), []byte(""))

// trim out all the space we padded in the buffer to read into
b = bytes.ReplaceAll(b, []byte("\x00"), []byte(""))

if bytes.Contains(b, []byte("\x1b")) {
b = util.StripANSI(b)
}
Expand All @@ -61,6 +58,7 @@ func (c *Channel) Read() ([]byte, error) {
case err := <-c.Errs:
return nil, err
default:
time.Sleep(c.ReadDelay)
}

b := c.Q.Dequeue()
Expand Down Expand Up @@ -117,15 +115,11 @@ func (c *Channel) ReadUntilPrompt() ([]byte, error) {
var rb []byte

for {
select {
case err := <-c.Errs:
nb, err := c.Read()
if err != nil {
return nil, err
default:
time.Sleep(c.ReadDelay)
}

nb := c.Q.Dequeue()

if nb == nil {
continue
}
Expand All @@ -146,23 +140,21 @@ func (c *Channel) ReadUntilAnyPrompt(prompts []*regexp.Regexp) ([]byte, error) {
var rb []byte

for {
select {
case err := <-c.Errs:
nb, err := c.Read()
if err != nil {
return nil, err
default:
time.Sleep(c.ReadDelay)
}

nb := c.Q.Dequeue()

if nb == nil {
continue
}

rb = append(rb, nb...)

prb := c.processReadBuf(rb)

for _, p := range prompts {
if p.Match(rb) {
if p.Match(prb) {
c.l.Debugf("channel read %#v", string(rb))

return rb, nil
Expand All @@ -177,22 +169,18 @@ func (c *Channel) ReadUntilExplicit(b []byte) ([]byte, error) {
var rb []byte

for {
select {
case err := <-c.Errs:
nb, err := c.Read()
if err != nil {
return nil, err
default:
time.Sleep(c.ReadDelay)
}

nb := c.Q.Dequeue()

if nb == nil {
continue
}

rb = append(rb, nb...)

if bytes.Contains(rb, b) {
if bytes.Contains(c.processReadBuf(rb), b) {
c.l.Debugf("channel read %#v", string(rb))

return rb, nil
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
github.com/creack/pty v1.1.18
github.com/google/go-cmp v0.5.9
github.com/sirikothe/gotextfsm v1.0.1-0.20200816110946-6aa2cfd355e4
golang.org/x/crypto v0.6.0
golang.org/x/term v0.8.0 // indirect
golang.org/x/crypto v0.12.0
gopkg.in/yaml.v3 v3.0.1
)
16 changes: 12 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,43 @@ github.com/sirikothe/gotextfsm v1.0.1-0.20200816110946-6aa2cfd355e4/go.mod h1:CJ
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.6.0 h1:qfktjS5LUO+fFKeJXZ+ikTRijMmljikvG68fpMMruSc=
golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58=
golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0 h1:n5xxQn2i3PC0yLAbjTpNT85q/Kgzcr2gIoX9OrJUols=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.11.0 h1:F9tnn/DA/Im8nCwm+fX+1/eBwi4qFjRT++MhtVC4ZX0=
golang.org/x/term v0.11.0/go.mod h1:zC9APTIj3jG3FdV/Ons+XE1riIZXG4aZ4GTHiPZJPIU=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
7 changes: 5 additions & 2 deletions transport/standard.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,12 @@ func (t *Standard) IsAlive() bool {
func (t *Standard) Read(n int) ([]byte, error) {
b := make([]byte, n)

_, err := t.reader.Read(b)
n, err := t.reader.Read(b)
if err != nil {
return nil, err
}

return b, err
return b[0:n], nil
}

// Write writes bytes b to the transport.
Expand Down
7 changes: 5 additions & 2 deletions transport/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,12 @@ func (t *System) IsAlive() bool {
func (t *System) Read(n int) ([]byte, error) {
b := make([]byte, n)

_, err := t.fd.Read(b)
n, err := t.fd.Read(b)
if err != nil {
return nil, err
}

return b, err
return b[0:n], nil
}

// Write writes bytes b to the transport.
Expand Down

0 comments on commit 74f5fd2

Please sign in to comment.