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 { return "TELNET" } func (tel *TelnetMatcher) String() string { 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 }