From 226c444d94efd61da5f5ab24eb8c123c36e263d9 Mon Sep 17 00:00:00 2001 From: Mygod Date: Sun, 5 Nov 2023 10:01:10 -0500 Subject: [PATCH] Remove duplicate code --- caddyfile.go | 13 +++++++++---- common_test.go | 15 ++++----------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/caddyfile.go b/caddyfile.go index bf0fcf7..87dde9e 100644 --- a/caddyfile.go +++ b/caddyfile.go @@ -22,6 +22,14 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) return &fp, err } +// EncodeAuthCredentials base64-encode credentials +func EncodeAuthCredentials(user, pass string) (result []byte) { + raw := []byte(user + ":" + pass) + result = make([]byte, base64.StdEncoding.EncodedLen(len(raw))) + base64.StdEncoding.Encode(result, raw) + return +} + // UnmarshalCaddyfile unmarshals Caddyfile tokens into h. func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { if !d.Next() { @@ -49,10 +57,7 @@ func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { if h.AuthCredentials == nil { h.AuthCredentials = [][]byte{} } - // base64-encode credentials - buf := make([]byte, base64.StdEncoding.EncodedLen(len(args[0])+1+len(args[1]))) - base64.StdEncoding.Encode(buf, []byte(args[0]+":"+args[1])) - h.AuthCredentials = append(h.AuthCredentials, buf) + h.AuthCredentials = append(h.AuthCredentials, EncodeAuthCredentials(args[0], args[1])) h.AuthRequired = true case "hosts": if len(args) == 0 { diff --git a/common_test.go b/common_test.go index 97bd277..18ce832 100644 --- a/common_test.go +++ b/common_test.go @@ -3,7 +3,6 @@ package forwardproxy import ( "context" "crypto/tls" - "encoding/base64" "encoding/hex" "encoding/json" "fmt" @@ -197,9 +196,6 @@ func TestMain(m *testing.M) { }, } - buf := make([]byte, base64.StdEncoding.EncodedLen(9)) - base64.StdEncoding.Encode(buf, []byte("test:pass")) - caddyForwardProxyAuth = caddyTestServer{ addr: "127.0.0.1:4891", root: "./test/forwardproxy", @@ -207,7 +203,7 @@ func TestMain(m *testing.M) { proxyHandler: &Handler{ PACPath: defaultPACPath, ACL: []ACLRule{{Subjects: []string{"all"}, Allow: true}}, - AuthCredentials: [][]byte{buf}, + AuthCredentials: [][]byte{EncodeAuthCredentials("test", "pass")}, AuthRequired: true, }, } @@ -218,7 +214,7 @@ func TestMain(m *testing.M) { proxyHandler: &Handler{ PACPath: defaultPACPath, ACL: []ACLRule{{Subjects: []string{"all"}, Allow: true}}, - AuthCredentials: [][]byte{buf}, + AuthCredentials: [][]byte{EncodeAuthCredentials("test", "pass")}, AuthRequired: true, }, } @@ -231,7 +227,7 @@ func TestMain(m *testing.M) { PACPath: "/superhiddenfile.pac", ACL: []ACLRule{{Subjects: []string{"all"}, Allow: true}}, ProbeResistance: &ProbeResistance{Domain: "test.localhost"}, - AuthCredentials: [][]byte{buf}, + AuthCredentials: [][]byte{EncodeAuthCredentials("test", "pass")}, AuthRequired: true, }, httpRedirPort: "8880", @@ -254,16 +250,13 @@ func TestMain(m *testing.M) { root: "./test/index", } - upstreamBuf := make([]byte, base64.StdEncoding.EncodedLen(25)) - base64.StdEncoding.Encode(upstreamBuf, []byte("upstreamtest:upstreampass")) - caddyAuthedUpstreamEnter = caddyTestServer{ addr: "127.0.65.25:6585", root: "./test/upstreamingproxy", tls: true, proxyHandler: &Handler{ Upstream: "https://test:pass@127.0.0.1:4891", - AuthCredentials: [][]byte{upstreamBuf}, + AuthCredentials: [][]byte{EncodeAuthCredentials("upstreamtest", "upstreampass")}, AuthRequired: true, }, }