-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
78 lines (64 loc) · 1.87 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
package main
import (
"io/ioutil"
"log"
"github.com/valyala/fasthttp"
"gopkg.in/yaml.v2"
)
type Config struct {
CertFile string `yaml:"certFile"`
KeyFile string `yaml:"keyFile"`
Port string `yaml:"port"`
}
type RoutesConfig struct {
Routes map[string]string `yaml:"routes"`
}
func main() {
// Read YAML config file
configFile := "/etc/detour/config.yaml" // Replace with your YAML config file path
configData, err := ioutil.ReadFile(configFile)
if err != nil {
log.Fatalf("Error reading config file: %s", err)
}
// Parse YAML config
var config Config
err = yaml.Unmarshal(configData, &config)
if err != nil {
log.Fatalf("Error parsing config file: %s", err)
}
// Read YAML routes file
routesFile := "/etc/detour/routes.yaml" // Replace with your YAML routes file path
routesData, err := ioutil.ReadFile(routesFile)
if err != nil {
log.Fatalf("Error reading routes file: %s", err)
}
// Parse YAML routes
var routesConfig RoutesConfig
err = yaml.Unmarshal(routesData, &routesConfig)
if err != nil {
log.Fatalf("Error parsing routes file: %s", err)
}
redirectHandler := func(ctx *fasthttp.RequestCtx) {
requestURI := string(ctx.Request.URI().Path())
redirectURL, ok := routesConfig.Routes[requestURI]
if !ok {
ctx.Response.SetStatusCode(fasthttp.StatusNotFound)
return
}
// Set the appropriate redirect status code
ctx.Response.SetStatusCode(fasthttp.StatusMovedPermanently)
// Set the a server header
ctx.Response.Header.Set("Server", "deTour proxy")
// Set the Location header to the redirect URL
ctx.Response.Header.Set("Location", redirectURL)
}
// Create the FastHTTP server
server := &fasthttp.Server{
Handler: redirectHandler,
}
// Start the server with TLS support
err = server.ListenAndServeTLS(config.Port, config.CertFile, config.KeyFile)
if err != nil {
log.Fatalf("Error in ListenAndServeTLS: %s", err)
}
}