Class: Blur::Network::Connection
- Inherits:
-
EM::Protocols::LineAndTextProtocol
- Object
- EM::Protocols::LineAndTextProtocol
- Blur::Network::Connection
- Defined in:
- library/blur/network/connection.rb
Overview
The Connection
class inherits the LineAndText protocol bundled with the eventmachine library.
It merely acts as a receiving handler for all messages eventmachine throws at it through its lifetime.
Constant Summary collapse
- SSLValidationError =
Class.new StandardError
Instance Method Summary collapse
-
#connection_completed ⇒ Object
Called once the connection is finally established.
-
#established? ⇒ Boolean
Check whether or not connection is established.
-
#initialize(network) ⇒ Connection
constructor
EventMachine instantiates this class, and then sends event messages to that instance.
-
#post_init ⇒ Object
Called when a new connection is being set up, all we’re going to use it for is to enable SSL/TLS on our connection.
-
#receive_line(line) ⇒ Object
Called when a line was received, the connection sends it to the network delegate which then sends it to the client.
-
#ssl_handshake_completed ⇒ Object
Called when the SSL handshake was completed with the remote server, the reason we tell the network that we’re connected here is to ensure that the SSL/TLS encryption succeeded before we start talking nonsense to the server.
-
#ssl_verify_peer(peer_cert) ⇒ Object
Validates that the peer certificate has the correct fingerprint as specified in the :fingerprint :ssl option.
-
#unbind ⇒ Object
Called just as the connection is being terminated, either by remote or local.
Constructor Details
#initialize(network) ⇒ Connection
EventMachine instantiates this class, and then sends event messages to that instance.
21 22 23 24 25 26 |
# File 'library/blur/network/connection.rb', line 21 def initialize network @network = network @connected = false super end |
Instance Method Details
#connection_completed ⇒ Object
Called once the connection is finally established.
99 100 101 102 103 104 |
# File 'library/blur/network/connection.rb', line 99 def connection_completed # We aren't completely connected yet if the connection is encrypted. unless @network.secure? connected! end end |
#established? ⇒ Boolean
Check whether or not connection is established.
17 |
# File 'library/blur/network/connection.rb', line 17 def established?; @connected == true end |
#post_init ⇒ Object
Called when a new connection is being set up, all we’re going to use it for is to enable SSL/TLS on our connection.
30 31 32 33 34 35 36 |
# File 'library/blur/network/connection.rb', line 30 def post_init if @network.secure? verify_peer = (@network.[:ssl_no_verify] ? false : true) start_tls verify_peer: verify_peer end end |
#receive_line(line) ⇒ Object
Called when a line was received, the connection sends it to the network delegate which then sends it to the client.
40 41 42 43 44 |
# File 'library/blur/network/connection.rb', line 40 def receive_line line = IRCParser::Message.parse line @network. end |
#ssl_handshake_completed ⇒ Object
Called when the SSL handshake was completed with the remote server, the reason we tell the network that we’re connected here is to ensure that the SSL/TLS encryption succeeded before we start talking nonsense to the server.
50 51 52 |
# File 'library/blur/network/connection.rb', line 50 def ssl_handshake_completed connected! end |
#ssl_verify_peer(peer_cert) ⇒ Object
This doesn’t support intermediate certificate authorities!
Validates that the peer certificate has the correct fingerprint as specified in the :fingerprint :ssl option.
match the certificates.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'library/blur/network/connection.rb', line 60 def ssl_verify_peer peer_cert ssl_cert_file = @network.[:ssl_cert_file] peer_certificate = OpenSSL::X509::Certificate.new peer_cert if ssl_cert_file unless File.readable? ssl_cert_file raise SSLValidationError, "Could not read the CA certificate file." return false end end if fingerprint_verification? fingerprint = @network.[:ssl_fingerprint].to_s peer_fingerprint = cert_sha1_fingerprint peer_certificate if fingerprint != peer_fingerprint raise SSLValidationError, "Expected fingerprint '#{fingerprint}', but got '#{peer_fingerprint}'" return false end end if certificate_verification? ca_certificate = OpenSSL::X509::Certificate.new File.read ssl_cert_file valid_signature = peer_certificate.verify ca_certificate.public_key if not valid_signature raise SSLValidationError, "Certificate verify failed" return false end end true end |
#unbind ⇒ Object
Called just as the connection is being terminated, either by remote or local.
108 109 110 111 112 113 |
# File 'library/blur/network/connection.rb', line 108 def unbind @connected = false @network.disconnected! super end |