From 584c969a7f122f29e6fed923586333ab38cb3d6a Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sun, 23 Mar 2025 00:54:04 +0800 Subject: Basic reverse proxy --- main.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 main.go (limited to 'main.go') diff --git a/main.go b/main.go new file mode 100644 index 0000000..3a0df60 --- /dev/null +++ b/main.go @@ -0,0 +1,33 @@ +package main + +import ( + "io" + "log" + "maps" + "net/http" +) + +var client = http.Client{ + CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse }, +} + +func main() { + log.Fatal(http.ListenAndServe(":8081", http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { + log.Println(request.RemoteAddr, request.RequestURI) + + request.Host = "127.0.0.1:8080" + request.URL.Host = "127.0.0.1:8080" + request.URL.Scheme = "http" + request.RequestURI = "" + + response, err := client.Do(request) + if err != nil { + http.Error(writer, err.Error(), http.StatusBadGateway) + return + } + + maps.Copy(writer.Header(), response.Header) + writer.WriteHeader(response.StatusCode) + io.Copy(writer, response.Body) + }))) +} -- cgit v1.2.3