client.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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. // JoinChannel makes the current account a member of the given channel/supergroup.
  329. // USER_ALREADY_PARTICIPANT is treated as success. FloodWait is wrapped normally.
  330. // Side effect: this account becomes visibly a member of the group — make sure
  331. // the caller actually wants that (private groups require it to see the member
  332. // list, but it leaves a trace in group join/leave activity logs).
  333. func (c *Client) JoinChannel(ctx context.Context, ch *tg.InputChannel) error {
  334. api, err := c.waitReady(ctx)
  335. if err != nil {
  336. return err
  337. }
  338. _, err = api.ChannelsJoinChannel(ctx, ch)
  339. if err != nil {
  340. if tgerr.Is(err, "USER_ALREADY_PARTICIPANT") {
  341. return nil
  342. }
  343. return wrapFloodWait(err)
  344. }
  345. return nil
  346. }
  347. // GetChatParticipantsByID fetches members of a basic (non-supergroup) chat.
  348. // Basic chats have no pagination — this returns everyone in one call.
  349. func (c *Client) GetChatParticipantsByID(ctx context.Context, chatID int64) ([]GroupParticipant, error) {
  350. api, err := c.waitReady(ctx)
  351. if err != nil {
  352. return nil, err
  353. }
  354. return c.getChatParticipants(ctx, api, chatID)
  355. }
  356. // ResolveGroupPeer is a broader resolver than ResolveGroupChannel: it also
  357. // returns a basic-chat ID when the target is not a supergroup/channel.
  358. // Exactly one of (inputCh, chatID) will be non-zero on success.
  359. func (c *Client) ResolveGroupPeer(ctx context.Context, username string) (*tg.InputChannel, *tg.Channel, int64, error) {
  360. api, err := c.waitReady(ctx)
  361. if err != nil {
  362. return nil, nil, 0, err
  363. }
  364. username = strings.TrimPrefix(username, "@")
  365. resolved, err := api.ContactsResolveUsername(ctx, &tg.ContactsResolveUsernameRequest{Username: username})
  366. if err != nil {
  367. return nil, nil, 0, wrapFloodWait(err)
  368. }
  369. for _, ch := range resolved.Chats {
  370. if v, ok := ch.(*tg.Channel); ok {
  371. accessHash, _ := v.GetAccessHash()
  372. return &tg.InputChannel{ChannelID: v.GetID(), AccessHash: accessHash}, v, 0, nil
  373. }
  374. }
  375. if p, ok := resolved.Peer.(*tg.PeerChat); ok {
  376. return nil, nil, p.ChatID, nil
  377. }
  378. return nil, nil, 0, fmt.Errorf("无法解析群组: %s", username)
  379. }
  380. // FetchParticipantsByQuery runs ChannelParticipantsSearch for one query string,
  381. // paginating through all pages. Returns the users surfaced by this query and
  382. // the total count reported by TG. On FloodWait, returns a *FloodWaitError.
  383. // The caller is responsible for deduping across queries.
  384. func (c *Client) FetchParticipantsByQuery(ctx context.Context, channel *tg.InputChannel, query string) ([]GroupParticipant, int, error) {
  385. api, err := c.waitReady(ctx)
  386. if err != nil {
  387. return nil, 0, err
  388. }
  389. const pageSize = 200
  390. offset := 0
  391. totalCount := 0
  392. var out []GroupParticipant
  393. for {
  394. result, err := api.ChannelsGetParticipants(ctx, &tg.ChannelsGetParticipantsRequest{
  395. Channel: channel,
  396. Filter: &tg.ChannelParticipantsSearch{Q: query},
  397. Offset: offset,
  398. Limit: pageSize,
  399. Hash: 0,
  400. })
  401. if err != nil {
  402. return out, totalCount, wrapFloodWait(err)
  403. }
  404. cp, ok := result.(*tg.ChannelsChannelParticipants)
  405. if !ok || len(cp.Users) == 0 {
  406. break
  407. }
  408. if cp.Count > totalCount {
  409. totalCount = cp.Count
  410. }
  411. for _, u := range cp.Users {
  412. user, ok := u.(*tg.User)
  413. if !ok {
  414. continue
  415. }
  416. p := GroupParticipant{
  417. ID: user.GetID(),
  418. IsBot: user.GetBot(),
  419. IsPremium: user.GetPremium(),
  420. }
  421. if un, ok := user.GetUsername(); ok {
  422. p.Username = un
  423. }
  424. if fn, ok := user.GetFirstName(); ok {
  425. p.FirstName = fn
  426. }
  427. if ln, ok := user.GetLastName(); ok {
  428. p.LastName = ln
  429. }
  430. out = append(out, p)
  431. }
  432. offset += len(cp.Users)
  433. if offset >= cp.Count {
  434. break
  435. }
  436. if err := jitterSleep(ctx, 800*time.Millisecond, 1500*time.Millisecond); err != nil {
  437. return out, totalCount, err
  438. }
  439. }
  440. return out, totalCount, nil
  441. }
  442. // GetGroupParticipants 获取群组/超级群组的成员列表(分页拉取全部)
  443. func (c *Client) GetGroupParticipants(ctx context.Context, username string) ([]GroupParticipant, error) {
  444. api, err := c.waitReady(ctx)
  445. if err != nil {
  446. return nil, err
  447. }
  448. username = strings.TrimPrefix(username, "@")
  449. // Resolve the channel/group
  450. resolved, err := api.ContactsResolveUsername(ctx, &tg.ContactsResolveUsernameRequest{
  451. Username: username,
  452. })
  453. if err != nil {
  454. return nil, wrapFloodWait(err)
  455. }
  456. // Find the channel in resolved chats
  457. var inputChannel *tg.InputChannel
  458. for _, ch := range resolved.Chats {
  459. switch v := ch.(type) {
  460. case *tg.Channel:
  461. accessHash, _ := v.GetAccessHash()
  462. inputChannel = &tg.InputChannel{
  463. ChannelID: v.GetID(),
  464. AccessHash: accessHash,
  465. }
  466. }
  467. }
  468. if inputChannel == nil {
  469. // Try as basic chat - get participants via MessagesGetFullChat
  470. if p, ok := resolved.Peer.(*tg.PeerChat); ok {
  471. return c.getChatParticipants(ctx, api, p.ChatID)
  472. }
  473. return nil, fmt.Errorf("无法解析群组: %s", username)
  474. }
  475. // Strategy: use ChannelParticipantsSearch with empty query (returns more than Recent),
  476. // then iterate alphabet queries to discover members beyond the 200 limit per query.
  477. seen := make(map[int64]bool)
  478. var allParticipants []GroupParticipant
  479. // Helper to extract users from a page
  480. extractUsers := func(cp *tg.ChannelsChannelParticipants) int {
  481. added := 0
  482. for _, u := range cp.Users {
  483. user, ok := u.(*tg.User)
  484. if !ok || seen[user.GetID()] {
  485. continue
  486. }
  487. seen[user.GetID()] = true
  488. p := GroupParticipant{
  489. ID: user.GetID(),
  490. IsBot: user.GetBot(),
  491. IsPremium: user.GetPremium(),
  492. }
  493. if un, ok := user.GetUsername(); ok {
  494. p.Username = un
  495. }
  496. if fn, ok := user.GetFirstName(); ok {
  497. p.FirstName = fn
  498. }
  499. if ln, ok := user.GetLastName(); ok {
  500. p.LastName = ln
  501. }
  502. allParticipants = append(allParticipants, p)
  503. added++
  504. }
  505. return added
  506. }
  507. // Phase 1: Search with empty query (gets up to ~200)
  508. totalCount := 0
  509. if err := c.fetchParticipantPages(ctx, api, inputChannel, "", seen, extractUsers, &totalCount); err != nil {
  510. if len(allParticipants) > 0 {
  511. return allParticipants, err
  512. }
  513. return nil, err
  514. }
  515. // Phase 2: If group has more members than we found, search by character sets to discover more.
  516. // We pace queries with jitter (2–4s) to avoid looking like a bot scanner and triggering FloodWait.
  517. // If FloodWait does hit, stop early and return what we already have — the calling task can
  518. // re-attempt later after the account cools down.
  519. if totalCount > len(allParticipants) && totalCount <= 10000 {
  520. queries := participantSearchQueries()
  521. for _, q := range queries {
  522. if ctx.Err() != nil {
  523. break
  524. }
  525. if len(allParticipants) >= totalCount {
  526. break // already collected everyone visible
  527. }
  528. beforeCount := len(allParticipants)
  529. err := c.fetchParticipantPages(ctx, api, inputChannel, q, seen, extractUsers, nil)
  530. if err != nil {
  531. if fwe, ok := err.(*FloodWaitError); ok {
  532. log.Printf("[tg_client] flood wait %ds during search q=%q for %s; returning %d/%d",
  533. fwe.Seconds, q, username, len(allParticipants), totalCount)
  534. } else {
  535. log.Printf("[tg_client] search q=%q for %s: %v (returning partial)", q, username, err)
  536. }
  537. break
  538. }
  539. if len(allParticipants) == beforeCount {
  540. continue // no new results; skip sleep and try next query
  541. }
  542. if err := jitterSleep(ctx, 2*time.Second, 4*time.Second); err != nil {
  543. return allParticipants, err
  544. }
  545. }
  546. }
  547. log.Printf("[tg_client] fetched %d/%d participants for %s", len(allParticipants), totalCount, username)
  548. return allParticipants, nil
  549. }
  550. // jitterSleep sleeps a random duration in [min, max) while respecting ctx.
  551. // Returns ctx.Err() if cancelled. Used to spread out TG API calls and avoid
  552. // looking like a deterministic scanner.
  553. func jitterSleep(ctx context.Context, min, max time.Duration) error {
  554. d := min + time.Duration(rand.Int64N(int64(max-min)))
  555. select {
  556. case <-ctx.Done():
  557. return ctx.Err()
  558. case <-time.After(d):
  559. return nil
  560. }
  561. }
  562. // participantSearchQueries returns search queries covering Latin, Cyrillic, Japanese,
  563. // Korean, and CJK scripts. TG's ChannelParticipantsSearch does substring matching on
  564. // first_name + last_name + username, so more starter-character coverage = more users
  565. // surfaced on groups beyond the 200-per-query cap. Total ~150 queries.
  566. func participantSearchQueries() []string {
  567. queries := make([]string, 0, 170)
  568. // Latin a-z
  569. for c := 'a'; c <= 'z'; c++ {
  570. queries = append(queries, string(c))
  571. }
  572. // Digits 0-9
  573. for c := '0'; c <= '9'; c++ {
  574. queries = append(queries, string(c))
  575. }
  576. // Cyrillic а-я
  577. for c := 'а'; c <= 'я'; c++ {
  578. queries = append(queries, string(c))
  579. }
  580. // Japanese Hiragana — common name-starter syllables
  581. queries = append(queries,
  582. "あ", "い", "う", "え", "お",
  583. "か", "さ", "た", "な", "ま",
  584. )
  585. // Korean Hangul — common initial syllables
  586. queries = append(queries,
  587. "가", "나", "다", "라", "마", "바", "사", "아", "자", "차",
  588. "카", "타", "파", "하",
  589. )
  590. // CJK: top Chinese surnames (百家姓 high frequency)
  591. surnames := []string{
  592. "王", "李", "张", "刘", "陈", "杨", "黄", "赵", "周", "吴",
  593. "徐", "孙", "马", "朱", "胡", "林", "何", "高", "郭", "罗",
  594. "谢", "宋", "唐", "许", "邓", "梁", "韩", "曹", "彭", "余",
  595. "潘", "袁", "蒋", "蔡", "卢", "田", "董", "叶", "程", "姜",
  596. }
  597. queries = append(queries, surnames...)
  598. // CJK: common given-name characters (高频二字名)
  599. given := []string{
  600. "伟", "芳", "娜", "秀", "敏", "静", "丽", "强", "磊", "军",
  601. "洋", "勇", "艳", "杰", "涛", "明", "超", "霞", "平", "刚",
  602. }
  603. queries = append(queries, given...)
  604. // CJK: common modifiers and city prefixes (covers nicknames/titles)
  605. misc := []string{
  606. "大", "小", "新", "老", "中", "天", "金", "一", "龙", "虎",
  607. "京", "沪", "深", "广", "杭", "苏",
  608. }
  609. queries = append(queries, misc...)
  610. return queries
  611. }
  612. // fetchParticipantPages paginates through ChannelParticipantsSearch results.
  613. func (c *Client) fetchParticipantPages(
  614. ctx context.Context,
  615. api *tg.Client,
  616. channel *tg.InputChannel,
  617. query string,
  618. seen map[int64]bool,
  619. extractUsers func(*tg.ChannelsChannelParticipants) int,
  620. outTotalCount *int,
  621. ) error {
  622. const pageSize = 200
  623. offset := 0
  624. for {
  625. result, err := api.ChannelsGetParticipants(ctx, &tg.ChannelsGetParticipantsRequest{
  626. Channel: channel,
  627. Filter: &tg.ChannelParticipantsSearch{Q: query},
  628. Offset: offset,
  629. Limit: pageSize,
  630. Hash: 0,
  631. })
  632. if err != nil {
  633. return wrapFloodWait(err)
  634. }
  635. cp, ok := result.(*tg.ChannelsChannelParticipants)
  636. if !ok || len(cp.Users) == 0 {
  637. break
  638. }
  639. if outTotalCount != nil && cp.Count > *outTotalCount {
  640. *outTotalCount = cp.Count
  641. }
  642. added := extractUsers(cp)
  643. offset += len(cp.Users)
  644. // If no new users were added in this page, stop
  645. if added == 0 || offset >= cp.Count {
  646. break
  647. }
  648. // Page interval: jittered to avoid a detectable request cadence.
  649. if err := jitterSleep(ctx, 800*time.Millisecond, 1500*time.Millisecond); err != nil {
  650. return err
  651. }
  652. }
  653. return nil
  654. }
  655. // getChatParticipants 获取普通群组的成员
  656. func (c *Client) getChatParticipants(ctx context.Context, api *tg.Client, chatID int64) ([]GroupParticipant, error) {
  657. full, err := api.MessagesGetFullChat(ctx, chatID)
  658. if err != nil {
  659. return nil, wrapFloodWait(err)
  660. }
  661. var participants []GroupParticipant
  662. for _, u := range full.Users {
  663. user, ok := u.(*tg.User)
  664. if !ok {
  665. continue
  666. }
  667. p := GroupParticipant{
  668. ID: user.GetID(),
  669. IsBot: user.GetBot(),
  670. IsPremium: user.GetPremium(),
  671. }
  672. if un, ok := user.GetUsername(); ok {
  673. p.Username = un
  674. }
  675. if fn, ok := user.GetFirstName(); ok {
  676. p.FirstName = fn
  677. }
  678. if ln, ok := user.GetLastName(); ok {
  679. p.LastName = ln
  680. }
  681. participants = append(participants, p)
  682. }
  683. return participants, nil
  684. }
  685. // buildProxyDialer creates a DialFunc that routes connections through the given proxy URL.
  686. // Supports socks5://, http://, https:// proxy protocols.
  687. func buildProxyDialer(rawURL string) (dcs.DialFunc, error) {
  688. u, err := url.Parse(rawURL)
  689. if err != nil {
  690. return nil, fmt.Errorf("parse proxy URL: %w", err)
  691. }
  692. switch u.Scheme {
  693. case "socks5", "socks5h":
  694. var auth *xproxy.Auth
  695. if u.User != nil {
  696. auth = &xproxy.Auth{User: u.User.Username()}
  697. if p, ok := u.User.Password(); ok {
  698. auth.Password = p
  699. }
  700. }
  701. dialer, err := xproxy.SOCKS5("tcp", u.Host, auth, xproxy.Direct)
  702. if err != nil {
  703. return nil, fmt.Errorf("create SOCKS5 dialer: %w", err)
  704. }
  705. ctxDialer, ok := dialer.(xproxy.ContextDialer)
  706. if !ok {
  707. return nil, fmt.Errorf("SOCKS5 dialer does not support DialContext")
  708. }
  709. return ctxDialer.DialContext, nil
  710. case "http", "https":
  711. // For HTTP proxies, use CONNECT tunneling
  712. return func(ctx context.Context, network, addr string) (net.Conn, error) {
  713. proxyConn, err := (&net.Dialer{Timeout: 15 * time.Second}).DialContext(ctx, "tcp", u.Host)
  714. if err != nil {
  715. return nil, fmt.Errorf("connect to proxy: %w", err)
  716. }
  717. // Set deadline for the CONNECT handshake
  718. if deadline, ok := ctx.Deadline(); ok {
  719. proxyConn.SetDeadline(deadline)
  720. } else {
  721. proxyConn.SetDeadline(time.Now().Add(15 * time.Second))
  722. }
  723. // Send CONNECT request
  724. connectReq := fmt.Sprintf("CONNECT %s HTTP/1.1\r\nHost: %s\r\n", addr, addr)
  725. if u.User != nil {
  726. pass, _ := u.User.Password()
  727. connectReq += fmt.Sprintf("Proxy-Authorization: Basic %s\r\n",
  728. encodeBasicAuth(u.User.Username(), pass))
  729. }
  730. connectReq += "\r\n"
  731. if _, err := proxyConn.Write([]byte(connectReq)); err != nil {
  732. proxyConn.Close()
  733. return nil, fmt.Errorf("write CONNECT: %w", err)
  734. }
  735. // Read HTTP response using bufio for proper line parsing
  736. br := bufio.NewReader(proxyConn)
  737. statusLine, err := br.ReadString('\n')
  738. if err != nil {
  739. proxyConn.Close()
  740. return nil, fmt.Errorf("read CONNECT status: %w", err)
  741. }
  742. // Parse status code from "HTTP/1.x NNN reason"
  743. parts := strings.SplitN(strings.TrimSpace(statusLine), " ", 3)
  744. if len(parts) < 2 || parts[1] != "200" {
  745. proxyConn.Close()
  746. return nil, fmt.Errorf("CONNECT failed: %s", strings.TrimSpace(statusLine))
  747. }
  748. // Consume remaining headers until empty line
  749. for {
  750. line, err := br.ReadString('\n')
  751. if err != nil {
  752. proxyConn.Close()
  753. return nil, fmt.Errorf("read CONNECT headers: %w", err)
  754. }
  755. if strings.TrimSpace(line) == "" {
  756. break
  757. }
  758. }
  759. // Clear deadline — gotd manages its own timeouts
  760. proxyConn.SetDeadline(time.Time{})
  761. return proxyConn, nil
  762. }, nil
  763. default:
  764. return nil, fmt.Errorf("unsupported proxy scheme: %s", u.Scheme)
  765. }
  766. }
  767. // encodeBasicAuth returns base64-encoded "user:password" for proxy auth.
  768. func encodeBasicAuth(user, password string) string {
  769. return base64.StdEncoding.EncodeToString([]byte(user + ":" + password))
  770. }
  771. // resolveInputPeer resolves a username to an InputPeer
  772. func (c *Client) resolveInputPeer(ctx context.Context, api *tg.Client, username string) (tg.InputPeerClass, error) {
  773. resolved, err := api.ContactsResolveUsername(ctx, &tg.ContactsResolveUsernameRequest{
  774. Username: username,
  775. })
  776. if err != nil {
  777. return nil, wrapFloodWait(err)
  778. }
  779. switch p := resolved.Peer.(type) {
  780. case *tg.PeerChannel:
  781. for _, ch := range resolved.Chats {
  782. if channel, ok := ch.(*tg.Channel); ok && channel.GetID() == p.ChannelID {
  783. accessHash, _ := channel.GetAccessHash()
  784. return &tg.InputPeerChannel{
  785. ChannelID: p.ChannelID,
  786. AccessHash: accessHash,
  787. }, nil
  788. }
  789. }
  790. return &tg.InputPeerChannel{ChannelID: p.ChannelID}, nil
  791. case *tg.PeerUser:
  792. for _, u := range resolved.Users {
  793. if user, ok := u.(*tg.User); ok && user.GetID() == p.UserID {
  794. accessHash, _ := user.GetAccessHash()
  795. return &tg.InputPeerUser{
  796. UserID: p.UserID,
  797. AccessHash: accessHash,
  798. }, nil
  799. }
  800. }
  801. return &tg.InputPeerUser{UserID: p.UserID}, nil
  802. case *tg.PeerChat:
  803. return &tg.InputPeerChat{ChatID: p.ChatID}, nil
  804. }
  805. return &tg.InputPeerEmpty{}, nil
  806. }
  807. // extractMessages extracts messages from a MessagesMessagesClass
  808. func extractMessages(result tg.MessagesMessagesClass) []Message {
  809. var rawMsgs []tg.MessageClass
  810. switch v := result.(type) {
  811. case *tg.MessagesMessages:
  812. rawMsgs = v.Messages
  813. case *tg.MessagesMessagesSlice:
  814. rawMsgs = v.Messages
  815. case *tg.MessagesChannelMessages:
  816. rawMsgs = v.Messages
  817. case *tg.MessagesMessagesNotModified:
  818. return nil
  819. }
  820. var msgs []Message
  821. for _, raw := range rawMsgs {
  822. switch m := raw.(type) {
  823. case *tg.Message:
  824. msg := Message{
  825. ID: m.GetID(),
  826. Text: m.GetMessage(),
  827. IsService: false,
  828. }
  829. // Extract forward source channel username
  830. if fwd, ok := m.GetFwdFrom(); ok {
  831. if fromID, ok := fwd.GetFromID(); ok {
  832. if peerCh, ok := fromID.(*tg.PeerChannel); ok {
  833. _ = peerCh // We'd need channel map to resolve username; skip for now
  834. }
  835. }
  836. }
  837. // Extract t.me links from text
  838. msg.Links = tmeRegexp.FindAllString(msg.Text, -1)
  839. msgs = append(msgs, msg)
  840. case *tg.MessageService:
  841. msgs = append(msgs, Message{
  842. ID: m.GetID(),
  843. IsService: true,
  844. })
  845. }
  846. }
  847. // Sort by ID ascending
  848. sort.Slice(msgs, func(i, j int) bool {
  849. return msgs[i].ID < msgs[j].ID
  850. })
  851. return msgs
  852. }
  853. // isFloodWait 检查错误是否是 FloodWait,提取等待时间
  854. func isFloodWait(err error) (bool, int) {
  855. if d, ok := tgerr.AsFloodWait(err); ok {
  856. return true, int(d.Seconds())
  857. }
  858. return false, 0
  859. }
  860. // wrapFloodWait wraps a FloodWait error into FloodWaitError
  861. func wrapFloodWait(err error) error {
  862. if ok, secs := isFloodWait(err); ok {
  863. return &FloodWaitError{Seconds: secs}
  864. }
  865. return err
  866. }