add chat creation for blink creator
This commit is contained in:
1 parent
6176f4eaf5
commit
a1bc54dd62
5 files changed
+1667
-53
No files matched your search
+16
-5
@@ -19,7 +19,7 @@ func main() {
|
|||||||
common.MaybePrintVersion()
|
common.MaybePrintVersion()
|
||||||
cookiesPath := flag.String("cookies", "", "path to cookies-blink.json with the Blink login tokens")
|
cookiesPath := flag.String("cookies", "", "path to cookies-blink.json with the Blink login tokens")
|
||||||
loginFlag := flag.Bool("login", false, "log in with a QR code, save the tokens to --cookies and exit")
|
loginFlag := flag.Bool("login", false, "log in with a QR code, save the tokens to --cookies and exit")
|
||||||
roomFlag := flag.String("room", "", "Blink chat id or blink://<chat id>, the creator joins the call in this chat or starts one")
|
roomFlag := flag.String("room", "", "Blink chat id or blink://<chat id> to rejoin (empty = create new room)")
|
||||||
resources := flag.String("resources", "default", "resource mode: default, moderate, unlimited, custom")
|
resources := flag.String("resources", "default", "resource mode: default, moderate, unlimited, custom")
|
||||||
customReadBuf := flag.Int("read-buf", 0, "DC read buffer size in bytes, used with -resources custom")
|
customReadBuf := flag.Int("read-buf", 0, "DC read buffer size in bytes, used with -resources custom")
|
||||||
customMemLimit := flag.Int64("mem-limit", 0, "memory limit in bytes, used with -resources custom")
|
customMemLimit := flag.Int64("mem-limit", 0, "memory limit in bytes, used with -resources custom")
|
||||||
@@ -74,15 +74,26 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
chatID, err := blink.ParseChatID(*roomFlag)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("[config] --room: %v", err)
|
|
||||||
}
|
|
||||||
credentials, err := blink.LoadCredentials(*cookiesPath, nil, log.Printf)
|
credentials, err := blink.LoadCredentials(*cookiesPath, nil, log.Printf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
common.EmitAuthError(common.AuthErrorSessionExpired)
|
common.EmitAuthError(common.AuthErrorSessionExpired)
|
||||||
log.Fatalf("[auth] %v", err)
|
log.Fatalf("[auth] %v", err)
|
||||||
}
|
}
|
||||||
|
var chatID int64
|
||||||
|
if *roomFlag != "" {
|
||||||
|
chatID, err = blink.ParseChatID(*roomFlag)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("[config] --room: %v", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
chatName := blink.RandomChatName()
|
||||||
|
chatID, err = credentials.CreateChat(chatName)
|
||||||
|
if err != nil {
|
||||||
|
common.EmitAuthErrorFor(err)
|
||||||
|
log.Fatalf("[room] %v", err)
|
||||||
|
}
|
||||||
|
log.Printf("[room] created chat=%d name=%q", chatID, chatName)
|
||||||
|
}
|
||||||
connection, err := credentials.ConnectCall(chatID)
|
connection, err := credentials.ConnectCall(chatID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
common.EmitAuthErrorFor(err)
|
common.EmitAuthErrorFor(err)
|
||||||
|
|||||||
+88
-37
@@ -11,6 +11,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
headless "github.com/kulikov0/headless-client"
|
headless "github.com/kulikov0/headless-client"
|
||||||
"whitelist-bypass/relay/common"
|
"whitelist-bypass/relay/common"
|
||||||
)
|
)
|
||||||
@@ -77,43 +78,6 @@ func IsUnauthorized(err error) bool {
|
|||||||
return errors.As(err, &statusErr) && statusErr.Status == http.StatusUnauthorized
|
return errors.As(err, &statusErr) && statusErr.Status == http.StatusUnauthorized
|
||||||
}
|
}
|
||||||
|
|
||||||
func apiDo(client *http.Client, method, path, bearer string, body any) ([]byte, int, error) {
|
|
||||||
var reader io.Reader
|
|
||||||
if body != nil {
|
|
||||||
payload, err := json.Marshal(body)
|
|
||||||
if err != nil {
|
|
||||||
return nil, 0, err
|
|
||||||
}
|
|
||||||
reader = bytes.NewReader(payload)
|
|
||||||
}
|
|
||||||
request, err := http.NewRequest(method, APIBase+path, reader)
|
|
||||||
if err != nil {
|
|
||||||
return nil, 0, err
|
|
||||||
}
|
|
||||||
for name, values := range headless.ChromeWindows.Headers(headless.DestEmpty) {
|
|
||||||
request.Header[name] = values
|
|
||||||
}
|
|
||||||
if bearer != "" {
|
|
||||||
request.Header.Set("Authorization", "Bearer "+bearer)
|
|
||||||
}
|
|
||||||
if body != nil {
|
|
||||||
request.Header.Set("Content-Type", "application/json")
|
|
||||||
}
|
|
||||||
request.Header.Set("Origin", Origin)
|
|
||||||
request.Header.Set("Referer", Origin+"/")
|
|
||||||
request.Header.Set("X-User-Agent", ClientUserAgent)
|
|
||||||
if client == nil {
|
|
||||||
client = headless.ChromeWindows.HTTPClient()
|
|
||||||
}
|
|
||||||
response, err := client.Do(request)
|
|
||||||
if err != nil {
|
|
||||||
return nil, 0, err
|
|
||||||
}
|
|
||||||
defer response.Body.Close()
|
|
||||||
raw, err := io.ReadAll(response.Body)
|
|
||||||
return raw, response.StatusCode, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func RequestLogin(client *http.Client) (LoginRequest, error) {
|
func RequestLogin(client *http.Client) (LoginRequest, error) {
|
||||||
var result LoginRequest
|
var result LoginRequest
|
||||||
raw, status, err := apiDo(client, http.MethodGet, "/login/webapp/token/request", "", nil)
|
raw, status, err := apiDo(client, http.MethodGet, "/login/webapp/token/request", "", nil)
|
||||||
@@ -190,3 +154,90 @@ func ConnectCall(client *http.Client, accessToken string, chatID int64) (CallCon
|
|||||||
}
|
}
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func FetchAccountID(client *http.Client, accessToken string) (int64, error) {
|
||||||
|
var result struct {
|
||||||
|
AccountID int64 `json:"account_id"`
|
||||||
|
}
|
||||||
|
raw, status, err := apiDo(client, http.MethodGet, "/account/profile", accessToken, nil)
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("account profile: %w", err)
|
||||||
|
}
|
||||||
|
if status != http.StatusOK {
|
||||||
|
return 0, &StatusError{Operation: "account profile", Status: status, Body: common.BodySnippet(raw)}
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(raw, &result); err != nil {
|
||||||
|
return 0, fmt.Errorf("account profile decode: %w", err)
|
||||||
|
}
|
||||||
|
if result.AccountID <= 0 {
|
||||||
|
return 0, fmt.Errorf("account profile: no account_id in response")
|
||||||
|
}
|
||||||
|
return result.AccountID, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateGroupChat(client *http.Client, accessToken string, accountID int64, name string) (int64, error) {
|
||||||
|
var result struct {
|
||||||
|
Data struct {
|
||||||
|
ChatID int64 `json:"chat_id"`
|
||||||
|
} `json:"data"`
|
||||||
|
}
|
||||||
|
raw, status, err := apiDo(client, http.MethodPost, "/messenger/v2/group-chat/create", accessToken, map[string]any{
|
||||||
|
"action_type": "create_group_chat",
|
||||||
|
"request_id": uuid.New().String(),
|
||||||
|
"actor_id": accountID,
|
||||||
|
"action_data": map[string]any{
|
||||||
|
"members_ids": []int64{},
|
||||||
|
"name": name,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("group chat create: %w", err)
|
||||||
|
}
|
||||||
|
if status != http.StatusOK {
|
||||||
|
return 0, &StatusError{Operation: "group chat create", Status: status, Body: common.BodySnippet(raw)}
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(raw, &result); err != nil {
|
||||||
|
return 0, fmt.Errorf("group chat create decode: %w", err)
|
||||||
|
}
|
||||||
|
if result.Data.ChatID <= 0 {
|
||||||
|
return 0, fmt.Errorf("group chat create: no chat_id in %s", common.BodySnippet(raw))
|
||||||
|
}
|
||||||
|
return result.Data.ChatID, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func apiDo(client *http.Client, method, path, bearer string, body any) ([]byte, int, error) {
|
||||||
|
var reader io.Reader
|
||||||
|
if body != nil {
|
||||||
|
payload, err := json.Marshal(body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
reader = bytes.NewReader(payload)
|
||||||
|
}
|
||||||
|
request, err := http.NewRequest(method, APIBase+path, reader)
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
for name, values := range headless.ChromeWindows.Headers(headless.DestEmpty) {
|
||||||
|
request.Header[name] = values
|
||||||
|
}
|
||||||
|
if bearer != "" {
|
||||||
|
request.Header.Set("Authorization", "Bearer "+bearer)
|
||||||
|
}
|
||||||
|
if body != nil {
|
||||||
|
request.Header.Set("Content-Type", "application/json")
|
||||||
|
}
|
||||||
|
request.Header.Set("Origin", Origin)
|
||||||
|
request.Header.Set("Referer", Origin+"/")
|
||||||
|
request.Header.Set("X-User-Agent", ClientUserAgent)
|
||||||
|
if client == nil {
|
||||||
|
client = headless.ChromeWindows.HTTPClient()
|
||||||
|
}
|
||||||
|
response, err := client.Do(request)
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
defer response.Body.Close()
|
||||||
|
raw, err := io.ReadAll(response.Body)
|
||||||
|
return raw, response.StatusCode, err
|
||||||
|
}
|
||||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,16 @@
|
|||||||
|
package blink
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "embed"
|
||||||
|
"math/rand/v2"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
//go:embed chat-names.txt
|
||||||
|
var chatNamesFile string
|
||||||
|
|
||||||
|
var chatNames = strings.Split(strings.TrimSpace(chatNamesFile), "\n")
|
||||||
|
|
||||||
|
func RandomChatName() string {
|
||||||
|
return chatNames[rand.IntN(len(chatNames))]
|
||||||
|
}
|
||||||
+28
-11
@@ -62,19 +62,19 @@ func (c *Credentials) AccessToken() (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Credentials) ConnectCall(chatID int64) (CallConnection, error) {
|
func (c *Credentials) ConnectCall(chatID int64) (CallConnection, error) {
|
||||||
accessToken, err := c.AccessToken()
|
return withAccessToken(c, "calls connect", func(accessToken string) (CallConnection, error) {
|
||||||
|
return ConnectCall(c.client, accessToken, chatID)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Credentials) CreateChat(name string) (int64, error) {
|
||||||
|
return withAccessToken(c, "group chat create", func(accessToken string) (int64, error) {
|
||||||
|
accountID, err := FetchAccountID(c.client, accessToken)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return CallConnection{}, err
|
return 0, err
|
||||||
}
|
}
|
||||||
connection, err := ConnectCall(c.client, accessToken, chatID)
|
return CreateGroupChat(c.client, accessToken, accountID, name)
|
||||||
if !IsUnauthorized(err) {
|
})
|
||||||
return connection, err
|
|
||||||
}
|
|
||||||
c.logFn("[auth] calls connect returned 401, refreshing tokens")
|
|
||||||
if err := c.refresh(); err != nil {
|
|
||||||
return CallConnection{}, err
|
|
||||||
}
|
|
||||||
return ConnectCall(c.client, c.tokens.Token, chatID)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Credentials) refresh() error {
|
func (c *Credentials) refresh() error {
|
||||||
@@ -102,6 +102,23 @@ func (c *Credentials) refresh() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func withAccessToken[T any](c *Credentials, operation string, request func(accessToken string) (T, error)) (T, error) {
|
||||||
|
var empty T
|
||||||
|
accessToken, err := c.AccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return empty, err
|
||||||
|
}
|
||||||
|
result, err := request(accessToken)
|
||||||
|
if !IsUnauthorized(err) {
|
||||||
|
return result, err
|
||||||
|
}
|
||||||
|
c.logFn("[auth] %s returned 401, refreshing tokens", operation)
|
||||||
|
if err := c.refresh(); err != nil {
|
||||||
|
return empty, err
|
||||||
|
}
|
||||||
|
return request(c.tokens.Token)
|
||||||
|
}
|
||||||
|
|
||||||
func readTokens(path string) (TokenPair, error) {
|
func readTokens(path string) (TokenPair, error) {
|
||||||
var tokens TokenPair
|
var tokens TokenPair
|
||||||
raw, err := os.ReadFile(path)
|
raw, err := os.ReadFile(path)
|
||||||
|
|||||||
Reference in new issue
Block a user