-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
115 lines (97 loc) · 2.48 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
package main
import (
"fmt"
"io"
"net"
"net/url"
"strings"
)
func handleConnection(clientConn net.Conn) {
defer clientConn.Close()
buff := make([]byte, 1024)
// Read the initial request from the client (browser)
n, err := clientConn.Read(buff)
if err != nil {
fmt.Println("Error reading from client:", err)
return
}
// Convert the buffer to a string
requestLine := string(buff[:n])
// fmt.Println("Request Line:", requestLine)
// Split the request line
splitStr := strings.Split(requestLine, " ")
// Check if the request is a CONNECT request
if splitStr[0] == "CONNECT" {
// Extract the host:port from the CONNECT request
hostPort := splitStr[1]
// fmt.Println("HostPort:", hostPort)
// Establish a connection to the target server (e.g., alive.github.com:443)
serverConn, err := net.Dial("tcp", hostPort)
if err != nil {
fmt.Println("Error connecting to target server:", err)
return
}
defer serverConn.Close()
// Respond to the client with a successful connection status
_, err = clientConn.Write([]byte("HTTP/1.1 200\r\n\r\n"))
if err != nil {
fmt.Println("Error writing 200 OK to client:", err)
return
}
go io.Copy(serverConn, clientConn)
io.Copy(clientConn, serverConn)
}
rawURL := splitStr[1]
// Parse the URL
parsedURL, err := url.Parse(rawURL)
if err != nil {
fmt.Println("Error parsing URL:", err)
return
}
conn, dialErr := net.Dial("tcp", parsedURL.Host+":http")
if dialErr != nil {
fmt.Println("Error dialing:", dialErr)
return
}
defer conn.Close()
conn.Write(buff[:n])
io.Copy(clientConn, conn)
}
func main() {
// Listen on port 8080 for incoming connections
listen, err := net.Listen("tcp", ":8080")
if err != nil {
fmt.Println("Error starting server:", err)
return
}
defer listen.Close()
fmt.Println("Listening on :8080...")
for {
conn, err := listen.Accept()
if err != nil {
fmt.Println("Error accepting connection:", err)
continue
}
go handleConnection(conn)
}
}
// func manualCopy(dst io.Writer, src io.Reader) error {
// buffer := make([]byte, 32*1024) // 32 KB buffer
// for {
// n, err := src.Read(buffer) // Read into buffer
// if n > 0 {
// fmt.Println("Read", n, "bytes")
// fmt.Println("Buffer:", string(buffer[:n]))
// if _, writeErr := dst.Write(buffer[:n]); writeErr != nil { // Write to destination
// return writeErr
// }
// }
// if err == io.EOF {
// break
// }
// if err != nil {
// return err // Handle read error
// }
// }
// return nil
// }