mirror of
https://github.com/hanzoai/oxy.git
synced 2026-09-30 23:42:31 +00:00
40 lines
825 B
Go
40 lines
825 B
Go
package stickycookie
|
|
|
|
import (
|
|
"net/url"
|
|
"strconv"
|
|
|
|
"github.com/segmentio/fasthash/fnv1a"
|
|
)
|
|
|
|
// HashValue manages hashed sticky value.
|
|
type HashValue struct {
|
|
// Salt secret to anonymize the hashed cookie
|
|
Salt string
|
|
}
|
|
|
|
// Get hashes the sticky value.
|
|
func (v *HashValue) Get(raw *url.URL) string {
|
|
return v.hash(raw.String())
|
|
}
|
|
|
|
// FindURL gets url from array that match the value.
|
|
func (v *HashValue) FindURL(raw string, urls []*url.URL) (*url.URL, error) {
|
|
for _, u := range urls {
|
|
if raw == v.hash(normalized(u)) {
|
|
return u, nil
|
|
}
|
|
}
|
|
|
|
return nil, nil
|
|
}
|
|
|
|
func (v *HashValue) hash(input string) string {
|
|
return strconv.FormatUint(fnv1a.HashString64(v.Salt+input), 16)
|
|
}
|
|
|
|
func normalized(u *url.URL) string {
|
|
normalized := url.URL{Scheme: u.Scheme, Host: u.Host, Path: u.Path}
|
|
return normalized.String()
|
|
}
|