Skip to content

Commit ac43f9b

Browse files
committed
updating proxy server
1 parent 7da10f7 commit ac43f9b

File tree

5 files changed

+54
-11
lines changed

5 files changed

+54
-11
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ go.work
2424
detour2-devel
2525
server.crt
2626
server.key
27+
detour.yaml
28+
detour-mappings.yaml
2729
old-ssl/
2830

2931
# Backup

detour.yaml.sample

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
port: ":443"
2+
certificate: "path/to/server.crt"
3+
keyfile: "path/to/server.key"
4+

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ module pulsiot/detour2-devel
22

33
go 1.19
44

5-
require github.com/valyala/fasthttp v1.48.0
5+
require (
6+
github.com/valyala/fasthttp v1.48.0
7+
gopkg.in/yaml.v2 v2.4.0
8+
)
69

710
require (
811
github.com/andybalholm/brotli v1.0.5 // indirect

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6Kllzaw
66
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
77
github.com/valyala/fasthttp v1.48.0 h1:oJWvHb9BIZToTQS3MuQ2R3bJZiNSa2KiNdeI8A+79Tc=
88
github.com/valyala/fasthttp v1.48.0/go.mod h1:k2zXd82h/7UZc3VOdJ2WaUqt1uZ/XpXAfE9i+HBC3lA=
9+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
10+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
11+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

main.go

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,40 @@ package main
22

33
import (
44
"crypto/tls"
5+
"fmt"
6+
"io/ioutil"
57
"log"
68
"net/url"
79

810
"github.com/valyala/fasthttp"
11+
"gopkg.in/yaml.v2"
912
)
1013

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+
1120
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+
1239
// Create a new TLS configuration
1340
tlsConfig := &tls.Config{
1441
MinVersion: tls.VersionTLS12, // Set the minimum TLS version
@@ -17,7 +44,10 @@ func main() {
1744

1845
// Create a new HTTP client with TLS support
1946
client := &fasthttp.Client{
20-
TLSConfig: tlsConfig,
47+
TLSConfig: tlsConfig,
48+
MaxResponseBodySize: 10 * 1024 * 1024, // 10MB max response size
49+
ReadBufferSize: 4096,
50+
WriteBufferSize: 4096,
2151
}
2252

2353
// Create a new fasthttp server
@@ -26,15 +56,16 @@ func main() {
2656
// Extract the requested host
2757
host := string(ctx.Host())
2858

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+
}
3366
}
3467

35-
// Get the target URL for the requested host
36-
targetURL, ok := targetURLs[host]
37-
if !ok {
68+
if targetURL == "" {
3869
ctx.Error("Unknown host", fasthttp.StatusNotFound)
3970
return
4071
}
@@ -50,6 +81,7 @@ func main() {
5081
ctx.URI().SetScheme(target.Scheme)
5182
ctx.URI().SetHost(target.Host)
5283
ctx.URI().SetPath(string(target.Path) + string(ctx.Path()))
84+
ctx.URI().SetQueryStringBytes(ctx.QueryArgs().QueryString())
5385

5486
// Set the host header to the target host
5587
ctx.Request.Header.SetHost(target.Host)
@@ -70,9 +102,8 @@ func main() {
70102
}
71103

72104
// 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)
74106
if err != nil {
75107
log.Fatal(err)
76108
}
77109
}
78-

0 commit comments

Comments
 (0)