package agentd import ( "crypto/hmac" "crypto/sha256" "encoding/base64" ) // DeriveHy2Password derives a node-agnostic Hysteria2 password from the opaque // data-plane credential id (dp_uuid). The REALITY inbound uses dp_uuid directly as // the VLESS user uuid; the Hy2 inbound cannot reuse a UUID as a password verbatim // (it must look like an opaque secret), so it is derived from the SAME source — // "password = dp_uuid 同源派生" — via a keyed HMAC. // // The derivation is deterministic given (dp_uuid, key): the control plane runs the // exact same function when it builds the client's connect config (doc/02 §3.1), so // both sides agree without the password ever crossing the agent contract. // // Risk note (carried from task #5): the derivation key is shared material. If a // node is seized the attacker still only sees dp_uuids and this node's key, which // lets them recompute Hy2 passwords for dp_uuids THEY ALREADY HOLD — it does not // reveal other subscribers' credentials or any account identity. Rotating the key // rotates every Hy2 password. When key == "" the password falls back to the raw // dp_uuid (acceptable for dev; production cloud-init always injects a key). func DeriveHy2Password(dpUUID, key string) string { if key == "" { return dpUUID } mac := hmac.New(sha256.New, []byte(key)) mac.Write([]byte(dpUUID)) sum := mac.Sum(nil) // base64url without padding → URL/JSON-safe, 43 chars. return base64.RawURLEncoding.EncodeToString(sum) }