-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
75 lines (66 loc) · 1.69 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
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"log"
"net"
"net/http"
)
func main() {
// Start the server
fmt.Println("Starting the server")
listener, err := net.Listen("tcp", ":8080")
if err != nil {
log.Fatalf("Failed to start TCP Server: %v", err)
}
defer listener.Close()
log.Println("TCP Server started on port 8080")
// Accept incoming connections
for {
conn, err := listener.Accept()
if err != nil {
log.Printf("Failed to accept incoming connection: %v", err)
continue
}
go handleConnection(conn)
//go handleConnection2(conn)
}
}
func handleConnection(conn net.Conn) {
defer conn.Close()
// Read HTTP request from the connection
request, err := http.ReadRequest(bufio.NewReader(conn))
if err != nil {
log.Printf("Failed to read HTTP request: %v", err)
return
}
// Extract necessary information from the HTTP request
method := request.Method
url := request.URL.String()
headers := request.Header
body, err := io.ReadAll(request.Body)
if err != nil {
log.Printf("Failed to read request body: %v", err)
return
}
// Create a new HTTP request based on the extracted information
newRequest, err := http.NewRequest(method, url, bytes.NewReader(body))
if err != nil {
log.Printf("Failed to create new HTTP request: %v", err)
return
}
newRequest.Header = headers
// Forward the new HTTP request to the target server
response, err := http.DefaultClient.Do(newRequest)
if err != nil {
log.Printf("Failed to forward request to target server: %v", err)
return
}
defer response.Body.Close()
// Handle the response from the target server
// You can forward the response back to the client or process it as needed
}
func handleConnection2(conn net.Conn) {
}