## Problem and outcome / 问题与结果 Browser sharing can remain connected to a local encoding transport that never becomes usable, leaving viewers without frames. This phase bounds that startup at eight seconds and restores ordinary encoding through the existing failure path. It also keeps ready App services running after a browser-open failure, preserves user presentation/playback choices, and aligns room feedback, illustrated hints, metrics and documentation. 部分环境下,本机编码连接一直无法建立会让观众收不到画面。本轮在八秒后进入现有降级流程,同时修复客户端启动移交、播放状态和前端反馈的一致性问题,并更新官网与中英文教程。 Closes #399. Related: #396. The release contains relevant startup/error-handling repairs, but the reporting machine has not been retested. Keep this issue open for a current-build result. #401 (optional Windows capture-border hiding) remains deferred. Release / 发布版本:**v1.3.1**. ## Release notes ### 中文 - 客户端记住上次确认的启动模式;启动移交保留已保存的语言、纯视觉模式和主题选择。 - 系统浏览器未能自动打开时,客户端继续运行,并在终端保留页面地址、重试入口和具体错误。 - 改善浏览器共享画面的启动兜底,避免 App 媒体因短暂信令中断被误判为失败。 - 音轨变化保持现有视频与观众的本机暂停状态;连接恢复和暂停恢复不再重复播放电视开机动画。 - 修复昵称编辑及长昵称查看,减少重复的角色提示,统一人物选择和连接状态的区别。 - 完善房间准入、密码和邀请说明;复制反馈对应当前链接及当前操作,避免迟到结果覆盖新反馈。 - 修正采集参数、统计单位与采样区间的展示,未知参数不再用配置上限代替。 - 统一提示漫画、状态图标与动效,改善选源器还焦、键盘操作、剧场布局和触控目标尺寸。 - 服务端默认支持 20 位观众,可在配置中设为 1–20 位,房主不占观众名额。 - 更新官网、教程入口和中英文说明,清理过时预览及失用代码。 兼容说明:使用旧版 App 连接更新后的站点时,站点会优先保留自己的显示偏好。如需更改语言或主题,请在进入后的页面调整。 ### English - Remember the last confirmed App launch mode and preserve saved language, visual-mode and theme choices during launch handoff. - Keep the App running if the system browser fails to open, with the page address, retry action and error details available in the terminal. - Improve the browser sharing startup fallback and avoid treating brief signaling outages as App media failures. - Keep existing video and local pause state when audio tracks change. Reconnecting and resuming no longer replay the television startup animation over the picture. - Fix nickname editing and full-name access, remove redundant role hints, and distinguish participant selection from connection state. - Clarify room access, passwords and invitations. Copy feedback now belongs to the current link and operation, so late results cannot replace newer feedback. - Correct capture details, metric units and observation intervals. Unknown capture values are no longer replaced with configured ceilings. - Align illustrated hints, status icons and motion. Improve source-picker focus, keyboard operation, theater layout and touch targets. - Default the Server to 20 viewers, configurable from 1 to 20. The host does not count toward that limit. - Update the website, guide links and bilingual instructions, and remove obsolete previews and unused code. Compatibility: when an older App connects to an updated Site, the Site keeps its saved presentation preferences. Change language or theme on the destination page when using an older launcher. ## Verification / 验证 Non-publishing [CI candidate run 34919320511](https://github.com/TNTcraftHIM/Piik/actions/runs/34919320511) passed Go core, race, Web checks, Server/container verification and all three native App package checks at source 188376e8. Subsequent commits only update deployment documentation, status and TODO; documentation/hygiene checks passed. Product inputs are unchanged. - `npm run check`: TypeScript, 64 test files / 854 tests, and the Web production build passed. - `npm run check:client-core`: final Go source passed tests, formatting, vet and core target builds. Windows capture compilation and geometry checks passed earlier in this phase; later edits did not change native capture. - Website build, documentation checks, repository hygiene and diff checks passed. Existing documentation-size and bundle-size warnings remain. - Actual browser flows covered sharing, source changes, pause/resume, late audio, brief signaling loss, invitation/copy feedback, entry preferences, source-picker focus, and narrow layouts. - A local three-viewer P2P relay check covered quality-report expiry, recovery and cleanup without interrupting video. A controlled browser-helper failure returning `0xc0000005` left the real App launcher and Local service available. - Homepage checks covered Chinese/English, light/dark preferences and inline-film open/close after removing the separate-page entry. Limits: no new reporter-machine confirmation for #396 or the Windows URL-handler crash; physical Safari/mobile, macOS/Linux capture and wider public-network/endurance coverage remain deferred. Formal release artifacts will be built from the approved squash-merge revision. Hero-loop controls and video-adjacent tooltip placement remain open design items in TODO. ## Impact / 影响 Compatible patch; signaling, Native control and capture contracts are unchanged. Older App launch handoff keeps the updated Site's saved presentation preferences. The Server's default viewer limit is corrected to 20; an explicit 1–20 configuration remains valid. Service updates retain persisted room authority but restart active media sessions. Deploy the verified immutable Server artifact through the existing bounded health/recovery process. Public Demo remains open and P2P-only; deployment does not enable SFU or change infrastructure/configuration.
411 lines
12 KiB
Go
411 lines
12 KiB
Go
package launcher
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"io/fs"
|
|
"log/slog"
|
|
"net"
|
|
"net/http"
|
|
"net/url"
|
|
"runtime"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
appconfig "github.com/TNTcraftHIM/Piik/internal/app/config"
|
|
"github.com/TNTcraftHIM/Piik/internal/app/lan"
|
|
"github.com/TNTcraftHIM/Piik/internal/diagnostics"
|
|
)
|
|
|
|
const (
|
|
maxRequestBytes = 2 << 10
|
|
shutdownTimeout = 2 * time.Second
|
|
// launcherIndex is the Browser entry document. /client serves it directly
|
|
// because that route belongs to the client-side router, not to the assets.
|
|
launcherIndex = "index.html"
|
|
)
|
|
|
|
type Mode string
|
|
|
|
const (
|
|
ModeLocal Mode = "local"
|
|
ModeLink Mode = "link"
|
|
ModeSite Mode = "site"
|
|
)
|
|
|
|
type Selection struct {
|
|
Mode Mode `json:"mode"`
|
|
Language string `json:"language"`
|
|
Site string `json:"site,omitempty"`
|
|
LocalAccessPassword string `json:"localAccessPassword"`
|
|
Debug bool `json:"debug,omitempty"`
|
|
LANAddress *string `json:"lanAddress,omitempty"`
|
|
}
|
|
|
|
type Options struct {
|
|
DefaultMode Mode
|
|
Site string
|
|
Version string
|
|
Revision string
|
|
LocalAccessPassword string
|
|
Debug bool
|
|
LANAddress string
|
|
}
|
|
|
|
var listLANAddresses = lan.List
|
|
|
|
type Server struct {
|
|
ctx context.Context
|
|
cancel context.CancelFunc
|
|
listener net.Listener
|
|
httpServer *http.Server
|
|
assets fs.FS
|
|
static http.Handler
|
|
site string
|
|
defaultMode Mode
|
|
localAccessPassword string
|
|
version string
|
|
revision string
|
|
debug bool
|
|
lanAddress string
|
|
selection chan Selection
|
|
resultReady chan struct{}
|
|
handled chan struct{}
|
|
done chan error
|
|
ready chan struct{}
|
|
|
|
mu sync.Mutex
|
|
selected bool
|
|
resultSet bool
|
|
target string
|
|
resultErr error
|
|
closeOnce sync.Once
|
|
resultOnce sync.Once
|
|
handledOnce sync.Once
|
|
}
|
|
|
|
// Start serves the launcher page and its API on a loopback port. assets is the
|
|
// built Browser UI, which the App embeds; a nil file system means the binary
|
|
// carries no build and the launcher cannot run.
|
|
func Start(parent context.Context, assets fs.FS, options Options) (*Server, error) {
|
|
if parent == nil {
|
|
parent = context.Background()
|
|
}
|
|
normalizedSite, err := appconfig.NormalizeSite(options.Site)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("App launcher Site is invalid: %w", err)
|
|
}
|
|
if assets == nil {
|
|
return nil, errors.New("App launcher assets are unavailable")
|
|
}
|
|
if metadata, err := fs.Stat(assets, launcherIndex); err != nil || !metadata.Mode().IsRegular() {
|
|
return nil, errors.New("App launcher assets are unavailable")
|
|
}
|
|
listener, err := net.Listen("tcp4", "127.0.0.1:0")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("App launcher could not start: %w", err)
|
|
}
|
|
ctx, cancel := context.WithCancel(parent)
|
|
server := &Server{
|
|
ctx: ctx,
|
|
cancel: cancel,
|
|
listener: listener,
|
|
assets: assets,
|
|
static: http.FileServer(http.FS(assets)),
|
|
site: normalizedSite,
|
|
defaultMode: options.DefaultMode,
|
|
localAccessPassword: options.LocalAccessPassword,
|
|
version: options.Version,
|
|
revision: strings.TrimSpace(options.Revision),
|
|
debug: options.Debug,
|
|
lanAddress: options.LANAddress,
|
|
selection: make(chan Selection, 1),
|
|
resultReady: make(chan struct{}),
|
|
handled: make(chan struct{}),
|
|
done: make(chan error, 1),
|
|
ready: make(chan struct{}),
|
|
}
|
|
server.httpServer = &http.Server{
|
|
Handler: server,
|
|
ReadHeaderTimeout: 5 * time.Second,
|
|
IdleTimeout: 30 * time.Second,
|
|
}
|
|
go server.serve()
|
|
<-server.ready
|
|
go func() {
|
|
<-ctx.Done()
|
|
_ = server.Close()
|
|
}()
|
|
return server, nil
|
|
}
|
|
|
|
func (server *Server) URL() string {
|
|
return "http://" + server.listener.Addr().String() + "/client"
|
|
}
|
|
|
|
func (server *Server) Selection() <-chan Selection {
|
|
return server.selection
|
|
}
|
|
|
|
func (server *Server) Handled() <-chan struct{} {
|
|
return server.handled
|
|
}
|
|
|
|
func (server *Server) Done() <-chan error {
|
|
return server.done
|
|
}
|
|
|
|
func (server *Server) SetResult(target string, resultErr error) {
|
|
server.resultOnce.Do(func() {
|
|
if resultErr == nil && !validTarget(target) {
|
|
resultErr = errors.New("App target is invalid")
|
|
}
|
|
server.mu.Lock()
|
|
server.resultSet = true
|
|
server.target = target
|
|
server.resultErr = resultErr
|
|
server.mu.Unlock()
|
|
close(server.resultReady)
|
|
})
|
|
}
|
|
|
|
func (server *Server) Close() error {
|
|
server.closeOnce.Do(server.cancel)
|
|
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
|
|
defer cancel()
|
|
return server.httpServer.Shutdown(ctx)
|
|
}
|
|
|
|
func (server *Server) serve() {
|
|
close(server.ready)
|
|
err := server.httpServer.Serve(server.listener)
|
|
if errors.Is(err, http.ErrServerClosed) {
|
|
err = nil
|
|
}
|
|
server.done <- err
|
|
close(server.done)
|
|
}
|
|
|
|
func (server *Server) ServeHTTP(response http.ResponseWriter, request *http.Request) {
|
|
if !server.requestAllowed(request) {
|
|
http.NotFound(response, request)
|
|
return
|
|
}
|
|
response.Header().Set("X-Content-Type-Options", "nosniff")
|
|
switch request.URL.Path {
|
|
case "/api/client-launcher":
|
|
server.handleState(response, request)
|
|
case "/api/client-launcher/launch":
|
|
server.handleLaunch(response, request)
|
|
case "/client", "/client/":
|
|
if request.Method != http.MethodGet && request.Method != http.MethodHead {
|
|
response.Header().Set("Allow", "GET, HEAD")
|
|
http.Error(response, "method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
response.Header().Set("Cache-Control", "no-store")
|
|
http.ServeFileFS(response, request, server.assets, launcherIndex)
|
|
default:
|
|
server.static.ServeHTTP(response, request)
|
|
}
|
|
}
|
|
|
|
func (server *Server) handleState(response http.ResponseWriter, request *http.Request) {
|
|
if request.Method != http.MethodGet {
|
|
response.Header().Set("Allow", http.MethodGet)
|
|
http.Error(response, "method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
addresses, err := listLANAddresses()
|
|
if err != nil {
|
|
slog.DebugContext(request.Context(), "launcher-lan-unavailable", diagnostics.Error(err))
|
|
}
|
|
if addresses == nil {
|
|
addresses = []lan.Address{}
|
|
}
|
|
values := make([]string, 0, len(addresses))
|
|
for _, address := range addresses {
|
|
values = append(values, address.Address)
|
|
}
|
|
selected, _ := lan.Select(values, server.lanAddress)
|
|
writeJSON(response, http.StatusOK, map[string]any{
|
|
"site": server.site,
|
|
"localAccessPassword": server.localAccessPassword,
|
|
"version": server.version,
|
|
"revision": server.revision,
|
|
"packageTarget": runtime.GOOS + "-" + runtime.GOARCH,
|
|
"debug": server.debug,
|
|
"lan": map[string]any{"addresses": addresses, "selected": selected},
|
|
"defaultMode": func() Mode {
|
|
if validMode(server.defaultMode) && (server.defaultMode != ModeSite || server.site != "") {
|
|
return server.defaultMode
|
|
}
|
|
if server.site != "" {
|
|
return ModeSite
|
|
}
|
|
return ModeLink
|
|
}(),
|
|
})
|
|
}
|
|
|
|
func (server *Server) handleLaunch(response http.ResponseWriter, request *http.Request) {
|
|
if request.Method != http.MethodPost {
|
|
response.Header().Set("Allow", http.MethodPost)
|
|
http.Error(response, "method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
request.Body = http.MaxBytesReader(response, request.Body, maxRequestBytes)
|
|
selection, err := decodeSelection(request.Body)
|
|
if err != nil {
|
|
writeJSON(response, http.StatusBadRequest, map[string]string{
|
|
"error": "invalid launch request", "detail": errorDetail(err),
|
|
})
|
|
return
|
|
}
|
|
server.mu.Lock()
|
|
if server.selected {
|
|
server.mu.Unlock()
|
|
writeJSON(response, http.StatusConflict, map[string]string{
|
|
"error": "launch already requested", "detail": "Close and reopen Piik before starting again.",
|
|
})
|
|
return
|
|
}
|
|
server.selected = true
|
|
server.mu.Unlock()
|
|
|
|
select {
|
|
case server.selection <- selection:
|
|
case <-request.Context().Done():
|
|
server.markHandled()
|
|
return
|
|
case <-server.ctx.Done():
|
|
server.markHandled()
|
|
return
|
|
}
|
|
select {
|
|
case <-server.resultReady:
|
|
case <-request.Context().Done():
|
|
server.markHandled()
|
|
return
|
|
case <-server.ctx.Done():
|
|
server.markHandled()
|
|
return
|
|
}
|
|
server.mu.Lock()
|
|
target, resultErr, resultSet := server.target, server.resultErr, server.resultSet
|
|
server.mu.Unlock()
|
|
if !resultSet || resultErr != nil {
|
|
writeJSON(response, http.StatusServiceUnavailable, map[string]string{
|
|
"error": "Piik App could not start", "detail": errorDetail(resultErr),
|
|
})
|
|
server.markHandled()
|
|
return
|
|
}
|
|
writeJSON(response, http.StatusOK, map[string]string{"target": target})
|
|
server.markHandled()
|
|
}
|
|
|
|
func errorDetail(err error) string {
|
|
if err == nil {
|
|
return ""
|
|
}
|
|
// Redact before truncation so a long credential cannot lose its delimiter.
|
|
detail := []rune(diagnostics.SafeText(err.Error()))
|
|
if len(detail) > 2048 {
|
|
return string(detail[:2048]) + "..."
|
|
}
|
|
return string(detail)
|
|
}
|
|
|
|
func (server *Server) markHandled() {
|
|
server.handledOnce.Do(func() { close(server.handled) })
|
|
}
|
|
|
|
func (server *Server) requestAllowed(request *http.Request) bool {
|
|
_, port, err := net.SplitHostPort(request.Host)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
_, listenerPort, listenerErr := net.SplitHostPort(server.listener.Addr().String())
|
|
if listenerErr != nil || port != listenerPort {
|
|
return false
|
|
}
|
|
host := strings.TrimSuffix(strings.ToLower(request.Host), ":"+port)
|
|
if host != "127.0.0.1" && host != "localhost" {
|
|
return false
|
|
}
|
|
origin := strings.TrimSpace(request.Header.Get("Origin"))
|
|
return origin == "" || sameOrigin(origin, "http://"+request.Host)
|
|
}
|
|
|
|
func sameOrigin(actual, expected string) bool {
|
|
actualURL, actualErr := url.Parse(actual)
|
|
expectedURL, expectedErr := url.Parse(expected)
|
|
return actualErr == nil && expectedErr == nil &&
|
|
actualURL.Scheme == expectedURL.Scheme &&
|
|
strings.EqualFold(actualURL.Host, expectedURL.Host) &&
|
|
actualURL.Path == "" && actualURL.RawQuery == "" && actualURL.Fragment == ""
|
|
}
|
|
|
|
func decodeSelection(reader io.Reader) (Selection, error) {
|
|
var selection Selection
|
|
decoder := json.NewDecoder(reader)
|
|
decoder.DisallowUnknownFields()
|
|
if decoder.Decode(&selection) != nil || decoder.Decode(&struct{}{}) != io.EOF {
|
|
return Selection{}, errors.New("invalid launch request")
|
|
}
|
|
if selection.Language != "zh" && selection.Language != "en" && selection.Language != "vis" {
|
|
return Selection{}, errors.New("invalid launch language")
|
|
}
|
|
if selection.LANAddress != nil {
|
|
if selection.Mode != ModeLocal {
|
|
return Selection{}, errors.New("only Local launch can select a LAN invitation address")
|
|
}
|
|
value := strings.TrimSpace(*selection.LANAddress)
|
|
if ip := net.ParseIP(value); ip == nil || ip.To4() == nil {
|
|
return Selection{}, errors.New("Local invitation address must be an active IPv4 address")
|
|
}
|
|
selection.LANAddress = &value
|
|
}
|
|
switch selection.Mode {
|
|
case ModeLocal, ModeLink:
|
|
if strings.TrimSpace(selection.Site) != "" {
|
|
return Selection{}, errors.New("local launch cannot include a Site")
|
|
}
|
|
selection.Site = ""
|
|
case ModeSite:
|
|
if selection.LocalAccessPassword != "" {
|
|
return Selection{}, errors.New("Site launch cannot include a local access password")
|
|
}
|
|
normalized, err := appconfig.NormalizeSite(selection.Site)
|
|
if err != nil || normalized == "" {
|
|
return Selection{}, errors.New("Site launch requires an HTTP or HTTPS origin")
|
|
}
|
|
selection.Site = normalized
|
|
default:
|
|
return Selection{}, errors.New("unknown launch mode")
|
|
}
|
|
return selection, nil
|
|
}
|
|
|
|
func validTarget(raw string) bool {
|
|
parsed, err := url.Parse(raw)
|
|
return err == nil && (parsed.Scheme == "http" || parsed.Scheme == "https") && parsed.Host != ""
|
|
}
|
|
|
|
func writeJSON(response http.ResponseWriter, status int, value any) {
|
|
payload, err := json.Marshal(value)
|
|
if err != nil {
|
|
http.Error(response, "response unavailable", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
response.Header().Set("Cache-Control", "no-store")
|
|
response.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
response.WriteHeader(status)
|
|
_, _ = response.Write(payload)
|
|
}
|