service_matcher-go/telnet/telnet.go

93 lines
1.7 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
}
2018-09-03 13:36:57 +00:00
func (m *TelnetMatcher) Key() string {
2018-08-13 07:48:32 +00:00
return "TELNET"
}
2018-09-12 04:26:27 +00:00
func (m *TelnetMatcher) Type() string {
return "NETWORK"
}
func (m *TelnetMatcher) Vendor(matchCtx *osm.MatchCtx) string {
return "UNKNOWN"
}
func (m *TelnetMatcher) Version(matchCtx *osm.MatchCtx) string {
return "UNKNOWN"
}
func (m *TelnetMatcher) OsType(matchCtx *osm.MatchCtx) string {
return "UNKNOWN"
}
func (m *TelnetMatcher) OsVersion(matchCtx *osm.MatchCtx) string {
return "UNKNOWN"
}
2018-09-12 04:26:27 +00:00
2018-09-03 13:36:57 +00:00
func (m *TelnetMatcher) Name(matchCtx *osm.MatchCtx) string {
2018-08-13 07:48:32 +00:00
return "Telnet"
}
2018-09-03 13:41:28 +00:00
func (m *TelnetMatcher) IsPrePacket() bool {
2018-08-13 07:48:32 +00:00
return true
}
2018-09-03 13:36:57 +00:00
func (m *TelnetMatcher) HasResponse(matchCtx *osm.MatchCtx, index int) bool {
2018-08-13 07:48:32 +00:00
return true
}
2018-09-03 13:36:57 +00:00
func (m *TelnetMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool {
2018-08-13 07:48:32 +00:00
return false
}
2018-09-03 13:36:57 +00:00
func (m *TelnetMatcher) Match(matchCtx *osm.MatchCtx, 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
}