Esempio n. 1
0
func (i *MemcacheOutput) Write(data []byte, srcPort int, destPort int, srcAddr string, destAddr string, seq uint32) (int, error) {
	cmd := string(data[:4])
	switch cmd {
	case "get ":
		fallthrough
	case "set ":
		fallthrough
	case "incr":
		fallthrough
	case "decr":
		idx := strings.Index(string(data), "\n")
		cmdstr := string(data[:idx])
		fmt.Println("[MC]" + srcAddr + " -> " + destAddr + " " + cmdstr)
	default:
		//if req, found := i.requests[seq]; found {
		//	size := strconv.Itoa(len(data))
		//	fmt.Println("[MC]" + req.srcAddr + " -> " + req.destAddr + " " + req.cmd + " size:" + size + "B")
		//}

		if len(data) > 5 {
			resCmd := string(data[:5])
			switch resCmd {
			case "VALUE":
				idx := strings.Index(string(data), "\n")
				cmdstr := string(data[6 : idx-1])
				arr := strings.Split(cmdstr, " ")
				color.Println("[MC]"+srcAddr+" -> "+destAddr+" get "+arr[0]+" size: "+arr[2]+"B", color.Cyan)
			default:
			}
		}
	}

	return 0, nil
}
Esempio n. 2
0
func (i *HttpOutput) OutputRAW(requestData *httpRequestData, response *http.Response, rawResponseHeader string) {
	if requestData != nil && response != nil {
		color.PrintlnRequest(requestData.header)
		if requestData.request.Body != nil {
			defer requestData.request.Body.Close()

			body, _ := ioutil.ReadAll(requestData.request.Body)
			i.OutputBody(body)
		}

		color.Println("Response:", color.Yellow)
		color.PrintlnRequest(rawResponseHeader)
		if response.Body != nil {
			defer response.Body.Close()

			body, _ := ioutil.ReadAll(response.Body)
			i.OutputBody(body)
		}
	} else if requestData != nil {
		color.PrintlnRequest(requestData.header)
		if requestData.request.Body != nil {
			defer requestData.request.Body.Close()

			body, _ := ioutil.ReadAll(requestData.request.Body)
			i.OutputBody(body)
		}

	} else {
		color.Println("Response:", color.Yellow)
		color.PrintlnRequest(rawResponseHeader)
		if response.Body != nil {
			defer response.Body.Close()

			body, _ := ioutil.ReadAll(response.Body)
			i.OutputBody(body)
		}
	}

	fmt.Println("")
}
Esempio n. 3
0
func (i *HttpOutput) OutputBody(body []byte) {
	if config.Setting.TruncateBodyLength == 0 {
		return
	}

	content := string(body)
	if config.Setting.TruncateBodyLength > 0 {
		content = i.SubString(content, config.Setting.TruncateBodyLength)
	}
	if strings.TrimSpace(content) == "" || len(content) == 0 {
		return
	}
	if i.IsPrintable(content) {
		color.PrintlnResponse(content)
	} else {
		// can't printable char, encode to hex
		color.PrintResponse(hex.EncodeToString([]byte(content)) + " ")
		color.Println("<unprintable characters>", color.Gray)
	}
}
Esempio n. 4
0
func (i *HttpOutput) OutputRequestLine(requestData *httpRequestData, response *http.Response, rawResponseHeader string) {
	requestBody := ""
	if requestData.request != nil && requestData.request.Body != nil {
		bytes, _ := ioutil.ReadAll(requestData.request.Body)
		requestBody = string(bytes)
	}
	responseBody := ""
	if response != nil && response.Body != nil {
		bytes, _ := ioutil.ReadAll(response.Body)
		responseBody = string(bytes)
	}
	variables := map[string]string{
		"%request.time":                     time.Now().Format("2006-01-02 15:04:05"),
		"%dest.ip":                          requestData.destAddr,
		"%dest.port":                        fmt.Sprintf("%d", requestData.destPort),
		"%source.ip":                        requestData.srcAddr,
		"%source.port":                      fmt.Sprintf("%d", requestData.srcPort),
		"%request.method":                   requestData.request.Method,
		"%request.url":                      requestData.request.RequestURI,
		"%request.user-agent":               requestData.request.UserAgent(),
		"%request.host":                     requestData.request.Host,
		"%request.header":                   requestData.header,
		"%request.body":                     requestBody,
		"%request.header.protocol":          requestData.request.Proto,
		"%request.header.accept":            requestData.request.Header.Get("Accept"),
		"%request.header.accept-encoding":   requestData.request.Header.Get("Accept-Encoding"),
		"%request.header.content-length":    fmt.Sprintf("%d", requestData.request.ContentLength),
		"%request.header.cookie":            requestData.request.Header.Get("Cookie"),
		"%request.header.referer":           requestData.request.Referer(),
		"%response.status":                  fmt.Sprintf("%d", response.StatusCode),
		"%response.header":                  rawResponseHeader,
		"%response.body":                    responseBody,
		"%response.header.content-type":     response.Header.Get("Content-Type"),
		"%response.header.content-encoding": response.Header.Get("Content-Encoding"),
		"%response.header.content-length":   fmt.Sprintf("%d", response.ContentLength),
		"%response.header.etag":             response.Header.Get("Etag"),
		"%response.header.cache-control":    response.Header.Get("Cache-Control"),
		"%response.header.connection":       response.Header.Get("Connection"),
		"%response.header.last-modified":    response.Header.Get("Last-Modified"),
		"%response.header.set-cookie":       response.Header.Get("Set-Cookie"),
	}

	// names := map[string]string{
	// 	"%request.time":                     "time",
	// 	"%dest.ip":                          "dest-ip",
	// 	"%dest.port":                        "dest-port",
	// 	"%source.ip":                        "src-ip",
	// 	"%source.port":                      "src-port",
	// 	"%request.method":                   "method",
	// 	"%request.url":                      "url",
	// 	"%request.user-agent":               "useragent",
	// 	"%request.host":                     "host",
	// 	"%request.header":                   "request-header",
	// 	"%request.body":                     "request-body",
	// 	"%request.header.protocol":          "protocol",
	// 	"%request.header.accept":            "accept",
	// 	"%request.header.accept-encoding":   "accept-encoding",
	// 	"%request.header.content-length":    "content-length",
	// 	"%request.header.cookie":            "cookie",
	// 	"%request.header.referer":           "referer",
	// 	"%response.status":                  "status",
	// 	"%response.header":                  "response-header",
	// 	"%response.body":                    "response-body",
	// 	"%response.header.content-type":     "content-type",
	// 	"%response.header.content-encoding": "content-encoding",
	// 	"%response.header.content-length":   "content-length",
	// 	"%response.header.etag":             "etag",
	// 	"%response.header.cache-control":    "cache-control",
	// 	"%response.header.connection":       "connection",
	// 	"%response.header.last-modified":    "last-modified",
	// 	"%response.header.set-cookie":       "set-cookie",
	// }

	var keys = []string{}
	for key, _ := range variables {
		keys = append(keys, key)
	}
	sort.Sort(common.ByLength(keys))

	line := config.Setting.Format
	for _, key := range keys {
		if v, found := variables[key]; found {
			line = strings.Replace(line, key, v, -1)
		}
	}

	line = strings.Replace(line, `\n`, "\n", -1)
	line = strings.Replace(line, `\t`, "\t", -1)
	color.Println(line, color.MethodColor(requestData.request.Method))

}