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)’ ).
-
.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.
-
.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 (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)’, filter: ‘optional - comma-delimited string(s) to filter on in message (default: nil)’, gps_metadata: ‘optional - include GPS metadata in output (default: false)’ ).
Class Method Details
.authors ⇒ Object
- Author(s)
-
0day Inc. <[email protected]>
296 297 298 299 300 |
# File 'lib/meshtastic/mqtt.rb', line 296 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)'
)
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'
)
285 286 287 288 289 290 291 292 |
# File 'lib/meshtastic/mqtt.rb', line 285 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)',
)
268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/meshtastic/mqtt.rb', line 268 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 |
.help ⇒ Object
Display Usage for this Module
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 |
# File 'lib/meshtastic/mqtt.rb', line 304 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 (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)', 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)', ) 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' root_topic: 'optional - root topic (default: msh)', 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)', filter: 'optional - comma-delimited string(s) to filter on in message (default: nil)', gps_metadata: 'optional - include GPS metadata in output (default: false)'
)
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 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 |
# File 'lib/meshtastic/mqtt.rb', line 58 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] ||= 'LongFast' psk = opts[:psk] ||= 'AQ==' qos = opts[:qos] ||= 0 json = opts[:json] ||= false filter = opts[:filter] = opts[:gps_metadata] ||= false # TODO: Find JSON URI for this full_topic = "#{root_topic}/#{region}/2/json/#{channel}/#" if json full_topic = "#{root_topic}/#{region}/2/c/#{channel}/#" unless json puts "Subscribing to: #{full_topic}" mqtt_obj.subscribe(full_topic, 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') filter_arr = filter.to_s.split(',').map(&:strip) mqtt_obj.get_packet do |packet_bytes| raw_packet = packet_bytes.to_s.b raw_topic = packet_bytes.topic ||= '' = packet_bytes.payload begin disp = false = {} = '' if json = JSON.parse(, symbolize_names: true) else decoded_packet = Meshtastic::ServiceEnvelope.decode() = decoded_packet.to_h[:packet] end [:topic] = raw_topic [:node_id_from] = "!#{message[:from].to_i.to_s(16)}" [:node_id_to] = "!#{message[:to].to_i.to_s(16)}" = [:encrypted] # If encrypted_message is not nil, then decrypt the message if .to_s.length.positive? 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}".b # Decrypt the message # Key must be 32 bytes # IV mustr be 16 bytes cipher.decrypt cipher.key = dec_psk cipher.iv = nonce decrypted = cipher.update() + cipher.final [:decrypted] = decrypted # Vvv Decode the decrypted message vvV end if [:decoded] payload = [:decoded][:payload] msg_type = [:decoded][:portnum] case msg_type when :ADMIN_APP pb_obj = Meshtastic::AdminMessage.decode(payload) when :ATAK_FORWARDER, :ATAK_PLUGIN pb_obj = Meshtastic::TAKPacket.decode(payload) # when :AUDIO_APP # pb_obj = Meshtastic::Audio.decode(payload) when :DETECTION_SENSOR_APP pb_obj = Meshtastic::DeviceState.decode(payload) # when :IP_TUNNEL_APP # pb_obj = Meshtastic::IpTunnel.decode(payload) when :MAP_REPORT_APP pb_obj = Meshtastic::MapReport.decode(payload) # when :MAX # pb_obj = Meshtastic::Max.decode(payload) when :NEIGHBORINFO_APP pb_obj = Meshtastic::NeighborInfo.decode(payload) when :NODEINFO_APP pb_obj = Meshtastic::User.decode(payload) when :PAXCOUNTER_APP pb_obj = Meshtastic::Paxcount.decode(payload) when :POSITION_APP pb_obj = Meshtastic::Position.decode(payload) # when :PRIVATE_APP # pb_obj = Meshtastic::Private.decode(payload) when :RANGE_TEST_APP # Unsure if this is the correct protobuf object pb_obj = Meshtastic::FromRadio.decode(payload) when :REMOTE_HARDWARE_APP pb_obj = Meshtastic::HardwareMessage.decode(payload) # when :REPLY_APP # pb_obj = Meshtastic::Reply.decode(payload) when :ROUTING_APP pb_obj = Meshtastic::Routing.decode(payload) when :SERIAL_APP pb_obj = Meshtastic::SerialConnectionStatus.decode(payload) when :SIMULATOR_APP, :TEXT_MESSAGE_COMPRESSED_APP # Unsure if this is the correct protobuf object # for TEXT_MESSAGE_COMPRESSED_APP pb_obj = Meshtastic::Compressed.decode(payload) when :STORE_FORWARD_APP pb_obj = Meshtastic::StoreAndForward.decode(payload) when :TEXT_MESSAGE_APP # Unsure if this is the correct protobuf object # pb_obj = Meshtastic::MqttClientProxyMessage.decode(payload) pb_obj = Meshtastic::Data.decode(payload) when :TELEMETRY_APP pb_obj = Meshtastic::Telemetry.decode(payload) when :TRACEROUTE_APP pb_obj = Meshtastic::RouteDiscovery.decode(payload) # when :UNKNOWN_APP # pb_obj = Meshtastic.Unknown.decode(payload) when :WAYPOINT_APP pb_obj = Meshtastic::Waypoint.decode(payload) # when :ZPS_APP # pb_obj = Meshtastic::Zps.decode(payload) else puts "WARNING: Unknown message type: #{msg_type}" end # Overwrite the payload with the decoded protobuf object # message[:decoded][:payload] = pb_obj.to_h unless msg_type == :TRACEROUTE_APP [:decoded][:payload] = pb_obj.to_h if [:decoded][:payload].keys.include?(:latitude_i) && [:decoded][:payload].keys.include?(:longitude_i) && latitude = pb_obj.to_h[:latitude_i] * 0.0000001 longitude = pb_obj.to_h[:longitude_i] * 0.0000001 [:decoded][:payload][:gps_metadata] = gps_search( lat: latitude, lon: longitude ).first.data end # If we there's a mac address, make it look like one. if [:decoded][:payload].keys.include?(:macaddr) macaddr = [:decoded][:payload][:macaddr] macaddr_fmt = macaddr.bytes.map { |byte| byte.to_s(16).rjust(2, '0') }.join(':') [:decoded][:payload][:macaddr] = macaddr_fmt end # puts pb_obj.public_methods # message[:decoded][:pb_obj] = pb_obj end [:raw_packet] = raw_packet if block_given? unless block_given? [:stdout] = 'pretty' = JSON.pretty_generate() end rescue Google::Protobuf::ParseError, JSON::GeneratorError unless block_given? [:stdout] = 'inspect' = .inspect end next ensure filter_arr = [[:id].to_s] if filter.nil? = .values.join(' ') disp = true if filter_arr.first == [:id] || filter_arr.all? { |filter| .include?(filter) } if disp if block_given? yield else puts "\n" puts '-' * 80 puts 'MSG:' puts puts '-' * 80 puts "\n\n\n" end else print '.' end end end rescue Interrupt puts "\nCTRL+C detected. Exiting..." rescue StandardError => e raise e ensure mqtt_obj.disconnect if mqtt_obj end |