forked from dkoukoul/cjdns-proxy-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
277 lines (242 loc) · 7 KB
/
main.go
File metadata and controls
277 lines (242 loc) · 7 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package main
import (
"bufio"
"bytes"
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"net/http/httputil"
"net/url"
"os"
"os/exec"
"regexp"
"strings"
"time"
)
var (
toHost string
fromHost string
proxyPort string
)
func modifyRequestHeaders(request *http.Request) http.Header {
modifiedHeaders := request.Header.Clone()
modifiedHeaders.Set("X-Real-IP", request.RemoteAddr)
modifiedHeaders.Set("X-Forwarded-For", request.RemoteAddr)
referer := modifiedHeaders.Get("Referer")
if referer != "" && strings.HasPrefix(referer, "http://["+fromHost+"]") {
modifiedLocation := strings.ReplaceAll(referer, "http://["+fromHost+"]", "https://"+toHost)
modifiedHeaders.Set("Referer", modifiedLocation)
}
return modifiedHeaders
}
func modifyResponseHeaders(response *http.Response) http.Header {
modifiedHeaders := response.Header.Clone()
modifiedHeaders.Del("Content-Security-Policy")
modifiedHeaders.Del("Strict-Transport-Security")
location := modifiedHeaders.Get("Location")
if location != "" && (strings.HasPrefix(location, "https://"+toHost) || strings.HasPrefix(location, "https://yunohost.local")) {
modifiedLocation := strings.ReplaceAll(location, "https://"+toHost, "http://["+fromHost+"]")
modifiedLocation = strings.ReplaceAll(modifiedLocation, "https://yunohost.local", "http://["+fromHost+"]")
//log.Printf("From: %s To: %s\n", location, modifiedLocation)
modifiedHeaders.Set("Location", modifiedLocation)
}
refresh := modifiedHeaders.Get("Refresh")
if refresh != "" {
modifiedRefresh := strings.ReplaceAll(refresh, "https://"+toHost, "http://["+fromHost+"]")
//log.Printf("From: %s To: %s\n", refresh, modifiedRefresh)
modifiedHeaders.Set("Refresh", modifiedRefresh)
}
for _, cookie := range response.Cookies() {
cookie.Domain = fromHost
cookie.Secure = false
modifiedHeaders.Add("Set-Cookie", cookie.String())
}
return modifiedHeaders
}
func modifyBody(body []byte) string {
modifiedBody := strings.ReplaceAll(string(body), "https://"+toHost, "http://["+fromHost+"]")
return modifiedBody
}
type tcpKeepAliveListener struct {
*net.TCPListener
}
func (ln tcpKeepAliveListener) Accept() (c net.Conn, err error) {
if c, err = ln.AcceptTCP(); err != nil {
return
}
c.(*net.TCPConn).SetKeepAlive(true)
c.(*net.TCPConn).SetKeepAlivePeriod(3 * time.Minute)
return
}
func ListenAndServe(server *http.Server) error {
addr := server.Addr
if addr == "" {
addr = ":http"
}
ln, err := net.Listen("tcp6", addr)
if err != nil {
return err
}
return server.Serve(tcpKeepAliveListener{ln.(*net.TCPListener)})
}
func commentOut(filename string) error {
// Open the file in read-only mode.
file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()
// Read the file line by line.
lines := []string{}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
// If the line contains "listen [::]:80", comment it out.
if strings.Contains(line, "listen [::]:80") {
if !strings.HasPrefix(line, "#") {
line = "#" + line + " # by cjdns-proxy-server"
}
if !strings.Contains(lines[len(lines)-1], "listen 81;") {
lines = append(lines, " listen 81; # by cjdns-proxy-server")
}
}
lines = append(lines, line)
}
// Check for errors from scanner.
if err := scanner.Err(); err != nil {
return err
}
// Open the file in write mode.
file, err = os.OpenFile(filename, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
if err != nil {
return err
}
defer file.Close()
// Write the modified lines back to the file.
writer := bufio.NewWriter(file)
for _, line := range lines {
_, err := writer.WriteString(line + "\n")
if err != nil {
return err
}
}
return writer.Flush()
}
func modifyNginxConfig() error {
log.Println("Modifying nginx configuration")
files, err := ioutil.ReadDir("/etc/nginx/conf.d/")
if err != nil {
return err
}
var confFiles []string
for _, file := range files {
if strings.HasSuffix(file.Name(), ".conf") {
confFiles = append(confFiles, "/etc/nginx/conf.d/"+file.Name())
}
}
for _, file := range confFiles {
err := commentOut(file)
if err != nil {
return err
}
}
// Restart nginx
log.Println("Restarting nginx...")
cmd := exec.Command("systemctl", "restart", "nginx")
err = cmd.Run()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
// Wait until nginx has restarted
for {
cmd = exec.Command("systemctl", "is-active", "nginx")
output, err := cmd.Output()
if err != nil {
log.Fatalf("cmd.Output() failed with %s\n", err)
}
if strings.TrimSpace(string(output)) == "active" {
break
}
time.Sleep(1 * time.Second)
}
log.Println("Nginx restarted")
return nil
}
func main() {
data, err := os.ReadFile("/etc/hostname")
if err != nil {
log.Fatal("Error reading hostname file:", err)
}
toHost = strings.TrimSpace(string(data))
data, err = os.ReadFile("/var/www/cjdns/cjdroute.conf")
if err != nil {
data, err = os.ReadFile("/etc/cjdroute.conf")
if err != nil {
log.Fatal("Error reading cjdroute.conf file:", err)
}
}
re := regexp.MustCompile(`"ipv6"\s*:\s*"([^"]*)"`)
match := re.FindStringSubmatch(string(data))
if len(match) < 2 {
log.Fatal("Error parsing cjdroute.conf file: IPv6 address not found")
}
fromHost = match[1]
ip := net.ParseIP(fromHost)
if ip == nil {
log.Fatalf("Invalid IP address: %s", fromHost)
}
fromHost = ip.String()
proxyPort = "80"
if toHost == "" || fromHost == "" || proxyPort == "" {
fmt.Println("One or more environment variables are not set. Please set cjdroute.conf and hostname file.")
os.Exit(1)
}
modifyNginxConfig()
// Create a reverse proxy
target, _ := url.Parse("https://127.0.0.1")
proxy := httputil.NewSingleHostReverseProxy(target)
proxy.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
MinVersion: tls.VersionTLS13, // Use TLSv1.3
InsecureSkipVerify: true, // Turn off SSL verification
ServerName: toHost, // Set the SSL server name
},
}
// Modify the request before sending it to the backend
proxy.ModifyResponse = func(response *http.Response) error {
log.Printf("Sending response: %s\n", response.Status)
body, err := io.ReadAll(response.Body)
if err != nil {
log.Println("Error reading response body:", err)
return err
}
response.Body.Close()
modifiedBody := modifyBody(body)
response.Body = io.NopCloser(bytes.NewBufferString(modifiedBody))
response.Header = modifyResponseHeaders(response)
return nil
}
// Set up the HTTP server
server := &http.Server{
Addr: "[" + fromHost + "]:" + proxyPort,
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("Received request: %s %s\n", r.Method, r.URL)
r.Host = toHost
r.Header = modifyRequestHeaders(r)
r.URL.Host = fromHost
r.URL.Scheme = "http"
proxy.ServeHTTP(w, r)
}),
}
// Start the server
log.Println("Starting cjdns proxy server on port", proxyPort)
log.Println("Proxying from", fromHost, "to", toHost)
err = ListenAndServe(server)
if err != nil {
log.Println("Error starting server:", err)
}
}