Remove duplicate code

This commit is contained in:
Mygod 2023-11-05 10:01:10 -05:00
parent 9976c57edb
commit 226c444d94
2 changed files with 13 additions and 15 deletions

View file

@ -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 {

View file

@ -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,
},
}