add blink headless creator and joiner
This commit is contained in:
1 parent
820a6310af
commit
0eb1e66838
11 files changed
+1322
-2
No files matched your search
@@ -0,0 +1,192 @@
|
||||
package blink
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
headless "github.com/kulikov0/headless-client"
|
||||
"whitelist-bypass/relay/common"
|
||||
)
|
||||
|
||||
const (
|
||||
APIBase = "https://pin.apiblink.ru/api"
|
||||
Origin = "https://web.blinkmap.com"
|
||||
ClientUserAgent = "web_client (client 0.1.85)"
|
||||
LoginLinkBase = "https://blinkmap.com/deeplink/webapp/login"
|
||||
JoinLinkScheme = "blink://"
|
||||
)
|
||||
|
||||
var ErrLoginPending = errors.New("blink login is not approved yet")
|
||||
|
||||
type StatusError struct {
|
||||
Operation string
|
||||
Status int
|
||||
Body string
|
||||
}
|
||||
|
||||
type TokenPair struct {
|
||||
Token string `json:"token"`
|
||||
TokenExpiresAt int64 `json:"token_expires_at"`
|
||||
RefreshToken string `json:"refresh_token"`
|
||||
}
|
||||
|
||||
type LoginRequest struct {
|
||||
TTL int `json:"ttl"`
|
||||
WAT string `json:"wat"`
|
||||
}
|
||||
|
||||
type CallConnection struct {
|
||||
CallID string `json:"call_id"`
|
||||
Token string `json:"token"`
|
||||
APIEndpoint string `json:"api_endpoint"`
|
||||
Created bool `json:"created"`
|
||||
}
|
||||
|
||||
func (e *StatusError) Error() string {
|
||||
return fmt.Sprintf("%s: status %d: %s", e.Operation, e.Status, e.Body)
|
||||
}
|
||||
|
||||
func ParseChatID(input string) (int64, error) {
|
||||
trimmed := strings.TrimSpace(input)
|
||||
trimmed = strings.TrimPrefix(trimmed, JoinLinkScheme)
|
||||
trimmed = strings.Trim(trimmed, "/")
|
||||
chatID, err := strconv.ParseInt(trimmed, 10, 64)
|
||||
if err != nil || chatID <= 0 {
|
||||
return 0, fmt.Errorf("not a Blink chat id: %q", input)
|
||||
}
|
||||
return chatID, nil
|
||||
}
|
||||
|
||||
func JoinLink(chatID int64) string {
|
||||
return JoinLinkScheme + strconv.FormatInt(chatID, 10)
|
||||
}
|
||||
|
||||
func LoginLink(wat string) string {
|
||||
return LoginLinkBase + "?wat=" + url.QueryEscape(wat)
|
||||
}
|
||||
|
||||
func IsUnauthorized(err error) bool {
|
||||
var statusErr *StatusError
|
||||
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) {
|
||||
var result LoginRequest
|
||||
raw, status, err := apiDo(client, http.MethodGet, "/login/webapp/token/request", "", nil)
|
||||
if err != nil {
|
||||
return result, fmt.Errorf("login request: %w", err)
|
||||
}
|
||||
if status != http.StatusOK {
|
||||
return result, &StatusError{Operation: "login request", Status: status, Body: common.BodySnippet(raw)}
|
||||
}
|
||||
if err := json.Unmarshal(raw, &result); err != nil {
|
||||
return result, fmt.Errorf("login request decode: %w", err)
|
||||
}
|
||||
if result.WAT == "" {
|
||||
return result, fmt.Errorf("login request: empty wat in %s", common.BodySnippet(raw))
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func PollLogin(client *http.Client, wat string) (TokenPair, error) {
|
||||
var result TokenPair
|
||||
raw, status, err := apiDo(client, http.MethodGet, "/login/webapp/token?wat="+url.QueryEscape(wat), "", nil)
|
||||
if err != nil {
|
||||
return result, fmt.Errorf("login poll: %w", err)
|
||||
}
|
||||
if status == http.StatusNotFound {
|
||||
return result, ErrLoginPending
|
||||
}
|
||||
if status != http.StatusOK {
|
||||
return result, &StatusError{Operation: "login poll", Status: status, Body: common.BodySnippet(raw)}
|
||||
}
|
||||
if err := json.Unmarshal(raw, &result); err != nil {
|
||||
return result, fmt.Errorf("login poll decode: %w", err)
|
||||
}
|
||||
if result.Token == "" {
|
||||
return result, ErrLoginPending
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func RefreshTokens(client *http.Client, refreshToken string) (TokenPair, error) {
|
||||
var result TokenPair
|
||||
raw, status, err := apiDo(client, http.MethodPost, "/login/webapp/token/refresh", "",
|
||||
map[string]string{"refresh_token": refreshToken})
|
||||
if err != nil {
|
||||
return result, fmt.Errorf("token refresh: %w", err)
|
||||
}
|
||||
if status != http.StatusOK {
|
||||
return result, &StatusError{Operation: "token refresh", Status: status, Body: common.BodySnippet(raw)}
|
||||
}
|
||||
if err := json.Unmarshal(raw, &result); err != nil {
|
||||
return result, fmt.Errorf("token refresh decode: %w", err)
|
||||
}
|
||||
if result.Token == "" || result.RefreshToken == "" {
|
||||
return result, fmt.Errorf("token refresh: empty token in response")
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func ConnectCall(client *http.Client, accessToken string, chatID int64) (CallConnection, error) {
|
||||
var result CallConnection
|
||||
raw, status, err := apiDo(client, http.MethodPost, "/calls/connect", accessToken,
|
||||
map[string]int64{"chat_id": chatID})
|
||||
if err != nil {
|
||||
return result, fmt.Errorf("calls connect: %w", err)
|
||||
}
|
||||
if status != http.StatusOK {
|
||||
return result, &StatusError{Operation: "calls connect", Status: status, Body: common.BodySnippet(raw)}
|
||||
}
|
||||
if err := json.Unmarshal(raw, &result); err != nil {
|
||||
return result, fmt.Errorf("calls connect decode: %w", err)
|
||||
}
|
||||
if result.Token == "" || result.APIEndpoint == "" {
|
||||
return result, fmt.Errorf("calls connect: no token or endpoint in %s", common.BodySnippet(raw))
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
Reference in new issue
Block a user