Saving an App-hosted public site as the startup destination currently reopens its expired address after restart. Persist the hosting selection instead and enter the current ready address. External sites retain their existing fixed-address preference and approval checks. The Windows MFT request also uses ordinary Baseline while Piik negotiates Constrained Baseline, causing the AMD startup rejection in #538. Request and validate Constrained Baseline without weakening the emitted-stream check. This phase also clears obsolete launcher errors after readiness and restores compact-menu labels. Refs #538. Review the issue for closure after the release is published; merging this PR alone must not close it. Validation: - Web type-check, 108 test files / 1960 tests, production build, and repository hygiene passed. Actual 390px menu layout and the production launcher component's error-to-ready transition passed; the latter used controlled API responses. - Affected Go App, config, launcher and mobile bridge package tests and Go vet passed. - The current Windows App completed two real Cloudflare startup cycles: it stored the hosting mode rather than the address, restored it without a new launch request, entered the fresh loopback page in Chrome and created a room. Both cycles retired their ports and processes. Public HTTPS requests from this runner failed with TLS IOException / ECONNRESET, so public reachability is not claimed by this test. - The Windows hardware H.264 fixture passed on an RTX 4070 SUPER with 360/360 frames, bitrate down/up recovery, and unchanged SPS validation. The affected RX 7800 XT is covered by the reporter's controlled profile comparison and Browser decode evidence. The local integrated AMD adapter failed during MFT activation, before the profile request; it is not a passing AMD regression test. No wire-version change, dependency update, room-data migration or automatic App-discovery feature is included. ## Release notes ### English This maintenance update fixes Windows H.264 startup and reopening App-hosted sites. - Request the H.264 profile that Piik actually negotiates. This fixes the AMD encoder startup rejection reported in #538 without loosening the encoded-stream checks or changing the negotiated profile. - When an App-hosted site is saved as the startup destination, restart its Local or public-invitation mode instead of reopening an expired temporary address. This applies to desktop and Android Apps. - Clear a stale startup error once the site is ready, and restore only startup modes available in the current App. - Keep action labels visible in compact header menus on narrow screens. Existing 2.x App/site compatibility and saved room data are preserved. Update the App for the Windows encoder and startup fixes. Refresh open site pages after updating a hosted Server; App-local pages use the Web UI included in the updated App package. If a temporary invitation address was saved before this update, start the site again from the App launcher and select “Open this site when Piik App starts” again. Previously saved addresses do not identify which App created them. ### 中文 本次维护更新修复 Windows H.264 启动失败,以及 App 自托管站点的再次打开问题。 - 让 H.264 编码器请求与 Piik 实际协商的编码规格一致,修复 #538 报告的 AMD 编码器启动被拒绝问题;不放宽码流检查,也不改变协商规格。 - 将 App 自己托管的站点设为启动入口后,下次启动会恢复局域网或公网邀请模式,不再打开已经失效的临时网址。桌面和 Android App 均适用。 - 站点已经就绪时清除过期的启动错误,只恢复当前 App 可用的启动模式。 - 保留窄屏顶部菜单中的操作文字,避免按钮只剩图标。 保留现有 2.x App/站点兼容性及已保存的房间数据。Windows 编码器和启动修复需要更新 App。托管 Server 更新后请刷新已打开的页面;App 本地页面使用新版 App 包内的网页。 曾保存过旧临时网址的用户,请从 App 启动页重新开启站点,并重新勾选一次“启动 App 时打开此站点”。旧记录无法判断这个临时网址由哪个 App 创建。
522 lines
15 KiB
Go
522 lines
15 KiB
Go
package bridge
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"errors"
|
|
"log/slog"
|
|
"net"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/TNTcraftHIM/Piik/internal/app/buildinfo"
|
|
appconfig "github.com/TNTcraftHIM/Piik/internal/app/config"
|
|
"github.com/TNTcraftHIM/Piik/internal/app/failure"
|
|
"github.com/TNTcraftHIM/Piik/internal/app/lan"
|
|
"github.com/TNTcraftHIM/Piik/internal/app/launcher"
|
|
"github.com/TNTcraftHIM/Piik/internal/app/loopback"
|
|
"github.com/TNTcraftHIM/Piik/internal/app/nativecontrol"
|
|
"github.com/TNTcraftHIM/Piik/internal/app/publictunnel"
|
|
"github.com/TNTcraftHIM/Piik/internal/diagnostics"
|
|
serverapp "github.com/TNTcraftHIM/Piik/internal/server/app"
|
|
"github.com/TNTcraftHIM/Piik/internal/server/config"
|
|
"github.com/TNTcraftHIM/Piik/internal/server/webassets"
|
|
"github.com/TNTcraftHIM/Piik/internal/tlsconfig"
|
|
)
|
|
|
|
// The App process owns the room runtime; the platform owns capture resources.
|
|
type Runtime struct {
|
|
capture *capture
|
|
server *serverapp.Server
|
|
control *loopback.Server
|
|
launcher *launcher.Server
|
|
ctx context.Context
|
|
cancel context.CancelFunc
|
|
once sync.Once
|
|
url string
|
|
invitation string
|
|
configMu sync.Mutex
|
|
configPath string
|
|
config appconfig.Config
|
|
tunnel *publictunnel.Process
|
|
tunnelExecutable string
|
|
temporaryDirectory string
|
|
launchDone chan struct{}
|
|
closeErr error
|
|
}
|
|
|
|
func Start(platform Platform, lanAddress string, codecCapabilities string, dataDirectory, temporaryDirectory, tunnelExecutable, activationURL string) (*Runtime, error) {
|
|
if platform == nil {
|
|
return nil, errors.New("mobile capture platform is unavailable")
|
|
}
|
|
if activationURL != "" && activationURL != "piik://media" {
|
|
return nil, errors.New("mobile media activation URL is invalid")
|
|
}
|
|
if webassets.FS() == nil {
|
|
return nil, errors.New("build the Piik Web assets before the mobile library")
|
|
}
|
|
if !filepath.IsAbs(temporaryDirectory) {
|
|
return nil, errors.New("mobile cache directory is unavailable")
|
|
}
|
|
if err := os.MkdirAll(temporaryDirectory, 0700); err != nil {
|
|
return nil, err
|
|
}
|
|
catalog, err := decodeEncoderCapabilities(codecCapabilities)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
preferences, err := appconfig.LoadOrCreate(filepath.Join(dataDirectory, "client.json"))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
r := &Runtime{
|
|
capture: &capture{
|
|
platform: platform,
|
|
multiOutput: catalog.MultiOutput,
|
|
encoders: catalog.Encoders,
|
|
streams: make(map[int64]*captureStream),
|
|
},
|
|
ctx: ctx, cancel: cancel,
|
|
config: preferences, configPath: filepath.Join(dataDirectory, "client.json"),
|
|
temporaryDirectory: temporaryDirectory, tunnelExecutable: tunnelExecutable,
|
|
}
|
|
capabilities := catalog.nativeCapabilities()
|
|
microphones, microphoneErr := r.capture.ListMicrophones(ctx)
|
|
capabilities.Microphone = microphoneErr == nil && len(microphones) > 0
|
|
if microphoneErr != nil {
|
|
slog.Debug("mobile microphone enumeration failed", "error", microphoneErr)
|
|
}
|
|
mediaCapabilities := catalog.mediaCapabilities()
|
|
mediaCapabilities.Microphone = capabilities.Microphone
|
|
mediaCapabilities.SourceCancellation = true
|
|
mediaCapabilities.CaptureFailureDetails = true
|
|
r.control, err = loopback.Start(ctx, loopback.Options{
|
|
ActivationURL: activationURL,
|
|
MaxControlSessions: 1,
|
|
AllowedOrigins: preferences.ApprovedSites(),
|
|
NativeMedia: mediaCapabilities,
|
|
NewControl: func() loopback.ControlSession {
|
|
return nativecontrol.NewWithCapture(capabilities, false, nil, r.capture)
|
|
},
|
|
})
|
|
if err != nil {
|
|
cancel()
|
|
return nil, err
|
|
}
|
|
startup, err := launcher.LoadStartupSite(r.configPath, preferences.ApprovedSites()...)
|
|
if err != nil {
|
|
return nil, errors.Join(err, r.Close())
|
|
}
|
|
modes := []launcher.Mode{launcher.ModeLocal}
|
|
if helper, statErr := os.Stat(tunnelExecutable); statErr == nil && filepath.IsAbs(tunnelExecutable) && helper.Mode().IsRegular() {
|
|
modes = append(modes, launcher.ModeLink)
|
|
}
|
|
mode, err := launcher.LoadMode(r.configPath)
|
|
if err != nil {
|
|
slog.Debug("Could not restore the last launcher mode", diagnostics.Error(err))
|
|
}
|
|
if mode == "" {
|
|
mode = launcher.ModeLink
|
|
}
|
|
if !slices.Contains(modes, mode) {
|
|
mode = modes[0]
|
|
}
|
|
r.launcher, err = launcher.Start(ctx, webassets.FS(), launcher.Options{
|
|
Version: buildinfo.Version(), Revision: buildinfo.Revision,
|
|
ControlPort: r.control.Endpoint().Port,
|
|
Modes: modes, LocalAccessPassword: preferences.LocalAccessPassword, DefaultMode: mode, Sites: preferences.ApprovedSites(), StartupSite: startup, LANAddress: lanAddress,
|
|
ChangeSite: func(site string, allowed bool) ([]string, error) {
|
|
var err error
|
|
if allowed {
|
|
_, err = r.approveSite(site)
|
|
} else {
|
|
err = r.revokeSite(site)
|
|
}
|
|
r.configMu.Lock()
|
|
sites := r.config.ApprovedSites()
|
|
r.configMu.Unlock()
|
|
return sites, err
|
|
},
|
|
SaveStartupSite: func(site launcher.StartupIntent) error { return launcher.SaveStartupSite(r.configPath, site) },
|
|
})
|
|
if err != nil {
|
|
return nil, errors.Join(err, r.Close())
|
|
}
|
|
r.control.SetConnectURL(r.launcher.URL())
|
|
r.configMu.Lock()
|
|
err = r.syncAllowedOriginsLocked()
|
|
r.configMu.Unlock()
|
|
if err != nil {
|
|
return nil, errors.Join(err, r.Close())
|
|
}
|
|
if startup.Owned != nil {
|
|
r.launcher.Begin()
|
|
}
|
|
r.launchDone = make(chan struct{})
|
|
go func() {
|
|
defer close(r.launchDone)
|
|
r.runLauncher(lanAddress, startup.Owned)
|
|
}()
|
|
go r.watchServices()
|
|
return r, nil
|
|
}
|
|
|
|
// Failed startup has already retired its resources before a new selection starts.
|
|
func (r *Runtime) runLauncher(lanAddress string, initial *launcher.StartupLaunch) {
|
|
for {
|
|
var selected launcher.Selection
|
|
if initial != nil {
|
|
r.configMu.Lock()
|
|
selected = launcher.Selection{Mode: initial.Mode, LANAddress: initial.LANAddress, LocalAccessPassword: r.config.LocalAccessPassword}
|
|
r.configMu.Unlock()
|
|
initial = nil
|
|
} else {
|
|
select {
|
|
case selected = <-r.launcher.Selection():
|
|
case <-r.ctx.Done():
|
|
return
|
|
}
|
|
}
|
|
if err := launcher.SaveMode(r.configPath, selected.Mode); err != nil {
|
|
slog.Debug("Could not save the launcher mode", diagnostics.Error(err))
|
|
}
|
|
target, err := r.startLocal(selected, lanAddress)
|
|
if err != nil {
|
|
r.launcher.SetResult(target, err)
|
|
}
|
|
if err == nil {
|
|
r.configMu.Lock()
|
|
invitation, server, tunnel := r.invitation, r.server, r.tunnel
|
|
r.configMu.Unlock()
|
|
r.launcher.SetReady(target, invitation, selected)
|
|
err = r.watchRoom(server, tunnel)
|
|
if r.ctx.Err() != nil {
|
|
r.closeErr = errors.Join(r.closeErr, err)
|
|
return
|
|
}
|
|
r.launcher.SetResult("", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (r *Runtime) URL() string { return r.launcher.URL() }
|
|
func (r *Runtime) EntryURL() string { return r.launcher.EntryURL() }
|
|
|
|
func (r *Runtime) startLocal(selection launcher.Selection, address string) (target string, returnedErr error) {
|
|
r.configMu.Lock()
|
|
next := r.config
|
|
next.LocalAccessPassword = selection.LocalAccessPassword
|
|
err := appconfig.Save(r.configPath, next)
|
|
if err == nil {
|
|
r.config = next
|
|
}
|
|
r.configMu.Unlock()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if selection.LANAddress != nil {
|
|
address = *selection.LANAddress
|
|
}
|
|
link := selection.Mode == launcher.ModeLink
|
|
bind := "127.0.0.1:0"
|
|
if link {
|
|
bind = "0.0.0.0:0"
|
|
}
|
|
local, err := net.Listen("tcp4", bind)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
port := local.Addr().(*net.TCPAddr).Port
|
|
listeners := []net.Listener{local}
|
|
var server *serverapp.Server
|
|
var tunnel *publictunnel.Process
|
|
committed := false
|
|
defer func() {
|
|
if committed {
|
|
return
|
|
}
|
|
if server != nil {
|
|
returnedErr = errors.Join(returnedErr, endRuntimeServer(server))
|
|
}
|
|
if tunnel != nil {
|
|
returnedErr = errors.Join(returnedErr, tunnel.Close())
|
|
}
|
|
for _, listener := range listeners {
|
|
_ = listener.Close()
|
|
}
|
|
}()
|
|
options := config.LocalOptions{PublicAddress: address, Port: port, SiteAccessPassword: selection.LocalAccessPassword}
|
|
if link {
|
|
options.STUNURLs, options.NATPredictionSTUNURLs, err = appconfig.PublicSTUNURLs(os.Getenv("PIIK_STUN_URLS"))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
tunnel, err = publictunnel.Start(r.ctx, r.tunnelExecutable, "http://127.0.0.1:"+strconv.Itoa(port), r.temporaryDirectory)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
options.PublicOrigin = tunnel.Origin()
|
|
options.AllowedAddresses, _ = lan.Addresses()
|
|
// A public invitation does not require a currently selected LAN interface.
|
|
options.PublicAddress = ""
|
|
} else {
|
|
secure, err := net.Listen("tcp4", net.JoinHostPort(address, strconv.Itoa(port)))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
listeners = append(listeners, secure)
|
|
certificate, err := tlsconfig.SelfSigned(address)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
listeners[len(listeners)-1] = tls.NewListener(secure, certificate)
|
|
}
|
|
configuration, err := config.Local(options)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
server, err = serverapp.New(serverapp.Options{Config: configuration, Assets: webassets.FS(), Listeners: listeners})
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if _, err = server.Listen(r.ctx); err != nil {
|
|
return "", err
|
|
}
|
|
target = launcher.LocalEntryURL("http://127.0.0.1:"+strconv.Itoa(port)+"/", selection.LocalAccessPassword, r.control.Endpoint().Port)
|
|
targetURL, _ := url.Parse(target)
|
|
r.configMu.Lock()
|
|
if r.ctx.Err() != nil {
|
|
r.configMu.Unlock()
|
|
return "", r.ctx.Err()
|
|
}
|
|
if err = r.syncAllowedOriginsLocked(targetURL.Scheme + "://" + targetURL.Host); err != nil {
|
|
r.configMu.Unlock()
|
|
return "", err
|
|
}
|
|
r.server, r.tunnel, r.url = server, tunnel, target
|
|
r.invitation = config.Origin(configuration.PublicBaseURL)
|
|
committed = true
|
|
r.configMu.Unlock()
|
|
return target, nil
|
|
}
|
|
|
|
func endRuntimeServer(server *serverapp.Server) error {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
err := server.End(ctx)
|
|
if errors.Is(err, context.Canceled) || errors.Is(err, http.ErrServerClosed) {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (r *Runtime) watchRoom(server *serverapp.Server, tunnel *publictunnel.Process) error {
|
|
var tunnelDone <-chan struct{}
|
|
if tunnel != nil {
|
|
tunnelDone = tunnel.Done()
|
|
}
|
|
reason := failure.LocalServer
|
|
select {
|
|
case <-r.ctx.Done():
|
|
case <-server.Done():
|
|
case <-tunnelDone:
|
|
reason = failure.TunnelStopped
|
|
}
|
|
err := endRuntimeServer(server)
|
|
if tunnel != nil {
|
|
err = errors.Join(err, tunnel.Close())
|
|
}
|
|
r.configMu.Lock()
|
|
origin := ""
|
|
if r.server == server {
|
|
if r.url != "" {
|
|
origin = r.localOrigin()
|
|
}
|
|
r.server, r.tunnel, r.url = nil, nil, ""
|
|
r.invitation = ""
|
|
}
|
|
r.configMu.Unlock()
|
|
if origin != "" {
|
|
retireErr := r.control.RemoveAllowedOrigin(origin)
|
|
// Whole-runtime cancellation also cancels this permission wait. Close
|
|
// joins control cleanup with its own deadline after launchDone instead.
|
|
if r.ctx.Err() == nil {
|
|
err = errors.Join(err, retireErr)
|
|
}
|
|
}
|
|
if r.ctx.Err() != nil {
|
|
return err
|
|
}
|
|
return errors.Join(reason, err)
|
|
}
|
|
|
|
func (r *Runtime) watchServices() {
|
|
var reason failure.Reason
|
|
select {
|
|
case <-r.ctx.Done():
|
|
return
|
|
case <-r.launcher.Done():
|
|
reason = failure.Launcher
|
|
case <-r.control.Done():
|
|
reason = failure.NativeControl
|
|
}
|
|
r.stop(reason)
|
|
}
|
|
|
|
func (r *Runtime) stop(reason failure.Reason) {
|
|
if r.ctx.Err() != nil {
|
|
return
|
|
}
|
|
if err := r.close(reason); err != nil {
|
|
slog.Warn("mobile runtime cleanup failed", "error", err)
|
|
}
|
|
}
|
|
|
|
// Sites retain the desktop configuration and exact-origin approval owner.
|
|
func (r *Runtime) approveSite(value string) (string, error) {
|
|
site, err := appconfig.NormalizeSite(value)
|
|
if err != nil || !strings.HasPrefix(site, "https://") {
|
|
return "", errors.New("Enter an HTTPS Piik site origin")
|
|
}
|
|
r.configMu.Lock()
|
|
sites := r.config.ApprovedSites()
|
|
for _, current := range sites {
|
|
if current == site {
|
|
r.configMu.Unlock()
|
|
return site, nil
|
|
}
|
|
}
|
|
next := r.config
|
|
next.SetApprovedSites(append(sites, site))
|
|
if err = appconfig.Save(r.configPath, next); err != nil {
|
|
r.configMu.Unlock()
|
|
return "", err
|
|
}
|
|
r.config = next
|
|
err = r.syncAllowedOriginsLocked()
|
|
r.configMu.Unlock()
|
|
r.capture.platform.SiteAccessChanged()
|
|
return site, err
|
|
}
|
|
func (r *Runtime) revokeSite(value string) error {
|
|
r.configMu.Lock()
|
|
var sites []string
|
|
for _, site := range r.config.ApprovedSites() {
|
|
if site != value {
|
|
sites = append(sites, site)
|
|
}
|
|
}
|
|
next := r.config
|
|
next.SetApprovedSites(sites)
|
|
if err := appconfig.Save(r.configPath, next); err != nil {
|
|
r.configMu.Unlock()
|
|
return err
|
|
}
|
|
r.config = next
|
|
err := r.syncAllowedOriginsLocked()
|
|
r.configMu.Unlock()
|
|
r.capture.platform.SiteAccessChanged()
|
|
return err
|
|
}
|
|
|
|
// Caller holds configMu through the permission commit, so room startup cannot
|
|
// replay a stale Site snapshot or discard the newly installed local origin.
|
|
func (r *Runtime) syncAllowedOriginsLocked(pendingOrigins ...string) error {
|
|
origins := append(r.localOriginsLocked(), pendingOrigins...)
|
|
return r.control.SetAllowedOrigins(append(origins, r.config.ApprovedSites()...))
|
|
}
|
|
|
|
func (r *Runtime) localOrigins() []string {
|
|
r.configMu.Lock()
|
|
defer r.configMu.Unlock()
|
|
return r.localOriginsLocked()
|
|
}
|
|
|
|
func (r *Runtime) localOriginsLocked() []string {
|
|
origins := []string{}
|
|
if r.url != "" {
|
|
origins = append(origins, r.localOrigin())
|
|
}
|
|
if r.launcher != nil {
|
|
u, _ := url.Parse(r.launcher.URL())
|
|
origins = append(origins, u.Scheme+"://"+u.Host)
|
|
}
|
|
return origins
|
|
}
|
|
func (r *Runtime) localOrigin() string { u, _ := url.Parse(r.url); return u.Scheme + "://" + u.Host }
|
|
func (r *Runtime) TrustedURL(value string) bool {
|
|
u, err := url.Parse(value)
|
|
if err != nil || u.User != nil {
|
|
return false
|
|
}
|
|
origin, err := appconfig.NormalizeSite(u.Scheme + "://" + u.Host)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
for _, local := range r.localOrigins() {
|
|
if origin == local {
|
|
return true
|
|
}
|
|
}
|
|
r.configMu.Lock()
|
|
defer r.configMu.Unlock()
|
|
for _, site := range r.config.ApprovedSites() {
|
|
if site == origin {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (r *Runtime) Close() error {
|
|
return r.close("")
|
|
}
|
|
|
|
func (r *Runtime) close(reason failure.Reason) error {
|
|
closed := false
|
|
r.once.Do(func() {
|
|
closed = true
|
|
r.cancel()
|
|
if r.launchDone != nil {
|
|
<-r.launchDone
|
|
}
|
|
r.configMu.Lock()
|
|
server := r.server
|
|
tunnel := r.tunnel
|
|
r.configMu.Unlock()
|
|
if server != nil {
|
|
r.closeErr = errors.Join(r.closeErr, endRuntimeServer(server))
|
|
}
|
|
if tunnel != nil {
|
|
r.closeErr = errors.Join(r.closeErr, tunnel.Close())
|
|
}
|
|
if r.control != nil {
|
|
r.closeErr = errors.Join(r.closeErr, r.control.Close())
|
|
}
|
|
if r.launcher != nil {
|
|
r.closeErr = errors.Join(r.closeErr, r.launcher.Close())
|
|
}
|
|
r.capture.mu.Lock()
|
|
streams := make([]*captureStream, 0, len(r.capture.streams))
|
|
for _, s := range r.capture.streams {
|
|
streams = append(streams, s)
|
|
}
|
|
r.capture.mu.Unlock()
|
|
for _, s := range streams {
|
|
r.closeErr = errors.Join(r.closeErr, s.Close())
|
|
}
|
|
})
|
|
if closed && reason != "" {
|
|
r.capture.platform.RuntimeStopped(string(reason))
|
|
}
|
|
return r.closeErr
|
|
}
|