Module: Meshtastic::MQTT

Defined in:
lib/meshtastic/mqtt.rb

Class Method Summary collapse

Class Method Details

.authorsObject

Author(s)

0day Inc. <[email protected]>



252
253
254
255
256
# File 'lib/meshtastic/mqtt.rb', line 252

public_class_method def self.authors
  "AUTHOR(S):
    0day Inc. <[email protected]>
  "
end

.connect(opts = {}) ⇒ Object

Supported Method Parameters

mqtt_obj = Meshtastic::MQQT.connect(

host: 'optional - mqtt host (default: mqtt.meshtastic.org)',
port: 'optional - mqtt port (defaults: 1883)',
username: 'optional - mqtt username (default: meshdev)',
password: 'optional - (default: large4cats)'

)



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/meshtastic/mqtt.rb', line 24

public_class_method def self.connect(opts = {})
  # Publicly available MQTT server / credentials by default
  host = opts[:host] ||= 'mqtt.meshtastic.org'
  port = opts[:port] ||= 1883
  username = opts[:username] ||= 'meshdev'
  password = opts[:password] ||= 'large4cats'

  mqtt_obj = MQTTClient.connect(
    host: host,
    port: port,
    username: username,
    password: password
  )

  mqtt_obj.client_id = SecureRandom.random_bytes(8).unpack1('H*')

  mqtt_obj
rescue StandardError => e
  raise e
end

.disconnect(opts = {}) ⇒ Object

Supported Method Parameters

mqtt_obj = Meshtastic.disconnect(

mqtt_obj: 'required - mqtt_obj returned from #connect method'

)



241
242
243
244
245
246
247
248
# File 'lib/meshtastic/mqtt.rb', line 241

public_class_method def self.disconnect(opts = {})
  mqtt_obj = opts[:mqtt_obj]

  mqtt_obj.disconnect if mqtt_obj
  nil
rescue StandardError => e
  raise e
end

.gps_search(opts = {}) ⇒ Object

Supported Method Parameters

mqtt_obj = Meshtastic.gps_search(

lat: 'required - latitude float (e.g. 37.7749)',
lon: 'required - longitude float (e.g. -122.4194)',

)



224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/meshtastic/mqtt.rb', line 224

public_class_method def self.gps_search(opts = {})
  lat = opts[:lat]
  lon = opts[:lon]

  raise 'ERROR: Latitude and Longitude are required' unless lat && lon

  gps_arr = [lat.to_f, lon.to_f]

  Geocoder.search(gps_arr)
rescue StandardError => e
  raise e
end

.helpObject

Display Usage for this Module



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/meshtastic/mqtt.rb', line 260

public_class_method def self.help
  puts "USAGE:
    mqtt_obj = #{self}.connect(
      host: 'optional - mqtt host (default: mqtt.meshtastic.org)',
      port: 'optional - mqtt port (defaults: 1883)',
      username: 'optional - mqtt username (default: meshdev)',
      password: 'optional - (default: large4cats)'
    )

    #{self}.subscribe(
      mqtt_obj: 'required - mqtt_obj object returned from #connect method',
      region: 'optional - region (default: US)',
      channel: 'optional - channel name (default: LongFast)',
      psk: 'optional - channel pre-shared key (default: AQ==)',
      qos: 'optional - quality of service (default: 0)'
    )

    mqtt_obj = #{self}.disconnect(
      mqtt_obj: 'required - mqtt_obj object returned from #connect method'
    )

    #{self}.authors
  "
end

.subscribe(opts = {}) ⇒ Object

Supported Method Parameters

Meshtastic::MQQT.subscribe(

mqtt_obj: 'required - mqtt_obj returned from #connect method'
region: 'optional - region (default: US)',
channel: 'optional - channel name (default: LongFast)',
psk: 'optional - channel pre-shared key (default: AQ==)',
qos: 'optional - quality of service (default: 0)',
json: 'optional - JSON output (default: false)'

)



55
56
57
58
59
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/meshtastic/mqtt.rb', line 55

public_class_method def self.subscribe(opts = {})
  mqtt_obj = opts[:mqtt_obj]
  region = opts[:region] ||= 'US'
  channel = opts[:channel] ||= 'LongFast'
  psk = opts[:psk] ||= 'AQ=='
  qos = opts[:qos] ||= 0
  json = opts[:json] ||= false

  # TODO: Find JSON URI for this
  root_topic = "msh/#{region}/2/json" if json
  # root_topic = "msh/#{region}/2/e" unless json
  root_topic = "msh/#{region}/2/c" unless json
  mqtt_obj.subscribe("#{root_topic}/#{channel}/#", qos)

  # Decrypt the message
  # Our AES key is 128 or 256 bits, shared as part of the 'Channel' specification.

  # Actual pre-shared key for LongFast channel
  psk = '1PG7OiApB1nwvP+rz05pAQ==' if channel == 'LongFast'
  padded_psk = psk.ljust(psk.length + ((4 - (psk.length % 4)) % 4), '=')
  replaced_psk = padded_psk.gsub('-', '+').gsub('_', '/')
  psk = replaced_psk
  dec_psk = Base64.strict_decode64(psk)

  # cipher = OpenSSL::Cipher.new('AES-256-CTR')
  cipher = OpenSSL::Cipher.new('AES-128-CTR')
  mqtt_obj.get_packet do |packet_bytes|
    # puts "Packet Bytes: #{packet_bytes.public_methods}"
    raw_packet = packet_bytes.to_s.b
    raw_packet_len = raw_packet.to_s.b.length
    raw_topic = packet_bytes.topic
    raw_payload = packet_bytes.payload
    raw_payload_len = raw_payload.length

    begin
      puts '-' * 80

      payload = {}
      if json
        json_payload = JSON.parse(raw_payload, symbolize_names: true)
        payload = Meshtastic::ServiceEnvelope.json_decode(json_payload)
        map_report = Meshtastic::MapReport.json_decode(json_payload)
      else
        svc_envl= Meshtastic::ServiceEnvelope.decode(raw_payload)
        payload = svc_envl.to_h[:packet]
        # puts "STILL GOOD: #{payload.inspect}"
        # puts "Public Methods: #{Meshtastic::MapReport.public_methods}"
        # puts "Public Methods: #{Meshtastic::MapReport.class}"
        # map_report_decode = Meshtastic::MapReport.decode(raw_payload)
        # map_report = map_report_decode.to_h
        # puts "STILL GOOD: #{map_report.inspect}"
      end

      puts "*** MESSAGE ***"
      packet_from = payload[:from]
      puts "Packet From: #{packet_from}"
      packet_to = payload[:to]
      puts "Packet To: #{packet_to}"
      channel = payload[:channel]
      puts "Channel: #{channel}"
      packet_id = payload[:id]
      puts "Packet ID: #{packet_id}"
      puts "\nTopic: #{raw_topic}"

      decoded_payload = payload[:decoded]
      if decoded_payload
        port_num = decoded_payload[:portnum]
        puts "Port Number: #{port_num}"
        decp = decoded_payload[:payload].b
        puts "Decoded Payload: #{decp.inspect}"
        want_response = decoded_payload[:want_response]
        puts "Want Response: #{want_response}"
        dest = decoded_payload[:dest]
        puts "Destination: #{dest}"
        source = decoded_payload[:source]
        puts "Source: #{source}"
        request_id = decoded_payload[:request_id]
        puts "Request ID: #{request_id}"
        reply_id = decoded_payload[:reply_id]
        puts "Reply ID: #{reply_id}"
        emoji = decoded_payload[:emoji]
        puts "Emoji: #{emoji}"
      end

      encrypted_payload = payload[:encrypted]
      # If encrypted_payload is not nil, then decrypt the message
      if encrypted_payload.length.positive?
        nonce_packet_id = [packet_id].pack('V').ljust(8, "\x00")
        nonce_from_node = [packet_from].pack('V').ljust(8, "\x00")
        nonce = "#{nonce_packet_id}#{nonce_from_node}".b
        puts "Nonce: #{nonce.inspect} | Length: #{nonce.length}"

        # Decrypt the message
        # Key must be 32 bytes
        # IV mustr be 16 bytes
        cipher.decrypt
        cipher.key = dec_psk
        cipher.iv = nonce
        puts "\nEncrypted Payload:\n#{encrypted_payload.inspect}"
        puts "Length: #{encrypted_payload.length}" if encrypted_payload

        decrypted = cipher.update(encrypted_payload) + cipher.final
        puts "\nDecrypted Payload:\n#{decrypted.inspect}"
        puts "Length: #{decrypted.length}" if decrypted
      end
      puts '*' * 20

      # map_long_name = map_report[:long_name].b
      # puts "\n*** MAP STATS ***"
      # puts "Map Long Name: #{map_long_name.inspect}"
      # map_short_name = map_report[:short_name]
      # puts "Map Short Name: #{map_short_name}"
      # role = map_report[:role]
      # puts "Role: #{role}"
      # hw_model = map_report[:hw_model]
      # puts "Hardware Model: #{hw_model}"
      # firmware_version = map_report[:firmware_version]
      # puts "Firmware Version: #{firmware_version}"
      # region = map_report[:region]
      # puts "Region: #{region}"
      # modem_preset = map_report[:modem_preset]
      # puts "Modem Preset: #{modem_preset}"
      # has_default_channel = map_report[:has_default_channel]
      # puts "Has Default Channel: #{has_default_channel}"
      # latitiude_i = map_report[:latitude_i]
      # puts "Latitude: #{latitiude_i}"
      # longitude_i = map_report[:longitude_i]
      # puts "Longitude: #{longitude_i}"
      # altitude = map_report[:altitude]
      # puts "Altitude: #{altitude}"
      # position_precision = map_report[:position_precision]
      # puts "Position Precision: #{position_precision}"
      # num_online_local_nodes = map_report[:num_online_local_nodes]
      # puts "Number of Online Local Nodes: #{num_online_local_nodes}"
      # puts '*' * 20

      puts "\n*** PACKET DEBUGGING ***"
      puts "Payload: #{payload.inspect}"
      # puts "\nMap Report: #{map_report.inspect}"
      puts "\nRaw Packet: #{raw_packet.inspect}"
      puts "Length: #{raw_packet_len}"
      puts '*' * 20
    rescue Google::Protobuf::ParseError => e
      puts "ERROR: #{e.inspect}"
      puts "\n*** PACKET DEBUGGING ***"
      puts "Payload: #{payload.inspect}"
      # puts "\nMap Report: #{map_report.inspect}"
      puts "\nRaw Packet: #{raw_packet.inspect}"
      puts "Length: #{raw_packet_len}"
      puts '*' * 20
      next
    ensure
      puts '-' * 80
      puts "\n\n\n"
    end
  end
rescue Interrupt
  puts "\nCTRL+C detected. Exiting..."
rescue StandardError => e
  raise e
ensure
  mqtt_obj.disconnect if mqtt_obj
end