| 1 | package protocol |
| 2 | |
| 3 | import ( |
| 4 | "strconv" |
| 5 | "strings" |
| 6 | ) |
| 7 | |
| 8 | // The client a participation was opened from (spec §15.28, 2026-10-21): |
| 9 | // which app, at which version, so that the people in a call can tell |
| 10 | // someone their app is too old to talk to theirs. |
| 11 | |
| 12 | // ClientHeader carries an app's own "<app> <version>" on every request |
| 13 | // its core makes. A browser sends none: its page is the server's own. |
| 14 | const ClientHeader = "X-Calls-Client" |
| 15 | |
| 16 | // ClientNative is the roster's client for an app that did not say which it |
| 17 | // is: the core's requests carry Go's default user agent and no |
| 18 | // ClientHeader, which is every app from before §15.28. |
| 19 | const ClientNative = "native" |
| 20 | |
| 21 | // MinNativeVersion is the oldest app version that does not break a call |
| 22 | // for the people it is in it with (spec §15.28). Before 0.4.2 an app |
| 23 | // restarted ICE on every connection on every /ice answer — twelve times |
| 24 | // an hour, each one a pause for the screen and camera (§15.21) — and |
| 25 | // sent a Mac screen to browsers at a size their H.264 level refused, so |
| 26 | // the browser's picture froze for seconds at a time. Everything the wire |
| 27 | // needs to recover a connection — `pair` and generations (§4.6), answering |
| 28 | // an ICE restart, ignoring keys it does not know — every app has had |
| 29 | // since the rename (0.2), and the recovery of §15.28 needs nothing of the |
| 30 | // far side but those. |
| 31 | const MinNativeVersion = "0.4.2" |
| 32 | |
| 33 | // maxClientLen bounds the header: "ipad 9999.9999.9999.9999" is 24 bytes. |
| 34 | const maxClientLen = 32 |
| 35 | |
| 36 | // ClientApps are the apps a ClientHeader may name. |
| 37 | var ClientApps = []string{"mac", "iphone", "ipad"} |
| 38 | |
| 39 | // ParseClient splits "<app> <version>" into its two halves, ok only for a |
| 40 | // known app and a version of one to four dot-separated numbers of at most |
| 41 | // four digits each. "native" parses to ("native", "") with ok true. |
| 42 | func ParseClient(s string) (app, version string, ok bool) { |
| 43 | if s == ClientNative { |
| 44 | return ClientNative, "", true |
| 45 | } |
| 46 | if len(s) > maxClientLen { |
| 47 | return "", "", false |
| 48 | } |
| 49 | app, version, found := strings.Cut(s, " ") |
| 50 | if !found || !validVersion(version) { |
| 51 | return "", "", false |
| 52 | } |
| 53 | for _, a := range ClientApps { |
| 54 | if a == app { |
| 55 | return app, version, true |
| 56 | } |
| 57 | } |
| 58 | return "", "", false |
| 59 | } |
| 60 | |
| 61 | func validVersion(v string) bool { |
| 62 | parts := strings.Split(v, ".") |
| 63 | if len(parts) < 1 || len(parts) > 4 { |
| 64 | return false |
| 65 | } |
| 66 | for _, p := range parts { |
| 67 | if p == "" || len(p) > 4 { |
| 68 | return false |
| 69 | } |
| 70 | for _, c := range p { |
| 71 | if c < '0' || c > '9' { |
| 72 | return false |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | return true |
| 77 | } |
| 78 | |
| 79 | // CompareVersions orders two versions of digits and dots numerically, |
| 80 | // part by part, a missing part counting as zero: -1, 0 or 1. |
| 81 | func CompareVersions(a, b string) int { |
| 82 | pa, pb := strings.Split(a, "."), strings.Split(b, ".") |
| 83 | for i := 0; i < max(len(pa), len(pb)); i++ { |
| 84 | var x, y int |
| 85 | if i < len(pa) { |
| 86 | x, _ = strconv.Atoi(pa[i]) |
| 87 | } |
| 88 | if i < len(pb) { |
| 89 | y, _ = strconv.Atoi(pb[i]) |
| 90 | } |
| 91 | if x != y { |
| 92 | if x < y { |
| 93 | return -1 |
| 94 | } |
| 95 | return 1 |
| 96 | } |
| 97 | } |
| 98 | return 0 |
| 99 | } |
| 100 | |
| 101 | // ClientFor is the roster's client for a request: the app's own header |
| 102 | // when it is well formed, ClientNative for a request from the Go HTTP |
| 103 | // client with none (an app from before §15.28), and "" otherwise — a |
| 104 | // browser, or a header that is not one this server can read. |
| 105 | func ClientFor(header, userAgent string) string { |
| 106 | if header != "" { |
| 107 | if _, _, ok := ParseClient(header); ok && header != ClientNative { |
| 108 | return header |
| 109 | } |
| 110 | return "" |
| 111 | } |
| 112 | if strings.HasPrefix(userAgent, "Go-http-client/") { |
| 113 | return ClientNative |
| 114 | } |
| 115 | return "" |
| 116 | } |
| 117 | |