-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cmd/scripts/proxy_protocol_server): Create example HTTP server w…
…ith proxy protocol support
- Loading branch information
1 parent
a48a51f
commit 0a71aaa
Showing
4 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
cmd/scripts/proxy_protocol_server/proxy_protocol_server.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters