-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
215 lines (181 loc) · 4.8 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"fmt"
"io"
"log"
"net/url"
"os"
"strconv"
"strings"
"time"
fhttp "github.com/Noooste/fhttp"
"github.com/Noooste/azuretls-client"
"github.com/stanislav-milchev/tls-impersonator/browser"
)
var (
serverPort = getEnv("TLS_PORT", "8082")
urlHeaderName = getEnv("TLS_URL", "x-tls-url")
proxyHeaderName = getEnv("TLS_PROXY", "x-tls-proxy")
bufferingHeaderName = getEnv("TLS_BUFFER", "x-tls-buffer")
redirectHeaderName = getEnv("TLS_REDIRECT", "x-tls-allowredirect")
timeoutHeaderName = getEnv("TLS_TIMEOUT", "x-tls-timeout")
)
func main() {
port := fmt.Sprintf(":%s", serverPort)
log.Printf("Listening on localhost%s", port)
fhttp.HandleFunc("/", HandleReq)
fhttp.HandleFunc("/isalive", HandleIsAlive)
err := fhttp.ListenAndServe(port, nil)
if err != nil {
log.Fatalln("Error starting the HTTP server:", err)
}
}
func HandleIsAlive(w fhttp.ResponseWriter, r *fhttp.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(fhttp.StatusOK)
fmt.Fprintf(w, `{"isalive":true}`)
}
// HandleReq takes the incoming request, parses it, sends it towards the target host
func HandleReq(w fhttp.ResponseWriter, r *fhttp.Request) {
session, req, err := NewRequest(r)
if err != nil {
log.Print(err)
w.WriteHeader(fhttp.StatusBadRequest)
return
}
defer session.Close()
SetHeaders(session, r.Header)
SetCookies(req.Url, session, r.Cookies())
res, err := session.Do(req)
if err != nil {
if strings.Contains(err.Error(), "timeout") {
fmt.Print("timeout\n", err)
w.WriteHeader(fhttp.StatusRequestTimeout)
return
} else {
fmt.Print("other error:\n", err)
w.WriteHeader(fhttp.StatusInternalServerError)
return
}
}
// Forward the headers received
for h, v := range res.Header {
// Response we get is already decoded so this header will only cause issues with the
// client used for the request
if "content-encoding" == strings.ToLower(h) {
continue
}
if len(v) > 0 {
w.Header().Set(h, v[0])
} else {
fmt.Printf("Skipping \"%s\" header with invalid value", h)
continue
}
}
var buffering bool
switch b := r.Header.Get(bufferingHeaderName); b {
case "true", "True", "1":
buffering = true
default:
buffering = false
}
w.WriteHeader(res.StatusCode)
// Either return buffered response or a stream
if buffering {
if readBody, readErr := res.ReadBody(); readErr == nil {
w.Write(readBody)
} else {
log.Printf("Error buffering response: %v", readErr)
}
} else {
_, err = io.Copy(w, res.RawBody)
if err != nil {
log.Printf("Error streaming response: %v", err)
}
res.RawBody.Close()
}
}
// NewRequest opens a new azuretls session and a request, and sets it up with url,
// proxy, headers, cookies, redirects and timeouts
func NewRequest(r *fhttp.Request) (*azuretls.Session, *azuretls.Request, error) {
// Open and set-up session
session := azuretls.NewSession()
session.EnableLog()
// Parse URL
urlHeader := r.Header.Get(urlHeaderName)
if urlHeader == "" {
return nil, nil, fmt.Errorf(
"no valid request URL supplied via '%s'; skipping request", urlHeaderName,
)
}
// Parse redirects
var allowRedirects bool
switch rH := r.Header.Get(redirectHeaderName); rH {
case "true", "True", "1":
allowRedirects = true
default:
allowRedirects = false
}
// Parse timeout
timeoutHeader := r.Header.Get(timeoutHeaderName)
t, err := strconv.Atoi(timeoutHeader)
if err != nil || t <= 0 {
t = 30
}
timeout := time.Duration(t) * time.Second
session.SetTimeout(timeout)
// Parse proxy
proxy := r.Header.Get(proxyHeaderName)
session.SetProxy(proxy)
var body any
if r.Method == fhttp.MethodPost {
body = r.Body
} else {
body = nil
}
req := &azuretls.Request{
Method: r.Method,
Url: urlHeader,
DisableRedirects: !allowRedirects,
IgnoreBody: true,
Body: body,
}
return session, req, nil
}
// SetHeaders sets the custom headers received in the server to the session
func SetHeaders(s *azuretls.Session, headers fhttp.Header) {
browserHeaders := browser.Chrome126
customHeaderNames := []string{
urlHeaderName,
proxyHeaderName,
redirectHeaderName,
timeoutHeaderName,
bufferingHeaderName,
}
Outer:
for k, v := range headers {
for _, header := range customHeaderNames {
if strings.ToLower(header) == strings.ToLower(k) {
continue Outer
}
}
exist := browserHeaders.Get(strings.ToLower(k)) != ""
if !exist {
browserHeaders = append(browserHeaders, []string{k, v[0]})
}
}
s.OrderedHeaders = browserHeaders
}
func SetCookies(url_ string, s *azuretls.Session, c []*fhttp.Cookie) {
parsed, err := url.Parse(url_)
if err != nil {
return
}
s.CookieJar.SetCookies(parsed, c)
}
func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}