-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproxy.go
99 lines (86 loc) · 2.27 KB
/
proxy.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
package main
import (
"bufio"
"fmt"
"io"
"net"
"net/http"
"strings"
"time"
)
type Proxy struct {
DialTarget string
connected bool
}
func (proxy *Proxy) handleConnection(clientConn net.Conn) {
defer clientConn.Close()
clientReader := bufio.NewReader(clientConn)
// Read the request from the client
request, err := http.ReadRequest(clientReader)
if err != nil {
fmt.Printf("Error reading request: %v\n", err)
return
}
// Filter invalid headers
filterInvalidHeaders(request.Header)
oldpid := pid
// Connect to the destination server
retryonce:
destConn, err := proxy.handleDial()
if err != nil {
if !proxy.connected && oldpid == pid && !processExists(pid) {
fmt.Println("Process died, reinit")
initProcess()
goto retryonce
}
return
}
defer destConn.Close()
// Write the modified request to the destination
err = request.Write(destConn)
if err != nil {
fmt.Printf("Error writing request to destination: %v\n", err)
return
}
// Check if this is a WebSocket upgrade request
if isWebSocketUpgrade(request) {
// Handle WebSocket connection
handleWebSocket(destConn, clientConn)
} else {
// Handle HTTP connection as before
handleHTTP(destConn, clientConn)
}
}
func isWebSocketUpgrade(request *http.Request) bool {
return strings.ToLower(request.Header.Get("Connection")) == "upgrade" &&
strings.ToLower(request.Header.Get("Upgrade")) == "websocket"
}
func handleWebSocket(destConn net.Conn, clientConn net.Conn) {
// Now, simply relay data between client and destination
// Use goroutine to copy from client to destination
go io.Copy(destConn, clientConn)
// Copy from destination to client
io.Copy(clientConn, destConn)
}
func handleHTTP(destConn net.Conn, clientConn net.Conn) {
// Copy the response from the destination to the client
io.Copy(clientConn, destConn)
}
func (proxy *Proxy) handleDial() (destConn net.Conn, err error) {
retries := 0
retry:
destConn, err = net.DialTimeout("tcp", proxy.DialTarget, time.Second)
if err != nil {
if retries < MAX_RETRY && !proxy.connected {
retries += 1
fmt.Printf("Retrying to connect for %d-th time\n", retries)
time.Sleep(WAIT_RETRY)
goto retry
}
fmt.Printf("Error connecting to destination: %v\n", err)
proxy.connected = false
return
}
proxy.connected = true
return
}