Piik now has one public presentation and a coordinated App/Server release path. This phase unifies the room UI, rewrites the bilingual README and guides, adds a static website with an inline product film, and prepares matching GitHub/Gitee packages and update links. The film uses the current homepage and shared product controls on one seekable timeline. Runtime fixes preserve presentation when the App opens a room, normalize Site origins, and keep capture and media-recovery status in the television indicator. New visits use Chinese for a Chinese system language and English otherwise; explicit language and visual-mode choices remain available. An optional site passphrase controls site entry, while room authorization remains enforced. GitHub is the release authority. Gitee mirrors verified packages and provides update metadata when GitHub is unavailable. Reviewed PR release notes become the GitHub Release description. The separate public demo uses P2P only, with HTTPS and STUN verified. Validation: 756 Web tests, TypeScript and production build; Go core tests, formatting, vet and available target builds; bounded Windows capture and App/Site interoperability; browser film checks, audible playback and narrow layouts; anonymous Gitee Windows-package download, checksum and launcher/update-link checks. Public demo acceptance covers authenticated WebSocket signaling, synthetic P2P video and room cleanup on one network. macOS/Linux physical capture and the wider device/network/endurance matrix remain explicitly deferred. Publication builds every package from the merged source SHA. The repository is public, main protection is active, and Pages is configured for piik.tv. CI/publisher and Pages deployment records own publication results. ## Release notes ### 中文 Piik 是免费开源的屏幕共享工具。开个房间,发出邀请,最多 20 位朋友就能用浏览器加入观看。 - 使用浏览器或 Piik App 分享游戏、窗口和整个屏幕。 - 优先建立直连;自托管站点可开启媒体转发作为备用线路。 - Piik App 支持本地房间、临时公网邀请和已有站点。 - Piik Server 内置网页界面、房间管理和连接服务,方便自行部署。 - 提供中英文界面与使用指南,以及 GitHub、Gitee 两条下载线路。 [官网](https://piik.tv) · [在线体验](https://demo.piik.tv) · [使用指南](https://github.com/TNTcraftHIM/Piik/blob/main/docs/guide/getting-started.zh-CN.md) · [自行部署](https://github.com/TNTcraftHIM/Piik/blob/main/docs/operations/self-hosting.zh-CN.md) · [Gitee 下载](https://gitee.com/TNTcraftHIM/Piik/releases) Windows App 和桌面浏览器是本次主要验证的平台。macOS 和 Linux 尚未经过实机测试,欢迎有设备的朋友测试并反馈。演示站和 App 公网邀请使用 P2P,受限网络下可能无法连接;自托管站点可开启媒体转发。 ### English Piik is free, open-source screen sharing for you and up to 20 viewers. Start a room, send an invitation, and your friends can join in a browser. - Share games, windows or a whole screen from your browser or Piik App. - Direct connections come first, with optional media forwarding on self-hosted sites. - Piik App supports local rooms, temporary public invitations and existing sites. - Piik Server includes the web interface, rooms and connection services for self-hosting. - English and Chinese interfaces and guides are available, with GitHub and Gitee downloads. [Website](https://piik.tv) · [Online demo](https://demo.piik.tv) · [User guide](https://github.com/TNTcraftHIM/Piik/blob/main/docs/guide/getting-started.md) · [Self-hosting](https://github.com/TNTcraftHIM/Piik/blob/main/docs/operations/self-hosting.md) · [Gitee downloads](https://gitee.com/TNTcraftHIM/Piik/releases) Windows App and desktop browsers are the primary validated paths. macOS and Linux have not yet been tested on physical devices; testing and feedback are welcome. The demo and App public invitations rely on P2P and may not connect on restrictive networks. Self-hosted sites can enable media forwarding.
354 lines
10 KiB
Go
354 lines
10 KiB
Go
package launcher
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"io/fs"
|
|
"net"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
appconfig "github.com/TNTcraftHIM/Piik/internal/app/config"
|
|
)
|
|
|
|
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"`
|
|
}
|
|
|
|
type Server struct {
|
|
ctx context.Context
|
|
cancel context.CancelFunc
|
|
listener net.Listener
|
|
httpServer *http.Server
|
|
assets fs.FS
|
|
static http.Handler
|
|
site string
|
|
localAccessPassword string
|
|
version string
|
|
revision 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, site, version, revision, localAccessPassword string) (*Server, error) {
|
|
if parent == nil {
|
|
parent = context.Background()
|
|
}
|
|
normalizedSite, err := appconfig.NormalizeSite(site)
|
|
if err != nil {
|
|
return nil, errors.New("App launcher Site is invalid")
|
|
}
|
|
site = normalizedSite
|
|
normalizedPassword, err := appconfig.NormalizeLocalAccessPassword(localAccessPassword)
|
|
if err != nil {
|
|
return nil, errors.New("App launcher local access password is invalid")
|
|
}
|
|
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, errors.New("App launcher could not start")
|
|
}
|
|
ctx, cancel := context.WithCancel(parent)
|
|
server := &Server{
|
|
ctx: ctx,
|
|
cancel: cancel,
|
|
listener: listener,
|
|
assets: assets,
|
|
static: http.FileServer(http.FS(assets)),
|
|
site: site,
|
|
localAccessPassword: normalizedPassword,
|
|
version: version,
|
|
revision: strings.TrimSpace(revision),
|
|
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
|
|
}
|
|
writeJSON(response, http.StatusOK, map[string]any{
|
|
"site": server.site,
|
|
"localAccessPassword": server.localAccessPassword,
|
|
"version": server.version,
|
|
"revision": server.revision,
|
|
"defaultMode": func() Mode {
|
|
if server.site != "" {
|
|
return ModeSite
|
|
}
|
|
return ModeLocal
|
|
}(),
|
|
})
|
|
}
|
|
|
|
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 {
|
|
http.Error(response, "invalid launch request", http.StatusBadRequest)
|
|
return
|
|
}
|
|
server.mu.Lock()
|
|
if server.selected {
|
|
server.mu.Unlock()
|
|
http.Error(response, "launch already requested", http.StatusConflict)
|
|
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",
|
|
})
|
|
server.markHandled()
|
|
return
|
|
}
|
|
writeJSON(response, http.StatusOK, map[string]string{"target": target})
|
|
server.markHandled()
|
|
}
|
|
|
|
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")
|
|
}
|
|
switch selection.Mode {
|
|
case ModeLocal, ModeLink:
|
|
if strings.TrimSpace(selection.Site) != "" {
|
|
return Selection{}, errors.New("local launch cannot include a Site")
|
|
}
|
|
selection.Site = ""
|
|
normalizedPassword, err := appconfig.NormalizeLocalAccessPassword(
|
|
selection.LocalAccessPassword,
|
|
)
|
|
if err != nil {
|
|
return Selection{}, errors.New("local access password is invalid")
|
|
}
|
|
selection.LocalAccessPassword = normalizedPassword
|
|
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)
|
|
}
|