class Synacrb::Session

Public Class Methods

new(addr, hash, port = Common::DEFAULT_PORT, &callback) click to toggle source

Connect to the server

# 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

Public Instance Methods

close() click to toggle source

Close the connection

# File lib/synacrb.rb, line 86
def close()
    @stream.close
end
inner_stream() click to toggle source

Returns inner connection

# File lib/synacrb.rb, line 48
def inner_stream()
    @stream
end
login_with_password(bot, name, password) click to toggle source

Sends the login packet with specific password. Read the result with `read`. Warning: Strongly disencouraged. Use tokens instead, when possible.

# 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) click to toggle source

Sends the login packet with specific token. Read the result with `read`.

# File lib/synacrb.rb, line 61
def login_with_token(bot, name, token)
    send Common::Login.new(bot, name, nil, token)
end
read() click to toggle source

Read a packet from the connection

# 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) click to toggle source

Transmit a packet over the connection

# 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) click to toggle source

OpenSSL verify callback used by initialize when optional callback argument isn't set.

# 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