telegram-mtproto-ruby 0.1.0

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.
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Telegram
4
+ module Mtproto
5
+ module Ruby
6
+ VERSION = "0.1.0"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "ruby/version"
4
+
5
+ module Telegram
6
+ module Mtproto
7
+ module Ruby
8
+ class Error < StandardError; end
9
+ # Your code goes here...
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TelegramMtproto
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "telegram_mtproto/version"
4
+ require_relative "telegram_m_t_proto_clean"
5
+
6
+ module TelegramMtproto
7
+ class Error < StandardError; end
8
+
9
+ # Main MTProto client class
10
+ #
11
+ # Example usage:
12
+ # client = TelegramMtproto.new(api_id, api_hash, phone_number)
13
+ # result = client.send_code
14
+ # if result[:success]
15
+ # auth_result = client.sign_in(result[:phone_code_hash], pin_code)
16
+ # end
17
+ def self.new(api_id, api_hash, phone_number)
18
+ TelegramMTProtoClean.new(api_id, api_hash, phone_number)
19
+ end
20
+ end
@@ -0,0 +1,92 @@
1
+ require 'socket'
2
+ require 'timeout'
3
+ require 'openssl'
4
+
5
+ class TelegramPlainTcp
6
+ def initialize(host, port)
7
+ @host = host
8
+ @port = port
9
+ @socket = nil
10
+ end
11
+
12
+ def connect
13
+ return if @socket
14
+
15
+ Rails.logger.info "🔌 PLAIN TCP: connecting to #{@host}:#{@port}..."
16
+ @socket = TCPSocket.new(@host, @port)
17
+ Rails.logger.info "✅ PLAIN TCP: connected to #{@host}:#{@port}"
18
+ end
19
+
20
+ def send(data)
21
+ connect unless @socket
22
+
23
+ Rails.logger.info "📤 PLAIN TCP: sending #{data.length} bytes"
24
+ Rails.logger.info "📡 PLAIN TCP SEND (#{data.length} bytes): #{data.unpack1('H*')}"
25
+
26
+ written = @socket.write(data)
27
+ Rails.logger.info "📤 PLAIN TCP: wrote #{written} bytes to socket"
28
+ end
29
+
30
+ def recv(timeout: 60)
31
+ connect unless @socket
32
+
33
+ Rails.logger.info "📥 PLAIN TCP: waiting for response (#{timeout}s timeout)..."
34
+
35
+ begin
36
+ Timeout::timeout(timeout) do
37
+ # Read first 8 bytes for MTProto header
38
+ header = read_exactly(8)
39
+ Rails.logger.info "📥 PLAIN TCP: got header: #{header.unpack1('H*')}"
40
+
41
+ # auth_key_id (8 bytes) + msg_id (8 bytes) already read
42
+ # Now read length (4 bytes)
43
+ length_bytes = read_exactly(4)
44
+ length = length_bytes.unpack1('L<')
45
+ Rails.logger.info "📥 PLAIN TCP: message length: #{length} bytes"
46
+
47
+ # Read the actual message body
48
+ body = read_exactly(length)
49
+ Rails.logger.info "📥 PLAIN TCP: got body: #{length} bytes"
50
+
51
+ # Combine all parts
52
+ result = header + length_bytes + body
53
+ Rails.logger.info "📡 PLAIN TCP RECV (#{result.length} bytes): #{result.unpack1('H*')}"
54
+
55
+ result
56
+ end
57
+ rescue Timeout::Error
58
+ Rails.logger.error "❌ PLAIN TCP: timeout waiting for response"
59
+ raise "PLAIN TCP timeout"
60
+ rescue => e
61
+ Rails.logger.error "❌ PLAIN TCP: recv error: #{e.message}"
62
+ raise "PLAIN TCP recv error: #{e.message}"
63
+ end
64
+ end
65
+
66
+ def close
67
+ if @socket
68
+ Rails.logger.debug "🔌 Closing plain TCP connection"
69
+ begin
70
+ @socket.close
71
+ rescue => e
72
+ Rails.logger.debug "❌ Error closing plain TCP: #{e.message}"
73
+ end
74
+ @socket = nil
75
+ end
76
+ end
77
+
78
+ private
79
+
80
+ def read_exactly(bytes_needed)
81
+ result = ''.b
82
+ while result.length < bytes_needed
83
+ chunk = @socket.read(bytes_needed - result.length)
84
+ if chunk.nil? || chunk.empty?
85
+ raise "EOF while reading: expected #{bytes_needed}, got #{result.length}"
86
+ end
87
+ result += chunk
88
+ Rails.logger.debug "📥 PLAIN TCP: read #{chunk.length} bytes (#{result.length}/#{bytes_needed})"
89
+ end
90
+ result
91
+ end
92
+ end
@@ -0,0 +1,8 @@
1
+ module Telegram
2
+ module Mtproto
3
+ module Ruby
4
+ VERSION: String
5
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
6
+ end
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: telegram-mtproto-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - mikefluff
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: A complete Ruby implementation of Telegram's MTProto 2.0 protocol with
13
+ DH handshake, AES-IGE encryption, and TL schema support. Includes auth.sendCode,
14
+ auth.signIn, contacts.getContacts, messages.sendMessage, and updates.getDifference
15
+ methods.
16
+ email:
17
+ - mike@mikefluff.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - README.md
23
+ - Rakefile
24
+ - examples/complete_demo.rb
25
+ - lib/telegram/auth.rb
26
+ - lib/telegram/binary_reader.rb
27
+ - lib/telegram/connection/tcp_full_connection.rb
28
+ - lib/telegram/crypto.rb
29
+ - lib/telegram/crypto_rsa_keys.rb
30
+ - lib/telegram/senders/mtproto_encrypted_sender.rb
31
+ - lib/telegram/senders/mtproto_plain_sender.rb
32
+ - lib/telegram/serialization.rb
33
+ - lib/telegram/tl/api.tl
34
+ - lib/telegram/tl/mtproto.tl
35
+ - lib/telegram/tl_object.rb
36
+ - lib/telegram/tl_reader.rb
37
+ - lib/telegram/tl_schema.rb
38
+ - lib/telegram/tl_writer.rb
39
+ - lib/telegram_m_t_proto_clean.rb
40
+ - lib/telegram_mtproto.rb
41
+ - lib/telegram_mtproto/ruby.rb
42
+ - lib/telegram_mtproto/ruby/version.rb
43
+ - lib/telegram_mtproto/version.rb
44
+ - lib/telegram_plain_tcp.rb
45
+ - sig/telegram/mtproto/ruby.rbs
46
+ homepage: https://github.com/mikefluff/telegram-mtproto-ruby
47
+ licenses: []
48
+ metadata:
49
+ allowed_push_host: https://rubygems.org
50
+ homepage_uri: https://github.com/mikefluff/telegram-mtproto-ruby
51
+ source_code_uri: https://github.com/mikefluff/telegram-mtproto-ruby
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 3.0.0
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubygems_version: 3.6.9
67
+ specification_version: 4
68
+ summary: Pure Ruby MTProto 2.0 implementation for Telegram API
69
+ test_files: []