package protocol import ( "strconv" "strings" ) // The client a participation was opened from (spec §15.28, 2026-10-21): // which app, at which version, so that the people in a call can tell // someone their app is too old to talk to theirs. // ClientHeader carries an app's own " " on every request // its core makes. A browser sends none: its page is the server's own. const ClientHeader = "X-Calls-Client" // ClientNative is the roster's client for an app that did not say which it // is: the core's requests carry Go's default user agent and no // ClientHeader, which is every app from before §15.28. const ClientNative = "native" // MinNativeVersion is the oldest app version that does not break a call // for the people it is in it with (spec §15.28). Before 0.4.2 an app // restarted ICE on every connection on every /ice answer — twelve times // an hour, each one a pause for the screen and camera (§15.21) — and // sent a Mac screen to browsers at a size their H.264 level refused, so // the browser's picture froze for seconds at a time. Everything the wire // needs to recover a connection — `pair` and generations (§4.6), answering // an ICE restart, ignoring keys it does not know — every app has had // since the rename (0.2), and the recovery of §15.28 needs nothing of the // far side but those. const MinNativeVersion = "0.4.2" // maxClientLen bounds the header: "ipad 9999.9999.9999.9999" is 24 bytes. const maxClientLen = 32 // ClientApps are the apps a ClientHeader may name. var ClientApps = []string{"mac", "iphone", "ipad"} // ParseClient splits " " into its two halves, ok only for a // known app and a version of one to four dot-separated numbers of at most // four digits each. "native" parses to ("native", "") with ok true. func ParseClient(s string) (app, version string, ok bool) { if s == ClientNative { return ClientNative, "", true } if len(s) > maxClientLen { return "", "", false } app, version, found := strings.Cut(s, " ") if !found || !validVersion(version) { return "", "", false } for _, a := range ClientApps { if a == app { return app, version, true } } return "", "", false } func validVersion(v string) bool { parts := strings.Split(v, ".") if len(parts) < 1 || len(parts) > 4 { return false } for _, p := range parts { if p == "" || len(p) > 4 { return false } for _, c := range p { if c < '0' || c > '9' { return false } } } return true } // CompareVersions orders two versions of digits and dots numerically, // part by part, a missing part counting as zero: -1, 0 or 1. func CompareVersions(a, b string) int { pa, pb := strings.Split(a, "."), strings.Split(b, ".") for i := 0; i < max(len(pa), len(pb)); i++ { var x, y int if i < len(pa) { x, _ = strconv.Atoi(pa[i]) } if i < len(pb) { y, _ = strconv.Atoi(pb[i]) } if x != y { if x < y { return -1 } return 1 } } return 0 } // ClientFor is the roster's client for a request: the app's own header // when it is well formed, ClientNative for a request from the Go HTTP // client with none (an app from before §15.28), and "" otherwise — a // browser, or a header that is not one this server can read. func ClientFor(header, userAgent string) string { if header != "" { if _, _, ok := ParseClient(header); ok && header != ClientNative { return header } return "" } if strings.HasPrefix(userAgent, "Go-http-client/") { return ClientNative } return "" }