Documentation
¶
Index ¶
- Variables
- func AsBool(v interface{}) bool
- func AsInt64(v interface{}) (int64, bool)
- func AsString(v interface{}) string
- func AsStringList(v interface{}) []string
- func B64StrToByte(str string) ([]byte, error)
- func BurnPasswordCheck(plain string)
- func ByteToB64Str(b []byte) string
- func CertIsSelfSigned(pemData string) bool
- func CertPEMFromTLS(tlsConfig map[string]interface{}) string
- func CertPublicKeySha256(pemData string) string
- func CertSha256Hex(pemData string) string
- func CheckPassword(plain, stored string) bool
- func FillOutJson(i *model.Inbound, hostname string) error
- func GenerateTOTPSecret() (string, error)
- func GetExternalLink(url string) string
- func GetExternalSub(url string) ([]map[string]interface{}, error)
- func GetHeaders(client *model.Client, updateInterval int) []string
- func GetOutbound(uri string, i int) (*map[string]interface{}, string, error)
- func GetTlsPing(domain string, port string) (any, error)
- func HashPassword(plain string) (string, error)
- func HostForURI(host string) string
- func IsHashedPassword(stored string) bool
- func IsPlainDirectOutbound(outbound map[string]interface{}) bool
- func JoinRemark(clientRemark, inboundRemark string) string
- func LinkGenerator(clientConfig json.RawMessage, i *model.Inbound, hostname string, ...) []string
- func NormalizeHost(host string) string
- func NormalizePortRanges(v interface{}) []string
- func PortHoppingRanges(v interface{}) string
- func ShadowsocksClientConfigKey(method string) string
- func StrOrBase64AnyEncoded(str string) string
- func StrOrBase64Encoded(str string) string
- func StripServerTlsFields(tls map[string]interface{}) bool
- func TOTPKeyURI(secret, account, issuer string) string
- func ValidateTOTPAfter(secret, code string, after int64) (int64, bool)
- func VersionBefore(a, b string) bool
- type LinkParam
Constants ¶
This section is empty.
Variables ¶
var InboundTypeWithLink = []string{"socks", "http", "mixed", "shadowsocks", "naive", "hysteria", "hysteria2", "anytls", "tuic", "vless", "trojan", "vmess"}
Functions ¶
func AsBool ¶ added in v1.8.2
func AsBool(v interface{}) bool
AsBool reads a value the schema says is a bool, or false.
func AsInt64 ¶ added in v1.8.2
AsInt64 reads a value the schema says is a number, and reports whether it was one -- callers use that to tell an absent field from a zero.
func AsString ¶ added in v1.8.2
func AsString(v interface{}) string
AsString reads a value the schema says is a string, or "".
func AsStringList ¶ added in v1.8.2
func AsStringList(v interface{}) []string
AsStringList reads a value the schema says is a list of strings, skipping anything that is not one rather than asserting element by element.
A bare string counts as a list of one. Most list-shaped sing-box fields -- alpn, ech.config, reality.short_id -- are declared Listable[string], which unmarshals a scalar into a one-element list AND marshals a one-element list back out as a scalar. So `"alpn": "h3"` is valid stored config, it is what sing-box itself writes for a single value, and asserting only the array shape read it as absent.
func B64StrToByte ¶
func BurnPasswordCheck ¶ added in v1.8.2
func BurnPasswordCheck(plain string)
BurnPasswordCheck spends the work CheckPassword would and always fails. Call it on the no-such-user path, where returning early makes an unknown username answer a whole bcrypt round faster than a known one and so enumerable.
func ByteToB64Str ¶
func CertIsSelfSigned ¶ added in v1.5.4
CertIsSelfSigned reports whether the leaf certificate in pemData is self-signed, i.e. its signature verifies against its own public key. Only self-signed certificates should be pinned via certificate_public_key_sha256; CA-signed certificates are validated normally.
func CertPEMFromTLS ¶ added in v1.5.4
func CertPublicKeySha256 ¶ added in v1.5.4
CertPublicKeySha256 returns the base64-encoded SHA256 of the certificate's SubjectPublicKeyInfo (sing-box `certificate_public_key_sha256` / link pinSHA256).
func CertSha256Hex ¶ added in v1.5.4
CertSha256Hex returns the lowercase hex SHA256 of the whole certificate (DER), matching `openssl x509 -fingerprint -sha256` and Clash/mihomo's `fingerprint`.
func CheckPassword ¶ added in v1.5.4
func FillOutJson ¶
Fill Inbound's out_json
func GenerateTOTPSecret ¶ added in v1.7.1
GenerateTOTPSecret returns a fresh base32 shared secret.
func GetExternalLink ¶
func GetExternalSub ¶
func GetTlsPing ¶ added in v1.5.4
GetTlsPing performs a TLS handshake against domain:port (uTLS Chrome hello, no verification -- the point is to fetch whatever certificate is served) and returns the leaf certificate's SPKI SHA256 so the UI can show/pin it.
func HashPassword ¶ added in v1.5.4
func HostForURI ¶ added in v1.8.0
HostForURI returns the host formatted for a URI authority: an IPv6 literal is wrapped in brackets so "host:port" stays parseable, everything else is returned unchanged. Bracketing is idempotent -- the input is normalized first, so an already-bracketed literal does not gain a second pair.
The colon test is a real address parse, not a substring check: an address row's server field takes free text, and something like "example.com:443" contains a colon without being an IPv6 literal. Bracketing that would turn one malformed authority into a different malformed authority; leaving it alone at least keeps the operator's own spelling visible in the link.
func IsHashedPassword ¶ added in v1.5.4
func IsPlainDirectOutbound ¶ added in v1.8.0
IsPlainDirectOutbound reports whether a decoded outbound carries nothing but its identity -- a `direct` outbound with no options of its own.
sing-box refuses a detour to one of those: it would dial exactly what a dialer with no detour dials, and the flag that suppresses the check (DisableEmptyDirectCheck) has no JSON field, so the panel cannot set it. The two spellings mean the same thing, so wherever a detour is written out, such an outbound has to be written as "no detour" instead.
The argument is an outbound in sing-box shape -- type and tag, then the options spread beside them -- which is what both callers hold. A row still in the database is answered without rendering it, by asking whether its options blob is empty; see database.plainDirectOutboundTags.
func JoinRemark ¶ added in v1.8.0
func LinkGenerator ¶
func NormalizeHost ¶ added in v1.8.0
NormalizeHost strips URI brackets from an IPv6 literal ("[::1]" -> "::1"). Bare hosts, IPv4 addresses and domains are returned unchanged. Config formats -- sing-box JSON, Clash YAML, the vmess "add" field -- want the bare form; only a URI authority needs the brackets (#1220).
func NormalizePortRanges ¶ added in v1.8.2
func NormalizePortRanges(v interface{}) []string
NormalizePortRanges applies normalizePortRange to a stored server_ports list, reading either list shape and dropping entries that are not ports.
This is the write-side guard. The read side cannot be the only one: an out_json row reaches the JSON subscription verbatim through getOutbounds, so an operator typing the entirely reasonable "443,20000:30000" into the free text box in OutJson.vue hands every sing-box subscriber a config that parses and then will not start -- no link, no round trip, nothing for the link builders to fix.
func PortHoppingRanges ¶ added in v1.8.2
func PortHoppingRanges(v interface{}) string
PortHoppingRanges renders a stored server_ports list the way every consumer outside sing-box spells it: comma-separated, with a dash between the ends of a range where sing-box writes a colon.
Both consumers want the dash -- mihomo's `ports` and the "mport" query param hysteria and hysteria2 links carry -- and this package's own decoder says so too: linkToJson's hy2 reads mport back through strings.ReplaceAll(..., "-", ":"), which only round-trips a link that was written with dashes. Exported so the Clash converter shares it rather than keeping a second copy that can disagree about the separator, which is exactly how the link half came to emit "20000:30000" while the Clash half emitted "20000-30000" for the same row.
A numeric entry is accepted as well as a string one. Not because sing-box writes one -- server_ports is a Listable[string] there and a JSON number is refused at parse, so a row holding one already fails the JSON subscription before this runs; NormalizePortRanges is what keeps that shape out of the column. It is read here so a row that somehow holds one still renders a link and a Clash proxy rather than an entry silently folded to "".
Both list shapes are read too -- hy2 builds server_ports with a strings.Split, so an external or node-replica link carries []string where a stored row carries []interface{}.
func ShadowsocksClientConfigKey ¶ added in v1.6.3
ShadowsocksClientConfigKey returns the key a client's config blob stores its Shadowsocks secret under, for the given inbound method.
A client config only ever carries two of them (see the frontend's randomConfigs): "shadowsocks16" holds a 16-byte secret and "shadowsocks" a 32-byte one. 2022-blake3-aes-128-gcm is the only method deriving a 128-bit key, so it is the only one that reads the short secret. Everything else reads "shadowsocks" — including 2022-blake3-aes-256-gcm and -chacha20-poly1305, which need the full 32 bytes.
Upstream s-ui routes those two to a third key, "shadowsocks32", that no client config contains; the lookup then yields "" and the inbound ends up with an empty user list. Keep the two-key mapping.
func StrOrBase64AnyEncoded ¶ added in v1.8.2
StrOrBase64AnyEncoded is StrOrBase64Encoded for a string that is *known* to be base64, in whichever of the four forms the writer happened to use.
SIP002 specifies base64url without padding for the ss:// userinfo, which StdEncoding rejects on both counts, so the panel could not read back the links it generates itself. Third-party ss links are base64url too.
Kept separate from StrOrBase64Encoded rather than widening it: that one is also handed text that may not be base64 at all -- an external subscription body, a naive hostname -- and the unpadded alphabets have no padding to fail on, so widening it there would start "decoding" plain strings into garbage.
func StrOrBase64Encoded ¶
Function to return decoded bytes if a string is Base64 encoded
func StripServerTlsFields ¶ added in v1.5.7
StripServerTlsFields removes server-only TLS fields from a client-facing TLS object in place and reports whether anything was removed.
func TOTPKeyURI ¶ added in v1.7.1
TOTPKeyURI builds the otpauth:// URI the enrolment QR code encodes. issuer is repeated in the label as well as the parameter because that is what apps that predate the parameter read, and dropping it there makes every 2S-UI panel show up under the bare account name.
func ValidateTOTPAfter ¶ added in v1.7.1
ValidateTOTPAfter validates a code and rejects replays: after is the counter the last accepted code matched, and anything at or below it is refused (RFC 6238 §5.2). A malformed secret fails closed. The returned counter is what the caller persists, so the same six digits cannot be replayed for the rest of their acceptance window -- which is up to 90 seconds here, long enough for a code read over someone's shoulder to be worth something on its own.
func VersionBefore ¶ added in v1.8.2
VersionBefore reports whether release a is older than release b, comparing dot-separated numbers rather than text.
The lexical compare it replaces got "1.5.10" < "1.5.7" wrong, which is the kind of mistake that silently skips a migration. Missing segments count as zero, so "1.5" and "1.5.0" are the same release, and a segment that is not a number counts as zero too -- this is handed whatever string the database or a release tag happens to hold, and an answer is more useful here than a panic.
It lives in util because two callers need the same ordering and a second copy would drift: cmd/migration gates its one-off repairs on it, and service.shouldInstall decides whether a published release is actually newer than the running binary.