-
Notifications
You must be signed in to change notification settings - Fork 1
/
log_req_resp.go
131 lines (119 loc) · 2.6 KB
/
log_req_resp.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
package main
import (
"errors"
"flag"
"fmt"
"io/fs"
"log"
"math/rand"
"net"
"os"
"path"
"strconv"
"strings"
"github.com/valyala/fasthttp"
)
var isSaveFs = flag.Bool("save", false, "Save all targets to file system")
func fileMode(path string) (fs.FileMode, error) {
fi, err := os.Stat(path)
if err != nil {
return 0, err
}
return fi.Mode(), nil
}
func ensureDir(targetDir string, stop bool) {
targetDir = strings.TrimRight(targetDir, "/\\")
mode, err := fileMode(targetDir)
if err == nil {
// exist
if mode.IsDir() {
// is dir
return
}
if mode.IsRegular() {
// is file
// move file
targetFile := targetDir + strconv.FormatUint(rand.Uint64(), 36)
os.Rename(targetDir, targetFile)
os.Mkdir(targetDir, 0755)
os.Rename(targetFile, targetDir+"/index.html")
return
}
return
}
// not exists
err = os.Mkdir(targetDir, 0755)
if err == nil {
return
}
if stop {
return
}
curDir := "."
for _, dir := range strings.Split(targetDir, "/") {
curDir += "/" + dir
ensureDir(curDir, true)
}
}
// GetResponseBody return plain response body of resp
func GetResponseBody(resp *fasthttp.Response) ([]byte, error) {
var contentEncoding = string(resp.Header.Peek("Content-Encoding"))
if len(contentEncoding) < 1 {
return resp.Body(), nil
}
if hasToken(contentEncoding, "br") {
return resp.BodyUnbrotli()
}
if hasToken(contentEncoding, "gzip") {
return resp.BodyGunzip()
}
if hasToken(contentEncoding, "deflate") {
return resp.BodyInflate()
}
return nil, errors.New("unsupported response content encoding: " + string(contentEncoding))
}
func logReqResp(isHTTPS bool, req *fasthttp.Request, resp *fasthttp.Response) {
if !*isSaveFs {
fmt.Println(isHTTPS, req, resp)
return
}
body, err := GetResponseBody(resp)
if err != nil {
log.Println(err)
return
}
host := string(req.URI().Host())
hostname, _, err := net.SplitHostPort(host)
if err != nil {
if err1, ok := err.(*net.AddrError); ok && strings.Contains(err1.Err, "missing port") {
hostname, _, err = net.SplitHostPort(host + ":80")
}
if err != nil {
log.Println(err)
return
}
}
targetFile := hostname + "/" + string(req.URI().Path())
targetDir, targetFile := path.Split(targetFile)
ensureDir(targetDir, false)
// file 644
// dir 755
targetFile = targetDir + "/" + targetFile
mode, err := os.Stat(targetFile)
if err == nil {
// exists
if mode.IsDir() {
// is dir
targetFile = targetFile + "/index.html"
}
// is regular file?
// pass
}
// not exists
// pass
// expect file
err = os.WriteFile(targetFile, body, 0644)
if err != nil {
log.Println(targetFile, err)
}
}