From d2513d2bab29cd9ff058b3a4bb3ba0c38533ea95 Mon Sep 17 00:00:00 2001 From: WeebDataHoarder <57538841+WeebDataHoarder@users.noreply.github.com> Date: Tue, 8 Apr 2025 01:52:27 +0200 Subject: [PATCH] Add h2c support --- cmd/go-away/main.go | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/cmd/go-away/main.go b/cmd/go-away/main.go index 9894c1a..0ad3716 100644 --- a/cmd/go-away/main.go +++ b/cmd/go-away/main.go @@ -11,6 +11,8 @@ import ( "git.gammaspectra.live/git/go-away/lib" "git.gammaspectra.live/git/go-away/lib/policy" "git.gammaspectra.live/git/go-away/utils" + "golang.org/x/net/http2" + "golang.org/x/net/http2/h2c" "gopkg.in/yaml.v3" "log" "log/slog" @@ -79,6 +81,16 @@ func (v *MultiVar) Set(value string) error { return nil } +func newServer(handler http.Handler) *http.Server { + h2s := &http2.Server{} + + h1s := &http.Server{ + Handler: h2c.NewHandler(handler, h2s), + } + + return h1s +} + func main() { bind := flag.String("bind", ":8080", "network address to bind HTTP to") bindNetwork := flag.String("bind-network", "tcp", "network family to bind HTTP to, e.g. unix, tcp") @@ -196,17 +208,15 @@ func main() { go func() { defer wg.Done() - server := http.Server{ - Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - backend, ok := createdBackends[r.Host] - if !ok { - http.Error(w, http.StatusText(http.StatusServiceUnavailable), http.StatusServiceUnavailable) - return - } + server := newServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + backend, ok := createdBackends[r.Host] + if !ok { + http.Error(w, http.StatusText(http.StatusServiceUnavailable), http.StatusServiceUnavailable) + return + } - backend.ServeHTTP(w, r) - }), - } + backend.ServeHTTP(w, r) + })) listener, listenUrl := setupListener(*bindNetwork, *bind, *socketMode) slog.Warn( @@ -259,9 +269,7 @@ func main() { "url", listenUrl, ) - server := http.Server{ - Handler: state, - } + server := newServer(state) if err := server.Serve(listener); !errors.Is(err, http.ErrServerClosed) { log.Fatal(err)