-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
74 lines (55 loc) · 1.5 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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strconv"
"github.com/elazarl/goproxy"
"github.com/joho/godotenv"
)
func main() {
godotenv.Load()
proxy := goproxy.NewProxyHttpServer()
proxy.OnRequest().DoFunc(func (req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
var headers map[string]string
headersEnv := os.Getenv("HEADERS")
err := json.Unmarshal([]byte(headersEnv), &headers)
if err != nil {
log.Printf("Error unmarshalling JSON: %v\n", err)
return req, goproxy.NewResponse(req, goproxy.ContentTypeText, http.StatusInternalServerError, fmt.Sprintf("Internal Server Error: %v\n", err))
}
for key, value := range headers {
req.Header.Set(key, value)
}
return req, nil
})
proxy.OnRequest().HandleConnect(goproxy.AlwaysMitm)
host := os.Getenv("HOST")
if host == "" {
host = "0.0.0.0"
}
portEnv := os.Getenv("PORT")
if portEnv == "" {
portEnv = "3000"
}
port, err := strconv.Atoi(portEnv)
if err != nil {
log.Fatalf("Invalid port number: %s\n", portEnv)
}
verboseEnv := os.Getenv("VERBOSE")
if verboseEnv == "" {
verboseEnv = "f"
}
verbose, err := strconv.ParseBool(verboseEnv)
if err != nil {
log.Fatalf("Invalid verbose value: %s\n", verboseEnv)
}
proxy.Verbose = verbose
log.Printf("Proxy server is running on http://%s:%d", host, port)
listenErr := http.ListenAndServe(fmt.Sprintf("%s:%d", host, port), proxy)
if listenErr != nil {
log.Fatalf("Failed to start server: %v", listenErr)
}
}