Module: Meshtastic::MQTT
- Defined in:
- lib/meshtastic/mqtt.rb
Class Method Summary collapse
-
.authors ⇒ Object
- Author(s)
-
0day Inc.
-
.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)’ ).
-
.decode_payload(opts = {}) ⇒ Object
- Supported Method Parameters
-
Meshtastic::MQQT.decode_payload( payload: ‘required - payload to recursively decode’, msg_type: ‘required - message type (e.g. :TEXT_MESSAGE_APP)’, gps_metadata: ‘optional - include GPS metadata in output (default: false)’, ).
-
.disconnect(opts = {}) ⇒ Object
- Supported Method Parameters
-
mqtt_obj = Meshtastic.disconnect( mqtt_obj: ‘required - mqtt_obj returned from #connect method’ ).
-
.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)’, ).
-
.help ⇒ Object
Display Usage for this Module.
-
.send_text(opts = {}) ⇒ Object
- Supported Method Parameters
-
Meshtastic.send_text( mqtt_obj: ‘required - mqtt_obj returned from #connect method’, from: ‘ required - From ID (String or Integer)’, to: ‘optional - Destination ID (Default: 0xFFFFFFFF)’, topic: ‘optional - topic to publish to (default: “msh/US/2/e/LongFast/1”)’, channel: ‘optional - channel ID (Default: 6)’, text: ‘optional - Text Message (Default: SYN)’, want_ack: ‘optional - Want Acknowledgement (Default: false)’, want_response: ‘optional - Want Response (Default: false)’, hop_limit: ‘optional - Hop Limit (Default: 3)’, on_response: ‘optional - Callback on Response’, psks: ‘optional - hash of :channel => psk key value pairs (default: { LongFast: “AQ==” })’ ).
-
.subscribe(opts = {}) ⇒ Object
- Supported Method Parameters
-
Meshtastic::MQQT.subscribe( mqtt_obj: ‘required - mqtt_obj returned from #connect method’ root_topic: ‘optional - root topic (default: msh)’, region: ‘optional - region e.g. ’US/VA’, etc (default: US)‘, channel: ’optional - channel name e.g. “2/stat/#” (default: “2/e/LongFast/#”)‘, psks: ’optional - hash of :channel => psk key value pairs (default: { LongFast: “AQ==” })‘, qos: ’optional - quality of service (default: 0)‘, filter: ’optional - comma-delimited string(s) to filter on in message (default: nil)‘, gps_metadata: ’optional - include GPS metadata in output (default: false)‘, include_raw: ’optional - include raw packet data in output (default: false)‘ ).
Class Method Details
.authors ⇒ Object
- Author(s)
-
0day Inc. <[email protected]>
405 406 407 408 409 |
# File 'lib/meshtastic/mqtt.rb', line 405 public_class_method def self. "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)'
)
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/meshtastic/mqtt.rb', line 25 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 |
.decode_payload(opts = {}) ⇒ Object
- Supported Method Parameters
-
Meshtastic::MQQT.decode_payload(
payload: 'required - payload to recursively decode', msg_type: 'required - message type (e.g. :TEXT_MESSAGE_APP)', gps_metadata: 'optional - include GPS metadata in output (default: false)',
)
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 |
# File 'lib/meshtastic/mqtt.rb', line 73 public_class_method def self.decode_payload(opts = {}) payload = opts[:payload] msg_type = opts[:msg_type] = opts[:gps_metadata] case msg_type when :ADMIN_APP decoder = Meshtastic::AdminMessage when :ATAK_FORWARDER, :ATAK_PLUGIN decoder = Meshtastic::TAKPacket # when :AUDIO_APP # decoder = Meshtastic::Audio when :DETECTION_SENSOR_APP decoder = Meshtastic::DeviceState # when :IP_TUNNEL_APP # decoder = Meshtastic::IpTunnel when :MAP_REPORT_APP decoder = Meshtastic::MapReport # when :MAX # decoder = Meshtastic::Max when :NEIGHBORINFO_APP decoder = Meshtastic::NeighborInfo when :NODEINFO_APP decoder = Meshtastic::User when :PAXCOUNTER_APP decoder = Meshtastic::Paxcount when :POSITION_APP decoder = Meshtastic::Position # when :PRIVATE_APP # decoder = Meshtastic::Private when :RANGE_TEST_APP # Unsure if this is the correct protobuf object decoder = Meshtastic::FromRadio when :REMOTE_HARDWARE_APP decoder = Meshtastic::HardwareMessage # when :REPLY_APP # decoder = Meshtastic::Reply when :ROUTING_APP decoder = Meshtastic::Routing when :SERIAL_APP decoder = Meshtastic::SerialConnectionStatus when :SIMULATOR_APP decoder = Meshtastic::Compressed when :STORE_FORWARD_APP decoder = Meshtastic::StoreAndForward when :TEXT_MESSAGE_APP, :UNKNOWN_APP decoder = Meshtastic::Data when :TELEMETRY_APP decoder = Meshtastic::Telemetry when :TRACEROUTE_APP decoder = Meshtastic::RouteDiscovery when :WAYPOINT_APP decoder = Meshtastic::Waypoint # when :ZPS_APP # decoder = Meshtastic::Zps else puts "WARNING: Can't decode\n#{payload.inspect}\nw/ portnum: #{msg_type}" return payload end payload = decoder.decode(payload).to_h if payload.keys.include?(:latitude_i) lat = payload[:latitude_i] * 0.0000001 payload[:latitude] = lat end if payload.keys.include?(:longitude_i) lon = payload[:longitude_i] * 0.0000001 payload[:longitude] = lon end if payload.keys.include?(:macaddr) mac_raw = payload[:macaddr] mac_hex_arr = mac_raw.bytes.map { |byte| byte.to_s(16).rjust(2, '0') } mac_hex_str = mac_hex_arr.join(':') payload[:macaddr] = mac_hex_str end if payload.keys.include?(:time) time_int = payload[:time] if time_int.is_a?(Integer) time_utc = Time.at(time_int).utc.to_s payload[:time_utc] = time_utc end end if && payload[:latitude] && payload[:longitude] lat = payload[:latitude] lon = payload[:longitude] unless lat.zero? && lon.zero? gps_search_resp = gps_search(lat: lat, lon: lon) payload[:gps_metadata] = gps_search_resp end end payload rescue Encoding::CompatibilityError, Google::Protobuf::ParseError payload 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'
)
394 395 396 397 398 399 400 401 |
# File 'lib/meshtastic/mqtt.rb', line 394 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)',
)
377 378 379 380 381 382 383 384 385 386 387 388 |
# File 'lib/meshtastic/mqtt.rb', line 377 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).first.data rescue StandardError => e raise e end |
.help ⇒ Object
Display Usage for this Module
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 |
# File 'lib/meshtastic/mqtt.rb', line 413 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', root_topic: 'optional - root topic (default: msh)', region: 'optional - region e.g. 'US/VA', etc (default: US)', channel: 'optional - channel name e.g. '2/stat/#' (default: '2/e/LongFast/#')', psks: 'optional - hash of :channel => psk key value pairs (default: { LongFast: 'AQ==' })', qos: 'optional - quality of service (default: 0)', json: 'optional - JSON output (default: false)', filter: 'optional - comma-delimited string(s) to filter on in message (default: nil)', gps_metadata: 'optional - include GPS metadata in output (default: false)' ) #{self}.gps_search( lat: 'required - latitude float (e.g. 37.7749)', lon: 'required - longitude float (e.g. -122.4194)', ) #{self}.send_text( mqtt_obj: 'required - mqtt_obj returned from #connect method', from: ' required - From ID (String or Integer)', to: 'optional - Destination ID (Default: 0xFFFFFFFF)', topic: 'optional - topic to publish to (default: 'msh/US/2/e/LongFast/1')', channel: 'optional - channel ID (Default: 6)', text: 'optional - Text Message (Default: SYN)', want_ack: 'optional - Want Acknowledgement (Default: false)', want_response: 'optional - Want Response (Default: false)', hop_limit: 'optional - Hop Limit (Default: 3)', on_response: 'optional - Callback on Response', psks: 'optional - hash of :channel => psk key value pairs (default: { LongFast: 'AQ==' })' ) mqtt_obj = #{self}.disconnect( mqtt_obj: 'required - mqtt_obj object returned from #connect method' ) #{self}.authors " end |
.send_text(opts = {}) ⇒ Object
- Supported Method Parameters
-
Meshtastic.send_text(
mqtt_obj: 'required - mqtt_obj returned from #connect method', from: ' required - From ID (String or Integer)', to: 'optional - Destination ID (Default: 0xFFFFFFFF)', topic: 'optional - topic to publish to (default: "msh/US/2/e/LongFast/1")', channel: 'optional - channel ID (Default: 6)', text: 'optional - Text Message (Default: SYN)', want_ack: 'optional - Want Acknowledgement (Default: false)', want_response: 'optional - Want Response (Default: false)', hop_limit: 'optional - Hop Limit (Default: 3)', on_response: 'optional - Callback on Response', psks: 'optional - hash of :channel => psk key value pairs (default: { LongFast: "AQ==" })'
)
360 361 362 363 364 365 366 367 368 369 370 |
# File 'lib/meshtastic/mqtt.rb', line 360 public_class_method def self.send_text(opts = {}) mqtt_obj = opts[:mqtt_obj] topic = opts[:topic] ||= 'msh/US/2/e/LongFast/#' opts[:via] = :mqtt protobuf_text = Meshtastic.send_text(opts) mqtt_obj.publish(topic, protobuf_text) rescue StandardError => e raise e end |
.subscribe(opts = {}) ⇒ Object
- Supported Method Parameters
-
Meshtastic::MQQT.subscribe(
mqtt_obj: 'required - mqtt_obj returned from #connect method' root_topic: 'optional - root topic (default: msh)', region: 'optional - region e.g. 'US/VA', etc (default: US)', channel: 'optional - channel name e.g. "2/stat/#" (default: "2/e/LongFast/#")', psks: 'optional - hash of :channel => psk key value pairs (default: { LongFast: "AQ==" })', qos: 'optional - quality of service (default: 0)', filter: 'optional - comma-delimited string(s) to filter on in message (default: nil)', gps_metadata: 'optional - include GPS metadata in output (default: false)', include_raw: 'optional - include raw packet data in output (default: false)'
)
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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'lib/meshtastic/mqtt.rb', line 190 public_class_method def self.subscribe(opts = {}) mqtt_obj = opts[:mqtt_obj] root_topic = opts[:root_topic] ||= 'msh' region = opts[:region] ||= 'US' channel = opts[:channel] ||= '2/e/LongFast/#' # TODO: Support Array of PSKs and attempt each until decrypted public_psk = '1PG7OiApB1nwvP+rz05pAQ==' psks = opts[:psks] ||= { LongFast: public_psk } raise 'ERROR: psks parameter must be a hash of :channel => psk key value pairs' unless psks.is_a?(Hash) psks[:LongFast] = public_psk if psks[:LongFast] == 'AQ==' psks = get_cipher_keys(psks: psks) qos = opts[:qos] ||= 0 json = opts[:json] ||= false filter = opts[:filter] = opts[:gps_metadata] ||= false include_raw = opts[:include_raw] ||= false # NOTE: Use MQTT Explorer for topic discovery full_topic = "#{root_topic}/#{region}/#{channel}" full_topic = "#{root_topic}/#{region}" if region == '#' puts "Subscribing to: #{full_topic}" mqtt_obj.subscribe(full_topic, qos) filter_arr = filter.to_s.split(',').map(&:strip) mqtt_obj.get_packet do |packet_bytes| raw_packet = packet_bytes.to_s if include_raw raw_topic = packet_bytes.topic ||= '' raw_payload = packet_bytes.payload ||= '' begin disp = false decoded_payload_hash = {} = {} = '' if json decoded_payload_hash = JSON.parse(raw_payload, symbolize_names: true) else decoded_payload = Meshtastic::ServiceEnvelope.decode(raw_payload) decoded_payload_hash = decoded_payload.to_h end next unless decoded_payload_hash[:packet].is_a?(Hash) = decoded_payload_hash[:packet] if decoded_payload_hash.keys.include?(:packet) [:topic] = raw_topic [:node_id_from] = "!#{[:from].to_i.to_s(16)}" [:node_id_to] = "!#{[:to].to_i.to_s(16)}" if .keys.include?(:rx_time) rx_time_int = [:rx_time] if rx_time_int.is_a?(Integer) rx_time_utc = Time.at(rx_time_int).utc.to_s [:rx_time_utc] = rx_time_utc end end # If encrypted_message is not nil, then decrypt # the message prior to decoding. = [:encrypted] if .to_s.length.positive? && [:topic] packet_id = [:id] packet_from = [:from] 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}" psk = psks[:LongFast] target_channel = [:topic].split('/')[-2].to_sym psk = psks[target_channel] if psks.keys.include?(target_channel) dec_psk = Base64.strict_decode64(psk) cipher = OpenSSL::Cipher.new('AES-128-CTR') cipher = OpenSSL::Cipher.new('AES-256-CTR') if dec_psk.length == 32 cipher.decrypt cipher.key = dec_psk cipher.iv = nonce decrypted = cipher.update() + cipher.final [:decoded] = Meshtastic::Data.decode(decrypted).to_h [:encrypted] = :decrypted end if [:decoded] # payload = Meshtastic::Data.decode(message[:decoded][:payload]).to_h payload = [:decoded][:payload] msg_type = [:decoded][:portnum] [:decoded][:payload] = decode_payload( payload: payload, msg_type: msg_type, gps_metadata: ) end [:raw_packet] = raw_packet if include_raw decoded_payload_hash[:packet] = unless block_given? [:stdout] = 'pretty' = JSON.pretty_generate(decoded_payload_hash) end rescue Encoding::CompatibilityError, Google::Protobuf::ParseError, JSON::GeneratorError, ArgumentError => e unless e.is_a?(Encoding::CompatibilityError) [:decrypted] = e. if e..include?('key must be') [:decrypted] = 'unable to decrypt - psk?' if e..include?('occurred during parsing') decoded_payload_hash[:packet] = unless block_given? puts "WARNING: #{e.inspect} - MSG IS >>>" # puts e.backtrace [:stdout] = 'inspect' = decoded_payload_hash.inspect end end next ensure filter_arr = [[:id].to_s] if filter.nil? if .is_a?(Hash) = .values.join(' ') disp = true if filter_arr.first == [:id] || filter_arr.all? { |filter| .include?(filter) } if disp if block_given? yield decoded_payload_hash else puts "\n" puts '-' * 80 puts 'MSG:' puts puts '-' * 80 puts "\n\n\n" end # else # print '.' end end end end rescue Interrupt puts "\nCTRL+C detected. Exiting..." rescue StandardError => e raise e ensure mqtt_obj.disconnect if mqtt_obj end |