service_matcher-go/telnet/telnet.go
2018-08-15 16:17:18 +09:00

78 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 (tel *TelnetMatcher) Key() string {
return "TELNET"
}
func (tel *TelnetMatcher) Name() string {
return "Telnet"
}
func (tel *TelnetMatcher) Meta() osm.Metadata {
return nil
}
func (tel *TelnetMatcher) IsPrePacket() bool {
return true
}
func (tel *TelnetMatcher) HasResponse(index int) bool {
return true
}
func (tel *TelnetMatcher) IsError(info osm.MatchInfo, index int, packet *osm.Packet) bool {
return false
}
func (tel *TelnetMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) error {
if packet == nil || packet.Buffer == nil || packet.Len == 0 {
return osm.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 osm.NotMatchedError()
}
func NewMatcher() osm.Matcher {
m := &TelnetMatcher{}
m.AddPacket(osm.NewPacket([]byte{CMD, DO, 37}, 3))
return m
}