-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxyserver.go
60 lines (52 loc) · 1.44 KB
/
proxyserver.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
package main
import (
"fmt"
"io"
"log"
"net/http"
)
func handleRequest(w http.ResponseWriter, r *http.Request) {
fmt.Println("Request: ", r)
targetURL := r.URL
// Attempting to construct proxy request from initial HTTP Request
proxyRequest, err := http.NewRequest(r.Method, targetURL.String(), r.Body)
if err != nil {
http.Error(w, "Error creating Proxy Server Request: ", http.StatusInternalServerError)
fmt.Println("Error creating Proxy Request")
return
}
// Adding initial HTTP Request headers to Proxy Request headers
for name, values := range r.Header {
for _, value := range values {
proxyRequest.Header.Add(name, value)
}
}
// Sending Server Request
response, err := http.DefaultTransport.RoundTrip(proxyRequest)
if err != nil {
http.Error(w, "Error sending Proxy Server Request: ", http.StatusInternalServerError)
fmt.Println("Error sending Proxy Request")
return
}
defer response.Body.Close()
for name, values := range response.Header {
for _, value := range values {
w.Header().Add(name, value)
}
}
w.WriteHeader(response.StatusCode)
io.Copy(w, response.Body)
}
func main() {
// Creating Server on Port 8080
server := &http.Server{
Addr: ":8080",
Handler: http.HandlerFunc(handleRequest),
}
// Attempting to start Proxy Server
fmt.Println("Starting Proxy Server on Port 8080")
err := server.ListenAndServe()
if err != nil {
log.Fatal("Error attempting to start Proxy Server: ", err)
}
}