diff --git a/cmd/piik-app/main.go b/cmd/piik-app/main.go index cf4252ba5..5b33788be 100644 --- a/cmd/piik-app/main.go +++ b/cmd/piik-app/main.go @@ -36,6 +36,7 @@ func run(args []string) int { flags.BoolVar(&options.Debug, "debug", false, "save opt-in App diagnostics to rotated files") flags.StringVar(&options.LogDir, "log-dir", "", "diagnostic directory (overrides PIIK_LOG_DIR)") flags.StringVar(&options.CaptureProcess, "capture-process", "", "path to the platform native capture process") + flags.StringVar(&options.PublicOrigin, "public-origin", "", "fixed HTTPS origin for an externally forwarded --link") flags.StringVar(&options.TunnelProcess, "tunnel-process", "", "path to the packaged public tunnel process") flags.StringVar(&options.ConfigPath, "config", "", "path to the App configuration file") flags.StringVar(&options.LANAddress, "lan-address", "", "LAN IPv4 address used in Local invitations") diff --git a/docs/operations/self-hosting.md b/docs/operations/self-hosting.md index 4ab2c5602..4f1eb2e92 100644 --- a/docs/operations/self-hosting.md +++ b/docs/operations/self-hosting.md @@ -246,6 +246,10 @@ equal `SFU_UDP_PORT`. Do not forward Piik's STUN ports through frp: its UDP prox STUN would report. Restart Piik and check HTTPS health, room access and **Server media** from another network. +### Forward a Piik App invitation + +Run `piik-app --link --public-origin https://share.example.com --port 8787` on the App computer. Use the public HTTPS reverse proxy described above and only the `piik-web` TCP proxy in frpc, targeting `127.0.0.1:8787`. Start frps and frpc yourself. The Host opens its loopback entry. Viewer invitations and QR codes use the fixed HTTPS domain. Room access settings still apply. App media uses WebRTC P2P. Use `--local` for LAN invitations. + ### Using a managed frp provider Services such as SakuraFrp supply the public server and their own client or diff --git a/docs/operations/self-hosting.zh-CN.md b/docs/operations/self-hosting.zh-CN.md index 7345a8101..b5f9ef2fc 100644 --- a/docs/operations/self-hosting.zh-CN.md +++ b/docs/operations/self-hosting.zh-CN.md @@ -221,6 +221,10 @@ frpc 的本地与公网 UDP 端口必须都等于 `SFU_UDP_PORT`。 [改变 STUN 所观察到的源地址映射](../research/nat-traversal.md#external-forwarding-and-participant-relays)。 重启 Piik,从另一网络验证 HTTPS 健康检查、房间访问及**服务器转发**。 +### 转发 Piik App 的公网邀请 + +在 App 电脑上运行 `piik-app --link --public-origin https://share.example.com --port 8787`。公网主机使用上文的 HTTPS 反向代理,frpc 仅配置 `piik-web` TCP 代理并转发至 `127.0.0.1:8787`。自行启动 frps 和 frpc。主持人打开本机回环入口,观众邀请和二维码使用固定的 HTTPS 域名。房间访问设置继续生效,音视频使用 WebRTC P2P。仅在局域网分享时使用 `--local`。 + ### 使用托管 frp 服务 SakuraFrp 等服务会提供公网服务器和自己的客户端或启动器;按服务商教程配置, diff --git a/docs/research/cross-platform-client-runtime.md b/docs/research/cross-platform-client-runtime.md index 60a57e74b..148b44ffa 100644 --- a/docs/research/cross-platform-client-runtime.md +++ b/docs/research/cross-platform-client-runtime.md @@ -221,7 +221,9 @@ No matched evidence establishes a universally more reachable replacement for Quick Tunnel, especially across mainland networks. These are candidates, not accepted Piik integrations. Viewers must only open the invitation in a Browser; requiring their own account, extra software or proxy/network configuration is -outside this path. Operator-managed tunnels belong with self-hosted Sites. +outside this path. An operator-managed HTTPS tunnel can also publish the App's +local room service at a fixed origin. The operator runs the tunnel and Piik +generates invitations for that origin. [App setup with frp](../operations/self-hosting.md#forward-a-piik-app-invitation) | Candidate | Relevant capability | Cost to Piik's account-free invitation | | --- | --- | --- | diff --git a/internal/app/app.go b/internal/app/app.go index ab85b19a5..f7db6ada2 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -59,6 +59,7 @@ type Options struct { Debug bool LogDir string CaptureProcess string + PublicOrigin string TunnelProcess string Ready func(string) localReady func(string, string) @@ -456,6 +457,9 @@ func applyMode(config appconfig.Config, options Options) (appconfig.Config, erro } func validateMode(options Options) error { + if options.PublicOrigin != "" && !options.Link { + return errors.New("--public-origin requires --link") + } if options.Link && (options.SiteSet || options.Local) { return errors.New("--link is a self-contained Local mode") } @@ -524,7 +528,7 @@ func runLocal(ctx context.Context, options Options, config appconfig.Config, slog.Debug("piik-client", "event", "mode", "mode", view.mode) options.console.show(view) bindAddress := net.IPv4zero - if !options.Link { + if !options.Link || options.PublicOrigin != "" { bindAddress = net.IPv4(127, 0, 0, 1) } listener, err := net.ListenTCP("tcp4", &net.TCPAddr{IP: bindAddress, Port: options.Port}) @@ -561,8 +565,8 @@ func runLocal(ctx context.Context, options Options, config appconfig.Config, view.certificate = tlsconfig.Fingerprint(configuration) } var tunnel *publictunnel.Process - publicOrigin := "" - if options.Link { + publicOrigin := options.PublicOrigin + if options.Link && publicOrigin == "" { tunnel, err = publictunnel.Start( ctx, tunnelExecutable(options.TunnelProcess), @@ -638,10 +642,7 @@ func runLocal(ctx context.Context, options Options, config appconfig.Config, config.LocalAccessPassword, control.Endpoint().Port, ) - view.state, view.entry, view.invite = "ready", launchURL, publicOrigin - if view.invite == "" { - view.invite = serverconfig.Origin(localConfig.PublicBaseURL) - } + view.state, view.entry, view.invite = "ready", launchURL, serverconfig.Origin(localConfig.PublicBaseURL) options.console.show(view) if !options.DisableBrowser { options.console.openBrowser(launchURL) diff --git a/internal/app/local_https_test.go b/internal/app/local_https_test.go index 38d3d8960..14b2f715f 100644 --- a/internal/app/local_https_test.go +++ b/internal/app/local_https_test.go @@ -202,3 +202,55 @@ func TestLocalPortCollisionReleasesTheOtherEntry(t *testing.T) { }) } } + +func TestFixedPublicInviteWithoutTunnelProcess(t *testing.T) { + reservation, err := net.Listen("tcp4", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + port := reservation.Addr().(*net.TCPAddr).Port + reservation.Close() + ctx, cancel := context.WithCancel(t.Context()) + control, err := loopback.Start(ctx, loopback.Options{}) + if err != nil { + cancel() + t.Fatal(err) + } + defer control.Close() + ready, done := make(chan string, 1), make(chan error, 1) + const origin = "https://share.example.test" + go func() { + done <- runLocal(ctx, Options{Link: true, PublicOrigin: origin, Port: port, TunnelProcess: "missing-test-helper", DisableBrowser: true, console: newConsole(cancel, true), localReady: func(_ string, invite string) { ready <- invite }}, appconfig.Config{}, control) + }() + defer cancel() + select { + case err := <-done: + t.Fatalf("startup: %v", err) + case invite := <-ready: + if invite != origin { + t.Fatal(invite) + } + case <-time.After(5 * time.Second): + t.Fatal("startup timeout") + } + base := fmt.Sprintf("http://127.0.0.1:%d", port) + req, err := http.NewRequest(http.MethodPost, base+"/api/rooms", strings.NewReader(`{"codeEntryPolicy":"private"}`)) + if err != nil { + t.Fatal(err) + } + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Origin", origin) + resp, err := http.DefaultClient.Do(req) + if err != nil { + t.Fatal(err) + } + defer resp.Body.Close() + var room protocol.CreateRoomResponse + if err = json.NewDecoder(resp.Body).Decode(&room); err != nil || resp.StatusCode != http.StatusCreated || !strings.HasPrefix(room.InviteURL, origin+"/r/") { + t.Fatalf("fixed invitation: status=%d err=%v", resp.StatusCode, err) + } + cancel() + if err = <-done; err != nil { + t.Fatal(err) + } +} diff --git a/internal/server/config/local.go b/internal/server/config/local.go index 1c5cd0e2c..717c5c2b1 100644 --- a/internal/server/config/local.go +++ b/internal/server/config/local.go @@ -137,8 +137,8 @@ func allValidStunURLs(values []string) bool { func publicHTTPSOrigin(value string) (*url.URL, error) { parsed, err := url.Parse(value) if err != nil || parsed.Scheme != "https" || parsed.Host == "" || - hasUserinfo(parsed) || (parsed.Path != "" && parsed.Path != "/") || - parsed.RawQuery != "" || parsed.Fragment != "" { + parsed.User != nil || (parsed.EscapedPath() != "" && parsed.EscapedPath() != "/") || + parsed.ForceQuery || parsed.RawQuery != "" || strings.Contains(value, "#") { return nil, errors.New("Local public origin must be an HTTPS origin") } host := normalizedHost(parsed) diff --git a/internal/server/config/local_test.go b/internal/server/config/local_test.go index 47a424783..1cea6785d 100644 --- a/internal/server/config/local_test.go +++ b/internal/server/config/local_test.go @@ -245,3 +245,11 @@ func TestLocalRejects(t *testing.T) { }) } } + +func TestFixedHTTPSOriginRejectsExtraURLComponents(t *testing.T) { + for _, value := range []string{"http://share.example", "https://user@share.example", "https://@share.example", "https://share.example/path", "https://share.example/%2f", "https://share.example?x", "https://share.example?", "https://share.example#x", "https://share.example#"} { + if _, err := publicHTTPSOrigin(value); err == nil { + t.Errorf("accepted %q", value) + } + } +}