service_matcher-go/rmi/rmi.go

107 lines
1.9 KiB
Go
Raw Normal View History

2018-08-13 07:48:32 +00:00
package rmi
import (
"bytes"
"encoding/binary"
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 (
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 {
2018-08-15 07:17:18 +00:00
osm.Matchers
2018-08-13 07:48:32 +00:00
}
func (r *RMIMatcher) Key() string {
return "RMI"
}
func (r *RMIMatcher) Name() string {
return "RMI"
}
2018-08-15 07:17:18 +00:00
func (r *RMIMatcher) Meta() osm.Metadata {
2018-08-13 07:48:32 +00:00
return nil
}
func (r *RMIMatcher) IsPrePacket() bool {
return false
}
func (r *RMIMatcher) HasResponse(index int) bool {
return true
}
2018-08-15 07:17:18 +00:00
func (r *RMIMatcher) 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 (r *RMIMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) error {
2018-08-13 07:48:32 +00:00
if packet == nil || packet.Buffer == nil || packet.Len == 0 {
2018-08-15 07:17:18 +00:00
return osm.NoPacketReceivedError()
2018-08-13 07:48:32 +00:00
}
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
}
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 := &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()
2018-08-15 07:17:18 +00:00
m.AddPacket(osm.NewPacket(sendByte, len(sendByte)))
2018-08-13 07:48:32 +00:00
return m
}