Class: Synacrb::Session
- Inherits:
-
Object
- Object
- Synacrb::Session
- Defined in:
- lib/synacrb.rb
Instance Method Summary collapse
-
#close ⇒ Object
Close the connection.
-
#initialize(addr, hash, port = Common::DEFAULT_PORT, &callback) ⇒ Session
constructor
Connect to the server.
-
#inner_stream ⇒ Object
Returns inner connection.
-
#login_with_password(bot, name, password) ⇒ Object
Sends the login packet with specific password.
-
#login_with_token(bot, name, token) ⇒ Object
Sends the login packet with specific token.
-
#read ⇒ Object
Read a packet from the connection.
-
#send(packet) ⇒ Object
Transmit a packet over the connection.
-
#verify_callback(_, cert) ⇒ Object
OpenSSL verify callback used by initialize when optional callback argument isn’t set.
Constructor Details
#initialize(addr, hash, port = Common::DEFAULT_PORT, &callback) ⇒ Session
Connect to the server
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/synacrb.rb', line 23 def initialize(addr, hash, port = Common::DEFAULT_PORT, &callback) tcp = TCPSocket.new addr, port @hash = hash context = OpenSSL::SSL::SSLContext.new if callback.nil? callback = method(:verify_callback) end context.verify_callback = callback context.verify_mode = OpenSSL::SSL::VERIFY_PEER @stream = OpenSSL::SSL::SSLSocket.new tcp, context @stream.connect end |
Instance Method Details
#close ⇒ Object
Close the connection
86 87 88 |
# File 'lib/synacrb.rb', line 86 def close() @stream.close end |
#inner_stream ⇒ Object
Returns inner connection
48 49 50 |
# File 'lib/synacrb.rb', line 48 def inner_stream() @stream end |
#login_with_password(bot, name, password) ⇒ Object
Sends the login packet with specific password. Read the result with ‘read`. Warning: Strongly disencouraged. Use tokens instead, when possible.
55 56 57 |
# File 'lib/synacrb.rb', line 55 def login_with_password(bot, name, password) send Common::Login.new(bot, name, password, nil) end |
#login_with_token(bot, name, token) ⇒ Object
Sends the login packet with specific token. Read the result with ‘read`.
61 62 63 |
# File 'lib/synacrb.rb', line 61 def login_with_token(bot, name, token) send Common::Login.new(bot, name, nil, token) end |
#read ⇒ Object
Read a packet from the connection
75 76 77 78 79 80 81 82 83 |
# File 'lib/synacrb.rb', line 75 def read() size_a = @stream.read 2 size = Common.decode_u16(size_a) data = @stream.read size data = MessagePack.unpack data class_ = Common.packet_from_id data[0] class_.new *data[1][0] end |
#send(packet) ⇒ Object
Transmit a packet over the connection
66 67 68 69 70 71 72 |
# File 'lib/synacrb.rb', line 66 def send(packet) id = Common.packet_to_id(packet) data = [id, [packet.to_a]].to_msgpack @stream.write Common.encode_u16(data.length) @stream.write data end |
#verify_callback(_, cert) ⇒ Object
OpenSSL verify callback used by initialize when optional callback argument isn’t set.
39 40 41 42 43 44 45 |
# File 'lib/synacrb.rb', line 39 def verify_callback(_, cert) pem = cert.current_cert.public_key.to_pem sha256 = OpenSSL::Digest::SHA256.new hash = sha256.digest(pem).unpack("H*") hash[0].casecmp(@hash).zero? end |