service_matcher-go/telnet/telnet.go

78 lines
1.3 KiB
Go
Raw Normal View History

2018-08-13 07:48:32 +00:00
package telnet
import (
2018-08-15 07:17:18 +00:00
osm "git.loafle.net/overflow/service_matcher-go"
2018-08-13 07:48:32 +00:00
)
const (
WILL = 251
WONT = 252
DO = 253
DONT = 254
CMD = 255
)
type TelnetMatcher struct {
2018-08-15 07:17:18 +00:00
osm.Matchers
2018-08-13 07:48:32 +00:00
}
func (tel *TelnetMatcher) Key() string {
return "TELNET"
}
func (tel *TelnetMatcher) Name() string {
return "Telnet"
}
2018-08-15 07:17:18 +00:00
func (tel *TelnetMatcher) Meta() osm.Metadata {
2018-08-13 07:48:32 +00:00
return nil
}
func (tel *TelnetMatcher) IsPrePacket() bool {
return true
}
func (tel *TelnetMatcher) HasResponse(index int) bool {
return true
}
2018-08-15 07:17:18 +00:00
func (tel *TelnetMatcher) IsError(info osm.MatchInfo, index int, packet *osm.Packet) bool {
2018-08-13 07:48:32 +00:00
return false
}
2018-08-15 07:17:18 +00:00
func (tel *TelnetMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) error {
2018-08-13 07:48:32 +00:00
2018-09-03 06:42:56 +00:00
if packet == nil || !packet.Valid() {
2018-08-15 07:17:18 +00:00
return osm.NoPacketReceivedError()
2018-08-13 07:48:32 +00:00
}
buf := make([]byte, 0, 0)
count := 0
2018-09-03 06:42:56 +00:00
for i := 0; i < packet.Len; i++ {
2018-08-13 07:48:32 +00:00
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
}
}
2018-08-15 07:17:18 +00:00
return osm.NotMatchedError()
2018-08-13 07:48:32 +00:00
}
2018-08-15 07:17:18 +00:00
func NewMatcher() osm.Matcher {
2018-08-13 07:48:32 +00:00
m := &TelnetMatcher{}
2018-08-15 07:17:18 +00:00
m.AddPacket(osm.NewPacket([]byte{CMD, DO, 37}, 3))
2018-08-13 07:48:32 +00:00
return m
}