Class: LiveF1::Source::Session

Inherits:
Struct
  • Object
show all
Defined in:
lib/live_f1/source/session.rb

Overview

A source’s Session object holds information about the current state of the data stream, and can use that to decrypt the encrypted packets which are retrieved this way.

Decryption

Decrypting a string of bytes from the timing stream relies on knowing two pieces of information, a decryption key which is specific to the session in progress, and a decryption salt which starts at a known value but is mutated with every byte that is decrypted.

The decryption key can only be obtained from the live timing servers with a valid formula1.com Live Timing account, as described in LiveF1::Source::Live

Constant Summary collapse

INITIAL_DECRYPTION_SALT =
0x55555555

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number, event_type, decryption_key) ⇒ Session

Returns a new instance of Session.



22
23
24
25
# File 'lib/live_f1/source/session.rb', line 22

def initialize number, event_type, decryption_key
  super
  reset_decryption_salt!
end

Instance Attribute Details

#decryption_keyObject

Returns the value of attribute decryption_key

Returns:

  • (Object)

    the current value of decryption_key



17
18
19
# File 'lib/live_f1/source/session.rb', line 17

def decryption_key
  @decryption_key
end

#decryption_saltObject

Returns the value of attribute decryption_salt.



20
21
22
# File 'lib/live_f1/source/session.rb', line 20

def decryption_salt
  @decryption_salt
end

#event_typeObject

Returns the value of attribute event_type

Returns:

  • (Object)

    the current value of event_type



17
18
19
# File 'lib/live_f1/source/session.rb', line 17

def event_type
  @event_type
end

#numberObject

Returns the value of attribute number

Returns:

  • (Object)

    the current value of number



17
18
19
# File 'lib/live_f1/source/session.rb', line 17

def number
  @number
end

Instance Method Details

#decrypt(input) ⇒ Object

Decrypts the given string using this session’s decryption_key and the current state of the decryption_salt.



29
30
31
32
33
34
35
36
37
# File 'lib/live_f1/source/session.rb', line 29

def decrypt input
  # Sometimes we don't have a decryption key, e.g. Notice is a
  # decryptable packet but sometimes appears between sessions
  return input unless decryption_key
  input.bytes.map do |b|
    self.decryption_salt = (decryption_salt >> 1) ^ ((decryption_salt & 0x01).zero? ? 0 : decryption_key)
    b ^ (decryption_salt & 0xff)
  end.pack("c*")
end

#reset_decryption_salt!Object



39
40
41
# File 'lib/live_f1/source/session.rb', line 39

def reset_decryption_salt!
  self.decryption_salt = INITIAL_DECRYPTION_SALT
end