network_service_matcher/telnet/telnet.go

75 lines
1.3 KiB
Go
Raw Normal View History

2017-12-04 07:27:08 +00:00
package telnet
import (
cnsm "git.loafle.net/commons_go/network_service_matcher"
)
const (
DO = 0xfd
WONT = 0x4b
WILL = 0xfb
DONT = 0xfe
CMD = 0xff
)
type TelnetMatcher struct {
cnsm.Matchers
}
func (tel *TelnetMatcher) ServiceName() string {
2018-03-29 13:55:08 +00:00
return "TELNET"
}
func (tel *TelnetMatcher) String() string {
2017-12-04 07:27:08 +00:00
return "Telnet"
}
func (tel *TelnetMatcher) IsPrePacket() bool {
return true
}
func (tel *TelnetMatcher) HasResponse(index int) bool {
return true
}
func (tel *TelnetMatcher) IsError(info cnsm.MatchInfo, index int, packet *cnsm.Packet) bool {
return false
}
func (tel *TelnetMatcher) Match(info cnsm.MatchInfo, index int, packet *cnsm.Packet) bool {
result := false
if packet == nil || packet.Buffer == nil || packet.Len == 0 {
return result
}
buf := make([]byte, 0, 0)
count := 0
for i := 0; i < len(packet.Buffer); i++ {
if packet.Buffer[i] > 0 {
buf = append(buf, packet.Buffer[i])
} else if count > 2 {
break
} else {
count++
}
}
for idx := 0; idx < len(buf); idx += 3 {
if buf[idx] == CMD && (buf[idx+1] == DO || buf[idx+1] == WONT || buf[idx+1] == WILL || buf[idx+1] == DONT) {
result = true
} else {
result = false
}
}
return result
}
func NewMatcher() cnsm.Matcher {
m := &TelnetMatcher{}
return m
}