init
This commit is contained in:
134
elasticsearch/elasticsearch.go
Normal file
134
elasticsearch/elasticsearch.go
Normal file
@@ -0,0 +1,134 @@
|
||||
package elasticsearch
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
csm "git.loafle.net/commons/service_matcher-go"
|
||||
)
|
||||
|
||||
type ElasticSearchMatcher struct {
|
||||
csm.Matchers
|
||||
meta csm.Metadata
|
||||
}
|
||||
|
||||
func (es *ElasticSearchMatcher) Key() string {
|
||||
return "ElasticSearch"
|
||||
}
|
||||
|
||||
func (es *ElasticSearchMatcher) Name() string {
|
||||
name := "ElasticSearch"
|
||||
if v, ok := es.meta["number"]; ok {
|
||||
name = name + " (" + v + ")"
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
func (es *ElasticSearchMatcher) Meta() csm.Metadata {
|
||||
return es.meta
|
||||
}
|
||||
|
||||
func (es *ElasticSearchMatcher) IsPrePacket() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (es *ElasticSearchMatcher) HasResponse(index int) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (es *ElasticSearchMatcher) IsError(info csm.MatchInfo, index int, packet *csm.Packet) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (es *ElasticSearchMatcher) Match(info csm.MatchInfo, index int, packet *csm.Packet) error {
|
||||
|
||||
if packet == nil || packet.Buffer == nil || packet.Len == 0 {
|
||||
return csm.NoPacketReceivedError()
|
||||
}
|
||||
|
||||
str := string(packet.Buffer)
|
||||
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/") {
|
||||
return csm.NotMatchedError()
|
||||
}
|
||||
|
||||
if strings.Contains(line, ":") {
|
||||
kv := strings.Split(line, ": ")
|
||||
if kv[0] == "content-type" && !strings.Contains(kv[1], "application/json") {
|
||||
return csm.NotMatchedError()
|
||||
}
|
||||
if kv[0] == "content-length" {
|
||||
len, err := strconv.Atoi(kv[1])
|
||||
if err != nil {
|
||||
return csm.NotMatchedError()
|
||||
}
|
||||
contentLen = len
|
||||
}
|
||||
}
|
||||
lineNo++
|
||||
}
|
||||
content := body[:contentLen]
|
||||
if strings.HasPrefix(content, "{") && strings.HasSuffix(content, "}") {
|
||||
return csm.NotMatchedError()
|
||||
}
|
||||
es.parseJson(content)
|
||||
if _, ok := es.meta["cluster_name"]; !ok {
|
||||
return csm.NotMatchedError()
|
||||
}
|
||||
if _, ok := es.meta["cluster_uuid"]; !ok {
|
||||
return csm.NotMatchedError()
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func NewMatcher() csm.Matcher {
|
||||
|
||||
m := &ElasticSearchMatcher{}
|
||||
m.meta = csm.NewMetadata()
|
||||
|
||||
reqStr := "GET / HTTP/1.1\r\n\r\n"
|
||||
byte := make([]byte, len(reqStr))
|
||||
copy(byte[:], reqStr)
|
||||
|
||||
m.AddPacket(csm.NewPacket(byte, len(reqStr)))
|
||||
|
||||
return m
|
||||
}
|
||||
35
elasticsearch/elasticsearch_test.go
Normal file
35
elasticsearch/elasticsearch_test.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package elasticsearch
|
||||
|
||||
import (
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
csm "git.loafle.net/commons/service_matcher-go"
|
||||
)
|
||||
|
||||
func TestES(t *testing.T) {
|
||||
m := NewMatcher()
|
||||
|
||||
conn, err := net.Dial("tcp", "localhost:9200")
|
||||
if err != nil {
|
||||
t.Errorf("ERR %s", err)
|
||||
}
|
||||
|
||||
defer conn.Close()
|
||||
|
||||
for i := 0; i < m.PacketCount(); i++ {
|
||||
_, err := conn.Write(m.Packet(i).Buffer)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
bytes := make([]byte, 1024)
|
||||
n, _ := conn.Read(bytes)
|
||||
p := csm.NewPacket(bytes, n)
|
||||
|
||||
if err := m.Match(nil, i, p); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
t.Log(m.Name())
|
||||
t.Log(m.Meta())
|
||||
}
|
||||
Reference in New Issue
Block a user