@@ -2,13 +2,40 @@ package main
2
2
3
3
import (
4
4
"crypto/tls"
5
+ "fmt"
6
+ "io/ioutil"
5
7
"log"
6
8
"net/url"
7
9
8
10
"github.com/valyala/fasthttp"
11
+ "gopkg.in/yaml.v2"
9
12
)
10
13
14
+ // Mapping struct to hold the proxy domain and target URL
15
+ type Mapping struct {
16
+ Domain string `yaml:"domain"`
17
+ TargetURL string `yaml:"targetURL"`
18
+ }
19
+
11
20
func main () {
21
+ // Read the detour.yaml file
22
+ detourData , err := ioutil .ReadFile ("detour.yaml" )
23
+ if err != nil {
24
+ log .Fatal ("Failed to read detour.yaml:" , err )
25
+ }
26
+
27
+ // Parse the YAML data
28
+ var detourConfig struct {
29
+ Port int `yaml:"port"`
30
+ CertFile string `yaml:"certFile"`
31
+ KeyFile string `yaml:"keyFile"`
32
+ Mappings []Mapping `yaml:"mappings"`
33
+ }
34
+ err = yaml .Unmarshal (detourData , & detourConfig )
35
+ if err != nil {
36
+ log .Fatal ("Failed to parse detour.yaml:" , err )
37
+ }
38
+
12
39
// Create a new TLS configuration
13
40
tlsConfig := & tls.Config {
14
41
MinVersion : tls .VersionTLS12 , // Set the minimum TLS version
@@ -17,7 +44,10 @@ func main() {
17
44
18
45
// Create a new HTTP client with TLS support
19
46
client := & fasthttp.Client {
20
- TLSConfig : tlsConfig ,
47
+ TLSConfig : tlsConfig ,
48
+ MaxResponseBodySize : 10 * 1024 * 1024 , // 10MB max response size
49
+ ReadBufferSize : 4096 ,
50
+ WriteBufferSize : 4096 ,
21
51
}
22
52
23
53
// Create a new fasthttp server
@@ -26,15 +56,16 @@ func main() {
26
56
// Extract the requested host
27
57
host := string (ctx .Host ())
28
58
29
- // Define the mapping of domains to target URLs
30
- targetURLs := map [string ]string {
31
- "domain.com" : "https://www.google.com" ,
32
- "sales.domain.com" : "https://debuggerboy.com" ,
59
+ // Find the target URL for the requested host in the mappings
60
+ var targetURL string
61
+ for _ , mapping := range detourConfig .Mappings {
62
+ if mapping .Domain == host {
63
+ targetURL = mapping .TargetURL
64
+ break
65
+ }
33
66
}
34
67
35
- // Get the target URL for the requested host
36
- targetURL , ok := targetURLs [host ]
37
- if ! ok {
68
+ if targetURL == "" {
38
69
ctx .Error ("Unknown host" , fasthttp .StatusNotFound )
39
70
return
40
71
}
@@ -50,6 +81,7 @@ func main() {
50
81
ctx .URI ().SetScheme (target .Scheme )
51
82
ctx .URI ().SetHost (target .Host )
52
83
ctx .URI ().SetPath (string (target .Path ) + string (ctx .Path ()))
84
+ ctx .URI ().SetQueryStringBytes (ctx .QueryArgs ().QueryString ())
53
85
54
86
// Set the host header to the target host
55
87
ctx .Request .Header .SetHost (target .Host )
@@ -70,9 +102,8 @@ func main() {
70
102
}
71
103
72
104
// Listen for incoming HTTPS/TLS connections
73
- err : = server .ListenAndServeTLS (":443 " , "server.crt" , "server.key" )
105
+ err = server .ListenAndServeTLS (fmt . Sprintf ( ":%d " , detourConfig . Port ), detourConfig . CertFile , detourConfig . KeyFile )
74
106
if err != nil {
75
107
log .Fatal (err )
76
108
}
77
109
}
78
-
0 commit comments