-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
129 lines (110 loc) · 3 KB
/
server.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"context"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"os/signal"
"strings"
"syscall"
"time"
)
func proxyHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
if r.Method == "OPTIONS" {
return
}
targetURL := r.URL.Query().Get("url")
if targetURL == "" {
http.Error(w, "Missing url parameter", http.StatusBadRequest)
return
}
proxyURL, err := url.Parse(targetURL)
if err != nil {
http.Error(w, "Invalid url parameter", http.StatusBadRequest)
return
}
var req *http.Request
if r.Method == http.MethodPost {
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "GHISA: Failed to read request body", http.StatusInternalServerError)
return
}
req, err = http.NewRequest(http.MethodPost, proxyURL.String(), strings.NewReader(string(body)))
if err != nil {
http.Error(w, "GHISA: Failed to create request", http.StatusInternalServerError)
return
}
req.Header = r.Header
} else {
req, err = http.NewRequest(http.MethodGet, proxyURL.String(), nil)
if err != nil {
http.Error(w, "GHISA: Failed to create request", http.StatusInternalServerError)
return
}
req.Header = r.Header
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
http.Error(w, "GHISA: Failed to make request", http.StatusInternalServerError)
return
}
defer resp.Body.Close()
for key, values := range resp.Header {
for _, value := range values {
w.Header().Add(key, value)
}
}
w.WriteHeader(resp.StatusCode)
body, err := io.ReadAll(resp.Body)
if err != nil {
http.Error(w, "GHISA: Failed to read response body", http.StatusInternalServerError)
return
}
w.Write(body)
}
func healthHandler(w http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
if req.URL.Path != "/health" {
http.NotFound(w, req)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Server is healthy")
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", proxyHandler)
mux.HandleFunc("/health", healthHandler)
server := &http.Server{
Addr: ":5552",
Handler: mux,
ReadHeaderTimeout: 5 * time.Second,
}
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, syscall.SIGTERM)
go func() {
<-quit
log.Println("GHISA: Shutting down server...")
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Fatalf("GHISA: Could not gracefully shut down server: %v\n", err)
}
log.Println("GHISA server stopped")
}()
log.Println("GHISA: proxy server is running on port 5552...")
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("GHISA: Could not listen on port 5552: %v\n", err)
}
}