-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
138 lines (123 loc) · 2.98 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
package main
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"golang.org/x/net/http2"
"net/http"
"net/url"
"os"
"reflect"
"time"
)
type Result struct {
Headers map[string]interface{} `json:"headers"`
Status string `json:"status"`
HttpStatusCode int `json:"http_status_code"`
}
type Input struct {
Url string `json:"url"`
ProxyUrl string `json:"proxy_url"`
TimeoutMs uint64 `json:"timeout_ms"`
RequestHeaders map[string]string `json:"headers"`
}
func main() {
if len(os.Args) < 2 {
fmt.Print(`{"status": "error missing json input"}`)
os.Exit(1)
}
var input Input
err := json.Unmarshal([]byte(os.Args[1]), &input)
if err != nil {
fmt.Print(`{"status": "error invalid json input"}`)
os.Exit(1)
}
fmt.Print(FakeHead(input.Url, input.ProxyUrl, input.TimeoutMs, input.RequestHeaders))
}
func FakeHead(rawUrl string, rawProxyUrl string, timeoutMs uint64, requestHeaders map[string]string) string {
proxyURL, err := url.Parse(rawProxyUrl)
if err != nil {
panic(err)
}
proxy := http.ProxyURL(proxyURL)
transport := &http.Transport{
Proxy: proxy,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
err = http2.ConfigureTransport(transport)
if err != nil {
panic(err)
}
client := &http.Client{
Transport: transport,
}
req, err := http.NewRequest("GET", rawUrl, nil)
if err != nil {
panic(err)
}
for key, value := range requestHeaders {
req.Header.Add(key, value)
}
ctx, cancel := context.WithCancel(context.Background())
timeoutChan := time.NewTimer(time.Duration(timeoutMs) * time.Millisecond).C
defer cancel()
req = req.WithContext(ctx)
c1 := make(chan string, 1)
go func() {
resp, respErr := client.Do(req)
if respErr != nil {
errorResult, marshalErr := json.Marshal(Result{
Headers: make(map[string]interface{}),
Status: "error",
})
if marshalErr != nil {
panic(marshalErr)
}
c1 <- string(errorResult)
}
cancel()
//got ctx cancelled
if resp == nil || respErr != nil {
return
}
responseHeaders, marshalErr := json.Marshal(Result{
Headers: flattenSingleElementArrays(resp.Header),
Status: "ok",
HttpStatusCode: resp.StatusCode,
})
if marshalErr != nil {
panic(marshalErr)
}
if resp.Body != nil {
resp.Body.Close()
}
c1 <- string(responseHeaders)
}()
select {
case <-timeoutChan:
cancel()
timeoutResult, marshalErr := json.Marshal(Result{Status: "timeout"})
if marshalErr != nil {
panic(marshalErr)
}
return string(timeoutResult)
case result := <-c1:
close(c1)
return result
}
}
func flattenSingleElementArrays(originalMap map[string][]string) map[string]interface{} {
flattenedMap := make(map[string]interface{})
for key, value := range originalMap {
v := reflect.ValueOf(value)
if v.Kind() == reflect.Slice && v.Len() == 1 {
flattenedMap[key] = v.Index(0).String()
} else {
flattenedMap[key] = value
}
}
return flattenedMap
}