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.
194 lines
5.8 KiB
Go
194 lines
5.8 KiB
Go
package app
|
|
|
|
import (
|
|
"crypto/hmac"
|
|
"crypto/sha256"
|
|
"crypto/subtle"
|
|
"encoding/base64"
|
|
"errors"
|
|
"net/http"
|
|
"net/url"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/TNTcraftHIM/Piik/internal/server/config"
|
|
"github.com/TNTcraftHIM/Piik/internal/server/protocol"
|
|
)
|
|
|
|
const (
|
|
defaultSessionTTLSeconds = 24 * 60 * 60
|
|
cookieVersion = "v1"
|
|
)
|
|
|
|
var (
|
|
accessExpiresPattern = regexp.MustCompile(`^[0-9]+$`)
|
|
accessSignaturePattern = regexp.MustCompile(`^[A-Za-z0-9_-]{43}$`)
|
|
)
|
|
|
|
// siteAccessOptions configures cookie lifetime, signing and transport policy.
|
|
type siteAccessOptions struct {
|
|
// Password empty means site access is not required.
|
|
Password string
|
|
// Secure selects the __Host- cookie name and appends the Secure attribute.
|
|
Secure bool
|
|
// Now returns Unix milliseconds; nil uses the wall clock.
|
|
Now func() int64
|
|
// TTLSeconds of 0 selects the default 24-hour lifetime.
|
|
TTLSeconds int
|
|
}
|
|
|
|
// siteAccessGate issues and verifies the stateless site-access cookie.
|
|
type siteAccessGate struct {
|
|
password string
|
|
secure bool
|
|
now func() int64
|
|
ttlSeconds int
|
|
}
|
|
|
|
// One Local authority can be served through HTTPS public links and HTTP LAN
|
|
// origins. Select the cookie contract by its configured destination, never by
|
|
// the initiating Origin or an untrusted forwarded-protocol header.
|
|
func (s *Server) siteAccessForRequest(request *http.Request) *siteAccessGate {
|
|
if !s.siteAccess.secure || request.TLS != nil {
|
|
return s.siteAccess
|
|
}
|
|
plain := config.Origin(&url.URL{Scheme: "http", Host: request.Host})
|
|
secure := config.Origin(&url.URL{Scheme: "https", Host: request.Host})
|
|
_, allowsPlain := s.config.AllowedOrigins[plain]
|
|
_, allowsSecure := s.config.AllowedOrigins[secure]
|
|
if !allowsPlain || allowsSecure || strings.EqualFold(request.Host, s.config.PublicBaseURL.Host) {
|
|
return s.siteAccess
|
|
}
|
|
access := *s.siteAccess
|
|
access.secure = false
|
|
return &access
|
|
}
|
|
|
|
// newSiteAccess mirrors the SiteAccess constructor, including its TTL check.
|
|
func newSiteAccess(options siteAccessOptions) (*siteAccessGate, error) {
|
|
ttlSeconds := options.TTLSeconds
|
|
if ttlSeconds == 0 {
|
|
ttlSeconds = defaultSessionTTLSeconds
|
|
}
|
|
if ttlSeconds < 0 || int64(ttlSeconds) > protocol.MaxSafeInteger {
|
|
return nil, errors.New("Access session TTL must be a positive integer")
|
|
}
|
|
now := options.Now
|
|
if now == nil {
|
|
now = func() int64 { return time.Now().UnixMilli() }
|
|
}
|
|
return &siteAccessGate{
|
|
password: options.Password,
|
|
secure: options.Secure,
|
|
now: now,
|
|
ttlSeconds: ttlSeconds,
|
|
}, nil
|
|
}
|
|
|
|
// required reports whether a password is configured.
|
|
func (a *siteAccessGate) required() bool {
|
|
return a.password != ""
|
|
}
|
|
|
|
// passwordMatches mirrors passwordMatches: an unset password accepts anything,
|
|
// a set password needs a non-empty exact match.
|
|
func (a *siteAccessGate) passwordMatches(value string) bool {
|
|
return !a.required() || (value != "" && secretMatches(value, a.password))
|
|
}
|
|
|
|
// isAuthenticated mirrors isAuthenticated over a raw Cookie request header.
|
|
func (a *siteAccessGate) isAuthenticated(cookieHeader string) bool {
|
|
if !a.required() {
|
|
return true
|
|
}
|
|
value := readCookie(cookieHeader, a.cookieName())
|
|
if value == "" {
|
|
return false
|
|
}
|
|
segments := strings.Split(value, ".")
|
|
if len(segments) != 3 ||
|
|
segments[0] != cookieVersion ||
|
|
!accessExpiresPattern.MatchString(segments[1]) ||
|
|
!accessSignaturePattern.MatchString(segments[2]) {
|
|
return false
|
|
}
|
|
// Expiry is already decimal; parsing still enforces integer bounds.
|
|
expiresAt, err := strconv.ParseInt(segments[1], 10, 64)
|
|
if err != nil || expiresAt > protocol.MaxSafeInteger {
|
|
return false
|
|
}
|
|
if expiresAt <= floorSeconds(a.now()) {
|
|
return false
|
|
}
|
|
// Sign the received text, not a re-rendered payload.
|
|
return secretMatches(segments[2], a.sign(segments[0]+"."+segments[1]))
|
|
}
|
|
|
|
// createCookie mirrors createCookie and returns the Set-Cookie header value,
|
|
// or "" where TS returned undefined (site access is not required).
|
|
func (a *siteAccessGate) createCookie() string {
|
|
if !a.required() {
|
|
return ""
|
|
}
|
|
expiresAt := floorSeconds(a.now()) + int64(a.ttlSeconds)
|
|
payload := cookieVersion + "." + strconv.FormatInt(expiresAt, 10)
|
|
value := payload + "." + a.sign(payload)
|
|
// Attribute order is the TS array order; the app writes it verbatim.
|
|
attributes := []string{
|
|
a.cookieName() + "=" + value,
|
|
"Path=/",
|
|
"Max-Age=" + strconv.Itoa(a.ttlSeconds),
|
|
"HttpOnly",
|
|
"SameSite=Strict",
|
|
}
|
|
if a.secure {
|
|
attributes = append(attributes, "Secure")
|
|
}
|
|
return strings.Join(attributes, "; ")
|
|
}
|
|
|
|
func (a *siteAccessGate) cookieName() string {
|
|
if a.secure {
|
|
return "__Host-piik-site-access"
|
|
}
|
|
return "piik-site-access"
|
|
}
|
|
|
|
func (a *siteAccessGate) sign(payload string) string {
|
|
mac := hmac.New(sha256.New, []byte(a.password))
|
|
mac.Write([]byte(payload))
|
|
return base64.RawURLEncoding.EncodeToString(mac.Sum(nil))
|
|
}
|
|
|
|
// floorSeconds is Math.floor(milliseconds / 1000); Go's integer division
|
|
// truncates toward zero, so negative clocks need the adjustment.
|
|
func floorSeconds(milliseconds int64) int64 {
|
|
seconds := milliseconds / 1_000
|
|
if milliseconds < 0 && milliseconds%1_000 != 0 {
|
|
seconds--
|
|
}
|
|
return seconds
|
|
}
|
|
|
|
// readCookie mirrors the TS reader: the first part whose trimmed name matches
|
|
// wins, and an empty value counts as absent.
|
|
func readCookie(header string, name string) string {
|
|
for _, part := range strings.Split(header, ";") {
|
|
separator := strings.IndexByte(part, '=')
|
|
if separator != -1 && strings.TrimSpace(part[:separator]) == name {
|
|
return strings.TrimSpace(part[separator+1:])
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// secretMatches compares through SHA-256 digests so the comparison is constant
|
|
// time regardless of length, exactly as the TS helper does.
|
|
func secretMatches(actual string, expected string) bool {
|
|
actualDigest := sha256.Sum256([]byte(actual))
|
|
expectedDigest := sha256.Sum256([]byte(expected))
|
|
return subtle.ConstantTimeCompare(actualDigest[:], expectedDigest[:]) == 1
|
|
}
|