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.
146 lines
4.9 KiB
Go
146 lines
4.9 KiB
Go
package app
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// maxJSONRequestBytes bounds the body every JSON route accepts.
|
|
const maxJSONRequestBytes = 1024
|
|
|
|
// errorBody is the { error } shape every failing route answers with.
|
|
type errorBody struct {
|
|
Error string `json:"error"`
|
|
}
|
|
|
|
// healthBody is the /healthz payload.
|
|
type healthBody struct {
|
|
Status string `json:"status"`
|
|
}
|
|
|
|
// siteAccessBody reports whether site access is required and already granted.
|
|
type siteAccessBody struct {
|
|
Required bool `json:"required"`
|
|
Authenticated bool `json:"authenticated"`
|
|
}
|
|
|
|
// sendJSON sets the exact Content-Length and the JSON charset.
|
|
// Encoding one of this package's own values cannot fail, so a marshal error is
|
|
// a programming error and reaches the recover wrapper.
|
|
func sendJSON(writer http.ResponseWriter, status int, body any) {
|
|
var buffer bytes.Buffer
|
|
encoder := json.NewEncoder(&buffer)
|
|
// D9: JSON.stringify does not escape HTML either.
|
|
encoder.SetEscapeHTML(false)
|
|
if err := encoder.Encode(body); err != nil {
|
|
panic(err)
|
|
}
|
|
encoded := bytes.TrimSuffix(buffer.Bytes(), []byte("\n"))
|
|
header := writer.Header()
|
|
header.Set("Content-Type", "application/json; charset=utf-8")
|
|
header.Set("Content-Length", strconv.Itoa(len(encoded)))
|
|
writer.WriteHeader(status)
|
|
_, _ = writer.Write(encoded)
|
|
}
|
|
|
|
// readJSONBody ports readJsonBody: the content type must be exactly
|
|
// application/json once its parameters are cut, the body must be at most 1024
|
|
// bytes and must not be empty. Every failure collapses to the route's 400, so
|
|
// the messages only exist to keep the TS reasons readable.
|
|
func readJSONBody(request *http.Request) ([]byte, error) {
|
|
contentType, _, _ := strings.Cut(request.Header.Get("Content-Type"), ";")
|
|
if strings.TrimSpace(contentType) != "application/json" {
|
|
return nil, errors.New("Request content type must be application/json")
|
|
}
|
|
body, err := io.ReadAll(io.LimitReader(request.Body, maxJSONRequestBytes+1))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(body) > maxJSONRequestBytes {
|
|
return nil, errors.New("Request body is too large")
|
|
}
|
|
if len(body) == 0 {
|
|
return nil, errors.New("Request body is required")
|
|
}
|
|
return body, nil
|
|
}
|
|
|
|
// hasRequestBody ports hasRequestBody. net/http parses Content-Length and moves
|
|
// Transfer-Encoding out of Header, so a chunked body is ContentLength -1 and an
|
|
// unparseable length never reaches a handler at all.
|
|
func hasRequestBody(request *http.Request) bool {
|
|
return len(request.TransferEncoding) > 0 || request.ContentLength != 0
|
|
}
|
|
|
|
// requestPath is the WHATWG `url.pathname` app.ts routed and logged on. It is
|
|
// the escaped path on purpose: `new URL()` does not percent-decode, so
|
|
// "/api%2frooms" must not reach the /api/rooms route.
|
|
//
|
|
// Deviation: `new URL()` also resolves "." and ".." segments, which net/http
|
|
// leaves in the path. "/x/../api/rooms" therefore reaches the frontend handler
|
|
// here where TypeScript routed it to room creation. Only stricter: a raw path
|
|
// that already names a route still matches, so nothing can slip past an origin,
|
|
// cookie or token check that way.
|
|
func requestPath(request *http.Request) string {
|
|
return request.URL.EscapedPath()
|
|
}
|
|
|
|
// cookieHeader rebuilds the single Cookie header value the Node parser handed
|
|
// to access-session.ts; Go keeps repeated Cookie headers apart.
|
|
func cookieHeader(request *http.Request) string {
|
|
return strings.Join(request.Header.Values("Cookie"), "; ")
|
|
}
|
|
|
|
// bearerToken ports readBearerToken: the prefix is case sensitive and an empty
|
|
// token counts as absent.
|
|
func bearerToken(request *http.Request) string {
|
|
authorization := request.Header.Get("Authorization")
|
|
if !strings.HasPrefix(authorization, "Bearer ") {
|
|
return ""
|
|
}
|
|
return strings.TrimPrefix(authorization, "Bearer ")
|
|
}
|
|
|
|
// isUpgradeRequest reproduces the condition under which Node emitted "upgrade"
|
|
// instead of "request": the parser needs both a Connection: upgrade token and
|
|
// an Upgrade header. The SignalingServer, not this router, owns the rejection
|
|
// ladder for a path or query that is not exactly /signal.
|
|
func isUpgradeRequest(request *http.Request) bool {
|
|
if request.Header.Get("Upgrade") == "" {
|
|
return false
|
|
}
|
|
for _, token := range strings.Split(request.Header.Get("Connection"), ",") {
|
|
if strings.EqualFold(strings.TrimSpace(token), "upgrade") {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// responseRecorder tracks response.headersSent so the recover wrapper can pick
|
|
// between the JSON 500 and destroying the connection, as app.ts did.
|
|
type responseRecorder struct {
|
|
http.ResponseWriter
|
|
wrote bool
|
|
}
|
|
|
|
func (recorder *responseRecorder) WriteHeader(status int) {
|
|
recorder.wrote = true
|
|
recorder.ResponseWriter.WriteHeader(status)
|
|
}
|
|
|
|
func (recorder *responseRecorder) Write(data []byte) (int, error) {
|
|
recorder.wrote = true
|
|
return recorder.ResponseWriter.Write(data)
|
|
}
|
|
|
|
// Unwrap lets http.ResponseController reach the real writer's Flush and Hijack.
|
|
func (recorder *responseRecorder) Unwrap() http.ResponseWriter {
|
|
return recorder.ResponseWriter
|
|
}
|