Waiting feedback and tooltips now share consistent presentation lifecycles, while App shutdown and native-media operation failures stay within their owning session or output. This phase also consolidates the project standards and publishes searchable English/Chinese reader documentation alongside the homepage. Validation: repository hygiene and Markdown links; TypeScript, 971 Web tests and production build; website/documentation build; Go tests, vet and Windows/Linux builds with Windows capture checks; desktop/mobile UI and documentation review. Linux race, container and three-platform packaging checks run in the release pipeline. Physical macOS/Linux capture and broader network coverage remain outside this acceptance. The owner approved v1.4.0 publication. Existing empty-source, Windows URL-handler and public-invitation timeout reports remain tracked; this release makes no claim to resolve those device/network reports. The Windows capture-border request is next-phase work. ## Release notes ### 中文 这是一次以稳定性和交互一致性为重点的功能更新。等待与提示更有反馈,App 启动、原生分享和连接恢复的边界处理得到改善,使用与部署指南也有了统一的文档中心。 #### 新增与改进 - 欢迎语、等待短句和六类可轮换的标签页标题共用一致的显示规则。等待短句从一开始就出现,并在停留时轮换;明确的错误与操作提示保持固定。 - 房间准入等漫画采用更克制的小人物与循环动效。改进 tooltip 的定位与指向,减少对播放器的遮挡;状态、图标和文案保持相同含义。 - 新增中英文文档中心,提供搜索、手机目录、使用教程、问题排查与自部署指南。官网 FAQ 聚焦开始使用前的问题,详细排查统一进入文档中心。 #### 修复 - 使用 `--local` 启动不再清空已保存的 Piik 站点地址。 - 打开系统浏览器不再阻塞 App 的退出流程;改进临时公网邀请、终端和诊断导出在关闭时的清理及错误反馈。 - 单条原生媒体连接的操作失败不再被误判为整个会话的协议错误;重复停止分享保持幂等。 - 修复连接替换、转发停止和异常退出时的部分资源清理问题,避免迟到结果影响当前连接。 - Windows 原生采集中,单个输出的编码错误只结束对应输出;共享采集源本身的错误仍会结束分享。 #### 升级与已知边界 本次保持现有协议版本与配置格式。更新 App 或自托管 Server 后,重新打开页面即可使用对应的新界面。 个别设备的空窗口列表、Windows 浏览器启动异常,以及 Linux 编码输出的故障隔离仍在跟进。macOS/Linux 的物理采集和更广泛的公网网络场景仍需实机验证;本次不宣称这些问题已经解决,也不承诺所有网络都能直连。 ### English This feature update focuses on stability and consistent feedback. Waiting states are easier to follow, App startup and native sharing handle several failure cases more reliably, and the new documentation center brings usage and deployment guides together. #### New and improved - Welcome lines, waiting captions and six groups of rotating tab titles follow one presentation lifecycle. Waiting captions appear immediately and rotate while visible; actionable errors and instructions remain fixed. - Room-admission comics use restrained characters and looping motion. Tooltips point more accurately to their controls and interfere less with the player, with consistent meanings across illustrations, icons and copy. - A new English/Chinese documentation center includes search, mobile navigation, usage guides, troubleshooting and self-hosting. The homepage FAQ covers questions before getting started and links to the documentation for detailed help. #### Fixes - Starting with `--local` no longer clears a saved Piik site address. - Opening the system browser no longer blocks App shutdown. Temporary public invitations, the terminal and diagnostic exports handle cleanup and exit errors more consistently. - An operation failure on one native media connection no longer becomes a protocol failure for the entire session. Repeated stop-sharing requests are safe. - Fixed cleanup cases during connection replacement, relay shutdown and unexpected exits, preventing late results from affecting the current connection. - On Windows, an encoder failure for one native output stops that output only. A failure of the shared capture source still ends sharing. #### Updating and known limits Protocol versions and configuration formats are unchanged. After updating the App or your self-hosted Server, reopen the page to use its updated interface. Empty source lists on some devices, Windows browser-launch failures and Linux output-failure isolation remain under investigation. Physical macOS/Linux capture and broader public-network coverage still need device testing; this update does not claim to resolve those cases or guarantee a direct connection on every network.
385 lines
11 KiB
Go
385 lines
11 KiB
Go
package protocol
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"net/url"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
"unicode"
|
|
"unicode/utf8"
|
|
|
|
"golang.org/x/text/unicode/norm"
|
|
)
|
|
|
|
// UTF16Length counts UTF-16 code units, which is what zod's string .min/.max/
|
|
// .length and String.prototype.length measure: not bytes, not code points.
|
|
// It is the single owner of that measurement for the server packages.
|
|
func UTF16Length(value string) int {
|
|
length := 0
|
|
for _, character := range value {
|
|
if character > 0xffff {
|
|
length += 2
|
|
} else {
|
|
length++
|
|
}
|
|
}
|
|
return length
|
|
}
|
|
|
|
// IsJSWhitespace matches the JavaScript regular-expression \s class, which is
|
|
// also what String.prototype.trim and Number(string) strip. It is the single
|
|
// owner of that predicate for the server packages.
|
|
func IsJSWhitespace(character rune) bool {
|
|
switch character {
|
|
case '\t', '\n', '\v', '\f', '\r', ' ',
|
|
0x00a0, 0x1680, 0x2028, 0x2029, 0x202f, 0x205f, 0x3000, 0xfeff:
|
|
return true
|
|
}
|
|
return character >= 0x2000 && character <= 0x200a
|
|
}
|
|
|
|
var (
|
|
opaqueIDPattern = regexp.MustCompile(`^[A-Za-z0-9_-]+$`)
|
|
roomCodePattern = regexp.MustCompile(`^[1-9][0-9]*$`)
|
|
viewerGrantPattern = regexp.MustCompile(`^[A-Za-z0-9_-]{21}[AQgw]$`)
|
|
viewerPasswordPattern = regexp.MustCompile(`^[\x21-\x7e]+$`)
|
|
)
|
|
|
|
// ValidOpaqueID mirrors opaqueIdSchema: 8..128 code units of [A-Za-z0-9_-].
|
|
func ValidOpaqueID(value string) bool {
|
|
length := UTF16Length(value)
|
|
return length >= 8 && length <= 128 && opaqueIDPattern.MatchString(value)
|
|
}
|
|
|
|
// ValidToken mirrors tokenSchema: 32..128 code units of [A-Za-z0-9_-].
|
|
func ValidToken(value string) bool {
|
|
length := UTF16Length(value)
|
|
return length >= 32 && length <= 128 && opaqueIDPattern.MatchString(value)
|
|
}
|
|
|
|
// ValidRoomCode mirrors roomCodeSchema.
|
|
func ValidRoomCode(value string) bool {
|
|
return UTF16Length(value) == RoomCodeLength && roomCodePattern.MatchString(value)
|
|
}
|
|
|
|
// ValidViewerGrant mirrors viewerGrantSchema.
|
|
func ValidViewerGrant(value string) bool {
|
|
return UTF16Length(value) == 22 && viewerGrantPattern.MatchString(value)
|
|
}
|
|
|
|
// ValidViewerPassword mirrors viewerPasswordSchema.
|
|
func ValidViewerPassword(value string) bool {
|
|
length := UTF16Length(value)
|
|
return length >= MinViewerPasswordLength &&
|
|
length <= MaxViewerPasswordLength &&
|
|
viewerPasswordPattern.MatchString(value)
|
|
}
|
|
|
|
// forbiddenDisplayNameCharacters is FORBIDDEN_DISPLAY_NAME_CHARACTERS from
|
|
// src/shared/protocol.ts.
|
|
var forbiddenDisplayNameCharacters = regexp.MustCompile(
|
|
`[\p{Cc}\p{Zl}\p{Zp}\x{061c}\x{200b}\x{200e}\x{200f}\x{202a}-\x{202e}` +
|
|
`\x{2060}\x{2066}-\x{2069}\x{feff}]`)
|
|
|
|
// NormalizeDisplayName returns false where the shared TypeScript normalizer returns null.
|
|
func NormalizeDisplayName(value string) (string, bool) {
|
|
if forbiddenDisplayNameCharacters.MatchString(value) {
|
|
return "", false
|
|
}
|
|
// The TypeScript rejects code points in D800..DFFF, which a JavaScript
|
|
// string can hold as an unpaired surrogate. A Go string can only carry one
|
|
// as invalid UTF-8 (WTF-8), so utf8.ValidString is the exact analogue.
|
|
if !utf8.ValidString(value) {
|
|
return "", false
|
|
}
|
|
|
|
normalized := collapseUnicodeSpaces(
|
|
strings.TrimFunc(norm.NFC.String(value), isUnicodeSpaceSeparator))
|
|
count := utf8.RuneCountInString(normalized)
|
|
if count < 1 || count > MaxDisplayNameCodePoints {
|
|
return "", false
|
|
}
|
|
return normalized, true
|
|
}
|
|
|
|
// isUnicodeSpaceSeparator matches \p{Zs}. Every other character trimmed by
|
|
// JavaScript's String.prototype.trim (Cc, Zl, Zp, FEFF) is already forbidden
|
|
// above, so trimming Zs alone is equivalent here.
|
|
func isUnicodeSpaceSeparator(character rune) bool {
|
|
return unicode.Is(unicode.Zs, character)
|
|
}
|
|
|
|
// collapseUnicodeSpaces implements .replace(/\p{Zs}+/gu, " ").
|
|
func collapseUnicodeSpaces(value string) string {
|
|
var builder strings.Builder
|
|
pendingSpace := false
|
|
for _, character := range value {
|
|
if isUnicodeSpaceSeparator(character) {
|
|
pendingSpace = true
|
|
continue
|
|
}
|
|
if pendingSpace {
|
|
builder.WriteByte(' ')
|
|
pendingSpace = false
|
|
}
|
|
builder.WriteRune(character)
|
|
}
|
|
if pendingSpace {
|
|
builder.WriteByte(' ')
|
|
}
|
|
return builder.String()
|
|
}
|
|
|
|
// DisplayName mirrors displayNameSchema: 1..96 UTF-16 code units that
|
|
// normalizeDisplayName leaves unchanged.
|
|
type DisplayName string
|
|
|
|
// UnmarshalJSON implements json.Unmarshaler.
|
|
func (d *DisplayName) UnmarshalJSON(data []byte) error {
|
|
// encoding/json folds an unpaired \uD800-\uDFFF escape to U+FFFD, so the
|
|
// raw token is the only place the TypeScript surrogate rejection is still
|
|
// observable.
|
|
if hasUnpairedSurrogateEscape(data) {
|
|
return errors.New("displayName contains an unpaired surrogate")
|
|
}
|
|
var value string
|
|
if err := json.Unmarshal(data, &value); err != nil {
|
|
return err
|
|
}
|
|
if !ValidDisplayName(value) {
|
|
return errors.New("display name must be canonical")
|
|
}
|
|
*d = DisplayName(value)
|
|
return nil
|
|
}
|
|
|
|
// ValidDisplayName mirrors displayNameSchema's bounds and canonical refine.
|
|
func ValidDisplayName(value string) bool {
|
|
length := UTF16Length(value)
|
|
if length < 1 || length > MaxDisplayNameCodePoints*4 {
|
|
return false
|
|
}
|
|
normalized, ok := NormalizeDisplayName(value)
|
|
return ok && normalized == value
|
|
}
|
|
|
|
func hasUnpairedSurrogateEscape(token []byte) bool {
|
|
text := string(token)
|
|
for index := 0; index+6 <= len(text); index++ {
|
|
if text[index] != '\\' {
|
|
continue
|
|
}
|
|
if text[index+1] != 'u' {
|
|
// Consume the whole escape, including \\, before looking for \u.
|
|
index++
|
|
continue
|
|
}
|
|
unit, err := strconv.ParseUint(text[index+2:index+6], 16, 32)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
switch {
|
|
case unit >= 0xd800 && unit <= 0xdbff:
|
|
if index+12 > len(text) || text[index+6] != '\\' || text[index+7] != 'u' {
|
|
return true
|
|
}
|
|
low, err := strconv.ParseUint(text[index+8:index+12], 16, 32)
|
|
if err != nil || low < 0xdc00 || low > 0xdfff {
|
|
return true
|
|
}
|
|
index += 11
|
|
case unit >= 0xdc00 && unit <= 0xdfff:
|
|
return true
|
|
default:
|
|
index += 5
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// ValidStunURL ports isValidStunUrl from src/shared/protocol.ts. The
|
|
// TypeScript feeds "http://<authority>" to the WHATWG URL parser; net/url is
|
|
// more permissive in several places, so the extra checks below restore the
|
|
// WHATWG answers (see stun_test.go for the recorded comparisons).
|
|
func ValidStunURL(value string) bool {
|
|
separator := strings.IndexByte(value, ':')
|
|
if separator <= 0 || !asciiEqualFold(value[:separator], "stun") {
|
|
return false
|
|
}
|
|
|
|
authority := value[separator+1:]
|
|
if authority == "" || strings.HasSuffix(authority, ":") {
|
|
return false
|
|
}
|
|
for _, character := range authority {
|
|
if character == '\\' || character == '/' || character == '?' ||
|
|
character == '#' || IsJSWhitespace(character) {
|
|
return false
|
|
}
|
|
}
|
|
|
|
parsed, err := url.Parse("http://" + authority)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
// WHATWG reports an empty username/password for "@host" and ":@host";
|
|
// net/url reports a non-nil Userinfo, so compare the parts, not the pointer.
|
|
if parsed.User != nil {
|
|
if parsed.User.Username() != "" {
|
|
return false
|
|
}
|
|
if password, _ := parsed.User.Password(); password != "" {
|
|
return false
|
|
}
|
|
}
|
|
if hostname := parsed.Hostname(); hostname == "" || !validWHATWGHost(parsed.Host, hostname) {
|
|
return false
|
|
}
|
|
if port := parsed.Port(); port != "" {
|
|
// net/url accepts any digit run; WHATWG fails above 65535.
|
|
number, err := strconv.Atoi(port)
|
|
if err != nil || number <= 0 || number > 65535 {
|
|
return false
|
|
}
|
|
}
|
|
// net/url leaves Path empty where WHATWG reports "/". The authority scan
|
|
// above already rejected "/", "?" and "#", so these are belt and braces.
|
|
return (parsed.Path == "" || parsed.Path == "/") &&
|
|
parsed.RawQuery == "" && parsed.Fragment == ""
|
|
}
|
|
|
|
func asciiEqualFold(left, right string) bool {
|
|
if len(left) != len(right) {
|
|
return false
|
|
}
|
|
for index := 0; index < len(left); index++ {
|
|
if asciiLower(left[index]) != asciiLower(right[index]) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func asciiLower(character byte) byte {
|
|
if character >= 'A' && character <= 'Z' {
|
|
return character + 'a' - 'A'
|
|
}
|
|
return character
|
|
}
|
|
|
|
// forbiddenDomainCodePoints is the WHATWG "forbidden domain code point" set
|
|
// minus the characters the authority scan already rejected.
|
|
const forbiddenDomainCodePoints = " #/:<>?@[\\]^|%"
|
|
|
|
func validWHATWGHost(rawHost, hostname string) bool {
|
|
if strings.HasPrefix(rawHost, "[") {
|
|
// net/url accepts "[1.2.3.4]" and IPv6 zone identifiers; the WHATWG
|
|
// IPv6 parser accepts neither.
|
|
return strings.Contains(hostname, ":") && !strings.Contains(hostname, "%")
|
|
}
|
|
for _, character := range hostname {
|
|
if character <= 0x1f || character == 0x7f ||
|
|
strings.ContainsRune(forbiddenDomainCodePoints, character) {
|
|
return false
|
|
}
|
|
}
|
|
if hostEndsInNumber(hostname) {
|
|
return validIPv4Host(hostname)
|
|
}
|
|
return true
|
|
}
|
|
|
|
// hostEndsInNumber implements the WHATWG "ends in a number" checker.
|
|
func hostEndsInNumber(host string) bool {
|
|
parts := strings.Split(host, ".")
|
|
if len(parts) > 1 && parts[len(parts)-1] == "" {
|
|
parts = parts[:len(parts)-1]
|
|
}
|
|
last := parts[len(parts)-1]
|
|
if last != "" && isASCIIDigits(last) {
|
|
return true
|
|
}
|
|
_, ok := parseIPv4Number(last)
|
|
return ok
|
|
}
|
|
|
|
// validIPv4Host implements the WHATWG IPv4 parser's failure conditions.
|
|
func validIPv4Host(host string) bool {
|
|
parts := strings.Split(host, ".")
|
|
if len(parts) > 1 && parts[len(parts)-1] == "" {
|
|
parts = parts[:len(parts)-1]
|
|
}
|
|
if len(parts) > 4 {
|
|
return false
|
|
}
|
|
numbers := make([]uint64, 0, len(parts))
|
|
for _, part := range parts {
|
|
number, ok := parseIPv4Number(part)
|
|
if !ok {
|
|
return false
|
|
}
|
|
numbers = append(numbers, number)
|
|
}
|
|
for _, number := range numbers[:len(numbers)-1] {
|
|
if number > 255 {
|
|
return false
|
|
}
|
|
}
|
|
limit := uint64(1)
|
|
for index := 0; index < 5-len(numbers); index++ {
|
|
limit *= 256
|
|
}
|
|
return numbers[len(numbers)-1] < limit
|
|
}
|
|
|
|
// parseIPv4Number implements the WHATWG IPv4 number parser (decimal, 0-octal
|
|
// and 0x-hexadecimal forms).
|
|
func parseIPv4Number(input string) (uint64, bool) {
|
|
if input == "" {
|
|
return 0, false
|
|
}
|
|
radix := 10
|
|
switch {
|
|
case len(input) >= 2 && (strings.HasPrefix(input, "0x") || strings.HasPrefix(input, "0X")):
|
|
input, radix = input[2:], 16
|
|
case len(input) >= 2 && input[0] == '0':
|
|
input, radix = input[1:], 8
|
|
}
|
|
if input == "" {
|
|
return 0, true
|
|
}
|
|
for index := 0; index < len(input); index++ {
|
|
if radixDigit(input[index]) >= radix {
|
|
return 0, false
|
|
}
|
|
}
|
|
number, err := strconv.ParseUint(input, radix, 64)
|
|
if err != nil {
|
|
return 0, false
|
|
}
|
|
return number, true
|
|
}
|
|
|
|
func radixDigit(character byte) int {
|
|
switch {
|
|
case character >= '0' && character <= '9':
|
|
return int(character - '0')
|
|
case character >= 'a' && character <= 'f':
|
|
return int(character-'a') + 10
|
|
case character >= 'A' && character <= 'F':
|
|
return int(character-'A') + 10
|
|
}
|
|
return 99
|
|
}
|
|
|
|
func isASCIIDigits(value string) bool {
|
|
for index := 0; index < len(value); index++ {
|
|
if value[index] < '0' || value[index] > '9' {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|