package telnet import ( csm "git.loafle.net/commons/service_matcher-go" ) const ( WILL = 251 WONT = 252 DO = 253 DONT = 254 CMD = 255 ) type TelnetMatcher struct { csm.Matchers } func (tel *TelnetMatcher) Key() string { return "TELNET" } func (tel *TelnetMatcher) Name() string { return "Telnet" } func (tel *TelnetMatcher) Meta() csm.Metadata { return nil } func (tel *TelnetMatcher) IsPrePacket() bool { return true } func (tel *TelnetMatcher) HasResponse(index int) bool { return true } func (tel *TelnetMatcher) IsError(info csm.MatchInfo, index int, packet *csm.Packet) bool { return false } func (tel *TelnetMatcher) Match(info csm.MatchInfo, index int, packet *csm.Packet) error { if packet == nil || packet.Buffer == nil || packet.Len == 0 { return csm.NoPacketReceivedError() } 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) { return nil } } return csm.NotMatchedError() } func NewMatcher() csm.Matcher { m := &TelnetMatcher{} m.AddPacket(csm.NewPacket([]byte{CMD, DO, 37}, 3)) return m }