3ba88e5ac465bc201d60f35932a82ca4fc7e82fb
[project/luci.git] / applications / luci-app-mosquitto / luasrc / model / cbi / mosquitto.lua
1 --[[
2 LuCI model for mosquitto MQTT broker configuration management
3 Copyright eTactica ehf, 2018
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11 ]]--
12
13 local datatypes = require("luci.cbi.datatypes")
14 local _ = luci.i18n.translate
15
16 --- Like a Flag, but with an option to remove/set to default.
17 local function OptionalFlag(section, key, title, description)
18 local o = section:option(ListValue, key, title, description)
19 o.optional = true
20 o:value("", "Default")
21 o:value("1", "Enabled")
22 o:value("0", "Disabled")
23 return o
24 end
25
26 m = Map("mosquitto", _("Mosquitto MQTT Broker"),
27 _([[mosquitto - the <a href='http://www.mosquitto.org'>blood thirsty</a>
28 MQTT messaging broker. Note, only some of the available configuration files
29 are supported at this stage, use the checkbox below to use config generated
30 by this page, or the stock mosquitto configuration file in
31 /etc/mosquitto/mosquitto.conf]]))
32
33 s = m:section(TypedSection, "owrt", "OpenWRT")
34 s.anonymous = true
35 p = s:option(Flag, "use_uci", _("Use this LuCI configuration page"),
36 _([[If checked, mosquitto runs with a config generated
37 from this page. (Or from UCI directly) If unchecked, mosquitto
38 runs with the config in /etc/mosquitto/mosquitto.conf
39 (and this page is ignored)]]))
40
41 s = m:section(TypedSection, "mosquitto", "Mosquitto")
42 s.anonymous = true
43
44 p = s:option(MultiValue, "log_dest", _("Log destination"),
45 _("You can have multiple, but 'none' will override all others"))
46 p:value("stderr", "stderr")
47 p:value("stdout", "stdout")
48 p:value("syslog", "syslog")
49 p:value("topic", "$SYS/broker/log/[severity]")
50 p:value("none", "none")
51
52 OptionalFlag(s, "no_remote_access", _("Disallow remote access to this broker"),
53 _([[Outbound bridges will still work, but this will make the primary listener
54 only available from localhost]]))
55
56 local o
57 o = s:option(Value, "sys_interval", _("Time in seconds between updates of the $SYS tree"), _("Set to zero to disable"))
58 o.datatype = "uinteger"
59 o.optional = true
60
61 o = s:option(Value, "max_inflight_messages", _("Max Inflight Messages"), _("Limit for message allowed inflight"))
62 o.datatype = "uinteger"
63 o.optional = true
64 o = s:option(Value, "max_queued_messages", _("Max Queued Messages"), _("Limit for message queue when offline"))
65 o.datatype = "uinteger"
66 o.optional = true
67 o = s:option(Value, "max_queued_bytes", _("Max Queued bytes"), _("Limit for message queue when offline, zero to disable)"))
68 o.datatype = "uinteger"
69 o.optional = true
70
71
72 s = m:section(TypedSection, "persistence", _("Persistence"))
73 s.anonymous = true
74 s.addremove = false
75 s:option(Flag, "persistence", _("Persistence enabled"), _("Should persistence to disk be enabled at all")).rmempty = false
76 o = s:option(Value, "client_expiration", _("Client expiration"), _("Remove persistent clients if they haven't reconnected in this period, eg 6h, 3d, 2w"))
77 o.optional = true
78 o:depends("persistence", true)
79 o = OptionalFlag(s, "autosave_on_changes", _("Autosave on changes"), _("Autosave interval applies to change counts instead of time"))
80 o:depends("persistence", true)
81 o = s:option(Value, "autosave_interval", _("Autosave interval"), _("Save persistence file after this many seconds or changes"))
82 o.optional = true
83 o:depends("persistence", true)
84 o = s:option(Value, "file", _("Persistent file name"))
85 o.optional = true
86 o:depends("persistence", true)
87 o = s:option(Value, "location", _("Persistent file path (with trailing/)"), _("Path to persistent file"))
88 o.optional = true
89 o:depends("persistence", true)
90
91 s = m:section(TypedSection, "listener", _("Listeners"), _("You can configure additional listeners here"))
92 s.addremove = true
93 s.anonymous = true
94 s:option(Value, "port", _("Port")).datatype = "port"
95
96 o = s:option(ListValue, "protocol", _("Protocol to use when listening"))
97 o:value("", "Default")
98 o:value("mqtt", _("MQTT"))
99 o:value("websockets", _("WebSockets"))
100
101 s:option(Value, "http_dir", _("http_dir to serve on websockets listeners")).optional = true
102 OptionalFlag(s, "use_username_as_clientid", "use_username_as_clientid")
103 o = s:option(Value, "cafile", _("CA file path"))
104 o.optional = true
105 o.datatype = "file"
106 o = s:option(Value, "capath", _("CA path to search"))
107 o.optional = true
108 o.datatype = "directory"
109 o = s:option(Value, "certfile", _("server certificate file (PEM encoded)"))
110 o.optional = true
111 o.datatype = "file"
112 o = s:option(Value, "keyfile", _("keyfile (PEM encoded)"))
113 o.optional = true
114 o.datatype = "file"
115
116 o = s:option(ListValue, "tls_version", _("TLS Version"),
117 _("Depends on your openssl version, empty to support all"))
118 o.optional = true
119 o:value("", "Default")
120 o:value("tlsv1.1")
121 o:value("tlsv1.2")
122 o:value("tlsv1.3")
123
124 OptionalFlag(s, "require_certificate", _("Require clients to present a certificate"))
125 OptionalFlag(s, "use_identity_as_username", "use_identity_as_username")
126 s:option(Value, "crlfile", _("CRL to use if require_certificate is enabled")).optional = true
127 s:option(Value, "ciphers", _("Ciphers control. Should match 'openssl ciphers' format")).optional = true
128 s:option(Value, "psk_hint", _("PSK Hint to provide to connecting clients")).optional = true
129
130 -- we want to allow multiple bridge sections
131 s = m:section(TypedSection, "bridge", _("Bridges"),
132 _("You can configure multiple bridge connections here"))
133 s.anonymous = true
134 s.addremove = true
135
136 conn = s:option(Value, "connection", _("Connection name"),
137 _("unique name for this bridge configuration"))
138
139 local function validate_address(self, value)
140 local host, port = unpack(luci.util.split(value, ":"))
141 if (datatypes.host(host)) then
142 if port and #port then
143 if not datatypes.port(port) then
144 return nil, _("Please enter a valid port after the :")
145 end
146 end
147 return value
148 end
149 return nil, _("Please enter a hostname or an IP address")
150 end
151
152 addr = s:option(Value, "address", _("address"), _("address[:port] of remote broker"))
153 addr.datatype = "string"
154 addr.validate = validate_address
155
156 -- TODO - make the in/out/both a dropdown/radio or something....
157 topics = s:option(DynamicList, "topic", _("topic"),
158 _("full topic string for mosquitto.conf, eg: 'power/# out 2'"))
159
160 OptionalFlag(s, "cleansession", _("Clean session"))
161 OptionalFlag(s, "notifications", _("notifications"),
162 _("Attempt to notify the local and remote broker of connection status, defaults to $SYS/broker/connections/<clientid>/state"))
163 s:option(Value, "notification_topic", _("Topic to use for local+remote remote for notifications.")).optional = true
164 OptionalFlag(s, "notification_local_only", _("Notifications local only"), _("Bridge connection states should only be published locally"))
165
166 s:option(Value, "remote_clientid", _("Client id to use on remote end of this bridge connection")).optional = true
167 s:option(Value, "local_clientid", _("Client id to use locally. Important when bridging to yourself")).optional = true
168 o = s:option(Value, "keepalive_interval", _("Keepalive interval for this bridge"))
169 o.datatype = "uinteger"
170 o.optional = true
171 o = s:option(ListValue, "start_type", _("How should this bridge be started"))
172 o.optional = true
173 o:value("", "Default")
174 o:value("automatic", _("Automatic, includes restarts"))
175 o:value("lazy", _("Automatic, but stopped when not used"))
176 o:value("once", _("Automatic, but no restarts"))
177 o = s:option(Value, "restart_timeout", _("How long to wait before reconnecting"))
178 o.datatype = "uinteger"
179 o.optional = true
180 o = s:option(Value, "idle_timeout", _("How long to wait before disconnecting"))
181 o.datatype = "uinteger"
182 o.optional = true
183 o = s:option(Value, "threshold", _("How many messages to queue before restarting lazy bridge"))
184 o.datatype = "uinteger"
185 o.optional = true
186
187 OptionalFlag(s, "try_private", "try_private",
188 _("attempt to notify the remote broker that this is a bridge, not all brokers support this."))
189 s:option(Value, "remote_username", _("Remote username")).optional = true
190 o = s:option(Value, "remote_password", _("Remote password"))
191 o.optional = true
192 o.password = true
193
194 s:option(Value, "identity", _("PSK Bridge Identity"), _("Identity for TLS-PSK")).optional = true
195
196 -- no hex validation available in datatypes
197 local function validate_psk_key(self, value)
198 if (value:match("^[a-fA-F0-9]+$")) then
199 return value
200 end
201 return nil, _("Only hex numbers are allowed (use A-F characters and 0-9 digits)")
202 end
203
204 psk_key = s:option(Value, "psk", _("Bridge PSK"), _("Key for TLS-PSK"))
205 psk_key.password = true
206 psk_key.optional = true
207 psk_key.datatype = "string"
208 psk_key.validate = validate_psk_key
209
210 b_tls_version = s:option(ListValue, "tls_version", _("TLS Version"),
211 _("The remote broker must support the same version of TLS for the connection to succeed."))
212 b_tls_version:value("", "Default")
213 b_tls_version:value("tlsv1.1")
214 b_tls_version:value("tlsv1.2")
215 b_tls_version:value("tlsv1.3")
216 b_tls_version.optional = true
217
218 o = s:option(Value, "cafile", _("Path to CA file"))
219 o.optional = true
220 o.datatype = "file"
221 o = s:option(Value, "capath", _("Directory to search for CA files"))
222 o.optional = true
223 o.datatype = "directory"
224 o = s:option(Value, "certfile", _("Path to PEM encoded server certificate file"))
225 o.optional = true
226 o.datatype = "file"
227 o = s:option(Value, "keyfile", _("Path to PEM encoded keyfile"))
228 o.optional = true
229 o.datatype = "file"
230
231 return m