From 8ceeb875ebd24d941b326467e88970ec059464f3 Mon Sep 17 00:00:00 2001
From: Garet Halliday <me@garet.holiday>
Date: Fri, 29 Sep 2023 16:54:08 -0500
Subject: [PATCH] use caddy listener

---
 go.mod                          |  1 -
 go.sum                          |  2 --
 lib/gat/gatcaddyfile/gattype.go | 13 ++-----------
 lib/gat/listen.go               | 19 +++++++++++++------
 lib/gat/server.go               |  7 +++++--
 5 files changed, 20 insertions(+), 22 deletions(-)

diff --git a/go.mod b/go.mod
index 25e8915c..706ee3ef 100644
--- a/go.mod
+++ b/go.mod
@@ -7,7 +7,6 @@ require (
 	github.com/caddyserver/caddy/v2 v2.7.4
 	github.com/digitalocean/godo v1.102.1
 	github.com/google/uuid v1.3.0
-	github.com/libp2p/go-reuseport v0.4.0
 	github.com/zalando/postgres-operator v1.8.2
 	go.uber.org/zap v1.25.0
 	google.golang.org/api v0.126.0
diff --git a/go.sum b/go.sum
index 77f5f2b5..91e67319 100644
--- a/go.sum
+++ b/go.sum
@@ -238,8 +238,6 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
 github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
 github.com/libdns/libdns v0.2.1 h1:Wu59T7wSHRgtA0cfxC+n1c/e+O3upJGWytknkmFEDis=
 github.com/libdns/libdns v0.2.1/go.mod h1:yQCXzk1lEZmmCPa857bnk4TsOiqYasqpyOEeSObbb40=
-github.com/libp2p/go-reuseport v0.4.0 h1:nR5KU7hD0WxXCJbmw7r2rhRYruNRl2koHw8fQscQm2s=
-github.com/libp2p/go-reuseport v0.4.0/go.mod h1:ZtI03j/wO5hZVDFo2jKywN6bYKWLOy8Se6DrI2E1cLU=
 github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
 github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
 github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
diff --git a/lib/gat/gatcaddyfile/gattype.go b/lib/gat/gatcaddyfile/gattype.go
index 13452f0d..6800adc0 100644
--- a/lib/gat/gatcaddyfile/gattype.go
+++ b/lib/gat/gatcaddyfile/gattype.go
@@ -68,17 +68,8 @@ func (ServerType) Setup(blocks []caddyfile.ServerBlock, m map[string]any) (*cadd
 
 		server.Listen = make([]gat.ListenerConfig, 0, len(block.Keys))
 		for _, key := range block.Keys {
-			var listen gat.ListenerConfig
-			if strings.HasPrefix(key, "/") {
-				listen = gat.ListenerConfig{
-					Network: "unix",
-					Address: key,
-				}
-			} else {
-				listen = gat.ListenerConfig{
-					Network: "tcp",
-					Address: key,
-				}
+			listen := gat.ListenerConfig{
+				Address: key,
 			}
 			server.Listen = append(server.Listen, listen)
 		}
diff --git a/lib/gat/listen.go b/lib/gat/listen.go
index fb2e718e..f6e81380 100644
--- a/lib/gat/listen.go
+++ b/lib/gat/listen.go
@@ -1,19 +1,18 @@
 package gat
 
 import (
+	"context"
 	"encoding/json"
 	"fmt"
 	"net"
 
 	"github.com/caddyserver/caddy/v2"
-	"github.com/libp2p/go-reuseport"
 	"go.uber.org/zap"
 
 	"gfx.cafe/gfx/pggat/lib/fed"
 )
 
 type ListenerConfig struct {
-	Network string          `json:"network"`
 	Address string          `json:"address"`
 	SSL     json.RawMessage `json:"ssl,omitempty" caddy:"namespace=pggat.ssl.servers inline_key=provider"`
 }
@@ -21,7 +20,8 @@ type ListenerConfig struct {
 type Listener struct {
 	ListenerConfig
 
-	ssl SSLServer
+	networkAddress caddy.NetworkAddress
+	ssl            SSLServer
 
 	listener net.Listener
 
@@ -41,8 +41,15 @@ func (T *Listener) accept() (*fed.Conn, error) {
 func (T *Listener) Provision(ctx caddy.Context) error {
 	T.log = ctx.Logger()
 
+	var err error
+	T.networkAddress, err = caddy.ParseNetworkAddressWithDefaults(T.Address, "tcp", 5432)
+	if err != nil {
+		return fmt.Errorf("parsing address: %v", err)
+	}
+
 	if T.SSL != nil {
-		val, err := ctx.LoadModule(T, "SSL")
+		var val any
+		val, err = ctx.LoadModule(T, "SSL")
 		if err != nil {
 			return fmt.Errorf("loading ssl module: %v", err)
 		}
@@ -53,11 +60,11 @@ func (T *Listener) Provision(ctx caddy.Context) error {
 }
 
 func (T *Listener) Start() error {
-	var err error
-	T.listener, err = reuseport.Listen(T.Network, T.Address)
+	listener, err := T.networkAddress.Listen(context.Background(), 0, net.ListenConfig{})
 	if err != nil {
 		return err
 	}
+	T.listener = listener.(net.Listener)
 
 	T.log.Info("listening", zap.String("address", T.listener.Addr().String()))
 
diff --git a/lib/gat/server.go b/lib/gat/server.go
index 94501b18..f544c89b 100644
--- a/lib/gat/server.go
+++ b/lib/gat/server.go
@@ -173,8 +173,11 @@ func (T *Server) accept(listener *Listener, conn *fed.Conn) {
 func (T *Server) acceptFrom(listener *Listener) bool {
 	conn, err := listener.accept()
 	if err != nil {
-		if errors.Is(err, net.ErrClosed) {
-			return false
+		if netErr, ok := err.(*net.OpError); ok {
+			// why can't they just expose this error
+			if netErr.Err.Error() == "listener 'closed' 😉" {
+				return false
+			}
 		}
 		T.log.Warn("error accepting client", zap.Error(err))
 		return true
-- 
GitLab