service_matcher-go/http/http.go
Administrator 8bc927cf91 Revert "Revert "ing""
This reverts commit 7e4a2f1775
2018-09-13 08:31:11 +00:00

106 lines
2.0 KiB
Go

package http
import (
"strings"
osm "git.loafle.net/overflow/service_matcher-go"
)
type HTTPMatcher struct {
osm.Matchers
}
func (m *HTTPMatcher) Key() string {
return "HTTP"
}
func (m *HTTPMatcher) Type() string {
return "WEB"
}
func (m *HTTPMatcher) Vendor(matchCtx *osm.MatchCtx) string {
return "UNKNOWN"
}
func (m *HTTPMatcher) Version(matchCtx *osm.MatchCtx) string {
return "UNKNOWN"
}
func (m *HTTPMatcher) OsType(matchCtx *osm.MatchCtx) string {
return "UNKNOWN"
}
func (m *HTTPMatcher) OsVersion(matchCtx *osm.MatchCtx) string {
return "UNKNOWN"
}
func (m *HTTPMatcher) Name(matchCtx *osm.MatchCtx) string {
name := "HTTP"
if v, ok := matchCtx.GetAttribute("server"); ok {
name = name + " (" + v + ")"
}
return name
}
func (m *HTTPMatcher) IsPrePacket() bool {
return false
}
func (m *HTTPMatcher) HasResponse(matchCtx *osm.MatchCtx, index int) bool {
return true
}
func (m *HTTPMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool {
return false
}
func (m *HTTPMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error {
if packet == nil || !packet.Valid() {
return osm.NoPacketReceivedError()
}
str := string(packet.Bytes())
elems := strings.Split(str, "\r\n")
if len(elems) <= 0 || 9 > len(elems[0]) {
return osm.NotMatchedError()
}
protocol := (elems[0])[:8]
if !strings.HasPrefix(protocol, "HTTP/") {
return osm.NotMatchedError()
}
serverName := ""
for _, valueStr := range elems {
tempElems := strings.Split(valueStr, ":")
if 0 == strings.Compare(tempElems[0], "Server") {
serverName = strings.TrimSpace(tempElems[1])
break
}
}
matchCtx.SetAttribute("protocol_version", protocol)
if len(serverName) > 0 {
matchCtx.SetAttribute("server", serverName)
}
return nil
}
func NewMatcher() osm.Matcher {
m := &HTTPMatcher{}
reqStr := "GET / HTTP/1.1\r\n\r\n"
byte := make([]byte, len(reqStr))
copy(byte[:], reqStr)
m.AddPacket(osm.NewPacket(byte, len(reqStr)))
return m
}