service_matcher-go/elasticsearch/elasticsearch.go

135 lines
2.7 KiB
Go
Raw Normal View History

2018-08-13 07:48:32 +00:00
package elasticsearch
import (
"bufio"
"encoding/json"
"strconv"
"strings"
2018-08-15 07:17:18 +00:00
osm "git.loafle.net/overflow/service_matcher-go"
2018-08-13 07:48:32 +00:00
)
type ElasticSearchMatcher struct {
2018-08-15 07:17:18 +00:00
osm.Matchers
meta osm.Metadata
2018-08-13 07:48:32 +00:00
}
func (es *ElasticSearchMatcher) Key() string {
2018-08-15 06:41:41 +00:00
return "ELASTICSEARCH"
2018-08-13 07:48:32 +00:00
}
func (es *ElasticSearchMatcher) Name() string {
name := "ElasticSearch"
if v, ok := es.meta["number"]; ok {
name = name + " (" + v + ")"
}
return name
}
2018-08-15 07:17:18 +00:00
func (es *ElasticSearchMatcher) Meta() osm.Metadata {
2018-08-13 07:48:32 +00:00
return es.meta
}
func (es *ElasticSearchMatcher) IsPrePacket() bool {
return false
}
func (es *ElasticSearchMatcher) HasResponse(index int) bool {
return true
}
2018-08-15 07:17:18 +00:00
func (es *ElasticSearchMatcher) 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 (es *ElasticSearchMatcher) 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
}
2018-09-03 06:42:56 +00:00
str := string(packet.Bytes())
2018-08-13 07:48:32 +00:00
hnb := strings.Split(str, "\r\n\r\n")
header := hnb[0]
body := hnb[1]
lineNo := 0
scanner := bufio.NewScanner(strings.NewReader(header))
contentLen := 0
for scanner.Scan() {
line := scanner.Text()
if strings.Compare(line, "") == 0 {
continue
}
if lineNo == 0 && !strings.HasPrefix(line, "HTTP/") {
2018-08-15 07:17:18 +00:00
return osm.NotMatchedError()
2018-08-13 07:48:32 +00:00
}
if strings.Contains(line, ":") {
kv := strings.Split(line, ": ")
if kv[0] == "content-type" && !strings.Contains(kv[1], "application/json") {
2018-08-15 07:17:18 +00:00
return osm.NotMatchedError()
2018-08-13 07:48:32 +00:00
}
if kv[0] == "content-length" {
len, err := strconv.Atoi(kv[1])
if err != nil {
2018-08-15 07:17:18 +00:00
return osm.NotMatchedError()
2018-08-13 07:48:32 +00:00
}
contentLen = len
}
}
lineNo++
}
content := body[:contentLen]
if strings.HasPrefix(content, "{") && strings.HasSuffix(content, "}") {
2018-08-15 07:17:18 +00:00
return osm.NotMatchedError()
2018-08-13 07:48:32 +00:00
}
es.parseJson(content)
if _, ok := es.meta["cluster_name"]; !ok {
2018-08-15 07:17:18 +00:00
return osm.NotMatchedError()
2018-08-13 07:48:32 +00:00
}
if _, ok := es.meta["cluster_uuid"]; !ok {
2018-08-15 07:17:18 +00:00
return osm.NotMatchedError()
2018-08-13 07:48:32 +00:00
}
return nil
}
func (es *ElasticSearchMatcher) parseJson(jsonstr string) error {
jsonMap := make(map[string]interface{})
err := json.Unmarshal([]byte(jsonstr), &jsonMap)
if err != nil {
return err
}
es.dumpMap(jsonMap)
return nil
}
func (es *ElasticSearchMatcher) dumpMap(m map[string]interface{}) {
for k, v := range m {
if mv, ok := v.(map[string]interface{}); ok {
es.dumpMap(mv)
} else {
s, ok := v.(string)
if ok {
es.meta[k] = s
}
}
}
}
2018-08-15 07:17:18 +00:00
func NewMatcher() osm.Matcher {
2018-08-13 07:48:32 +00:00
m := &ElasticSearchMatcher{}
2018-08-15 07:17:18 +00:00
m.meta = osm.NewMetadata()
2018-08-13 07:48:32 +00:00
reqStr := "GET / HTTP/1.1\r\n\r\n"
byte := make([]byte, len(reqStr))
copy(byte[:], reqStr)
2018-08-15 07:17:18 +00:00
m.AddPacket(osm.NewPacket(byte, len(reqStr)))
2018-08-13 07:48:32 +00:00
return m
}