Try simulating caddy forward_auth

This commit is contained in:
cheddar 2025-02-16 13:51:53 -05:00
parent 1af1d774d3
commit 65934ea570
No known key found for this signature in database
3 changed files with 56 additions and 14 deletions

22
main.go
View file

@ -22,16 +22,18 @@ func main() {
keyPath := flag.String("key", "", "Path to the private (client mode) or public (server mode) to use")
simulateCaddy := flag.Bool("caddy", false, "Simulate caddy reverse proxy")
flag.Parse()
if *useClient {
runClient(keyPath)
runClient(keyPath, *simulateCaddy)
} else {
runServer(keyPath)
runServer(keyPath, *simulateCaddy)
}
}
func runClient(keyFile *string) {
func runClient(keyFile *string, simulateCaddy bool) {
testData := map[string]string{"hello": "world"}
json_data, _ := json.Marshal(testData)
@ -49,7 +51,9 @@ func runClient(keyFile *string) {
id := digest.FromBytes(json_data)
req, err := http.NewRequest("POST", "http://localhost:8080/post", bytes.NewBuffer(json_data))
var req *http.Request
req, err = http.NewRequest("POST", "http://localhost:8080/post", bytes.NewBuffer(json_data))
if err != nil {
log.Fatal(err)
@ -58,6 +62,11 @@ func runClient(keyFile *string) {
req.Header.Add("Content-Digest", string(id.Algorithm())+"="+id.Encoded())
req.Header.Add("Content-Type", "application/json")
if simulateCaddy {
req.Header.Add("X-Forwarded-Method", req.Method)
req.Header.Add("X-Forwarded-Uri", req.RequestURI)
}
resp, err := client.Do(req)
if err != nil {
@ -73,17 +82,18 @@ func runClient(keyFile *string) {
}
fmt.Println(resp.StatusCode)
fmt.Println(resp.Header)
fmt.Println(string(out[:]))
}
func runServer(keyFile *string) {
func runServer(keyFile *string, simulateCaddy bool) {
key, err := loadPublicKey(*keyFile)
if err != nil {
log.Fatal(err)
}
server.Start(key)
server.Start(key, simulateCaddy)
}
func loadPrivateKey(keyFile string) (crypto.PrivateKey, error) {