CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/rex/proto/mqtt/client.rb
Views: 11704
1
# -*- coding: binary -*-
2
3
require 'mqtt'
4
5
##
6
# MQTT protocol support
7
##
8
9
module Rex
10
module Proto
11
module MQTT
12
class Client
13
14
def initialize(sock, opts = {})
15
@sock = sock
16
@opts = opts
17
end
18
19
def connect
20
connect_opts = {
21
client_id: @opts[:client_id]
22
}
23
24
unless @opts[:username].blank?
25
connect_opts[:username] = @opts[:username]
26
end
27
unless @opts[:password].blank?
28
connect_opts[:password] = @opts[:password]
29
end
30
connect = ::MQTT::Packet::Connect.new(connect_opts).to_s
31
@sock.put(connect)
32
res = @sock.get_once(-1, @opts[:read_timeout])
33
::MQTT::Packet.parse(res)
34
end
35
36
def connect?
37
connect.return_code.zero?
38
end
39
40
def disconnect
41
disconnect = ::MQTT::Packet::Disconnect.new().to_s
42
@sock.put(disconnect)
43
end
44
end
45
end
46
end
47
end
48
49