client.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. package telegram
  2. import (
  3. "bufio"
  4. "context"
  5. "encoding/base64"
  6. "fmt"
  7. "log"
  8. "math/rand/v2"
  9. "net"
  10. "net/url"
  11. "regexp"
  12. "sort"
  13. "strings"
  14. "sync"
  15. "time"
  16. "github.com/gotd/td/session"
  17. "github.com/gotd/td/telegram"
  18. "github.com/gotd/td/telegram/dcs"
  19. "github.com/gotd/td/tg"
  20. "github.com/gotd/td/tgerr"
  21. xproxy "golang.org/x/net/proxy"
  22. )
  23. var tmeRegexp = regexp.MustCompile(`https?://t\.me/[^\s"'<>)\]]+`)
  24. // Client TG 客户端
  25. type Client struct {
  26. account Account
  27. sessionPath string
  28. proxyURL string // SOCKS5/HTTP proxy URL
  29. mu sync.Mutex
  30. tgc *telegram.Client
  31. api *tg.Client
  32. cancel context.CancelFunc
  33. ready chan struct{} // closed when connected
  34. runErr error
  35. }
  36. // New 创建客户端(不连接,只初始化)
  37. func New(account Account) *Client {
  38. return &Client{
  39. account: account,
  40. sessionPath: account.SessionFile,
  41. ready: make(chan struct{}),
  42. }
  43. }
  44. // SetProxy sets the proxy URL for this client's connections.
  45. func (c *Client) SetProxy(proxyURL string) {
  46. c.mu.Lock()
  47. c.proxyURL = proxyURL
  48. c.mu.Unlock()
  49. }
  50. // Connect 连接并认证(从 session 文件恢复)
  51. // session 文件不存在时返回错误(不做交互式登录,session 需要预先生成)
  52. func (c *Client) Connect(ctx context.Context) error {
  53. storage := &session.FileStorage{Path: c.sessionPath}
  54. opts := telegram.Options{
  55. SessionStorage: storage,
  56. NoUpdates: true,
  57. Device: telegram.DeviceConfig{
  58. DeviceModel: c.account.Device,
  59. AppVersion: c.account.AppVersion,
  60. SystemVersion: c.account.SystemVersion,
  61. LangPack: c.account.LangPack,
  62. SystemLangCode: c.account.SystemLangCode,
  63. LangCode: c.account.LangCode,
  64. },
  65. }
  66. // Apply proxy if configured
  67. c.mu.Lock()
  68. proxyURL := c.proxyURL
  69. c.mu.Unlock()
  70. if proxyURL != "" {
  71. dialFunc, err := buildProxyDialer(proxyURL)
  72. if err != nil {
  73. log.Printf("[tg_client] failed to create proxy dialer: %v, connecting without proxy", err)
  74. } else {
  75. opts.Resolver = dcs.Plain(dcs.PlainOptions{Dial: dialFunc})
  76. log.Printf("[tg_client] connecting via proxy: %s", proxyURL)
  77. }
  78. }
  79. client := telegram.NewClient(c.account.AppID, c.account.AppHash, opts)
  80. runCtx, cancel := context.WithCancel(ctx)
  81. c.mu.Lock()
  82. c.tgc = client
  83. c.cancel = cancel
  84. c.ready = make(chan struct{})
  85. c.runErr = nil
  86. readyCh := c.ready
  87. c.mu.Unlock()
  88. errCh := make(chan error, 1)
  89. go func() {
  90. err := client.Run(runCtx, func(ctx context.Context) error {
  91. c.mu.Lock()
  92. c.api = client.API()
  93. close(readyCh)
  94. c.mu.Unlock()
  95. // Block until context is cancelled (Disconnect called)
  96. <-ctx.Done()
  97. return ctx.Err()
  98. })
  99. c.mu.Lock()
  100. c.runErr = err
  101. c.mu.Unlock()
  102. errCh <- err
  103. }()
  104. // Wait for ready or error
  105. select {
  106. case <-readyCh:
  107. return nil
  108. case err := <-errCh:
  109. if err != nil && err != context.Canceled {
  110. return err
  111. }
  112. return nil
  113. case <-ctx.Done():
  114. cancel()
  115. return ctx.Err()
  116. }
  117. }
  118. // Disconnect 断开连接
  119. func (c *Client) Disconnect() {
  120. c.mu.Lock()
  121. cancel := c.cancel
  122. c.mu.Unlock()
  123. if cancel != nil {
  124. cancel()
  125. }
  126. }
  127. // waitReady waits for the client to be connected and returns the api client
  128. func (c *Client) waitReady(ctx context.Context) (*tg.Client, error) {
  129. c.mu.Lock()
  130. readyCh := c.ready
  131. api := c.api
  132. c.mu.Unlock()
  133. if api != nil {
  134. return api, nil
  135. }
  136. select {
  137. case <-readyCh:
  138. c.mu.Lock()
  139. api = c.api
  140. c.mu.Unlock()
  141. return api, nil
  142. case <-ctx.Done():
  143. return nil, ctx.Err()
  144. }
  145. }
  146. // GetChannelInfo 获取频道/用户信息,通过用户名查找
  147. func (c *Client) GetChannelInfo(ctx context.Context, username string) (*ChannelInfo, error) {
  148. api, err := c.waitReady(ctx)
  149. if err != nil {
  150. return nil, err
  151. }
  152. username = strings.TrimPrefix(username, "@")
  153. resolved, err := api.ContactsResolveUsername(ctx, &tg.ContactsResolveUsernameRequest{
  154. Username: username,
  155. })
  156. if err != nil {
  157. return nil, wrapFloodWait(err)
  158. }
  159. info := &ChannelInfo{Username: username}
  160. // Look for channel/chat in the resolved chats
  161. for _, ch := range resolved.Chats {
  162. switch v := ch.(type) {
  163. case *tg.Channel:
  164. title := v.Title
  165. info.Title = title
  166. info.IsChannel = v.GetBroadcast()
  167. info.IsGroup = v.GetMegagroup()
  168. if count, ok := v.GetParticipantsCount(); ok {
  169. info.MemberCount = count
  170. }
  171. // Get full channel info for About
  172. accessHash, hasHash := v.GetAccessHash()
  173. if hasHash {
  174. full, ferr := api.ChannelsGetFullChannel(ctx, &tg.InputChannel{
  175. ChannelID: v.GetID(),
  176. AccessHash: accessHash,
  177. })
  178. if ferr == nil {
  179. if cf, ok := full.FullChat.(*tg.ChannelFull); ok {
  180. info.About = cf.GetAbout()
  181. if count, ok := cf.GetParticipantsCount(); ok && info.MemberCount == 0 {
  182. info.MemberCount = count
  183. }
  184. }
  185. }
  186. }
  187. return info, nil
  188. case *tg.Chat:
  189. info.Title = v.Title
  190. info.IsGroup = true
  191. info.MemberCount = v.ParticipantsCount
  192. return info, nil
  193. }
  194. }
  195. return info, nil
  196. }
  197. // GetMessages 获取频道历史消息
  198. // offsetID: 从哪条消息开始(断点续传)
  199. // limit: 最多取多少条
  200. // 返回的消息按 ID 从小到大排序
  201. func (c *Client) GetMessages(ctx context.Context, username string, offsetID, limit int) ([]Message, error) {
  202. api, err := c.waitReady(ctx)
  203. if err != nil {
  204. return nil, err
  205. }
  206. username = strings.TrimPrefix(username, "@")
  207. peer, err := c.resolveInputPeer(ctx, api, username)
  208. if err != nil {
  209. return nil, err
  210. }
  211. result, err := api.MessagesGetHistory(ctx, &tg.MessagesGetHistoryRequest{
  212. Peer: peer,
  213. OffsetID: offsetID,
  214. Limit: limit,
  215. })
  216. if err != nil {
  217. return nil, wrapFloodWait(err)
  218. }
  219. return extractMessages(result), nil
  220. }
  221. // GetPinnedMessages 获取置顶消息
  222. func (c *Client) GetPinnedMessages(ctx context.Context, username string) ([]Message, error) {
  223. api, err := c.waitReady(ctx)
  224. if err != nil {
  225. return nil, err
  226. }
  227. username = strings.TrimPrefix(username, "@")
  228. peer, err := c.resolveInputPeer(ctx, api, username)
  229. if err != nil {
  230. return nil, err
  231. }
  232. result, err := api.MessagesSearch(ctx, &tg.MessagesSearchRequest{
  233. Peer: peer,
  234. Filter: &tg.InputMessagesFilterPinned{},
  235. Limit: 100,
  236. })
  237. if err != nil {
  238. return nil, wrapFloodWait(err)
  239. }
  240. return extractMessages(result), nil
  241. }
  242. // VerifyUser 验证用户名是否存在,返回用户信息
  243. func (c *Client) VerifyUser(ctx context.Context, username string) (*UserInfo, error) {
  244. api, err := c.waitReady(ctx)
  245. if err != nil {
  246. return nil, err
  247. }
  248. username = strings.TrimPrefix(username, "@")
  249. resolved, err := api.ContactsResolveUsername(ctx, &tg.ContactsResolveUsernameRequest{
  250. Username: username,
  251. })
  252. if err != nil {
  253. if tgerr.Is(err, "USERNAME_NOT_OCCUPIED", "USERNAME_INVALID") {
  254. return &UserInfo{Username: username, Exists: false}, nil
  255. }
  256. return nil, wrapFloodWait(err)
  257. }
  258. // Check if the peer is a user
  259. if _, ok := resolved.Peer.(*tg.PeerUser); ok {
  260. for _, u := range resolved.Users {
  261. if user, ok := u.(*tg.User); ok {
  262. info := &UserInfo{
  263. ID: user.GetID(),
  264. Username: username,
  265. IsBot: user.GetBot(),
  266. IsPremium: user.GetPremium(),
  267. Exists: true,
  268. }
  269. if fn, ok := user.GetFirstName(); ok {
  270. info.FirstName = fn
  271. }
  272. if ln, ok := user.GetLastName(); ok {
  273. info.LastName = ln
  274. }
  275. if status, ok := user.GetStatus(); ok {
  276. if offline, ok := status.(*tg.UserStatusOffline); ok {
  277. t := time.Unix(int64(offline.GetWasOnline()), 0)
  278. info.LastOnline = &t
  279. }
  280. }
  281. return info, nil
  282. }
  283. }
  284. }
  285. // Check if it's a channel or group
  286. for _, ch := range resolved.Chats {
  287. switch v := ch.(type) {
  288. case *tg.Channel:
  289. return &UserInfo{
  290. ID: v.GetID(),
  291. Username: username,
  292. IsChannel: v.GetBroadcast(),
  293. IsGroup: v.GetMegagroup(),
  294. Exists: true,
  295. }, nil
  296. case *tg.Chat:
  297. return &UserInfo{
  298. ID: v.ID,
  299. IsGroup: true,
  300. Exists: true,
  301. }, nil
  302. }
  303. }
  304. return &UserInfo{Username: username, Exists: false}, nil
  305. }
  306. // ResolveGroupChannel looks up a group/channel by username and returns both
  307. // the InputChannel handle (for subsequent API calls) and the raw Channel
  308. // struct (for metadata like title and participant count). Returns
  309. // (nil, nil, error) for basic chats (no InputChannel).
  310. func (c *Client) ResolveGroupChannel(ctx context.Context, username string) (*tg.InputChannel, *tg.Channel, error) {
  311. api, err := c.waitReady(ctx)
  312. if err != nil {
  313. return nil, nil, err
  314. }
  315. username = strings.TrimPrefix(username, "@")
  316. resolved, err := api.ContactsResolveUsername(ctx, &tg.ContactsResolveUsernameRequest{Username: username})
  317. if err != nil {
  318. return nil, nil, wrapFloodWait(err)
  319. }
  320. for _, ch := range resolved.Chats {
  321. if v, ok := ch.(*tg.Channel); ok {
  322. accessHash, _ := v.GetAccessHash()
  323. return &tg.InputChannel{ChannelID: v.GetID(), AccessHash: accessHash}, v, nil
  324. }
  325. }
  326. return nil, nil, fmt.Errorf("无法解析群组为超级群组: %s", username)
  327. }
  328. // FetchParticipantsByQuery runs ChannelParticipantsSearch for one query string,
  329. // paginating through all pages. Returns the users surfaced by this query and
  330. // the total count reported by TG. On FloodWait, returns a *FloodWaitError.
  331. // The caller is responsible for deduping across queries.
  332. func (c *Client) FetchParticipantsByQuery(ctx context.Context, channel *tg.InputChannel, query string) ([]GroupParticipant, int, error) {
  333. api, err := c.waitReady(ctx)
  334. if err != nil {
  335. return nil, 0, err
  336. }
  337. const pageSize = 200
  338. offset := 0
  339. totalCount := 0
  340. var out []GroupParticipant
  341. for {
  342. result, err := api.ChannelsGetParticipants(ctx, &tg.ChannelsGetParticipantsRequest{
  343. Channel: channel,
  344. Filter: &tg.ChannelParticipantsSearch{Q: query},
  345. Offset: offset,
  346. Limit: pageSize,
  347. Hash: 0,
  348. })
  349. if err != nil {
  350. return out, totalCount, wrapFloodWait(err)
  351. }
  352. cp, ok := result.(*tg.ChannelsChannelParticipants)
  353. if !ok || len(cp.Users) == 0 {
  354. break
  355. }
  356. if cp.Count > totalCount {
  357. totalCount = cp.Count
  358. }
  359. for _, u := range cp.Users {
  360. user, ok := u.(*tg.User)
  361. if !ok {
  362. continue
  363. }
  364. p := GroupParticipant{
  365. ID: user.GetID(),
  366. IsBot: user.GetBot(),
  367. IsPremium: user.GetPremium(),
  368. }
  369. if un, ok := user.GetUsername(); ok {
  370. p.Username = un
  371. }
  372. if fn, ok := user.GetFirstName(); ok {
  373. p.FirstName = fn
  374. }
  375. if ln, ok := user.GetLastName(); ok {
  376. p.LastName = ln
  377. }
  378. out = append(out, p)
  379. }
  380. offset += len(cp.Users)
  381. if offset >= cp.Count {
  382. break
  383. }
  384. if err := jitterSleep(ctx, 800*time.Millisecond, 1500*time.Millisecond); err != nil {
  385. return out, totalCount, err
  386. }
  387. }
  388. return out, totalCount, nil
  389. }
  390. // GetGroupParticipants 获取群组/超级群组的成员列表(分页拉取全部)
  391. func (c *Client) GetGroupParticipants(ctx context.Context, username string) ([]GroupParticipant, error) {
  392. api, err := c.waitReady(ctx)
  393. if err != nil {
  394. return nil, err
  395. }
  396. username = strings.TrimPrefix(username, "@")
  397. // Resolve the channel/group
  398. resolved, err := api.ContactsResolveUsername(ctx, &tg.ContactsResolveUsernameRequest{
  399. Username: username,
  400. })
  401. if err != nil {
  402. return nil, wrapFloodWait(err)
  403. }
  404. // Find the channel in resolved chats
  405. var inputChannel *tg.InputChannel
  406. for _, ch := range resolved.Chats {
  407. switch v := ch.(type) {
  408. case *tg.Channel:
  409. accessHash, _ := v.GetAccessHash()
  410. inputChannel = &tg.InputChannel{
  411. ChannelID: v.GetID(),
  412. AccessHash: accessHash,
  413. }
  414. }
  415. }
  416. if inputChannel == nil {
  417. // Try as basic chat - get participants via MessagesGetFullChat
  418. if p, ok := resolved.Peer.(*tg.PeerChat); ok {
  419. return c.getChatParticipants(ctx, api, p.ChatID)
  420. }
  421. return nil, fmt.Errorf("无法解析群组: %s", username)
  422. }
  423. // Strategy: use ChannelParticipantsSearch with empty query (returns more than Recent),
  424. // then iterate alphabet queries to discover members beyond the 200 limit per query.
  425. seen := make(map[int64]bool)
  426. var allParticipants []GroupParticipant
  427. // Helper to extract users from a page
  428. extractUsers := func(cp *tg.ChannelsChannelParticipants) int {
  429. added := 0
  430. for _, u := range cp.Users {
  431. user, ok := u.(*tg.User)
  432. if !ok || seen[user.GetID()] {
  433. continue
  434. }
  435. seen[user.GetID()] = true
  436. p := GroupParticipant{
  437. ID: user.GetID(),
  438. IsBot: user.GetBot(),
  439. IsPremium: user.GetPremium(),
  440. }
  441. if un, ok := user.GetUsername(); ok {
  442. p.Username = un
  443. }
  444. if fn, ok := user.GetFirstName(); ok {
  445. p.FirstName = fn
  446. }
  447. if ln, ok := user.GetLastName(); ok {
  448. p.LastName = ln
  449. }
  450. allParticipants = append(allParticipants, p)
  451. added++
  452. }
  453. return added
  454. }
  455. // Phase 1: Search with empty query (gets up to ~200)
  456. totalCount := 0
  457. if err := c.fetchParticipantPages(ctx, api, inputChannel, "", seen, extractUsers, &totalCount); err != nil {
  458. if len(allParticipants) > 0 {
  459. return allParticipants, err
  460. }
  461. return nil, err
  462. }
  463. // Phase 2: If group has more members than we found, search by character sets to discover more.
  464. // We pace queries with jitter (2–4s) to avoid looking like a bot scanner and triggering FloodWait.
  465. // If FloodWait does hit, stop early and return what we already have — the calling task can
  466. // re-attempt later after the account cools down.
  467. if totalCount > len(allParticipants) && totalCount <= 10000 {
  468. queries := participantSearchQueries()
  469. for _, q := range queries {
  470. if ctx.Err() != nil {
  471. break
  472. }
  473. if len(allParticipants) >= totalCount {
  474. break // already collected everyone visible
  475. }
  476. beforeCount := len(allParticipants)
  477. err := c.fetchParticipantPages(ctx, api, inputChannel, q, seen, extractUsers, nil)
  478. if err != nil {
  479. if fwe, ok := err.(*FloodWaitError); ok {
  480. log.Printf("[tg_client] flood wait %ds during search q=%q for %s; returning %d/%d",
  481. fwe.Seconds, q, username, len(allParticipants), totalCount)
  482. } else {
  483. log.Printf("[tg_client] search q=%q for %s: %v (returning partial)", q, username, err)
  484. }
  485. break
  486. }
  487. if len(allParticipants) == beforeCount {
  488. continue // no new results; skip sleep and try next query
  489. }
  490. if err := jitterSleep(ctx, 2*time.Second, 4*time.Second); err != nil {
  491. return allParticipants, err
  492. }
  493. }
  494. }
  495. log.Printf("[tg_client] fetched %d/%d participants for %s", len(allParticipants), totalCount, username)
  496. return allParticipants, nil
  497. }
  498. // jitterSleep sleeps a random duration in [min, max) while respecting ctx.
  499. // Returns ctx.Err() if cancelled. Used to spread out TG API calls and avoid
  500. // looking like a deterministic scanner.
  501. func jitterSleep(ctx context.Context, min, max time.Duration) error {
  502. d := min + time.Duration(rand.Int64N(int64(max-min)))
  503. select {
  504. case <-ctx.Done():
  505. return ctx.Err()
  506. case <-time.After(d):
  507. return nil
  508. }
  509. }
  510. // participantSearchQueries returns search queries covering Latin, Cyrillic, Japanese,
  511. // Korean, and CJK scripts. TG's ChannelParticipantsSearch does substring matching on
  512. // first_name + last_name + username, so more starter-character coverage = more users
  513. // surfaced on groups beyond the 200-per-query cap. Total ~150 queries.
  514. func participantSearchQueries() []string {
  515. queries := make([]string, 0, 170)
  516. // Latin a-z
  517. for c := 'a'; c <= 'z'; c++ {
  518. queries = append(queries, string(c))
  519. }
  520. // Digits 0-9
  521. for c := '0'; c <= '9'; c++ {
  522. queries = append(queries, string(c))
  523. }
  524. // Cyrillic а-я
  525. for c := 'а'; c <= 'я'; c++ {
  526. queries = append(queries, string(c))
  527. }
  528. // Japanese Hiragana — common name-starter syllables
  529. queries = append(queries,
  530. "あ", "い", "う", "え", "お",
  531. "か", "さ", "た", "な", "ま",
  532. )
  533. // Korean Hangul — common initial syllables
  534. queries = append(queries,
  535. "가", "나", "다", "라", "마", "바", "사", "아", "자", "차",
  536. "카", "타", "파", "하",
  537. )
  538. // CJK: top Chinese surnames (百家姓 high frequency)
  539. surnames := []string{
  540. "王", "李", "张", "刘", "陈", "杨", "黄", "赵", "周", "吴",
  541. "徐", "孙", "马", "朱", "胡", "林", "何", "高", "郭", "罗",
  542. "谢", "宋", "唐", "许", "邓", "梁", "韩", "曹", "彭", "余",
  543. "潘", "袁", "蒋", "蔡", "卢", "田", "董", "叶", "程", "姜",
  544. }
  545. queries = append(queries, surnames...)
  546. // CJK: common given-name characters (高频二字名)
  547. given := []string{
  548. "伟", "芳", "娜", "秀", "敏", "静", "丽", "强", "磊", "军",
  549. "洋", "勇", "艳", "杰", "涛", "明", "超", "霞", "平", "刚",
  550. }
  551. queries = append(queries, given...)
  552. // CJK: common modifiers and city prefixes (covers nicknames/titles)
  553. misc := []string{
  554. "大", "小", "新", "老", "中", "天", "金", "一", "龙", "虎",
  555. "京", "沪", "深", "广", "杭", "苏",
  556. }
  557. queries = append(queries, misc...)
  558. return queries
  559. }
  560. // fetchParticipantPages paginates through ChannelParticipantsSearch results.
  561. func (c *Client) fetchParticipantPages(
  562. ctx context.Context,
  563. api *tg.Client,
  564. channel *tg.InputChannel,
  565. query string,
  566. seen map[int64]bool,
  567. extractUsers func(*tg.ChannelsChannelParticipants) int,
  568. outTotalCount *int,
  569. ) error {
  570. const pageSize = 200
  571. offset := 0
  572. for {
  573. result, err := api.ChannelsGetParticipants(ctx, &tg.ChannelsGetParticipantsRequest{
  574. Channel: channel,
  575. Filter: &tg.ChannelParticipantsSearch{Q: query},
  576. Offset: offset,
  577. Limit: pageSize,
  578. Hash: 0,
  579. })
  580. if err != nil {
  581. return wrapFloodWait(err)
  582. }
  583. cp, ok := result.(*tg.ChannelsChannelParticipants)
  584. if !ok || len(cp.Users) == 0 {
  585. break
  586. }
  587. if outTotalCount != nil && cp.Count > *outTotalCount {
  588. *outTotalCount = cp.Count
  589. }
  590. added := extractUsers(cp)
  591. offset += len(cp.Users)
  592. // If no new users were added in this page, stop
  593. if added == 0 || offset >= cp.Count {
  594. break
  595. }
  596. // Page interval: jittered to avoid a detectable request cadence.
  597. if err := jitterSleep(ctx, 800*time.Millisecond, 1500*time.Millisecond); err != nil {
  598. return err
  599. }
  600. }
  601. return nil
  602. }
  603. // getChatParticipants 获取普通群组的成员
  604. func (c *Client) getChatParticipants(ctx context.Context, api *tg.Client, chatID int64) ([]GroupParticipant, error) {
  605. full, err := api.MessagesGetFullChat(ctx, chatID)
  606. if err != nil {
  607. return nil, wrapFloodWait(err)
  608. }
  609. var participants []GroupParticipant
  610. for _, u := range full.Users {
  611. user, ok := u.(*tg.User)
  612. if !ok {
  613. continue
  614. }
  615. p := GroupParticipant{
  616. ID: user.GetID(),
  617. IsBot: user.GetBot(),
  618. IsPremium: user.GetPremium(),
  619. }
  620. if un, ok := user.GetUsername(); ok {
  621. p.Username = un
  622. }
  623. if fn, ok := user.GetFirstName(); ok {
  624. p.FirstName = fn
  625. }
  626. if ln, ok := user.GetLastName(); ok {
  627. p.LastName = ln
  628. }
  629. participants = append(participants, p)
  630. }
  631. return participants, nil
  632. }
  633. // buildProxyDialer creates a DialFunc that routes connections through the given proxy URL.
  634. // Supports socks5://, http://, https:// proxy protocols.
  635. func buildProxyDialer(rawURL string) (dcs.DialFunc, error) {
  636. u, err := url.Parse(rawURL)
  637. if err != nil {
  638. return nil, fmt.Errorf("parse proxy URL: %w", err)
  639. }
  640. switch u.Scheme {
  641. case "socks5", "socks5h":
  642. var auth *xproxy.Auth
  643. if u.User != nil {
  644. auth = &xproxy.Auth{User: u.User.Username()}
  645. if p, ok := u.User.Password(); ok {
  646. auth.Password = p
  647. }
  648. }
  649. dialer, err := xproxy.SOCKS5("tcp", u.Host, auth, xproxy.Direct)
  650. if err != nil {
  651. return nil, fmt.Errorf("create SOCKS5 dialer: %w", err)
  652. }
  653. ctxDialer, ok := dialer.(xproxy.ContextDialer)
  654. if !ok {
  655. return nil, fmt.Errorf("SOCKS5 dialer does not support DialContext")
  656. }
  657. return ctxDialer.DialContext, nil
  658. case "http", "https":
  659. // For HTTP proxies, use CONNECT tunneling
  660. return func(ctx context.Context, network, addr string) (net.Conn, error) {
  661. proxyConn, err := (&net.Dialer{Timeout: 15 * time.Second}).DialContext(ctx, "tcp", u.Host)
  662. if err != nil {
  663. return nil, fmt.Errorf("connect to proxy: %w", err)
  664. }
  665. // Set deadline for the CONNECT handshake
  666. if deadline, ok := ctx.Deadline(); ok {
  667. proxyConn.SetDeadline(deadline)
  668. } else {
  669. proxyConn.SetDeadline(time.Now().Add(15 * time.Second))
  670. }
  671. // Send CONNECT request
  672. connectReq := fmt.Sprintf("CONNECT %s HTTP/1.1\r\nHost: %s\r\n", addr, addr)
  673. if u.User != nil {
  674. pass, _ := u.User.Password()
  675. connectReq += fmt.Sprintf("Proxy-Authorization: Basic %s\r\n",
  676. encodeBasicAuth(u.User.Username(), pass))
  677. }
  678. connectReq += "\r\n"
  679. if _, err := proxyConn.Write([]byte(connectReq)); err != nil {
  680. proxyConn.Close()
  681. return nil, fmt.Errorf("write CONNECT: %w", err)
  682. }
  683. // Read HTTP response using bufio for proper line parsing
  684. br := bufio.NewReader(proxyConn)
  685. statusLine, err := br.ReadString('\n')
  686. if err != nil {
  687. proxyConn.Close()
  688. return nil, fmt.Errorf("read CONNECT status: %w", err)
  689. }
  690. // Parse status code from "HTTP/1.x NNN reason"
  691. parts := strings.SplitN(strings.TrimSpace(statusLine), " ", 3)
  692. if len(parts) < 2 || parts[1] != "200" {
  693. proxyConn.Close()
  694. return nil, fmt.Errorf("CONNECT failed: %s", strings.TrimSpace(statusLine))
  695. }
  696. // Consume remaining headers until empty line
  697. for {
  698. line, err := br.ReadString('\n')
  699. if err != nil {
  700. proxyConn.Close()
  701. return nil, fmt.Errorf("read CONNECT headers: %w", err)
  702. }
  703. if strings.TrimSpace(line) == "" {
  704. break
  705. }
  706. }
  707. // Clear deadline — gotd manages its own timeouts
  708. proxyConn.SetDeadline(time.Time{})
  709. return proxyConn, nil
  710. }, nil
  711. default:
  712. return nil, fmt.Errorf("unsupported proxy scheme: %s", u.Scheme)
  713. }
  714. }
  715. // encodeBasicAuth returns base64-encoded "user:password" for proxy auth.
  716. func encodeBasicAuth(user, password string) string {
  717. return base64.StdEncoding.EncodeToString([]byte(user + ":" + password))
  718. }
  719. // resolveInputPeer resolves a username to an InputPeer
  720. func (c *Client) resolveInputPeer(ctx context.Context, api *tg.Client, username string) (tg.InputPeerClass, error) {
  721. resolved, err := api.ContactsResolveUsername(ctx, &tg.ContactsResolveUsernameRequest{
  722. Username: username,
  723. })
  724. if err != nil {
  725. return nil, wrapFloodWait(err)
  726. }
  727. switch p := resolved.Peer.(type) {
  728. case *tg.PeerChannel:
  729. for _, ch := range resolved.Chats {
  730. if channel, ok := ch.(*tg.Channel); ok && channel.GetID() == p.ChannelID {
  731. accessHash, _ := channel.GetAccessHash()
  732. return &tg.InputPeerChannel{
  733. ChannelID: p.ChannelID,
  734. AccessHash: accessHash,
  735. }, nil
  736. }
  737. }
  738. return &tg.InputPeerChannel{ChannelID: p.ChannelID}, nil
  739. case *tg.PeerUser:
  740. for _, u := range resolved.Users {
  741. if user, ok := u.(*tg.User); ok && user.GetID() == p.UserID {
  742. accessHash, _ := user.GetAccessHash()
  743. return &tg.InputPeerUser{
  744. UserID: p.UserID,
  745. AccessHash: accessHash,
  746. }, nil
  747. }
  748. }
  749. return &tg.InputPeerUser{UserID: p.UserID}, nil
  750. case *tg.PeerChat:
  751. return &tg.InputPeerChat{ChatID: p.ChatID}, nil
  752. }
  753. return &tg.InputPeerEmpty{}, nil
  754. }
  755. // extractMessages extracts messages from a MessagesMessagesClass
  756. func extractMessages(result tg.MessagesMessagesClass) []Message {
  757. var rawMsgs []tg.MessageClass
  758. switch v := result.(type) {
  759. case *tg.MessagesMessages:
  760. rawMsgs = v.Messages
  761. case *tg.MessagesMessagesSlice:
  762. rawMsgs = v.Messages
  763. case *tg.MessagesChannelMessages:
  764. rawMsgs = v.Messages
  765. case *tg.MessagesMessagesNotModified:
  766. return nil
  767. }
  768. var msgs []Message
  769. for _, raw := range rawMsgs {
  770. switch m := raw.(type) {
  771. case *tg.Message:
  772. msg := Message{
  773. ID: m.GetID(),
  774. Text: m.GetMessage(),
  775. IsService: false,
  776. }
  777. // Extract forward source channel username
  778. if fwd, ok := m.GetFwdFrom(); ok {
  779. if fromID, ok := fwd.GetFromID(); ok {
  780. if peerCh, ok := fromID.(*tg.PeerChannel); ok {
  781. _ = peerCh // We'd need channel map to resolve username; skip for now
  782. }
  783. }
  784. }
  785. // Extract t.me links from text
  786. msg.Links = tmeRegexp.FindAllString(msg.Text, -1)
  787. msgs = append(msgs, msg)
  788. case *tg.MessageService:
  789. msgs = append(msgs, Message{
  790. ID: m.GetID(),
  791. IsService: true,
  792. })
  793. }
  794. }
  795. // Sort by ID ascending
  796. sort.Slice(msgs, func(i, j int) bool {
  797. return msgs[i].ID < msgs[j].ID
  798. })
  799. return msgs
  800. }
  801. // isFloodWait 检查错误是否是 FloodWait,提取等待时间
  802. func isFloodWait(err error) (bool, int) {
  803. if d, ok := tgerr.AsFloodWait(err); ok {
  804. return true, int(d.Seconds())
  805. }
  806. return false, 0
  807. }
  808. // wrapFloodWait wraps a FloodWait error into FloodWaitError
  809. func wrapFloodWait(err error) error {
  810. if ok, secs := isFloodWait(err); ok {
  811. return &FloodWaitError{Seconds: secs}
  812. }
  813. return err
  814. }