Class: Blur::Network::Connection

Inherits:
EM::Protocols::LineAndTextProtocol
  • Object
show all
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.

See Also:

  • EventMachine::Protocols::LineAndTextProtocol
  • EventMachine::Connection

Constant Summary collapse

SSLValidationError =
Class.new StandardError

Instance Method Summary collapse

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_completedObject

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.

Returns:

  • (Boolean)


17
# File 'library/blur/network/connection.rb', line 17

def established?; @connected == true end

#post_initObject

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.options[: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
  message = IRCParser::Message.parse line

  @network.got_message message
end

#ssl_handshake_completedObject

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

Note:

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.

Raises:



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.options[: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.options[: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

#unbindObject

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