service_matcher-go/telnet/telnet.go
crusader d5bc04d5ee ing
2018-09-03 22:41:28 +09:00

74 lines
1.3 KiB
Go

package telnet
import (
osm "git.loafle.net/overflow/service_matcher-go"
)
const (
WILL = 251
WONT = 252
DO = 253
DONT = 254
CMD = 255
)
type TelnetMatcher struct {
osm.Matchers
}
func (m *TelnetMatcher) Key() string {
return "TELNET"
}
func (m *TelnetMatcher) Name(matchCtx *osm.MatchCtx) string {
return "Telnet"
}
func (m *TelnetMatcher) IsPrePacket() bool {
return true
}
func (m *TelnetMatcher) HasResponse(matchCtx *osm.MatchCtx, index int) bool {
return true
}
func (m *TelnetMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool {
return false
}
func (m *TelnetMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error {
if packet == nil || !packet.Valid() {
return osm.NoPacketReceivedError()
}
buf := make([]byte, 0, 0)
count := 0
for i := 0; i < packet.Len; 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 osm.NotMatchedError()
}
func NewMatcher() osm.Matcher {
m := &TelnetMatcher{}
m.AddPacket(osm.NewPacket([]byte{CMD, DO, 37}, 3))
return m
}