-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
139 lines (121 loc) · 3.27 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
package main
import (
"crypto/tls"
"flag"
"log"
"net/http"
"net/http/httputil"
"os"
)
func main() {
// Define flag variables
var certFile string
var keyFile string
var showHelp bool
// Define flags and usage
flag.StringVar(&certFile, "cert", "", "Path to the TLS certificate file")
flag.StringVar(&keyFile, "key", "", "Path to the TLS private key file")
flag.BoolVar(&showHelp, "help", false, "Show help message")
// Set custom usage function
flag.Usage = func() {
flag.PrintDefaults()
}
// Parse command-line arguments
flag.Parse()
// Check if help flag is provided
if showHelp {
flag.Usage()
os.Exit(0)
}
// Check if no flags were provided
if flag.NFlag() == 0 {
flag.Usage()
os.Exit(1)
}
// Create HTTP reverse proxy
httpProxy := &httputil.ReverseProxy{
Director: func(req *http.Request) {
// Set the target URL to the original request URL
req.URL.Scheme = "http"
req.URL.Host = req.Host
},
ErrorHandler: func(rw http.ResponseWriter, req *http.Request, err error) {
log.Println("Reverse proxy error:", err)
http.Error(rw, "Oops! Something went wrong. Inspect server logs.", http.StatusInternalServerError)
},
}
// Create HTTPS reverse proxy
httpsProxy := &httputil.ReverseProxy{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
Director: func(req *http.Request) {
// Set the target URL to the original request URL
req.URL.Scheme = "https"
req.URL.Host = req.Host
},
ErrorHandler: func(rw http.ResponseWriter, req *http.Request, err error) {
log.Println("Reverse proxy error:", err)
http.Error(rw, "Oops! Something went wrong. Inspect server logs.", http.StatusInternalServerError)
},
}
ingressHttpServer := &http.Server{
Addr: ":80",
Handler: httpProxy,
}
apiServer := &http.Server{
Addr: ":6443",
TLSConfig: &tls.Config{
Certificates: []tls.Certificate{
loadTLSCertificate(certFile, keyFile),
},
},
Handler: httpsProxy,
}
// Configure the HTTPS server
ingressHttpsServer := &http.Server{
Addr: ":443",
TLSConfig: &tls.Config{
Certificates: []tls.Certificate{
loadTLSCertificate(certFile, keyFile),
},
},
Handler: httpsProxy,
}
// Start the HTTPS server on port 6443
go func() {
log.Println("Starting reverse proxy server on port 6443 ...")
err := apiServer.ListenAndServeTLS("", "")
if err != nil {
log.Fatal("Error starting reverse proxy server:", err)
}
}()
// Start the HTTPS server on port 80
go func() {
log.Println("Starting reverse proxy server on port 80 ...")
err := ingressHttpServer.ListenAndServe()
if err != nil {
log.Fatal("Error starting reverse proxy server:", err)
}
}()
// Start the HTTPS server on port 443
go func() {
log.Println("Starting reverse proxy server on port 443...")
err := ingressHttpsServer.ListenAndServeTLS("", "")
if err != nil {
log.Fatal("Error starting reverse proxy server:", err)
}
}()
// Wait indefinitely to keep the program running
select {}
}
// LoadTLSKeyPair loads a TLS certificate and private key from files and returns a tls.Certificate.
func loadTLSCertificate(certFile, keyFile string) tls.Certificate {
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
log.Fatal("Error loading TLS certificate:", err)
}
return cert
}