forked from koalabearguo/php-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse.go
89 lines (84 loc) · 2.21 KB
/
response.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
package main
import (
"bufio"
"bytes"
"io"
"log"
"net/http"
"strconv"
//"strings"
"time"
)
type response struct {
res *http.Response
cfg *config
}
var ResDeleteHeader = []string{"Upgrade", "Alt-Svc", "Alternate-Protocol", "Expect-CT", "Report-To", "Nel"}
func (res *response) patch_response(r *bufio.Reader, req *http.Request) (resp *http.Response, error error) {
//
prefix := make([]byte, 7)
n, err := io.ReadFull(r, prefix)
if err != nil {
return nil, err
}
//
if string(prefix[:n]) == "HTTP/2 " {
// fix HTTP/2 proto
resp, error = http.ReadResponse(bufio.NewReader(io.MultiReader(bytes.NewBufferString("HTTP/2.0 "), r)), req)
} else if string(prefix[:n]) == "HTTP/3 " {
// fix HTTP/3 proto(feature may has this issue)
resp, error = http.ReadResponse(bufio.NewReader(io.MultiReader(bytes.NewBufferString("HTTP/3.0 "), r)), req)
} else {
// other proto
resp, error = http.ReadResponse(bufio.NewReader(io.MultiReader(bytes.NewBuffer(prefix[:n]), r)), req)
}
//
return resp, error
}
func (res *response) parse_response() *http.Response {
//
if res.cfg.Debug {
for k, v := range res.res.Header {
for _, value := range v {
log.Print(k + ":" + value)
}
}
for _, chunkstr := range res.res.TransferEncoding {
log.Print("Transfer-Encoding" + ":" + chunkstr)
}
if res.res.Uncompressed {
log.Print("Content-Encoding" + ":" + "compressed")
}
}
encrypt := &encrypt{cfg: res.cfg}
//
if res.res.StatusCode != http.StatusOK {
return res.res
}
start := time.Now()
if res.res.Header.Get("Content-Type") == "image/gif" && res.res.Body != nil {
res.res.Body = encrypt.decrypt_reader(res.res.Body)
}
if res.cfg.Debug {
elapsed := time.Since(start)
log.Println("First Data Decrypt Time:", elapsed)
}
//
//return res.res //for test
//resp, err := http.ReadResponse(bufio.NewReader(res.res.Body), res.res.Request)
resp, err := res.patch_response(bufio.NewReader(res.res.Body), res.res.Request)
if err != nil {
log.Println(err)
return res.res
}
//
if resp.Header.Get("Content-Length") == "" && resp.ContentLength >= 0 {
resp.Header.Set("Content-Length", strconv.FormatInt(resp.ContentLength, 10))
}
//
for _, h := range ResDeleteHeader {
resp.Header.Del(h)
}
//
return resp
}