mtproto 0.0.20 → 0.0.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9c0529af4d0277b14c02e0a618a865e67234aaf956873d749579f0aae5d2b4c8
4
- data.tar.gz: c519588e92e960402dfe515e9b202e8f9c83a05ca6a21952a05d339b031c8878
3
+ metadata.gz: 7b7ded80a63fcfa91991547984a7ffd73198a66216b920106accd235958fde49
4
+ data.tar.gz: ec61fe873b71bac923edf826c93a2c46c57ef4b602e92e17a2fc9891aa9ad052
5
5
  SHA512:
6
- metadata.gz: 782793b348cdd81fd8bfb43ceedd459bef20c0c2033c5b36068a392db9b3cc796273c0debfe9ddf0a1c2965b6b7aebe4a591f8b6afc35fed180e116c40d24a34
7
- data.tar.gz: e2221dcca9e4e15f92b88140ef5eeb523e838a1a88687497e87a0241b2c196f2a6c70b841fbfecca36fccbb2ac6f68fa2be912dbe6cdc94f2c32a7175cec9cc5
6
+ metadata.gz: cd4efbd5e89da27a91baa98abcc8b6dbffc113b3598ea954c2a20daa31510e5b9ad830cfd1c279ed04fca04a4550baf1c657d286fffd12d1ca452f86f27af473
7
+ data.tar.gz: 8ad12d3ff4820cbc0da0b3ccd00b81d4fc30e47c2412c88cd86c8fa15997026d2323255682410e2aa25f02bc4c34d0845aebee1f949d7cbc5fcbddc182e11ae9
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'dc'
4
+ require_relative 'errors'
5
+
6
+ module MTProto
7
+ # Logs a bot in with auth.importBotAuthorization, following a USER_MIGRATE_X
8
+ # redirect. A bot token is bound to a home DC; importing it from any other DC
9
+ # answers RPC 303 USER_MIGRATE_X naming that DC. On that reply we drop the seed
10
+ # connection and re-import against a fresh connection to the named DC. The DC
11
+ # switch lives here rather than in the DC-bound Client (mirroring
12
+ # FileDownloader); addresses come from MTProto::DC, never hardcoded per bot.
13
+ #
14
+ # Runs inside an Async reactor: the default connect starts a receiver.
15
+ class BotAuthorizer
16
+ def initialize(api_id:, api_hash:, public_key:, test_mode: false, timeout: 10, connect: nil)
17
+ @api_id = api_id
18
+ @api_hash = api_hash
19
+ @public_key = public_key
20
+ @test_mode = test_mode
21
+ @timeout = timeout
22
+ @connect = connect || method(:default_connect)
23
+ end
24
+
25
+ # Returns a connected, key-exchanged, receiving, initialised, authorized
26
+ # Client on the bot's home DC. seed_dc is only where the search starts; the
27
+ # real DC is discovered from the migrate redirect.
28
+ def authorize(bot_auth_token, seed_dc: 2)
29
+ dc = seed_dc
30
+ visited = []
31
+ loop do
32
+ visited << dc
33
+ client = @connect.call(dc)
34
+ begin
35
+ client.api.import_bot_authorization(bot_auth_token)
36
+ return client
37
+ rescue RpcError => e
38
+ raise if e.migrate_dc.nil?
39
+
40
+ client.disconnect!
41
+ dc = e.migrate_dc
42
+ raise Error, "bot authorization keeps migrating (revisited DC #{dc})" if visited.include?(dc)
43
+ end
44
+ end
45
+ end
46
+
47
+ private
48
+
49
+ def default_connect(dc)
50
+ addr = DC.address(dc, test: @test_mode)
51
+ client = Client.new(
52
+ api_id: @api_id, api_hash: @api_hash,
53
+ host: addr[:host], port: addr[:port],
54
+ public_key: @public_key, dc_number: dc, test_mode: @test_mode, timeout: @timeout
55
+ )
56
+ client.connect!
57
+ client.exchange_keys!
58
+ client.start_receiving!
59
+ client.init_connection!
60
+ client
61
+ end
62
+ end
63
+ end
@@ -31,6 +31,12 @@ module MTProto
31
31
  def flood_wait_seconds
32
32
  error_message&.match(/FLOOD_WAIT_(\d+)/)&.[](1)&.to_i
33
33
  end
34
+
35
+ # A 303 *_MIGRATE_N reply means the account/bot lives on DC N: reconnect there
36
+ # and retry. Returns that DC number, or nil for a non-migrate error.
37
+ def migrate_dc
38
+ error_message&.match(/\A(?:USER|NETWORK|PHONE)_MIGRATE_(\d+)\z/)&.[](1)&.to_i
39
+ end
34
40
  end
35
41
 
36
42
  class UnexpectedConstructorError < Error
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MTProto
4
- VERSION = '0.0.20'
4
+ VERSION = '0.0.21'
5
5
  end
data/lib/mtproto.rb CHANGED
@@ -39,6 +39,7 @@ require_relative 'mtproto/session'
39
39
  require_relative 'mtproto/encrypted_message'
40
40
  require_relative 'mtproto/client'
41
41
  require_relative 'mtproto/file_downloader'
42
+ require_relative 'mtproto/bot_authorizer'
42
43
 
43
44
  module MTProto
44
45
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mtproto
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.20
4
+ version: 0.0.21
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artem Levenkov
@@ -60,6 +60,7 @@ files:
60
60
  - lib/mtproto.rb
61
61
  - lib/mtproto/auth_key_generator.rb
62
62
  - lib/mtproto/binary.rb
63
+ - lib/mtproto/bot_authorizer.rb
63
64
  - lib/mtproto/client.rb
64
65
  - lib/mtproto/client/api.rb
65
66
  - lib/mtproto/client/api/check_password.rb