network_service_matcher/http/http.go

82 lines
1.5 KiB
Go
Raw Normal View History

2017-12-04 07:27:08 +00:00
package http
import (
"strings"
cnsm "git.loafle.net/commons_go/network_service_matcher"
)
type HTTPMatcher struct {
cnsm.Matchers
}
func (h *HTTPMatcher) ServiceName() string {
return "HTTP"
}
func (h *HTTPMatcher) IsPrePacket() bool {
return false
}
func (h *HTTPMatcher) HasResponse(index int) bool {
return true
}
func (h *HTTPMatcher) IsError(info cnsm.MatchInfo, index int, packet *cnsm.Packet) bool {
return false
}
func (h *HTTPMatcher) Match(info cnsm.MatchInfo, index int, packet *cnsm.Packet) bool {
result := false
if packet == nil || packet.Buffer == nil || packet.Len == 0 {
return result
}
str := string(packet.Buffer)
//fmt.Println(str)
elems := strings.Split(str, "\r\n")
if len(elems) <= 0 {
return result
}
protocol := (elems[0])[:8]
httpv0 := strings.Compare(protocol, "HTTP/1.0")
httpv1 := strings.Compare(protocol, "HTTP/1.1")
httpv2 := strings.Compare(protocol, "HTTP/1.2")
if 0 == httpv0 || 0 == httpv1 || 0 == httpv2 {
result = true
}
serverName := "Unknown Server"
for _, valueStr := range elems {
tempElems := strings.Split(valueStr, ":")
if 0 == strings.Compare(tempElems[0], "Server") {
serverName = tempElems[1]
break
}
}
strings.Compare(serverName, "Unknown")
//fmt.Println("HTTP Server Name: ", serverName)
return result
}
func NewMatcher() cnsm.Matcher {
m := &HTTPMatcher{}
reqStr := "GET / HTTP/1.1\r\n\r\n"
byte := make([]byte, len(reqStr))
copy(byte[:], reqStr)
m.AddPacket(cnsm.NewPacket(byte, len(reqStr)))
return m
}