add wbstream auth support
This commit is contained in:
1 parent
221c4f595f
commit
b341370904
18 files changed
+373
-77
No files matched your search
+31
-1
@@ -9,7 +9,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
const UserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36"
|
||||
const UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36"
|
||||
|
||||
func LoadCookies(path string) string {
|
||||
data, err := os.ReadFile(path)
|
||||
@@ -30,6 +30,36 @@ func LoadCookies(path string) string {
|
||||
return strings.Join(parts, "; ")
|
||||
}
|
||||
|
||||
func CookieValue(cookieHeader, name string) string {
|
||||
for _, part := range strings.Split(cookieHeader, ";") {
|
||||
part = strings.TrimSpace(part)
|
||||
eq := strings.IndexByte(part, '=')
|
||||
if eq != -1 && part[:eq] == name {
|
||||
return part[eq+1:]
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func FilterCookies(cookieHeader string, allow []string) string {
|
||||
allowed := make(map[string]struct{}, len(allow))
|
||||
for _, n := range allow {
|
||||
allowed[n] = struct{}{}
|
||||
}
|
||||
var out []string
|
||||
for _, part := range strings.Split(cookieHeader, ";") {
|
||||
trimmed := strings.TrimSpace(part)
|
||||
eq := strings.IndexByte(trimmed, '=')
|
||||
if eq == -1 {
|
||||
continue
|
||||
}
|
||||
if _, ok := allowed[trimmed[:eq]]; ok {
|
||||
out = append(out, trimmed)
|
||||
}
|
||||
}
|
||||
return strings.Join(out, "; ")
|
||||
}
|
||||
|
||||
func HttpGet(endpoint string) ([]byte, error) {
|
||||
req, _ := http.NewRequest("GET", endpoint, nil)
|
||||
req.Header.Set("User-Agent", UserAgent)
|
||||
|
||||
@@ -61,12 +61,13 @@ func (j *WBStreamHeadlessJoiner) RunWithParams(jsonParams string) {
|
||||
j.logFn("wbstream-joiner: room=%s name=%s vp8Fps=%d vp8Batch=%d", params.RoomID, params.DisplayName, params.VP8FPS, params.VP8Batch)
|
||||
j.Status.EmitStatus(common.StatusConnecting)
|
||||
|
||||
roomID, roomToken, _, err := wbstream.AuthAndGetToken(httpClient, params.RoomID, params.DisplayName)
|
||||
roomID, roomToken, _, serverURL, err := wbstream.AuthAndGetToken(httpClient, params.RoomID, params.DisplayName)
|
||||
if err != nil {
|
||||
j.logFn("wbstream-joiner: auth failed: %v", err)
|
||||
j.Status.EmitStatusError("auth: " + err.Error())
|
||||
return
|
||||
}
|
||||
j.logFn("wbstream-joiner: server=%s", serverURL)
|
||||
|
||||
obf, err := tunnel.NewTunnelObfuscator(tunnel.DeriveSecretFromJoinLink(roomID))
|
||||
if err != nil {
|
||||
@@ -85,6 +86,7 @@ func (j *WBStreamHeadlessJoiner) RunWithParams(jsonParams string) {
|
||||
|
||||
sess := wbstream.NewSession(wbstream.SessionConfig{
|
||||
RoomToken: roomToken,
|
||||
ServerURL: serverURL,
|
||||
DisplayName: params.DisplayName,
|
||||
TunnelMode: params.TunnelMode,
|
||||
Obfuscator: obf,
|
||||
|
||||
@@ -18,10 +18,6 @@ var vp8Keepalive = []byte{
|
||||
0x99, 0x84, 0x88, 0xfc,
|
||||
}
|
||||
|
||||
// vp8Interframe is a valid VP8 P-frame header. SFUs that VP8-validate forwarded
|
||||
// packets accept payloads following a P-frame header much more loosely than
|
||||
// after a keyframe header, so data frames use this prefix while keepalives keep
|
||||
// the keyframe-shaped vp8Keepalive prefix.
|
||||
var vp8Interframe = []byte{
|
||||
0xb1, 0x01, 0x00, 0x08, 0x11, 0x18, 0x00, 0x18,
|
||||
0x00, 0x18, 0x58, 0x2f, 0xf4, 0x00, 0x08, 0x00,
|
||||
|
||||
+156
-24
@@ -2,6 +2,7 @@ package wbstream
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -14,10 +15,33 @@ import (
|
||||
|
||||
const (
|
||||
APIBase = "https://stream.wb.ru"
|
||||
WSURL = "wss://rtc-el-01.wb.ru"
|
||||
Origin = "https://stream.wb.ru"
|
||||
)
|
||||
|
||||
// ParseRoomID accepts a bare room id, a wbstream://<id> link, or a
|
||||
// https://stream.wb.ru/room/<id> URL and returns the room id.
|
||||
func ParseRoomID(input string) string {
|
||||
trimmed := strings.TrimSpace(input)
|
||||
if trimmed == "" {
|
||||
return ""
|
||||
}
|
||||
if rest, ok := strings.CutPrefix(trimmed, "wbstream://"); ok {
|
||||
return strings.Trim(rest, "/")
|
||||
}
|
||||
if strings.HasPrefix(trimmed, "http://") || strings.HasPrefix(trimmed, "https://") {
|
||||
u, err := url.Parse(trimmed)
|
||||
if err == nil {
|
||||
parts := strings.Split(strings.Trim(u.Path, "/"), "/")
|
||||
for i := 0; i < len(parts)-1; i++ {
|
||||
if parts[i] == "room" && parts[i+1] != "" {
|
||||
return parts[i+1]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return strings.Trim(trimmed, "/")
|
||||
}
|
||||
|
||||
type guestRegisterRequest struct {
|
||||
DisplayName string `json:"displayName"`
|
||||
Device guestDeviceCfg `json:"device"`
|
||||
@@ -41,8 +65,9 @@ type createRoomResponse struct {
|
||||
RoomID string `json:"roomId"`
|
||||
}
|
||||
|
||||
type tokenResponse struct {
|
||||
type connectionDetailsResponse struct {
|
||||
RoomToken string `json:"roomToken"`
|
||||
ServerURL string `json:"serverUrl"`
|
||||
}
|
||||
|
||||
func httpDo(client *http.Client, req *http.Request) (*http.Response, error) {
|
||||
@@ -53,6 +78,38 @@ func httpDo(client *http.Client, req *http.Request) (*http.Response, error) {
|
||||
return client.Do(req)
|
||||
}
|
||||
|
||||
type cookieTransport struct {
|
||||
base http.RoundTripper
|
||||
cookie string
|
||||
}
|
||||
|
||||
func (t *cookieTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
req.Header.Set("Cookie", t.cookie)
|
||||
base := t.base
|
||||
if base == nil {
|
||||
base = http.DefaultTransport
|
||||
}
|
||||
return base.RoundTrip(req)
|
||||
}
|
||||
|
||||
func clientWithCookies(client *http.Client, cookieHeader string) *http.Client {
|
||||
if cookieHeader == "" {
|
||||
return client
|
||||
}
|
||||
if client == nil {
|
||||
client = &http.Client{}
|
||||
}
|
||||
wrapped := *client
|
||||
wrapped.Transport = &cookieTransport{base: client.Transport, cookie: cookieHeader}
|
||||
return &wrapped
|
||||
}
|
||||
|
||||
func setBearer(req *http.Request, accessToken string) {
|
||||
if accessToken != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+accessToken)
|
||||
}
|
||||
}
|
||||
|
||||
func RegisterGuest(client *http.Client, displayName string) (string, error) {
|
||||
body, _ := json.Marshal(guestRegisterRequest{
|
||||
DisplayName: displayName,
|
||||
@@ -94,7 +151,7 @@ func CreateRoom(client *http.Client, accessToken string) (string, error) {
|
||||
return "", err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer "+accessToken)
|
||||
setBearer(req, accessToken)
|
||||
|
||||
resp, err := httpDo(client, req)
|
||||
if err != nil {
|
||||
@@ -120,7 +177,7 @@ func JoinRoom(client *http.Client, accessToken, roomID string) error {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer "+accessToken)
|
||||
setBearer(req, accessToken)
|
||||
|
||||
resp, err := httpDo(client, req)
|
||||
if err != nil {
|
||||
@@ -134,51 +191,126 @@ func JoinRoom(client *http.Client, accessToken, roomID string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetRoomToken(client *http.Client, accessToken, roomID, displayName string) (string, error) {
|
||||
tokenURL := fmt.Sprintf("%s/api-room-manager/api/v1/room/%s/token?deviceType=PARTICIPANT_DEVICE_TYPE_WEB_DESKTOP&displayName=%s",
|
||||
func GetConnectionDetails(client *http.Client, accessToken, roomID, displayName string) (string, string, error) {
|
||||
detailsURL := fmt.Sprintf("%s/api-room-manager/v2/room/%s/connection-details?deviceType=PARTICIPANT_DEVICE_TYPE_WEB_DESKTOP&displayName=%s",
|
||||
APIBase, roomID, url.QueryEscape(displayName))
|
||||
req, err := http.NewRequest(http.MethodGet, tokenURL, nil)
|
||||
req, err := http.NewRequest(http.MethodGet, detailsURL, nil)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
setBearer(req, accessToken)
|
||||
|
||||
resp, err := httpDo(client, req)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
raw, _ := io.ReadAll(resp.Body)
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", "", fmt.Errorf("connection-details: status %d: %s", resp.StatusCode, string(raw))
|
||||
}
|
||||
|
||||
var r connectionDetailsResponse
|
||||
if err := json.Unmarshal(raw, &r); err != nil {
|
||||
return "", "", fmt.Errorf("connection-details decode: %w", err)
|
||||
}
|
||||
return r.RoomToken, r.ServerURL, nil
|
||||
}
|
||||
|
||||
func AuthAndGetToken(client *http.Client, roomID, displayName string) (string, string, string, string, error) {
|
||||
accessToken, err := RegisterGuest(client, displayName)
|
||||
if err != nil {
|
||||
return "", "", "", "", fmt.Errorf("register guest: %w", err)
|
||||
}
|
||||
return joinAndGetDetails(client, accessToken, roomID, displayName)
|
||||
}
|
||||
|
||||
func AuthAsLoggedIn(client *http.Client, cookieHeader, accessToken, roomID, displayName string) (string, string, string, string, error) {
|
||||
if cookieHeader == "" && accessToken == "" {
|
||||
return "", "", "", "", fmt.Errorf("cookies or access token required for logged-in auth")
|
||||
}
|
||||
client = clientWithCookies(client, cookieHeader)
|
||||
return joinAndGetDetails(client, accessToken, roomID, displayName)
|
||||
}
|
||||
|
||||
var WBStreamCookieAllowlist = []string{
|
||||
"wbx-refresh",
|
||||
"x_wbaas_token",
|
||||
"_wbauid",
|
||||
"wbx-validation-key",
|
||||
}
|
||||
|
||||
type slideV3Response struct {
|
||||
Payload struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
} `json:"payload"`
|
||||
}
|
||||
|
||||
func newRequestID() string {
|
||||
var b [16]byte
|
||||
if _, err := rand.Read(b[:]); err != nil {
|
||||
return "00000000-0000-0000-0000-000000000000"
|
||||
}
|
||||
b[6] = (b[6] & 0x0f) | 0x40
|
||||
b[8] = (b[8] & 0x3f) | 0x80
|
||||
return fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:16])
|
||||
}
|
||||
|
||||
func RefreshAccessToken(client *http.Client, cookieHeader, deviceID string) (string, error) {
|
||||
req, err := http.NewRequest(http.MethodPost, "https://auth-stream.wb.ru/v2/auth/slide-v3", bytes.NewReader(nil))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+accessToken)
|
||||
if deviceID == "" {
|
||||
deviceID = newRequestID()
|
||||
}
|
||||
req.Header.Set("wb-apptype", "web")
|
||||
req.Header.Set("X-Real-IP", "")
|
||||
req.Header.Set("deviceId", deviceID)
|
||||
req.Header.Set("X-Request-ID", newRequestID())
|
||||
req.Header.Set("Origin", Origin)
|
||||
req.Header.Set("Referer", Origin+"/")
|
||||
req.Header.Set("Cookie", cookieHeader)
|
||||
req.Header.Set("User-Agent", common.UserAgent)
|
||||
|
||||
resp, err := httpDo(client, req)
|
||||
if client == nil {
|
||||
client = http.DefaultClient
|
||||
}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
raw, _ := io.ReadAll(resp.Body)
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", fmt.Errorf("get-token: status %d: %s", resp.StatusCode, string(raw))
|
||||
return "", fmt.Errorf("slide-v3: status %d: %s", resp.StatusCode, string(raw))
|
||||
}
|
||||
|
||||
var r tokenResponse
|
||||
var r slideV3Response
|
||||
if err := json.Unmarshal(raw, &r); err != nil {
|
||||
return "", fmt.Errorf("get-token decode: %w", err)
|
||||
return "", fmt.Errorf("slide-v3 decode: %w", err)
|
||||
}
|
||||
return r.RoomToken, nil
|
||||
if r.Payload.AccessToken == "" {
|
||||
return "", fmt.Errorf("slide-v3: empty access_token in response: %s", string(raw))
|
||||
}
|
||||
return r.Payload.AccessToken, nil
|
||||
}
|
||||
|
||||
func AuthAndGetToken(client *http.Client, roomID, displayName string) (string, string, string, error) {
|
||||
accessToken, err := RegisterGuest(client, displayName)
|
||||
if err != nil {
|
||||
return "", "", "", fmt.Errorf("register guest: %w", err)
|
||||
}
|
||||
func joinAndGetDetails(client *http.Client, accessToken, roomID, displayName string) (string, string, string, string, error) {
|
||||
var err error
|
||||
if roomID == "" {
|
||||
roomID, err = CreateRoom(client, accessToken)
|
||||
if err != nil {
|
||||
return "", "", "", fmt.Errorf("create room: %w", err)
|
||||
return "", "", "", "", fmt.Errorf("create room: %w", err)
|
||||
}
|
||||
}
|
||||
if err := JoinRoom(client, accessToken, roomID); err != nil {
|
||||
return "", "", "", fmt.Errorf("join room: %w", err)
|
||||
return "", "", "", "", fmt.Errorf("join room: %w", err)
|
||||
}
|
||||
roomToken, err := GetRoomToken(client, accessToken, roomID, displayName)
|
||||
roomToken, serverURL, err := GetConnectionDetails(client, accessToken, roomID, displayName)
|
||||
if err != nil {
|
||||
return "", "", "", fmt.Errorf("get token: %w", err)
|
||||
return "", "", "", "", fmt.Errorf("get connection details: %w", err)
|
||||
}
|
||||
return roomID, roomToken, accessToken, nil
|
||||
return roomID, roomToken, accessToken, serverURL, nil
|
||||
}
|
||||
|
||||
func KickParticipant(client *http.Client, accessToken, roomID, participantID string) error {
|
||||
|
||||
@@ -31,6 +31,7 @@ const (
|
||||
|
||||
type SessionConfig struct {
|
||||
RoomToken string
|
||||
ServerURL string
|
||||
DisplayName string
|
||||
TunnelMode string
|
||||
Obfuscator *tunnel.TunnelObfuscator
|
||||
@@ -84,7 +85,7 @@ func (s *Session) Done() <-chan struct{} { return s.done }
|
||||
|
||||
func (s *Session) Start() error {
|
||||
s.lk = livekit.NewClient(livekit.Config{
|
||||
ServerURL: WSURL,
|
||||
ServerURL: s.cfg.ServerURL,
|
||||
Token: s.cfg.RoomToken,
|
||||
Origin: Origin,
|
||||
UserAgent: common.UserAgent,
|
||||
|
||||
Reference in new issue
Block a user