-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
144 lines (127 loc) · 3.64 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"archive/zip"
"bufio"
"encoding/xml"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"regexp"
"time"
)
var reFileName *regexp.Regexp
func init() {
reFileName, _ = regexp.Compile("(\\d+)_(\\w)")
}
type Session struct {
XMLName xml.Name `xml:"Session"`
Timers SessionTimers `xml:"SessionTimers"`
Flags SessionFlags `xml:"SessionFlags"`
}
type SessionTimers struct {
XMLName xml.Name `xml:"SessionTimers"`
ClientConnected string `xml:"ClientConnected,attr"`
ClientBeginRequest string `xml:"ClientBeginRequest,attr"`
GotRequestHeaders string `xml:"GotRequestHeaders,attr"`
ClientDoneRequest string `xml:"ClientDoneRequest,attr"`
GatewayTime string `xml:"GatewayTime,attr"`
DNSTime string `xml:"DNSTime,attr"`
TCPConnectTime string `xml:"TCPConnectTime,attr"`
HTTPSHandshakeTime string `xml:"HTTPSHandshakeTime,attr"`
ServerConnected string `xml:"ServerConnected,attr"`
FiddlerBeginRequest string `xml:"FiddlerBeginRequest,attr"`
ServerGotRequest string `xml:"ServerGotRequest,attr"`
ServerBeginResponse string `xml:"ServerBeginResponse,attr"`
GotResponseHeaders string `xml:"GotResponseHeaders,attr"`
ServerDoneResponse string `xml:"ServerDoneResponse,attr"`
ClientBeginResponse string `xml:"ClientBeginResponse,attr"`
ClientDoneResponse string `xml:"ClientDoneResponse,attr"`
}
type SessionFlags struct {
XMLName xml.Name `xml:"SessionFlags"`
Flags []SessionFlag `xml:"SessionFlag"`
}
type SessionFlag struct {
XMLName xml.Name `xml:"SessionFlag"`
Name string `xml:"N,attr"`
Value string `xml:"V,attr"`
}
func main() {
flag.Parse()
r, err := zip.OpenReader(flag.Arg(0))
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(-1)
}
defer r.Close()
var request *http.Request
var response *http.Response
var session Session
for _, f := range r.File {
match, num, t := parseFileName(f.Name)
if match == false {
continue
}
if t == "c" {
read, err := f.Open()
if nil != err {
fmt.Printf("%v\n", err)
os.Exit(-1)
}
defer read.Close()
reqReader := bufio.NewReader(read)
request, _ = http.ReadRequest(reqReader)
}
if t == "m" {
read, err := f.Open()
if nil != err {
fmt.Printf("%v\n", err)
os.Exit(-1)
}
defer read.Close()
bytes, _ := ioutil.ReadAll(read)
xml.Unmarshal(bytes, &session)
}
if t == "s" {
read, err := f.Open()
if nil != err {
fmt.Printf("%v\n", err)
os.Exit(-1)
}
defer read.Close()
respReader := bufio.NewReader(read)
response, _ = http.ReadResponse(respReader, request)
printResult(num, request, response, session)
}
}
}
func parseFileName(name string) (bool, string, string) {
match := reFileName.FindAllStringSubmatch(name, -1)
if len(match) == 0 {
return false, "", ""
}
return true, match[0][1], match[0][2]
}
func printResult(num string, request *http.Request, response *http.Response, session Session) {
clientBeginRequest, err := time.Parse(time.RFC3339, session.Timers.ClientBeginRequest)
if nil != err {
fmt.Println("Error while parsing clientConnected:", err)
os.Exit(-1)
}
clientDoneResponse, err := time.Parse(time.RFC3339, session.Timers.ClientDoneResponse)
if nil != err {
fmt.Println("Error while parsing clientDoneResponse:", err)
os.Exit(-1)
}
var process string
for _, flag := range session.Flags.Flags {
if flag.Name == "x-processinfo" {
process = flag.Value
break
}
}
fmt.Printf("%v\t%v\t%v\t%v\t%v\t%v\t%v\n", num, request.Method, response.StatusCode, request.URL.String(),
clientBeginRequest.Format("15:04:05.000"), clientDoneResponse.Format("15:04:05.000"), process)
}