Skip to content

Commit

Permalink
feat(cmd/scripts/proxy_protocol_server): Create example HTTP server w…
Browse files Browse the repository at this point in the history
…ith proxy protocol support
  • Loading branch information
ondrejsika committed Nov 6, 2024
1 parent a48a51f commit 0a71aaa
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ import (
_ "github.com/sikalabs/slu/cmd/scripts/kubernetes/install_prometheus_operator_crd"
_ "github.com/sikalabs/slu/cmd/scripts/nginx_log_generator"
_ "github.com/sikalabs/slu/cmd/scripts/podcast_rss_download"
_ "github.com/sikalabs/slu/cmd/scripts/proxy_protocol_server"
_ "github.com/sikalabs/slu/cmd/scripts/rename"
_ "github.com/sikalabs/slu/cmd/scripts/rename/spaces_and_commas"
_ "github.com/sikalabs/slu/cmd/scripts/run_tcp_proxy_in_docker"
Expand Down
59 changes: 59 additions & 0 deletions cmd/scripts/proxy_protocol_server/proxy_protocol_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package proxy_protocol_server

import (
"fmt"
"log"

parent_cmd "github.com/sikalabs/slu/cmd/scripts"
"github.com/spf13/cobra"

"net"
"net/http"
"time"

"github.com/pires/go-proxyproto"
h2proxy "github.com/pires/go-proxyproto/helper/http2"
)

var Cmd = &cobra.Command{
Use: "proxy-protocol-server",
Short: "Start Proxy Protocol HTTP Server on port 8000",
Run: func(c *cobra.Command, args []string) {
server()
},
}

func init() {
parent_cmd.Cmd.AddCommand(Cmd)
}

func server() {
server := http.Server{
Addr: ":8000",
ConnState: func(c net.Conn, s http.ConnState) {
if s == http.StateNew {
log.Printf("[ConnState] %s -> %s\n", c.LocalAddr().String(), c.RemoteAddr().String())
}
},
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("[Handler] remote ip %q\n", r.RemoteAddr)
w.Write([]byte(fmt.Sprintf("remote ip %q\n", r.RemoteAddr)))
}),
}

ln, err := net.Listen("tcp", server.Addr)
if err != nil {
panic(err)
}

proxyListener := &proxyproto.Listener{
Listener: ln,
ReadHeaderTimeout: 10 * time.Second,
}
defer proxyListener.Close()

// Create an HTTP server which can handle proxied incoming connections for
// both HTTP/1 and HTTP/2. HTTP/2 support relies on TLS ALPN, the reverse
// proxy needs to be configured to accept "h2".
h2proxy.NewServer(&server, nil).Serve(proxyListener)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ require (
github.com/olekukonko/tablewriter v0.0.5
github.com/ondrejsika/go-dela v1.1.0
github.com/ondrejsika/go-iceland v0.1.0
github.com/pires/go-proxyproto v0.8.0
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
github.com/prometheus/client_golang v1.20.4
github.com/qeesung/image2ascii v1.0.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1158,6 +1158,8 @@ github.com/peterbourgon/diskv v2.0.1+incompatible h1:UBdAOUP5p4RWqPBg048CAvpKN+v
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4=
github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8=
github.com/pires/go-proxyproto v0.8.0 h1:5unRmEAPbHXHuLjDg01CxJWf91cw3lKHc/0xzKpXEe0=
github.com/pires/go-proxyproto v0.8.0/go.mod h1:iknsfgnH8EkjrMeMyvfKByp9TiBZCKZM0jx2xmKqnVY=
github.com/pjbgf/sha1cd v0.3.0 h1:4D5XXmUUBUl/xQ6IjCkEAbqXskkq/4O7LmGn0AqMDs4=
github.com/pjbgf/sha1cd v0.3.0/go.mod h1:nZ1rrWOcGJ5uZgEEVL1VUM9iRQiZvWdbZjkKyFzPPsI=
github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4/go.mod h1:N6UoU20jOqggOuDwUaBQpluzLNDqif3kq9z2wpdYEfQ=
Expand Down

0 comments on commit 0a71aaa

Please sign in to comment.