96f35bd7aa
- Add Header field to PHT Client and clientConn structs - Support custom headers in authorize, push, and pull requests - Add metadata parsing for header configuration as map[string]string - Enables PHT usage with header-based authentication (e.g., Cloudflare Access) Backwards compatible - headers are optional.
61 lines
1.3 KiB
Go
61 lines
1.3 KiB
Go
package pht
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
mdata "github.com/go-gost/core/metadata"
|
|
mdutil "github.com/go-gost/x/metadata/util"
|
|
)
|
|
|
|
const (
|
|
dialTimeout = "dialTimeout"
|
|
defaultAuthorizePath = "/authorize"
|
|
defaultPushPath = "/push"
|
|
defaultPullPath = "/pull"
|
|
)
|
|
|
|
type metadata struct {
|
|
authorizePath string
|
|
pushPath string
|
|
pullPath string
|
|
host string
|
|
header http.Header
|
|
}
|
|
|
|
func (d *phtDialer) parseMetadata(md mdata.Metadata) (err error) {
|
|
const (
|
|
authorizePath = "authorizePath"
|
|
pushPath = "pushPath"
|
|
pullPath = "pullPath"
|
|
host = "host"
|
|
header = "header"
|
|
)
|
|
|
|
d.md.authorizePath = mdutil.GetString(md, authorizePath)
|
|
if !strings.HasPrefix(d.md.authorizePath, "/") {
|
|
d.md.authorizePath = defaultAuthorizePath
|
|
}
|
|
d.md.pushPath = mdutil.GetString(md, pushPath)
|
|
if !strings.HasPrefix(d.md.pushPath, "/") {
|
|
d.md.pushPath = defaultPushPath
|
|
}
|
|
d.md.pullPath = mdutil.GetString(md, pullPath)
|
|
if !strings.HasPrefix(d.md.pullPath, "/") {
|
|
d.md.pullPath = defaultPullPath
|
|
}
|
|
|
|
d.md.host = mdutil.GetString(md, host)
|
|
|
|
// Parse custom headers
|
|
if m := mdutil.GetStringMapString(md, header); len(m) > 0 {
|
|
h := http.Header{}
|
|
for k, v := range m {
|
|
h.Add(k, v)
|
|
}
|
|
d.md.header = h
|
|
}
|
|
|
|
return
|
|
}
|