service_matcher-go/rmi/rmi.go
2018-09-03 22:36:57 +09:00

103 lines
1.9 KiB
Go

package rmi
import (
"bytes"
"encoding/binary"
osm "git.loafle.net/overflow/service_matcher-go"
)
const (
MAGIC_NUMBER = 0x4a524d49
STREAM_PROTOCOL = 0x4b
VERSION = 0x0002
ACK_PROTOCOL = 0x4e
)
type RMI_SEND_MESSAGE struct {
magic uint32
version uint16
protocol uint8
}
type RMI_RECV_MESSAGE struct {
streamMessage uint8
packetLen uint16
host []byte
port [2]byte
}
type RMIMatcher struct {
osm.Matchers
}
func (m *RMIMatcher) Key() string {
return "RMI"
}
func (m *RMIMatcher) Name(matchCtx *osm.MatchCtx) string {
return "RMI"
}
func (m *RMIMatcher) IsPrePacket(matchCtx *osm.MatchCtx) bool {
return false
}
func (m *RMIMatcher) HasResponse(matchCtx *osm.MatchCtx, index int) bool {
return true
}
func (m *RMIMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool {
return false
}
func (m *RMIMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error {
if packet == nil || !packet.Valid() {
return osm.NoPacketReceivedError()
}
rmiRecv := RMI_RECV_MESSAGE{}
buf := bytes.NewReader(packet.Buffer)
binary.Read(buf, binary.BigEndian, &rmiRecv.streamMessage)
binary.Read(buf, binary.BigEndian, &rmiRecv.packetLen)
lenInt := int(rmiRecv.packetLen)
var tempHost = make([]byte, lenInt, lenInt)
copy(rmiRecv.host, tempHost)
rmiRecv.host = tempHost
binary.Read(buf, binary.BigEndian, &rmiRecv.host)
binary.Read(buf, binary.BigEndian, &rmiRecv.port)
hostIp := string(rmiRecv.host[:lenInt])
if rmiRecv.streamMessage == ACK_PROTOCOL && lenInt == len(hostIp) {
return nil
}
return osm.NotMatchedError()
}
func NewMatcher() osm.Matcher {
m := &RMIMatcher{}
rsm := RMI_SEND_MESSAGE{
magic: MAGIC_NUMBER,
version: VERSION,
protocol: STREAM_PROTOCOL,
}
mCache := new(bytes.Buffer)
binary.Write(mCache, binary.BigEndian, rsm)
sendByte := mCache.Bytes()
m.AddPacket(osm.NewPacket(sendByte, len(sendByte)))
return m
}