diff --git a/constants.go b/constants.go index ff976ad..5b84c9d 100644 --- a/constants.go +++ b/constants.go @@ -106,6 +106,8 @@ const ( TELOPT_TTYLOC = 28 // TELOPT_NAWS - Negotiate About Window Size TELOPT_NAWS = 31 + // TELOPT_FLOWCTRL - remote flow control + TELOPT_FLOWCTRL = 33 // TELOPT_SB_SEND SEND subneg TELOPT_SB_SEND = 1 diff --git a/negotiation.go b/negotiation.go index 8e39b96..e13341d 100644 --- a/negotiation.go +++ b/negotiation.go @@ -1,11 +1,13 @@ package tclient +// ignore: TELNET_AYT, TELNET_AO +// accept: 0x01, 0x03, 0x18, 0x1f == TELOPT_ECHO, TELOPT_SGA, TELOPT_TTYPE, TELOPT_NAWS func (c *TelnetClient) negotiate(sequence []byte) error { if len(sequence) < 3 { // do nothing return nil } - + //fmt.Println(sequence) var err error // DO sequence @@ -29,6 +31,16 @@ func (c *TelnetClient) negotiate(sequence []byte) error { } } + // WONT -> DONT + if sequence[1] == TELNET_WONT && len(sequence) == 3 { + err = c.WriteRaw([]byte{TELNET_IAC, TELNET_DONT, sequence[2]}) + } + + // WILL -> DO + if sequence[1] == TELNET_WILL && len(sequence) == 3 { + err = c.WriteRaw([]byte{TELNET_IAC, TELNET_DO, sequence[2]}) + } + // subseq SEND request if len(sequence) == 6 && sequence[1] == TELNET_SB && sequence[3] == TELOPT_SB_SEND { // what to send? @@ -42,6 +54,8 @@ func (c *TelnetClient) negotiate(sequence []byte) error { err = c.WriteRaw([]byte{TELNET_IAC, TELNET_SB, TELOPT_SB_NEV_ENVIRON, TELOPT_SB_IS, TELNET_IAC, TELNET_SE}) break default: + // accept all + err = c.WriteRaw([]byte{TELNET_IAC, TELNET_SB, sequence[2], 0, TELNET_IAC, TELNET_SE}) break } } diff --git a/reader.go b/reader.go index a073190..1644a02 100644 --- a/reader.go +++ b/reader.go @@ -108,6 +108,25 @@ func (c *TelnetClient) ReadUntil(waitfor string) (string, error) { } } + // not IAC sequence, but IAC char =\ + if b == TELNET_IAC { + continue + } + + // remove \r ; remove backspaces + if b == 8 { + if lastLine.Len() > 0 { + lastLine.Truncate(lastLine.Len() - 1) + } + continue + } + if b == '\r' { + continue + } + + //fmt.Printf("%s | %d\n", string(b), b) + //fmt.Printf("%s", string(b)) + // this is not escape sequence, so write this byte to buffer // update: strip '\r' /*if b != '\r' { @@ -130,16 +149,6 @@ func (c *TelnetClient) ReadUntil(waitfor string) (string, error) { } } - // remove \r ; remove backspaces - if b == 8 { - if lastLine.Len() > 0 { - lastLine.Truncate(lastLine.Len() - 1) - } - continue - } - if b == '\r' { - continue - } // check for CRLF. // We need last line to compare with prompt. //if b == '\n' && prev == '\r' { diff --git a/telnet.go b/telnet.go index 9d9873a..1e2f421 100644 --- a/telnet.go +++ b/telnet.go @@ -48,7 +48,7 @@ func New(tout int, login string, password string, prompt string) *TelnetClient { login: login, password: password, prompt: `(?msi:[\$%#>]$)`, - loginPrompt: `[Uu]ser(\s)?[Nn]ame\:$`, + loginPrompt: `[Uu]ser(\s)?[Nn]ame\:(\s+)?$`, passwordPrompt: `[Pp]ass[Ww]ord\:$`, closed: false, Options: make([]int,0),