## Problem and outcome / 问题与结果 Bring Android into Piik's shared App workflow and make phone control usable without a separate Web UI. One signed APK covers ARM64 and x64; screen capture, room authority, routing, voice and site approval reuse the existing Go and Browser owners. Android opens the user's system browser rather than shipping an embedded browser. This phase also fixes capture replacement and encoding-clock failures, improves source-audio startup, contains recoverable input and transport failures, adds a bounded relay-volunteer setting and supports hostnames in `SFU_PUBLIC_IP`. The website separates desktop and mobile downloads, gives iOS users an online entry and aligns the bilingual installation guides. Related reports: #495, #499, #500, #501, #502 and #503. For #502, input execution failures now preserve authorization, and temporary target observations suspend rather than revoke it; the original Windows report remains open for candidate feedback. ## Release notes **Piik, now in your pocket.** Piik App now supports Android screen sharing, with touch controls for a friend's computer. **手机,也能开播。** Piik App 现已支持 Android 屏幕分享,也能用手机控制朋友的电脑。 ### English Piik v2.5.0 brings the App to Android and improves capture reliability, touch control and everyday startup. The existing 2.x compatibility baseline is unchanged. - **Android App:** share your screen, join room voice or control a supported computer. One APK supports ARM64 and x64, using your own browser and the same Piik interface. - **Phone controls:** use direct touch or a compact floating mouse with dragging, left/right buttons and a scroll wheel. Pinch to zoom and pan the picture while controlling. - **More reliable sharing:** keep video progressing through source and encoder-output changes, and improve low-bitrate recovery and stationary-screen keyframes. Correct Windows window-size handling and Linux NVENC settings; start optional source sound after video is ready. - **Smoother App entry:** retry failed startup in place, reopen the running room and retain the correct App connection when returning from the browser. Compress shared page assets to reduce loading traffic and keep microphone choices stable during ordinary page updates. - **Recovery stays local:** a rejected input or canceled source replacement no longer revokes remote-control permission. Full ICE candidate queues and failed audio-parameter updates retain healthy connections. A failed local room leaves the App and unrelated approved sites running. - **Connection settings:** relay volunteers can cap their downstream viewers. Self-hosted SFU setups can use a DNS hostname in `SFU_PUBLIC_IP`. - **Clearer help:** refine sharing-error guidance, preserve input-failure diagnostics without recording input content, and update the bilingual platform and network guides. Update both the App and your self-hosted site to use the new features. Android can control a supported computer; the Android device itself is not a controlled endpoint. If you previously installed a test APK, uninstall it once before installing this release: the official signing key is different. Uninstalling clears App-local site approvals and starred-site choices; browser settings remain in the browser. Later official releases can be installed over this one. ### 中文 Piik v2.5.0 带来 Android App,并改善采集稳定性、手机远控与日常启动体验。沿用现有 2.x 兼容基线。 - **Android App:** 分享手机画面、加入房间语音,或控制支持远控的电脑。一个安装包同时支持 ARM64 与 x64,使用自己的浏览器和熟悉的 Piik 界面。 - **手机远控:** 直接触摸操作,或展开小巧的悬浮鼠标,使用拖拽、左右键与滚轮;控制时也能双指缩放、移动画面。 - **分享更稳定:** 修复切换采集来源、编码输出时画面停止推进的问题,改善低码率恢复与静止画面的关键帧响应。修正 Windows 窗口尺寸处理和 Linux NVENC 设置,来源声音会在画面就绪后启动。 - **App 进出更顺畅:** 启动失败可原地重试,再次打开会回到正在运行的房间;从浏览器返回时保持连接到正确的 App。共用页面资源启用压缩,减少加载流量,麦克风选项不再随普通页面更新反复加载。 - **局部问题就地恢复:** 单次操作失败或取消换源不再撤销远控授权;ICE 候选队列满或音频参数调整失败时保留健康连接。本地房间故障不会连带停止 App 和其他已授权站点的分享。 - **连接设置:** 帮助转发的观众可设置承接人数上限;自部署 SFU 的 `SFU_PUBLIC_IP` 现在支持 DNS 主机名。 - **排查更直接:** 打磨分享失败提示,补充不记录输入内容的远控故障诊断,并更新中英文平台说明与网络指南。 使用新功能时,请一起更新 App 与自部署站点。Android 可作为控制端操作电脑,手机本身不作为被控端。 曾安装测试 APK 的用户需先卸载一次,再安装正式版,两者签名不同。卸载会清除 App 内的站点授权和收藏,浏览器设置仍保留在浏览器中;后续正式版可直接覆盖安装。 ## Verification / 验证 - Local full Web/type/build checks passed: 104 test files, 1,848 tests; the website and documentation builds passed. - Android full-App emulator acceptance covered real decoded screen sharing, concurrent 1080p/540p encoder allocation, live bitrate changes, stationary keyframes, Viewer departure, consent reuse/reentry and final capture-resource retirement. - A local universal release APK passed signing, package identity, version-code, ABI and non-debuggable checks using the permanent release key. GitHub Actions receives the same key through encrypted Secrets; manual candidate runs use a disposable candidate signer. - Windows real capture checks covered H264/VP8 window resizing and restoration. Source-audio startup and failure/close ordering passed focused runtime checks; Linux race checks passed. - Desktop/mobile website layouts were reviewed in English and Chinese, both themes, including grouped downloads, the iOS online entry and self-hosting links. - All three Server and six App candidate targets, including the signed Android universal APK, passed in https://github.com/TNTcraftHIM/Piik/actions/runs/40350004792. This uses the same release packaging path with publication disabled. - The complete network laboratory passed in https://github.com/TNTcraftHIM/Piik/actions/runs/40354253735: 43 expected scenario outcomes (37 media connections, six expected-unavailable controls), six negative controls, and complete process/network cleanup. - Recoverable-error checks cover real encrypted input replies and legacy opt-in, retained grants and held-key cleanup, real Windows target geometry recovery, candidate queue overflow, and audio-parameter retry on the existing edge. Android room failure and full shutdown use real launcher, control WebSocket and supervised-process checks; unrelated site resources remain intact. - The lifecycle fixes at `7eb04de373567cfe7aff2db6c93adf2a38e0b1aa` passed all nine package targets and Go/vulnerability/race/Web checks in https://github.com/TNTcraftHIM/Piik/actions/runs/40362794088, including macOS x64/ARM64 cgo and Android. Publication was skipped. - Final cross-review found and fixed the same candidate-overflow false failure in the SFU voice owner. Its real-Pion regression verifies the first 64 candidates remain queued, a valid answer drains them, and renegotiation succeeds. Independent reviews confirmed real SDP and authorization failures still propagate. - A real-browser before/after check reduced microphone enumeration from nine calls to one across eight ordinary Host rerenders. Changing the native client or switching to Browser capture still enumerates the new devices. The separate focused contribution #507 preserves input focus during queued floating-panel opening and is not attributed to this phase. - The verified Windows candidate and reproduction steps have been updated in #502. macOS/Linux physical-device coverage and iOS device acceptance keep their documented boundaries. ## Impact / 影响 - Planned release: **v2.5.0**; no public wire or storage version bump. - Android is a sharing and controlling endpoint, not a remotely controlled Android device. iOS is held outside this release. - New Android release target joins the existing complete-target, checksum, GitHub and Gitee artifact pipeline. Signing material stays outside Git; optional input SDKs/drivers are not bundled. - The owner authorized closure and publication after the final design review and acceptance checks. Restore the publication gate after the held focused contribution and merge this complete phase once; CI publishes only after its full checks and registered packages pass. - Current TODO, platform guidance, media/routing evidence and release operations retain their existing document owners; no additional audit ledger or changelog archive is introduced.
764 lines
26 KiB
Go
764 lines
26 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
"log/slog"
|
|
"net"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"sync"
|
|
"syscall"
|
|
"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/mediaedge"
|
|
"github.com/TNTcraftHIM/Piik/internal/app/nativeaudio"
|
|
"github.com/TNTcraftHIM/Piik/internal/app/nativecapture"
|
|
"github.com/TNTcraftHIM/Piik/internal/app/nativecontrol"
|
|
"github.com/TNTcraftHIM/Piik/internal/app/publictunnel"
|
|
"github.com/TNTcraftHIM/Piik/internal/app/remoteinput"
|
|
"github.com/TNTcraftHIM/Piik/internal/diagnostics"
|
|
serverapp "github.com/TNTcraftHIM/Piik/internal/server/app"
|
|
serverconfig "github.com/TNTcraftHIM/Piik/internal/server/config"
|
|
"github.com/TNTcraftHIM/Piik/internal/server/protocol"
|
|
"github.com/TNTcraftHIM/Piik/internal/server/webassets"
|
|
"github.com/TNTcraftHIM/Piik/internal/tlsconfig"
|
|
)
|
|
|
|
const (
|
|
DefaultLocalPort = 8787
|
|
// localShutdownTimeout bounds the ordered Local stop. The packaged smoke
|
|
// allows 10 s between the stop request and the exit, so it stays well
|
|
// inside that budget.
|
|
localShutdownTimeout = 5 * time.Second
|
|
)
|
|
|
|
type Options struct {
|
|
Site string
|
|
SiteSet bool
|
|
Local bool
|
|
Link bool
|
|
ConfigPath string
|
|
LANAddress string
|
|
Port int
|
|
DisableBrowser bool
|
|
Debug bool
|
|
LogDir string
|
|
CaptureProcess string
|
|
TunnelProcess string
|
|
Ready func(string)
|
|
console *console
|
|
logger *slog.Logger
|
|
}
|
|
|
|
func Run(ctx context.Context, options Options) (returnedErr error) {
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
options.Debug = options.Debug || debugEnabled(os.Getenv("PIIK_DEBUG"))
|
|
options.console = newConsole(cancel, options.DisableBrowser)
|
|
var recorder *diagnostics.Recorder
|
|
var diagnosticsMu sync.Mutex
|
|
var restoreLogger func()
|
|
defer func() {
|
|
cancel()
|
|
diagnosticsMu.Lock()
|
|
defer diagnosticsMu.Unlock()
|
|
returnedErr = finishRun(options.console, recorder, restoreLogger, returnedErr)
|
|
}()
|
|
enableDiagnostics := func() error {
|
|
if recorder != nil {
|
|
return nil
|
|
}
|
|
var err error
|
|
recorder, err = openDiagnostics(options.LogDir)
|
|
if err != nil {
|
|
return errors.Join(failure.Diagnostics, fmt.Errorf("Piik App diagnostics are unavailable: %w", err))
|
|
}
|
|
recorder.Context("build", map[string]any{"version": buildinfo.Version(), "revision": buildinfo.Revision})
|
|
previous, previousWriter, previousFlags := slog.Default(), log.Writer(), log.Flags()
|
|
var dependencyLog *diagnostics.LineWriter
|
|
restoreLogger = func() {
|
|
_ = dependencyLog.Close()
|
|
slog.SetDefault(previous)
|
|
log.SetOutput(previousWriter)
|
|
log.SetFlags(previousFlags)
|
|
}
|
|
options.logger = recorder.Logger()
|
|
slog.SetDefault(options.logger)
|
|
dependencyLog = diagnostics.Writer("stdlib")
|
|
log.SetOutput(dependencyLog)
|
|
log.SetFlags(previousFlags)
|
|
options.console.send(consoleDebug{logPath: recorder.LogPath(), export: recorder.Export})
|
|
slog.Debug("piik-client", "event", "diagnostics-enabled")
|
|
return nil
|
|
}
|
|
if options.Debug {
|
|
if err := enableDiagnostics(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
slog.Debug("piik-client", "event", "start", "version", buildinfo.Version(), "revision", buildinfo.Revision)
|
|
options.console.show(consoleView{state: "starting"})
|
|
if err := validateMode(options); err != nil {
|
|
return errors.Join(failure.Configuration, err)
|
|
}
|
|
if options.Port == 0 {
|
|
options.Port = DefaultLocalPort
|
|
}
|
|
configPath := strings.TrimSpace(options.ConfigPath)
|
|
if configPath == "" {
|
|
var err error
|
|
configPath, err = appconfig.DefaultPath()
|
|
if err != nil {
|
|
return errors.Join(failure.Configuration, fmt.Errorf("Piik App configuration is unavailable: %w", err))
|
|
}
|
|
}
|
|
config, err := appconfig.LoadOrCreate(configPath)
|
|
if err != nil {
|
|
return errors.Join(failure.Configuration, fmt.Errorf("Piik App configuration is unavailable: %w", err))
|
|
}
|
|
config, err = applyMode(config, options)
|
|
if err != nil {
|
|
return errors.Join(failure.Configuration, err)
|
|
}
|
|
slog.Debug("piik-client", "event", "configuration", "siteConfigured", config.Site != "", "localAccessProtected", config.LocalAccessPassword != "")
|
|
if options.SiteSet {
|
|
if err = appconfig.Save(configPath, config); err != nil {
|
|
return errors.Join(failure.Configuration, fmt.Errorf("Piik App configuration is unavailable: %w", err))
|
|
}
|
|
}
|
|
nativeMedia := discoverNativeMedia(ctx, options.CaptureProcess)
|
|
input, inputErr := remoteinput.New(ctx, nativeMedia.capture.RemoteInput)
|
|
if inputErr != nil {
|
|
slog.Warn("Remote control is unavailable", "reason", inputErr)
|
|
}
|
|
defer input.Close()
|
|
nativeMedia.capabilities.RemoteInput = input != nil
|
|
nativeMedia.capabilities.RemoteCoPlay = input != nil
|
|
nativeMedia.capabilities.RemoteStopShortcut = input.SupportsStopShortcut()
|
|
recordContext := func(selected Options, config appconfig.Config) {
|
|
if recorder == nil {
|
|
return
|
|
}
|
|
recorder.Context("configuration", map[string]any{"siteConfigured": config.Site != "", "local": selected.Local,
|
|
"link": selected.Link, "port": selected.Port, "lanAddress": selected.LANAddress,
|
|
"localAccessProtected": config.LocalAccessPassword != ""})
|
|
recorder.Context("capture", nativeMedia.capture)
|
|
recorder.Binary("captureExecutable", nativeMedia.captureProcess)
|
|
slog.Debug("piik-client", "event", "native-capabilities",
|
|
"video", nativeMedia.capabilities.Video, "processAudio", nativeMedia.capabilities.ProcessAudio,
|
|
"systemAudio", nativeMedia.capabilities.SystemAudio, "hardwareH264", nativeMedia.capabilities.HardwareH264,
|
|
"softwareVP8", nativeMedia.capabilities.SoftwareVP8, diagnostics.Error(nativeMedia.discoveryErr))
|
|
}
|
|
recordContext(options, config)
|
|
qualityPreference, preferenceErr := appconfig.LoadQualityPreference(configPath)
|
|
if preferenceErr != nil {
|
|
slog.Debug("piik-client", "event", "quality-preference-load-failed", diagnostics.Error(preferenceErr))
|
|
}
|
|
control, err := loopback.Start(ctx, loopback.Options{
|
|
AllowedOrigins: allowedOrigins(config.ApprovedSites(), options.Port),
|
|
NativeMedia: nativeMedia.capabilities,
|
|
NewControl: nativeMedia.controlFactory(input),
|
|
Presentation: options.console.setLanguage,
|
|
QualityPreference: qualityPreference,
|
|
})
|
|
if err != nil {
|
|
return errors.Join(failure.NativeControl, fmt.Errorf("Piik App native control could not start: %w", err))
|
|
}
|
|
defer func() { returnedErr = errors.Join(returnedErr, control.Close()) }()
|
|
slog.Debug("piik-client", "event", "control-ready")
|
|
if options.console.machine {
|
|
if err = printEndpoint(control.Endpoint()); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return runLauncher(ctx, options, configPath, config, control, func(selected *Options, config appconfig.Config) error {
|
|
diagnosticsMu.Lock()
|
|
defer diagnosticsMu.Unlock()
|
|
if ctx.Err() != nil {
|
|
return ctx.Err()
|
|
}
|
|
if selected.Debug {
|
|
if err := enableDiagnostics(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if recorder != nil {
|
|
selected.Debug = true
|
|
selected.logger = recorder.Logger()
|
|
}
|
|
recordContext(*selected, config)
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func finishRun(console *console, recorder *diagnostics.Recorder, restoreLogger func(), err error) error {
|
|
err = errors.Join(err, console.stop(err))
|
|
exportPath := ""
|
|
if recorder != nil {
|
|
slog.Debug("piik-client", "event", "stopped", "failed", err != nil, diagnostics.Error(err))
|
|
if console.program == nil || err != nil {
|
|
path, exportErr := recorder.Export()
|
|
console.send(consoleExportResult{path: path, err: exportErr})
|
|
err = errors.Join(err, exportErr)
|
|
}
|
|
restoreLogger()
|
|
err = errors.Join(err, recorder.Close())
|
|
exportPath = recorder.LastExportPath()
|
|
}
|
|
console.finish(err, exportPath)
|
|
return err
|
|
}
|
|
|
|
func explicitMode(options Options) bool {
|
|
return options.SiteSet || options.Local || options.Link || options.DisableBrowser
|
|
}
|
|
|
|
func runLauncher(
|
|
ctx context.Context,
|
|
options Options,
|
|
configPath string,
|
|
config appconfig.Config,
|
|
control *loopback.Server,
|
|
configureDiagnostics func(*Options, appconfig.Config) error,
|
|
) (returnedErr error) {
|
|
automatic := explicitMode(options)
|
|
directRoom := automatic && (options.Local || options.Link || config.Site == "")
|
|
initialOptions := options
|
|
initialConfig := config
|
|
mode, err := launcher.LoadMode(configPath)
|
|
if err != nil {
|
|
slog.Debug("Could not restore the last launcher mode", diagnostics.Error(err))
|
|
}
|
|
startupSite, err := launcher.LoadStartupSite(configPath, config.ApprovedSites()...)
|
|
if err != nil {
|
|
slog.Debug("Could not restore the startup Site", diagnostics.Error(err))
|
|
}
|
|
// Site approval and room startup mutate separate fields of one config.
|
|
// Serialize persistence and publish only the complete successfully saved value.
|
|
var configMu sync.Mutex
|
|
launcherOrigin := ""
|
|
updateConfig := func(change func(*appconfig.Config)) (appconfig.Config, error) {
|
|
configMu.Lock()
|
|
defer configMu.Unlock()
|
|
next := config
|
|
change(&next)
|
|
if err := appconfig.Save(configPath, next); err != nil {
|
|
return config, err
|
|
}
|
|
config = next
|
|
err := control.SetAllowedOrigins(append(allowedOrigins(config.ApprovedSites(), initialOptions.Port), launcherOrigin))
|
|
return config, err
|
|
}
|
|
launch, err := launcher.Start(
|
|
ctx,
|
|
webassets.FS(),
|
|
launcher.Options{
|
|
DefaultMode: mode,
|
|
StartupSite: startupSite,
|
|
SaveStartupSite: func(site string) error {
|
|
return launcher.SaveStartupSite(configPath, site)
|
|
},
|
|
Sites: config.ApprovedSites(), Version: buildinfo.Version(), Revision: buildinfo.Revision,
|
|
ControlPort: control.Endpoint().Port,
|
|
LocalAccessPassword: config.LocalAccessPassword,
|
|
Debug: options.Debug, LANAddress: options.LANAddress,
|
|
ChangeSite: func(site string, allowed bool) ([]string, error) {
|
|
current, err := updateConfig(func(next *appconfig.Config) {
|
|
sites := next.ApprovedSites()
|
|
if allowed {
|
|
if !slices.Contains(sites, site) {
|
|
sites = append(sites, site)
|
|
}
|
|
} else {
|
|
sites = slices.DeleteFunc(sites, func(value string) bool { return value == site })
|
|
}
|
|
next.SetApprovedSites(sites)
|
|
})
|
|
return current.ApprovedSites(), err
|
|
},
|
|
EnableDebug: func() error {
|
|
configMu.Lock()
|
|
defer configMu.Unlock()
|
|
selected := initialOptions
|
|
selected.Debug = true
|
|
return configureDiagnostics(&selected, config)
|
|
},
|
|
},
|
|
)
|
|
if err != nil {
|
|
return errors.Join(failure.Launcher, err)
|
|
}
|
|
defer func() { returnedErr = errors.Join(returnedErr, launch.Close()) }()
|
|
control.SetConnectURL(launch.URL())
|
|
defer control.SetConnectURL("")
|
|
// Launcher entry lives with the resident App, independently of room views.
|
|
options.console.send(consoleLauncher(launch.URL()))
|
|
defer options.console.send(consoleLauncher(""))
|
|
launchAddress, err := url.Parse(launch.URL())
|
|
if err != nil {
|
|
return errors.Join(failure.Launcher, err)
|
|
}
|
|
configMu.Lock()
|
|
launcherOrigin = launchAddress.Scheme + "://" + launchAddress.Host
|
|
err = control.SetAllowedOrigins(append(allowedOrigins(config.ApprovedSites(), options.Port), launcherOrigin))
|
|
configMu.Unlock()
|
|
if err != nil {
|
|
return errors.Join(failure.NativeControl, err)
|
|
}
|
|
if !automatic && startupSite == "" {
|
|
options.console.show(consoleView{state: "setup", entry: launch.URL()})
|
|
options.console.openBrowser(launch.URL())
|
|
} else if initialConfig.Site != "" && !options.Local && !options.Link {
|
|
// A starred Site changes only the opening page. --site also remains an
|
|
// explicit approval shortcut; the launcher can start a Local room later.
|
|
site := initialConfig.Site
|
|
if !automatic {
|
|
site = startupSite
|
|
}
|
|
openSite(site, options, control.Endpoint().Port)
|
|
}
|
|
// The resident App outlives a failed interactive room. Retry only after the
|
|
// previous runtime has joined, leaving unrelated Site/native sessions intact.
|
|
runRoom := func(selection launcher.Selection) error {
|
|
options := initialOptions
|
|
if selection.Language != "" {
|
|
options.console.setLanguage(selection.Language)
|
|
}
|
|
if err := launcher.SaveMode(configPath, selection.Mode); err != nil {
|
|
slog.Debug("Could not save the launcher mode", diagnostics.Error(err))
|
|
}
|
|
options.Local = selection.Mode == launcher.ModeLocal
|
|
options.Link = selection.Mode == launcher.ModeLink
|
|
options.Debug = options.Debug || selection.Debug
|
|
if selection.LANAddress != nil {
|
|
options.LANAddress = *selection.LANAddress
|
|
}
|
|
selectedConfig, err := updateConfig(func(next *appconfig.Config) {
|
|
next.LocalAccessPassword = selection.LocalAccessPassword
|
|
})
|
|
if err != nil {
|
|
err = errors.Join(failure.Configuration, fmt.Errorf("Piik App configuration could not be saved: %w", err))
|
|
return err
|
|
}
|
|
if err = configureDiagnostics(&options, selectedConfig); err != nil {
|
|
return err
|
|
}
|
|
if !directRoom {
|
|
options.DisableBrowser = true
|
|
}
|
|
ready := make(chan string, 1)
|
|
onReady := options.Ready
|
|
options.Ready = func(target string) {
|
|
if onReady != nil {
|
|
onReady(target)
|
|
}
|
|
ready <- target
|
|
}
|
|
runtimeContext, cancelRuntime := context.WithCancel(ctx)
|
|
runtimeDone := make(chan struct{})
|
|
var runtimeErr error
|
|
go func() {
|
|
runtimeErr = runLocal(runtimeContext, options, selectedConfig, control)
|
|
close(runtimeDone)
|
|
}()
|
|
var exitErr error
|
|
select {
|
|
case target := <-ready:
|
|
launch.SetResult(target, nil)
|
|
select {
|
|
case <-runtimeDone:
|
|
case exitErr = <-launch.Done():
|
|
case <-ctx.Done():
|
|
}
|
|
case <-runtimeDone:
|
|
case <-ctx.Done():
|
|
launch.SetResult("", ctx.Err())
|
|
}
|
|
cancelRuntime()
|
|
<-runtimeDone
|
|
return errors.Join(exitErr, runtimeErr)
|
|
}
|
|
var roomErr error
|
|
for {
|
|
var selection launcher.Selection
|
|
if directRoom {
|
|
selection = launcher.Selection{Mode: launcher.ModeLocal, LocalAccessPassword: initialConfig.LocalAccessPassword}
|
|
if initialOptions.Link {
|
|
selection.Mode = launcher.ModeLink
|
|
}
|
|
launch.Begin()
|
|
} else {
|
|
select {
|
|
case selection = <-launch.Selection():
|
|
case <-ctx.Done():
|
|
return roomErr
|
|
case err := <-control.Done():
|
|
return errors.Join(roomErr, err)
|
|
case err := <-launch.Done():
|
|
if err != nil {
|
|
return errors.Join(roomErr, failure.Launcher, fmt.Errorf("Piik App launcher stopped unexpectedly: %w", err))
|
|
}
|
|
return roomErr
|
|
}
|
|
}
|
|
roomErr = runRoom(selection)
|
|
if directRoom || roomErr == nil || ctx.Err() != nil {
|
|
return roomErr
|
|
}
|
|
launch.SetResult("", roomErr)
|
|
initialOptions.console.show(consoleView{state: "setup", entry: launch.URL(), problem: roomErr})
|
|
slog.Debug("piik-client", "event", "local-room-stopped", diagnostics.Error(roomErr))
|
|
}
|
|
}
|
|
|
|
func applyMode(config appconfig.Config, options Options) (appconfig.Config, error) {
|
|
if options.SiteSet && options.Local {
|
|
return appconfig.Config{}, errors.New("choose either --site or --local")
|
|
}
|
|
if options.SiteSet {
|
|
site, err := appconfig.NormalizeSite(options.Site)
|
|
if err != nil || site == "" {
|
|
return appconfig.Config{}, errors.New("Piik Site must be an HTTP or HTTPS origin")
|
|
}
|
|
sites, _ := appconfig.NormalizeSites(append([]string{site}, config.ApprovedSites()...))
|
|
config.SetApprovedSites(sites)
|
|
}
|
|
return config, nil
|
|
}
|
|
|
|
func validateMode(options Options) error {
|
|
if options.Link && (options.SiteSet || options.Local) {
|
|
return errors.New("--link is a self-contained Local mode")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func debugEnabled(value string) bool {
|
|
for _, component := range strings.Split(value, ",") {
|
|
if strings.TrimSpace(component) == "client" {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func openDiagnostics(directory string) (*diagnostics.Recorder, error) {
|
|
if directory = strings.TrimSpace(directory); directory == "" {
|
|
directory = strings.TrimSpace(os.Getenv("PIIK_LOG_DIR"))
|
|
}
|
|
if directory != "" {
|
|
return diagnostics.Open(directory, "client", buildinfo.Revision)
|
|
}
|
|
executable, err := os.Executable()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
recorder, err := diagnostics.Open(filepath.Join(filepath.Dir(executable), "logs"), "client", buildinfo.Revision)
|
|
if err == nil || !errors.Is(err, os.ErrPermission) && !errors.Is(err, syscall.EROFS) {
|
|
return recorder, err
|
|
}
|
|
cache, cacheErr := os.UserCacheDir()
|
|
if cacheErr != nil {
|
|
return nil, errors.Join(err, cacheErr)
|
|
}
|
|
return diagnostics.Open(filepath.Join(cache, "Piik", "logs"), "client", buildinfo.Revision)
|
|
}
|
|
|
|
func openSite(site string, options Options, controlPort int) {
|
|
slog.Debug("piik-client", "event", "mode", "mode", "site")
|
|
targetURL := launcher.EntryURL(site, controlPort)
|
|
view := consoleView{mode: "site", state: "ready", entry: targetURL}
|
|
options.console.show(view)
|
|
if options.console.machine {
|
|
fmt.Printf("Piik Site: %s\n", site)
|
|
}
|
|
if !options.DisableBrowser {
|
|
options.console.openBrowser(targetURL)
|
|
}
|
|
if options.Ready != nil {
|
|
options.Ready(targetURL)
|
|
}
|
|
slog.Debug("piik-client", "event", "site-ready")
|
|
}
|
|
|
|
func runLocal(ctx context.Context, options Options, config appconfig.Config,
|
|
control *loopback.Server,
|
|
) (returnedErr error) {
|
|
stunURLs, natPredictionStunURLs, err := localSTUNURLs(options.Link)
|
|
if err != nil {
|
|
return errors.Join(failure.Configuration, err)
|
|
}
|
|
view := consoleView{mode: "local", state: "starting", protected: config.LocalAccessPassword != ""}
|
|
if options.Link {
|
|
view.mode = "link"
|
|
}
|
|
slog.Debug("piik-client", "event", "mode", "mode", view.mode)
|
|
options.console.show(view)
|
|
bindAddress := net.IPv4zero
|
|
if !options.Link {
|
|
bindAddress = net.IPv4(127, 0, 0, 1)
|
|
}
|
|
listener, err := net.ListenTCP("tcp4", &net.TCPAddr{IP: bindAddress, Port: options.Port})
|
|
if err != nil {
|
|
return errors.Join(failure.LocalPort, fmt.Errorf("local server port is unavailable: %w", err))
|
|
}
|
|
listeners := []net.Listener{listener}
|
|
defer func() {
|
|
for _, owned := range listeners {
|
|
_ = owned.Close()
|
|
}
|
|
}()
|
|
addresses, lanErr := lan.Addresses()
|
|
selectedAddress := ""
|
|
if lanErr == nil {
|
|
selectedAddress, lanErr = lan.Select(addresses, options.LANAddress)
|
|
}
|
|
// A public invitation only needs the tunnel origin; keep any resolved LAN
|
|
// addresses as allowed local origins but never block link startup on LAN
|
|
// selection (multi-interface hosts may not have one obvious address).
|
|
if lanErr != nil && !options.Link {
|
|
return errors.Join(failure.LocalNetwork, lanErr)
|
|
}
|
|
if !options.Link {
|
|
configuration, err := tlsconfig.SelfSigned(selectedAddress)
|
|
if err != nil {
|
|
return errors.Join(failure.LocalServer, fmt.Errorf("local HTTPS certificate could not be created: %w", err))
|
|
}
|
|
secureListener, err := net.ListenTCP("tcp4", &net.TCPAddr{IP: net.ParseIP(selectedAddress), Port: options.Port})
|
|
if err != nil {
|
|
return errors.Join(failure.LocalPort, fmt.Errorf("local HTTPS port is unavailable: %w", err))
|
|
}
|
|
listeners = append(listeners, tls.NewListener(secureListener, configuration))
|
|
view.certificate = tlsconfig.Fingerprint(configuration)
|
|
}
|
|
var tunnel *publictunnel.Process
|
|
publicOrigin := ""
|
|
if options.Link {
|
|
tunnel, err = publictunnel.Start(
|
|
ctx,
|
|
tunnelExecutable(options.TunnelProcess),
|
|
fmt.Sprintf("http://127.0.0.1:%d", options.Port),
|
|
"",
|
|
)
|
|
if err != nil {
|
|
// Only the caller's clean cancellation is a normal stopped outcome.
|
|
// A startup or retirement failure racing cancellation remains an error.
|
|
if err == context.Canceled && ctx.Err() == context.Canceled {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
defer func() { returnedErr = errors.Join(returnedErr, tunnel.Close()) }()
|
|
publicOrigin = tunnel.Origin()
|
|
slog.Debug("piik-client", "event", "public-link-ready")
|
|
}
|
|
|
|
localConfig, err := serverconfig.Local(serverconfig.LocalOptions{
|
|
Port: options.Port,
|
|
PublicAddress: selectedAddress,
|
|
PublicOrigin: publicOrigin,
|
|
AllowedAddresses: addresses,
|
|
SiteAccessPassword: config.LocalAccessPassword,
|
|
STUNURLs: stunURLs,
|
|
NATPredictionSTUNURLs: natPredictionStunURLs,
|
|
})
|
|
if err != nil {
|
|
return errors.Join(failure.Configuration, err)
|
|
}
|
|
logger := options.logger
|
|
if logger == nil {
|
|
logger = slog.New(slog.NewTextHandler(options.console.logWriter(), nil))
|
|
}
|
|
localServer, err := serverapp.New(serverapp.Options{
|
|
Config: localConfig,
|
|
Listeners: listeners,
|
|
Assets: webassets.FS(),
|
|
Logger: logger,
|
|
})
|
|
if err != nil {
|
|
return errors.Join(failure.LocalServer, err)
|
|
}
|
|
listeners = nil // Ownership is now in the application's startup/shutdown path.
|
|
defer func() { returnedErr = errors.Join(returnedErr, endLocalServer(localServer)) }()
|
|
if _, err = localServer.Listen(ctx); err != nil {
|
|
var socketError *net.OpError
|
|
if errors.As(err, &socketError) && socketError.Op == "listen" {
|
|
return errors.Join(failure.LocalPort, err)
|
|
}
|
|
return errors.Join(failure.LocalServer, err)
|
|
}
|
|
slog.Debug("piik-client", "event", "local-server-ready", "port", options.Port, "publicLink", publicOrigin != "")
|
|
|
|
// The readiness lines follow the listener, which the packaged smoke and the
|
|
// public-link gate both read from stdout before they probe the port.
|
|
if options.console.machine {
|
|
if config.LocalAccessPassword == "" {
|
|
fmt.Println("Local access: open")
|
|
} else {
|
|
fmt.Printf("Local access password: %s\n", config.LocalAccessPassword)
|
|
}
|
|
if publicOrigin != "" {
|
|
fmt.Printf("Public invitation origin: %s\n", publicOrigin)
|
|
} else {
|
|
fmt.Printf("LAN invitation origin: %s\n", serverconfig.Origin(localConfig.PublicBaseURL))
|
|
fmt.Printf("LAN certificate SHA-256: %s\n", view.certificate)
|
|
}
|
|
}
|
|
launchURL := launcher.LocalEntryURL(
|
|
fmt.Sprintf("http://127.0.0.1:%d/", options.Port),
|
|
config.LocalAccessPassword,
|
|
control.Endpoint().Port,
|
|
)
|
|
view.state, view.entry, view.invite = "ready", launchURL, publicOrigin
|
|
if view.invite == "" {
|
|
view.invite = serverconfig.Origin(localConfig.PublicBaseURL)
|
|
}
|
|
options.console.show(view)
|
|
if !options.DisableBrowser {
|
|
options.console.openBrowser(launchURL)
|
|
}
|
|
if options.Ready != nil {
|
|
options.Ready(launchURL)
|
|
}
|
|
var tunnelDone <-chan struct{}
|
|
if tunnel != nil {
|
|
tunnelDone = tunnel.Done()
|
|
}
|
|
|
|
select {
|
|
case <-ctx.Done():
|
|
return nil
|
|
case err = <-localServer.Done():
|
|
return errors.Join(failure.LocalServer, err)
|
|
case err = <-control.Done():
|
|
if err != nil {
|
|
return errors.Join(failure.NativeControl, fmt.Errorf("Piik App native control stopped unexpectedly: %w", err))
|
|
}
|
|
return nil
|
|
case <-tunnelDone:
|
|
if ctx.Err() != nil {
|
|
return nil
|
|
}
|
|
return errors.Join(failure.TunnelStopped, errors.New("public invitation link stopped"))
|
|
}
|
|
}
|
|
|
|
type nativeRuntime struct {
|
|
capabilities loopback.NativeMediaCapabilities
|
|
capture nativecapture.Capabilities
|
|
captureProcess string
|
|
discoveryErr error
|
|
}
|
|
|
|
func (runtime nativeRuntime) controlFactory(input *remoteinput.Manager) func() loopback.ControlSession {
|
|
return func() loopback.ControlSession {
|
|
return nativecontrol.New(runtime.captureProcess, runtime.capture, true, input)
|
|
}
|
|
}
|
|
|
|
func allowedOrigins(sites []string, localPort int) []string {
|
|
origins := []string{
|
|
fmt.Sprintf("http://localhost:%d", localPort),
|
|
fmt.Sprintf("http://127.0.0.1:%d", localPort),
|
|
}
|
|
return append(origins, sites...)
|
|
}
|
|
|
|
func discoverNativeMedia(ctx context.Context, configuredPath string) nativeRuntime {
|
|
path := strings.TrimSpace(configuredPath)
|
|
if path == "" {
|
|
path = nativecapture.PackagedExecutable()
|
|
}
|
|
capabilities, err := nativecapture.Discover(ctx, path)
|
|
if err != nil {
|
|
return nativeRuntime{captureProcess: path, discoveryErr: err,
|
|
capabilities: loopback.NativeMediaCapabilities{ReceiverReuse: true, MaxRelayChildren: mediaedge.MaxRelayChildren}}
|
|
}
|
|
summary := capabilities.Summary()
|
|
return nativeRuntime{
|
|
captureProcess: path,
|
|
capture: capabilities,
|
|
capabilities: loopback.NativeMediaCapabilities{
|
|
MaxVideoResolution: protocol.QualityResolution{
|
|
Width: nativecapture.MaxVideoDimension, Height: nativecapture.MaxVideoDimension,
|
|
},
|
|
MaxVideoPixels: nativecapture.MaxVideoPixels,
|
|
MaxVideoMacroblocksPerSecond: nativecapture.MaxVideoMacroblocksPerSecond,
|
|
MaxVideoFramerate: nativecapture.MaxVideoFramerate,
|
|
MaxVideoBitrate: nativecapture.MaxVideoBitrate,
|
|
MaxAudioBitrate: nativeaudio.MaxSendBitrate,
|
|
ReceiverReuse: true,
|
|
MaxRelayChildren: mediaedge.MaxRelayChildren,
|
|
Video: summary.Video,
|
|
CaptureBorderControl: summary.CaptureBorderControl,
|
|
Microphone: summary.Microphone,
|
|
ShareVolume: true,
|
|
ProcessAudio: summary.ProcessAudio,
|
|
ProcessAudioExclusion: summary.ProcessAudioExclusion,
|
|
SystemAudio: summary.SystemAudio,
|
|
HardwareH264: summary.HardwareH264,
|
|
SoftwareVP8: summary.SoftwareVP8,
|
|
},
|
|
}
|
|
}
|
|
|
|
// localSTUNURLs supplies public-link discovery. Pure LAN discovery is composed
|
|
// by serverconfig.Local on the selected interface. Only --link configures public
|
|
// STUN and the bounded NAT prediction survey. Native edges use that authority;
|
|
// serverconfig.Local deliberately does not inherit Hosted environment settings.
|
|
// An explicit App override replaces every public destination, including the
|
|
// survey pair. With fewer than three servers only ordinary ICE is available.
|
|
func localSTUNURLs(link bool) (stunURLs []string, natPredictionSTUNURLs []string, err error) {
|
|
if !link {
|
|
return nil, nil, nil
|
|
}
|
|
return appconfig.PublicSTUNURLs(os.Getenv("PIIK_STUN_URLS"))
|
|
}
|
|
|
|
// endLocalServer is step 1 of the ADR-0010 item 12 unwind: every room ends,
|
|
// signaling closes, the listener drains and the room store closes, all before
|
|
// the tunnel and the loopback server go away. serverapp.End is memoised, so the
|
|
// deferred call after an explicit stop is a no-op. A serve loop or a drain that
|
|
// was already stopping reports http.ErrServerClosed or context.Canceled;
|
|
// neither is an App failure, and Run turns any error into exit status 1,
|
|
// which is exactly what the packaged smoke fails on.
|
|
func endLocalServer(server *serverapp.Server) error {
|
|
ctx, cancel := context.WithTimeout(context.Background(), localShutdownTimeout)
|
|
defer cancel()
|
|
err := server.End(ctx)
|
|
if errors.Is(err, http.ErrServerClosed) || errors.Is(err, context.Canceled) {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
|
|
func tunnelExecutable(configured string) string {
|
|
if path := strings.TrimSpace(configured); path != "" {
|
|
return filepath.Clean(path)
|
|
}
|
|
return publictunnel.PackagedExecutable()
|
|
}
|
|
|
|
func printEndpoint(endpoint loopback.Endpoint) error {
|
|
payload, err := json.Marshal(endpoint)
|
|
if err != nil {
|
|
return errors.New("Piik App endpoint is unavailable")
|
|
}
|
|
fmt.Println(string(payload))
|
|
return nil
|
|
}
|