From 6ec21799314e78b9828bdf4eaf156d94b6c7853b Mon Sep 17 00:00:00 2001 From: a Date: Fri, 8 Jul 2022 00:20:57 -0500 Subject: [PATCH] ok --- app/go.mod | 7 ++++ app/go.sum | 6 ++++ app/main.go | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++ vite.config.ts | 1 - 4 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 app/go.mod create mode 100644 app/go.sum create mode 100644 app/main.go diff --git a/app/go.mod b/app/go.mod new file mode 100644 index 0000000..9fe25d7 --- /dev/null +++ b/app/go.mod @@ -0,0 +1,7 @@ +module app + +go 1.18 + +require gfx.cafe/open/gun v0.0.3 + +require gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/app/go.sum b/app/go.sum new file mode 100644 index 0000000..2d01311 --- /dev/null +++ b/app/go.sum @@ -0,0 +1,6 @@ +gfx.cafe/open/gun v0.0.3 h1:gHED0923ITIb+jD/8+g6sahK/7w4E722p9QwnYOeUmU= +gfx.cafe/open/gun v0.0.3/go.mod h1:bz41aOGPVKhSRXOEvaNcgkL7fjrl4XZPdy/VybZbKoM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/app/main.go b/app/main.go new file mode 100644 index 0000000..0c7d506 --- /dev/null +++ b/app/main.go @@ -0,0 +1,94 @@ +package main + +import ( + "io" + "log" + "net" + "net/http" + "strings" + + "gfx.cafe/open/gun" +) + +var Config struct { + Port string + Root string +} + +func init() { + gun.Load(&Config) + if Config.Port == "" { + Config.Port = "8080" + } + if Config.Root == "" { + Config.Root = "./dist" + } +} + +var hopHeaders = []string{ + "Connection", + "Keep-Alive", + "Proxy-Authenticate", + "Proxy-Authorization", + "Te", + "Trailers", + "Transfer-Encoding", + "Upgrade", +} + +func appendHostToXForwardHeader(header http.Header, host string) { + if prior, ok := header["X-Forwarded-For"]; ok { + host = strings.Join(prior, ", ") + ", " + host + } + header.Set("X-Forwarded-For", host) +} + +func copyHeader(dst, src http.Header) { + for k, vv := range src { + for _, v := range vv { + dst.Add(k, v) + } + } +} + +func delHopHeaders(header http.Header) { + for _, h := range hopHeaders { + header.Del(h) + } +} + +type Handler struct { + c http.Client +} + +func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + if strings.HasPrefix(strings.TrimPrefix("/", r.URL.Path), "lifeto") { + h.handleProxy(w, r) + return + } + h.handleProxy(w, r) +} +func (h *Handler) handleProxy(w http.ResponseWriter, r *http.Request) { + r.RequestURI = "" + if clientIP, _, err := net.SplitHostPort(r.RemoteAddr); err == nil { + appendHostToXForwardHeader(r.Header, clientIP) + } + resp, err := h.c.Do(r) + if err != nil { + http.Error(w, "Server Error", http.StatusInternalServerError) + log.Println("ServeHTTP:", err) + } + defer resp.Body.Close() + delHopHeaders(resp.Header) + copyHeader(w.Header(), resp.Header) + w.WriteHeader(resp.StatusCode) + io.Copy(w, resp.Body) +} + +func (h *Handler) handleSite(w http.ResponseWriter, r *http.Request) { + http.StripPrefix(Config.Root, http.FileServer(http.Dir(Config.Root))).ServeHTTP(w, r) +} + +func main() { + http.ListenAndServe(":"+Config.Port, &Handler{}) +} diff --git a/vite.config.ts b/vite.config.ts index 81ed4f1..9a21193 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -5,7 +5,6 @@ import vue from '@vitejs/plugin-vue' export default defineConfig({ plugins: [vue()], server: { - https: true, proxy: { // with options '/lifeto': {