smartdns.js 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  1. /*************************************************************************
  2. *
  3. * Copyright (C) 2018-2023 Ruilin Peng (Nick) <pymumu@gmail.com>.
  4. *
  5. * smartdns is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * smartdns is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. 'use strict';
  19. 'require fs';
  20. 'require uci';
  21. 'require form';
  22. 'require view';
  23. 'require poll';
  24. 'require rpc';
  25. 'require ui';
  26. var conf = 'smartdns';
  27. var callServiceList = rpc.declare({
  28. object: 'service',
  29. method: 'list',
  30. params: ['name'],
  31. expect: { '': {} }
  32. });
  33. var pollAdded = false;
  34. function getServiceStatus() {
  35. return L.resolveDefault(callServiceList(conf), {})
  36. .then(function (res) {
  37. var is_running = false;
  38. try {
  39. is_running = res[conf]['instances']['smartdns']['running'];
  40. } catch (e) { }
  41. return is_running;
  42. });
  43. }
  44. function smartdnsServiceStatus() {
  45. return Promise.all([
  46. getServiceStatus()
  47. ]);
  48. }
  49. function smartdnsRenderStatus(res) {
  50. var renderHTML = "";
  51. var isRunning = res[0];
  52. var autoSetDnsmasq = uci.get_first('smartdns', 'smartdns', 'auto_set_dnsmasq');
  53. var smartdnsPort = uci.get_first('smartdns', 'smartdns', 'port');
  54. var dnsmasqServer = uci.get_first('dhcp', 'dnsmasq', 'server');
  55. if (isRunning) {
  56. renderHTML += "<span style=\"color:green;font-weight:bold\">SmartDNS - " + _("RUNNING") + "</span>";
  57. } else {
  58. renderHTML += "<span style=\"color:red;font-weight:bold\">SmartDNS - " + _("NOT RUNNING") + "</span>";
  59. return renderHTML;
  60. }
  61. if (autoSetDnsmasq === '1' && smartdnsPort != '53') {
  62. var matchLine = "127.0.0.1#" + smartdnsPort;
  63. uci.unload('dhcp');
  64. uci.load('dhcp');
  65. if (dnsmasqServer == undefined || dnsmasqServer.indexOf(matchLine) < 0) {
  66. renderHTML += "<br /><span style=\"color:red;font-weight:bold\">" + _("Dnsmasq Forwarded To Smartdns Failure") + "</span>";
  67. }
  68. }
  69. return renderHTML;
  70. }
  71. return view.extend({
  72. load: function () {
  73. return Promise.all([
  74. uci.load('dhcp'),
  75. uci.load('smartdns'),
  76. ]);
  77. },
  78. render: function (stats) {
  79. var m, s, o;
  80. var ss, so;
  81. var servers, download_files;
  82. m = new form.Map('smartdns', _('SmartDNS'));
  83. m.title = _("SmartDNS Server");
  84. m.description = _("SmartDNS is a local high-performance DNS server, supports finding fastest IP, "
  85. + "supports ad filtering, and supports avoiding DNS poisoning.");
  86. s = m.section(form.NamedSection, '_status');
  87. s.anonymous = true;
  88. s.render = function (section_id) {
  89. var renderStatus = function () {
  90. return L.resolveDefault(smartdnsServiceStatus()).then(function (res) {
  91. var view = document.getElementById("service_status");
  92. if (view == null) {
  93. return;
  94. }
  95. view.innerHTML = smartdnsRenderStatus(res);
  96. });
  97. }
  98. if (pollAdded == false) {
  99. poll.add(renderStatus, 1);
  100. pollAdded = true;
  101. }
  102. return E('div', { class: 'cbi-section' }, [
  103. E('div', { id: 'service_status' },
  104. _('Collecting data ...'))
  105. ]);
  106. }
  107. ////////////////
  108. // Basic;
  109. ////////////////
  110. s = m.section(form.TypedSection, "smartdns", _("Settings"), _("General Settings"));
  111. s.anonymous = true;
  112. s.tab("settings", _("General Settings"));
  113. s.tab("advanced", _('Advanced Settings'));
  114. s.tab("seconddns", _("Second Server Settings"));
  115. s.tab("dns64", _("DNS64 Server Settings"));
  116. s.tab("files", _("Download Files Setting"), _("Download domain list files for domain-rule and include config files, please refresh the page after download to take effect."));
  117. s.tab("proxy", _("Proxy Server Settings"));
  118. s.tab("custom", _("Custom Settings"));
  119. ///////////////////////////////////////
  120. // Basic Settings
  121. ///////////////////////////////////////
  122. o = s.taboption("settings", form.Flag, "enabled", _("Enable"), _("Enable or disable smartdns server"));
  123. o.rmempty = false;
  124. o.default = o.disabled;
  125. // server name;
  126. o = s.taboption("settings", form.Value, "server_name", _("Server Name"), _("Smartdns server name"));
  127. o.default = "smartdns";
  128. o.datatype = "hostname";
  129. o.rempty = false;
  130. // Port;
  131. o = s.taboption("settings", form.Value, "port", _("Local Port"),
  132. _("Smartdns local server port, smartdns will be automatically set as main dns when the port is 53."));
  133. o.placeholder = 53;
  134. o.default = 53;
  135. o.datatype = "port";
  136. o.rempty = false;
  137. // auto-conf-dnsmasq;
  138. o = s.taboption("settings", form.Flag, "auto_set_dnsmasq", _("Automatically Set Dnsmasq"), _("Automatically set as upstream of dnsmasq when port changes."));
  139. o.rmempty = false;
  140. o.default = o.enabled;
  141. ///////////////////////////////////////
  142. // advanced settings;
  143. ///////////////////////////////////////
  144. // Speed check mode;
  145. o = s.taboption("advanced", form.Value, "speed_check_mode", _("Speed Check Mode"), _("Smartdns speed check mode."));
  146. o.rmempty = true;
  147. o.placeholder = "default";
  148. o.value("", _("default"));
  149. o.value("ping,tcp:80,tcp:443");
  150. o.value("ping,tcp:443,tcp:80");
  151. o.value("tcp:80,tcp:443,ping");
  152. o.value("tcp:443,tcp:80,ping");
  153. o.value("none", _("None"));
  154. o.validate = function (section_id, value) {
  155. if (value == "") {
  156. return true;
  157. }
  158. if (value == "none") {
  159. return true;
  160. }
  161. var check_mode = value.split(",")
  162. for (var i = 0; i < check_mode.length; i++) {
  163. if (check_mode[i] == "ping") {
  164. continue;
  165. }
  166. if (check_mode[i].indexOf("tcp:") == 0) {
  167. var port = check_mode[i].split(":")[1];
  168. if (port == "") {
  169. return _("TCP port is empty");
  170. }
  171. continue;
  172. }
  173. return _("Speed check mode is invalid.");
  174. }
  175. return true;
  176. }
  177. // response mode;
  178. o = s.taboption("advanced", form.ListValue, "response_mode", _("Response Mode"),
  179. _("Smartdns response mode, First Ping: return the first ping IP, Fastest IP: return the fastest IP, Fastest Response: return the fastest DNS response."));
  180. o.rmempty = true;
  181. o.placeholder = "default";
  182. o.value("", _("default"));
  183. o.value("first-ping", _("First Ping"));
  184. o.value("fastest-ip", _("Fastest IP"));
  185. o.value("fastest-response", _("Fastest Response"));
  186. // Enable TCP server;
  187. o = s.taboption("advanced", form.Flag, "tcp_server", _("TCP Server"), _("Enable TCP DNS Server"));
  188. o.rmempty = false;
  189. o.default = o.enabled;
  190. // Support IPV6;
  191. o = s.taboption("advanced", form.Flag, "ipv6_server", _("IPV6 Server"), _("Enable IPV6 DNS Server"));
  192. o.rmempty = false;
  193. o.default = o.enabled;
  194. // bind to device;
  195. o = s.taboption("advanced", form.Flag, "bind_device", _("Bind Device"), _("Listen only on the specified interfaces."));
  196. o.rmempty = false;
  197. o.default = o.enabled;
  198. // bind device name;
  199. o = s.taboption("advanced", form.Value, "bind_device_name", _("Bind Device Name"), _("Name of device name listen on."));
  200. o.placeholder = "default";
  201. o.rempty = true;
  202. o.datatype = "string";
  203. // Support DualStack ip selection;
  204. o = s.taboption("advanced", form.Flag, "dualstack_ip_selection", _("Dual-stack IP Selection"),
  205. _("Enable IP selection between IPV4 and IPV6"));
  206. o.rmempty = false;
  207. o.default = o.enabled;
  208. // Domain prefetch load ;
  209. o = s.taboption("advanced", form.Flag, "prefetch_domain", _("Domain prefetch"),
  210. _("Enable domain prefetch, accelerate domain response speed."));
  211. o.rmempty = true;
  212. o.default = o.disabled;
  213. // Domain Serve expired
  214. o = s.taboption("advanced", form.Flag, "serve_expired", _("Serve expired"),
  215. _("Attempts to serve old responses from cache with a TTL of 0 in the response without waiting for the actual resolution to finish."));
  216. o.rmempty = false;
  217. o.default = o.enabled;
  218. // cache-size;
  219. o = s.taboption("advanced", form.Value, "cache_size", _("Cache Size"), _("DNS domain result cache size"));
  220. o.rempty = true;
  221. // cache-persist;
  222. o = s.taboption("advanced", form.Flag, "cache_persist", _("Cache Persist"), _("Write cache to disk on exit and load on startup."));
  223. o.rmempty = false;
  224. o.default = o.enabled;
  225. // cache-size;
  226. o = s.taboption("advanced", form.Flag, "resolve_local_hostnames", _("Resolve Local Hostnames"), _("Resolve local hostnames by reading Dnsmasq lease file."));
  227. o.rmempty = false;
  228. o.default = o.enabled;
  229. // Force AAAA SOA
  230. o = s.taboption("advanced", form.Flag, "force_aaaa_soa", _("Force AAAA SOA"), _("Force AAAA SOA."));
  231. o.rmempty = true;
  232. o.default = o.disabled;
  233. // Force HTTPS SOA
  234. o = s.taboption("advanced", form.Flag, "force_https_soa", _("Force HTTPS SOA"), _("Force HTTPS SOA."));
  235. o.rmempty = false;
  236. o.default = o.enabled;
  237. // Ipset no speed.
  238. o = s.taboption("advanced", form.Value, "ipset_no_speed", _("No Speed IPset Name"),
  239. _("Ipset name, Add domain result to ipset when speed check fails."));
  240. o.rmempty = true;
  241. o.datatype = "string";
  242. o.rempty = true;
  243. o.validate = function (section_id, value) {
  244. if (value == "") {
  245. return true;
  246. }
  247. var ipset = value.split(",")
  248. for (var i = 0; i < ipset.length; i++) {
  249. if (!ipset[i].match(/^(#[4|6]:)?[a-zA-Z0-9\-_]+$/)) {
  250. return _("ipset name format error, format: [#[4|6]:]ipsetname");
  251. }
  252. }
  253. return true;
  254. }
  255. // NFTset no speed.
  256. o = s.taboption("advanced", form.Value, "nftset_no_speed", _("No Speed NFTset Name"),
  257. _("Nftset name, Add domain result to nftset when speed check fails, format: [#[4|6]:[family#table#set]]"));
  258. o.rmempty = true;
  259. o.datatype = "string";
  260. o.rempty = true;
  261. o.validate = function (section_id, value) {
  262. if (value == "") {
  263. return true;
  264. }
  265. var nftset = value.split(",")
  266. for (var i = 0; i < nftset.length; i++) {
  267. if (!nftset[i].match(/^#[4|6]:[a-zA-Z0-9\-_]+#[a-zA-Z0-9\-_]+#[a-zA-Z0-9\-_]+$/)) {
  268. return _("NFTset name format error, format: [#[4|6]:[family#table#set]]");
  269. }
  270. }
  271. return true;
  272. }
  273. // rr-ttl;
  274. o = s.taboption("advanced", form.Value, "rr_ttl", _("Domain TTL"), _("TTL for all domain result."));
  275. o.rempty = true;
  276. // rr-ttl-min;
  277. o = s.taboption("advanced", form.Value, "rr_ttl_min", _("Domain TTL Min"),
  278. _("Minimum TTL for all domain result."));
  279. o.rempty = true;
  280. o.placeholder = "600";
  281. o.default = 600;
  282. o.optional = true;
  283. // rr-ttl-max;
  284. o = s.taboption("advanced", form.Value, "rr_ttl_max", _("Domain TTL Max"),
  285. _("Maximum TTL for all domain result."));
  286. o.rempty = true;
  287. // rr-ttl-reply-max;
  288. o = s.taboption("advanced", form.Value, "rr_ttl_reply_max", _("Reply Domain TTL Max"),
  289. _("Reply maximum TTL for all domain result."));
  290. o.rempty = true;
  291. // other args
  292. o = s.taboption("advanced", form.Value, "server_flags", _("Additional Server Args"),
  293. _("Additional server args, refer to the help description of the bind option."))
  294. o.default = ""
  295. o.rempty = true
  296. // include config
  297. download_files = uci.sections('smartdns', 'download-file');
  298. o = s.taboption("advanced", form.DynamicList, "conf_files", _("Include Config Files<br>/etc/smartdns/conf.d"),
  299. _("Include other config files from /etc/smartdns/conf.d or custom path, can be downloaded from the download page."));
  300. for (var i = 0; i < download_files.length; i++) {
  301. if (download_files[i].type == undefined) {
  302. continue;
  303. }
  304. if (download_files[i].type != 'config') {
  305. continue
  306. }
  307. o.value(download_files[i].name);
  308. }
  309. ///////////////////////////////////////
  310. // second dns server;
  311. ///////////////////////////////////////
  312. // Enable;
  313. o = s.taboption("seconddns", form.Flag, "seconddns_enabled", _("Enable"),
  314. _("Enable or disable second DNS server."));
  315. o.default = o.disabled;
  316. o.rempty = true;
  317. // Port;
  318. o = s.taboption("seconddns", form.Value, "seconddns_port", _("Local Port"), _("Smartdns local server port"));
  319. o.placeholder = 6553;
  320. o.default = 6553;
  321. o.datatype = "port";
  322. o.rempty = false;
  323. // Enable TCP server;
  324. o = s.taboption("seconddns", form.Flag, "seconddns_tcp_server", _("TCP Server"), _("Enable TCP DNS Server"));
  325. o.rmempty = false;
  326. o.default = o.enabled;
  327. // dns server group;
  328. o = s.taboption("seconddns", form.Value, "seconddns_server_group", _("Server Group"),
  329. _("Query DNS through specific dns server group, such as office, home."));
  330. o.rmempty = true;
  331. o.placeholder = "default";
  332. o.datatype = "hostname";
  333. o.rempty = true;
  334. o = s.taboption("seconddns", form.Flag, "seconddns_no_speed_check", _("Skip Speed Check"),
  335. _("Do not check speed."));
  336. o.rmempty = true;
  337. o.default = o.disabled;
  338. // skip address rules;
  339. o = s.taboption("seconddns", form.Flag, "seconddns_no_rule_addr", _("Skip Address Rules"),
  340. _("Skip address rules."));
  341. o.rmempty = true;
  342. o.default = o.disabled;
  343. // skip name server rules;
  344. o = s.taboption("seconddns", form.Flag, "seconddns_no_rule_nameserver", _("Skip Nameserver Rule"),
  345. _("Skip nameserver rules."));
  346. o.rmempty = true;
  347. o.default = o.disabled;
  348. // skip ipset rules;
  349. o = s.taboption("seconddns", form.Flag, "seconddns_no_rule_ipset", _("Skip Ipset Rule"),
  350. _("Skip ipset rules."));
  351. o.rmempty = true;
  352. o.default = o.disabled;
  353. // skip soa address rule;
  354. o = s.taboption("seconddns", form.Flag, "seconddns_no_rule_soa", _("Skip SOA Address Rule"),
  355. _("Skip SOA address rules."));
  356. o.rmempty = true;
  357. o.default = o.disabled;
  358. o = s.taboption("seconddns", form.Flag, "seconddns_no_dualstack_selection", _("Skip Dualstack Selection"),
  359. _("Skip Dualstack Selection."));
  360. o.rmempty = true;
  361. o.default = o.disabled;
  362. // skip cache;
  363. o = s.taboption("seconddns", form.Flag, "seconddns_no_cache", _("Skip Cache"), _("Skip Cache."));
  364. o.rmempty = true;
  365. o.default = o.disabled;
  366. // Force AAAA SOA
  367. o = s.taboption("seconddns", form.Flag, "seconddns_force_aaaa_soa", _("Force AAAA SOA"), _("Force AAAA SOA."));
  368. o.rmempty = true;
  369. o.default = o.disabled;
  370. o = s.taboption("seconddns", form.Value, "seconddns_ipset_name", _("IPset Name"), _("IPset name."));
  371. o.rmempty = true;
  372. o.datatype = "string";
  373. o.rempty = true;
  374. o.validate = function (section_id, value) {
  375. if (value == "") {
  376. return true;
  377. }
  378. var ipset = value.split(",")
  379. for (var i = 0; i < ipset.length; i++) {
  380. if (!ipset[i].match(/^(#[4|6]:)?[a-zA-Z0-9\-_]+$/)) {
  381. return _("ipset name format error, format: [#[4|6]:]ipsetname");
  382. }
  383. }
  384. return true;
  385. }
  386. o = s.taboption("seconddns", form.Value, "seconddns_nftset_name", _("NFTset Name"), _("NFTset name, format: [#[4|6]:[family#table#set]]"));
  387. o.rmempty = true;
  388. o.datatype = "string";
  389. o.rempty = true;
  390. o.validate = function (section_id, value) {
  391. if (value == "") {
  392. return true;
  393. }
  394. var nftset = value.split(",")
  395. for (var i = 0; i < nftset.length; i++) {
  396. if (!nftset[i].match(/^#[4|6]:[a-zA-Z0-9\-_]+#[a-zA-Z0-9\-_]+#[a-zA-Z0-9\-_]+$/)) {
  397. return _("NFTset name format error, format: [#[4|6]:[family#table#set]]");
  398. }
  399. }
  400. return true;
  401. }
  402. // other args
  403. o = s.taboption("seconddns", form.Value, "seconddns_server_flags", _("Additional Server Args"),
  404. _("Additional server args, refer to the help description of the bind option."))
  405. o.default = ""
  406. o.rempty = true
  407. ///////////////////////////////////////
  408. // DNS64 Settings
  409. ///////////////////////////////////////
  410. o = s.taboption("dns64", form.Value, "dns64", _("DNS64"));
  411. o.placeholder = "64:ff9b::/96";
  412. o.datatype = "ip6addr";
  413. o.rempty = true;
  414. ///////////////////////////////////////
  415. // download Files Settings
  416. ///////////////////////////////////////
  417. o = s.taboption("files", form.Flag, "enable_auto_update", _("Enable Auto Update"), _("Enable daily auto update."));
  418. o.rmempty = true;
  419. o.default = o.disabled;
  420. o.rempty = true;
  421. o = s.taboption("files", form.FileUpload, "upload_conf_file", _("Upload Config File"),
  422. _("Upload smartdns config file to /etc/smartdns/conf.d"));
  423. o.rmempty = true
  424. o.datatype = "file"
  425. o.rempty = true
  426. o.root_directory = "/etc/smartdns/conf.d"
  427. o = s.taboption("files", form.FileUpload, "upload_list_file", _("Upload Domain List File"),
  428. _("Upload domain list file to /etc/smartdns/domain-set"));
  429. o.rmempty = true
  430. o.datatype = "file"
  431. o.rempty = true
  432. o.root_directory = "/etc/smartdns/domain-set"
  433. o = s.taboption('files', form.DummyValue, "_update", _("Update Files"));
  434. o.renderWidget = function () {
  435. return E('button', {
  436. 'class': 'btn cbi-button cbi-button-apply',
  437. 'id': 'btn_update',
  438. 'click': ui.createHandlerFn(this, function () {
  439. return fs.exec('/etc/init.d/smartdns', ['updatefiles'])
  440. .catch(function (e) { ui.addNotification(null, E('p', e.message), 'error') });
  441. })
  442. }, [_("Update")]);
  443. }
  444. o = s.taboption('files', form.SectionValue, '__files__', form.GridSection, 'download-file', _('Download Files'),
  445. _('List of files to download.'));
  446. ss = o.subsection;
  447. ss.addremove = true;
  448. ss.anonymous = true;
  449. ss.sortable = true;
  450. so = ss.option(form.Value, 'name', _('File Name'), _('File Name'));
  451. so.rmempty = true;
  452. so.datatype = 'file';
  453. so = ss.option(form.Value, 'url', _('URL'), _('URL'));
  454. so.rmempty = true;
  455. so.datatype = 'string';
  456. so.validate = function (section_id, value) {
  457. if (value == "") {
  458. return true;
  459. }
  460. if (!value.match(/^(http|https|ftp|sftp):\/\//)) {
  461. return _("URL format error, format: http:// or https://");
  462. }
  463. return true;
  464. }
  465. so = ss.option(form.ListValue, "type", _("type"), _("File Type"));
  466. so.value("list", _("domain list (/etc/smartdns/domain-set)"));
  467. so.value("config", _("smartdns config (/etc/smartdns/conf.d)"));
  468. so.default = "list";
  469. so.rempty = false;
  470. so = ss.option(form.Value, 'desc', _('Description'), _('Description'));
  471. so.rmempty = true;
  472. so.datatype = 'string';
  473. ///////////////////////////////////////
  474. // Proxy server settings;
  475. ///////////////////////////////////////
  476. o = s.taboption("proxy", form.Value, "proxy_server", _("Proxy Server"), _("Proxy Server URL, format: [socks5|http]://user:pass@ip:port."));
  477. o.datatype = 'string';
  478. o.validate = function (section_id, value) {
  479. if (value == "") {
  480. return true;
  481. }
  482. if (!value.match(/^(socks5|http):\/\//)) {
  483. return _("Proxy server URL format error, format: [socks5|http]://user:pass@ip:port.");
  484. }
  485. return true;
  486. }
  487. ///////////////////////////////////////
  488. // custom settings;
  489. ///////////////////////////////////////
  490. o = s.taboption("custom", form.TextValue, "custom_conf",
  491. "", _("smartdns custom settings"));
  492. o.rows = 20;
  493. o.cfgvalue = function (section_id) {
  494. return fs.trimmed('/etc/smartdns/custom.conf');
  495. };
  496. o.write = function (section_id, formvalue) {
  497. return this.cfgvalue(section_id).then(function (value) {
  498. if (value == formvalue) {
  499. return
  500. }
  501. return fs.write('/etc/smartdns/custom.conf', formvalue.trim().replace(/\r\n/g, '\n') + '\n');
  502. });
  503. };
  504. o = s.taboption("custom", form.Flag, "coredump", _("Generate Coredump"),
  505. _("Generate Coredump file when smartdns crash, coredump file is located at /tmp/smartdns.xxx.core."));
  506. o.rmempty = true;
  507. o.default = o.disabled;
  508. o = s.taboption("custom", form.Value, "log_size", _("Log Size"));
  509. o.rmempty = true;
  510. o.placeholder = "default";
  511. o = s.taboption("custom", form.ListValue, "log_level", _("Log Level"));
  512. o.rmempty = true;
  513. o.placeholder = "default";
  514. o.value("", _("default"));
  515. o.value("debug");
  516. o.value("info");
  517. o.value("notice");
  518. o.value("warn");
  519. o.value("error");
  520. o.value("fatal");
  521. o.value("off");
  522. o = s.taboption("custom", form.Value, "log_num", _("Log Number"));
  523. o.rmempty = true;
  524. o.placeholder = "default";
  525. o = s.taboption("custom", form.Value, "log_file", _("Log File"))
  526. o.rmempty = true
  527. o.placeholder = "/var/log/smartdns/smartdns.log"
  528. ////////////////
  529. // Upstream servers;
  530. ////////////////
  531. s = m.section(form.GridSection, "server", _("Upstream Servers"),
  532. _("Upstream Servers, support UDP, TCP protocol. Please configure multiple DNS servers, "
  533. + "including multiple foreign DNS servers."));
  534. s.anonymous = true;
  535. s.addremove = true;
  536. s.sortable = true;
  537. s.tab('general', _('General Settings'));
  538. s.tab('advanced', _('Advanced Settings'));
  539. // enable flag;
  540. o = s.taboption("general", form.Flag, "enabled", _("Enable"));
  541. o.rmempty = false;
  542. o.default = o.enabled;
  543. o.editable = true;
  544. // name;
  545. o = s.taboption("general", form.Value, "name", _("DNS Server Name"));
  546. // IP address;
  547. o = s.taboption("general", form.Value, "ip", _("DNS Server ip"));
  548. o.datatype = "or(ipaddr, string)";
  549. o.rmempty = false;
  550. // port;
  551. o = s.taboption("general", form.Value, "port", _("DNS Server port"));
  552. o.placeholder = "default";
  553. o.datatype = "port";
  554. o.rempty = true;
  555. o.depends("type", "udp");
  556. o.depends("type", "tcp");
  557. o.depends("type", "tls");
  558. // type;
  559. o = s.taboption("general", form.ListValue, "type", _("DNS Server type"));
  560. o.placeholder = "udp";
  561. o.value("udp", _("udp"));
  562. o.value("tcp", _("tcp"));
  563. o.value("tls", _("tls"));
  564. o.value("https", _("https"));
  565. o.default = "udp";
  566. o.rempty = false;
  567. // server group
  568. o = s.taboption("general", form.Value, "server_group", _("DNS Server group"))
  569. o.rmempty = true;
  570. o.placeholder = "default";
  571. o.datatype = "hostname";
  572. o.rempty = true;
  573. servers = uci.sections('smartdns', 'server');
  574. var groupnames = new Set();
  575. for (var i = 0; i < servers.length; i++) {
  576. if (servers[i].server_group == undefined) {
  577. continue;
  578. }
  579. groupnames.add(servers[i].server_group);
  580. }
  581. for (const groupname of groupnames) {
  582. o.value(groupname);
  583. }
  584. // Advanced Options
  585. o = s.taboption("advanced", form.Flag, "exclude_default_group", _("Exclude Default Group"), _("Exclude DNS Server from default group."))
  586. o.rmempty = true;
  587. o.default = o.disabled;
  588. o.editable = true;
  589. o.modalonly = true;
  590. // blacklist_ip
  591. o = s.taboption("advanced", form.Flag, "blacklist_ip", _("IP Blacklist Filtering"),
  592. _("Filtering IP with blacklist"))
  593. o.rmempty = true
  594. o.default = o.disabled
  595. o.modalonly = true;
  596. // TLS host verify
  597. o = s.taboption("advanced", form.Value, "tls_host_verify", _("TLS Hostname Verify"),
  598. _("Set TLS hostname to verify."))
  599. o.default = ""
  600. o.datatype = "string"
  601. o.rempty = true
  602. o.modalonly = true;
  603. o.depends("type", "tls")
  604. o.depends("type", "https")
  605. // certificate verify
  606. o = s.taboption("advanced", form.Flag, "no_check_certificate", _("No check certificate"),
  607. _("Do not check certificate."))
  608. o.rmempty = true
  609. o.default = o.disabled
  610. o.modalonly = true;
  611. o.depends("type", "tls")
  612. o.depends("type", "https")
  613. // SNI host name
  614. o = s.taboption("advanced", form.Value, "host_name", _("TLS SNI name"),
  615. _("Sets the server name indication for query. '-' for disable SNI name."))
  616. o.default = ""
  617. o.datatype = "hostname"
  618. o.rempty = true
  619. o.modalonly = true;
  620. o.depends("type", "tls")
  621. o.depends("type", "https")
  622. // http host
  623. o = s.taboption("advanced", form.Value, "http_host", _("HTTP Host"),
  624. _("Set the HTTP host used for the query. Use this parameter when the host of the URL address is an IP address."))
  625. o.default = ""
  626. o.datatype = "hostname"
  627. o.rempty = true
  628. o.modalonly = true;
  629. o.depends("type", "https")
  630. // SPKI pin
  631. o = s.taboption("advanced", form.Value, "spki_pin", _("TLS SPKI Pinning"),
  632. _("Used to verify the validity of the TLS server, The value is Base64 encoded SPKI fingerprint, "
  633. + "leaving blank to indicate that the validity of TLS is not verified."))
  634. o.default = ""
  635. o.datatype = "string"
  636. o.rempty = true
  637. o.modalonly = true;
  638. o.depends("type", "tls")
  639. o.depends("type", "https")
  640. // mark
  641. o = s.taboption("advanced", form.Value, "set_mark", _("Marking Packets"),
  642. _("Set mark on packets."))
  643. o.default = ""
  644. o.rempty = true
  645. o.datatype = "uinteger"
  646. o.modalonly = true;
  647. // use proxy
  648. o = s.taboption("advanced", form.Flag, "use_proxy", _("Use Proxy"),
  649. _("Use proxy to connect to upstream DNS server."))
  650. o.default = o.disabled
  651. o.modalonly = true;
  652. o.optional = true;
  653. o.rempty = true;
  654. o.validate = function (section_id, value) {
  655. var flag = this.formvalue(section_id);
  656. if (flag == "0") {
  657. return true;
  658. }
  659. var proxy_server = uci.sections("smartdns", "smartdns")[0].proxy_server;
  660. var server_type = this.section.formvalue(section_id, "type");
  661. if (proxy_server == "" || proxy_server == undefined) {
  662. return _("Please set proxy server first.");
  663. }
  664. if (server_type == "udp" && !proxy_server.match(/^(socks5):\/\//)) {
  665. return _("Only socks5 proxy support udp server.");
  666. }
  667. return true;
  668. }
  669. // other args
  670. o = s.taboption("advanced", form.Value, "addition_arg", _("Additional Server Args"),
  671. _("Additional Args for upstream dns servers"))
  672. o.default = ""
  673. o.rempty = true
  674. o.modalonly = true;
  675. ////////////////
  676. // domain rules;
  677. ////////////////
  678. s = m.section(form.TypedSection, "domain-rule", _("Domain Rules"), _("Domain Rules Settings"));
  679. s.anonymous = true;
  680. s.nodescriptions = true;
  681. s.tab("forwarding", _('DNS Forwarding Setting'));
  682. s.tab("block", _("DNS Block Setting"));
  683. s.tab("domain-rule-list", _("Domain Rule List"), _("Set Specific domain rule list."));
  684. s.tab("domain-address", _("Domain Address"), _("Set Specific domain ip address."));
  685. s.tab("blackip-list", _("IP Blacklist"), _("Set Specific ip blacklist."));
  686. ///////////////////////////////////////
  687. // domain forwarding;
  688. ///////////////////////////////////////
  689. o = s.taboption("forwarding", form.Value, "server_group", _("Server Group"), _("DNS Server group belongs to, such as office, home."))
  690. o.rmempty = true
  691. o.placeholder = "default"
  692. o.datatype = "hostname"
  693. o.rempty = true
  694. for (const groupname of groupnames) {
  695. o.value(groupname);
  696. }
  697. o.validate = function (section_id, value) {
  698. if (value == "") {
  699. return true;
  700. }
  701. var val = uci.sections('smartdns', 'server');
  702. for (var i = 0; i < val.length; i++) {
  703. if (value == val[i].server_group) {
  704. return true;
  705. }
  706. }
  707. return _('Server Group %s not exists').format(value);
  708. }
  709. o = s.taboption("forwarding", form.Flag, "no_speed_check", _("Skip Speed Check"),
  710. _("Do not check speed."));
  711. o.rmempty = true;
  712. o.default = o.disabled;
  713. o = s.taboption("forwarding", form.Flag, "force_aaaa_soa", _("Force AAAA SOA"), _("Force AAAA SOA."));
  714. o.rmempty = true;
  715. o.default = o.disabled;
  716. o = s.taboption("forwarding", form.Value, "ipset_name", _("IPset Name"), _("IPset name."));
  717. o.rmempty = true;
  718. o.datatype = "string";
  719. o.rempty = true;
  720. o.validate = function (section_id, value) {
  721. if (value == "") {
  722. return true;
  723. }
  724. var ipset = value.split(",")
  725. for (var i = 0; i < ipset.length; i++) {
  726. if (!ipset[i].match(/^(#[4|6]:)?[a-zA-Z0-9\-_]+$/)) {
  727. return _("ipset name format error, format: [#[4|6]:]ipsetname");
  728. }
  729. }
  730. return true;
  731. }
  732. o = s.taboption("forwarding", form.Value, "nftset_name", _("NFTset Name"), _("NFTset name, format: [#[4|6]:[family#table#set]]"));
  733. o.rmempty = true;
  734. o.datatype = "string";
  735. o.rempty = true;
  736. o.validate = function (section_id, value) {
  737. if (value == "") {
  738. return true;
  739. }
  740. var nftset = value.split(",")
  741. for (var i = 0; i < nftset.length; i++) {
  742. if (!nftset[i].match(/^#[4|6]:[a-zA-Z0-9\-_]+#[a-zA-Z0-9\-_]+#[a-zA-Z0-9\-_]+$/)) {
  743. return _("NFTset name format error, format: [#[4|6]:[family#table#set]]");
  744. }
  745. }
  746. return true;
  747. }
  748. // other args
  749. o = s.taboption("forwarding", form.Value, "addition_flag", _("Additional Rule Flag"),
  750. _("Additional Flags for rules, read help on domain-rule for more information."))
  751. o.default = ""
  752. o.rempty = true
  753. o.modalonly = true;
  754. o = s.taboption("forwarding", form.FileUpload, "forwarding_domain_set_file", _("Domain List File"),
  755. _("Upload domain list file, or configure auto download from Download File Setting page."));
  756. o.rmempty = true
  757. o.datatype = "file"
  758. o.rempty = true
  759. o.editable = true
  760. o.root_directory = "/etc/smartdns/domain-set"
  761. o = s.taboption("forwarding", form.TextValue, "domain_forwarding_list",
  762. _("Domain List"), _("Configure forwarding domain name list."));
  763. o.rows = 10;
  764. o.cols = 64;
  765. o.monospace = true;
  766. o.cfgvalue = function (section_id) {
  767. return fs.trimmed('/etc/smartdns/domain-forwarding.list').catch(function (e) {
  768. return "";
  769. });
  770. };
  771. o.write = function (section_id, formvalue) {
  772. return this.cfgvalue(section_id).then(function (value) {
  773. if (value == formvalue) {
  774. return
  775. }
  776. return fs.write('/etc/smartdns/domain-forwarding.list', formvalue.trim().replace(/\r\n/g, '\n') + '\n');
  777. });
  778. };
  779. ///////////////////////////////////////
  780. // domain block;
  781. ///////////////////////////////////////
  782. o = s.taboption("block", form.FileUpload, "block_domain_set_file", _("Domain List File"), _("Upload domain list file."));
  783. o.rmempty = true
  784. o.datatype = "file"
  785. o.rempty = true
  786. o.editable = true
  787. o.root_directory = "/etc/smartdns/domain-set"
  788. o = s.taboption("block", form.TextValue, "domain_block_list",
  789. _("Domain List"), _("Configure block domain list."));
  790. o.rows = 10;
  791. o.cols = 64;
  792. o.cfgvalue = function (section_id) {
  793. return fs.trimmed('/etc/smartdns/domain-block.list').catch(function (e) {
  794. return "";
  795. });
  796. };
  797. o.write = function (section_id, formvalue) {
  798. return this.cfgvalue(section_id).then(function (value) {
  799. if (value == formvalue) {
  800. return
  801. }
  802. return fs.write('/etc/smartdns/domain-block.list', formvalue.trim().replace(/\r\n/g, '\n') + '\n');
  803. });
  804. };
  805. ///////////////////////////////////////
  806. // domain rule list;
  807. ///////////////////////////////////////
  808. o = s.taboption('domain-rule-list', form.SectionValue, '__domain-rule-list__', form.GridSection, 'domain-rule-list', _('Domain Rule List'),
  809. _('Configure domain rule list.'));
  810. ss = o.subsection;
  811. ss.addremove = true;
  812. ss.anonymous = true;
  813. ss.sortable = true;
  814. // enable flag;
  815. so = ss.option(form.Flag, "enabled", _("Enable"), _("Enable"));
  816. so.rmempty = false;
  817. so.default = so.enabled;
  818. so.editable = true;
  819. // name;
  820. so = ss.option(form.Value, "name", _("Domain Rule Name"), _("Domain Rule Name"));
  821. so = ss.option(form.Value, "server_group", _("Server Group"), _("DNS Server group belongs to, such as office, home."))
  822. so.rmempty = true
  823. so.placeholder = "default"
  824. so.datatype = "hostname"
  825. so.rempty = true
  826. for (const groupname of groupnames) {
  827. so.value(groupname);
  828. }
  829. so.validate = function (section_id, value) {
  830. if (value == "") {
  831. return true;
  832. }
  833. var val = uci.sections('smartdns', 'server');
  834. for (var i = 0; i < val.length; i++) {
  835. if (value == val[i].server_group) {
  836. return true;
  837. }
  838. }
  839. return _('Server Group %s not exists').format(value);
  840. }
  841. so = ss.option(form.FileUpload, "domain_list_file", _("Domain List File"),
  842. _("Upload domain list file, or configure auto download from Download File Setting page."));
  843. so.rmempty = true
  844. so.datatype = "file"
  845. so.rempty = true
  846. so.root_directory = "/etc/smartdns/domain-set"
  847. so = ss.option(form.ListValue, "block_domain_type", _("Block domain"), _("Block domain."));
  848. so.rmempty = true;
  849. so.value("none", _("None"));
  850. so.value("all", "IPv4/IPv6");
  851. so.value("ipv4", "IPv4");
  852. so.value("ipv6", "IPv6");
  853. so.modalonly = true;
  854. // Support DualStack ip selection;
  855. so = ss.option(form.ListValue, "dualstack_ip_selection", _("Dual-stack IP Selection"),
  856. _("Enable IP selection between IPV4 and IPV6"));
  857. so.rmempty = true;
  858. so.default = "default";
  859. so.modalonly = true;
  860. so.value("", _("default"));
  861. so.value("yes", _("Yes"));
  862. so.value("no", _("No"));
  863. so = ss.option(form.Value, "speed_check_mode", _("Speed Check Mode"), _("Smartdns speed check mode."));
  864. so.rmempty = true;
  865. so.placeholder = "default";
  866. so.modalonly = true;
  867. so.value("", _("default"));
  868. so.value("ping,tcp:80,tcp:443");
  869. so.value("ping,tcp:443,tcp:80");
  870. so.value("tcp:80,tcp:443,ping");
  871. so.value("tcp:443,tcp:80,ping");
  872. so.value("none", _("None"));
  873. so.validate = function (section_id, value) {
  874. if (value == "") {
  875. return true;
  876. }
  877. if (value == "none") {
  878. return true;
  879. }
  880. var check_mode = value.split(",")
  881. for (var i = 0; i < check_mode.length; i++) {
  882. if (check_mode[i] == "ping") {
  883. continue;
  884. }
  885. if (check_mode[i].indexOf("tcp:") == 0) {
  886. var port = check_mode[i].split(":")[1];
  887. if (port == "") {
  888. return _("TCP port is empty");
  889. }
  890. continue;
  891. }
  892. return _("Speed check mode is invalid.");
  893. }
  894. return true;
  895. }
  896. so = ss.option(form.Flag, "force_aaaa_soa", _("Force AAAA SOA"), _("Force AAAA SOA."));
  897. so.rmempty = true;
  898. so.default = so.disabled;
  899. so.modalonly = true;
  900. so = ss.option(form.Value, "ipset_name", _("IPset Name"), _("IPset name."));
  901. so.rmempty = true;
  902. so.datatype = "hostname";
  903. so.rempty = true;
  904. so.modalonly = true;
  905. so = ss.option(form.Value, "nftset_name", _("NFTset Name"), _("NFTset name, format: [#[4|6]:[family#table#set]]"));
  906. so.rmempty = true;
  907. so.datatype = "string";
  908. so.rempty = true;
  909. so.modalonly = true;
  910. so.validate = function (section_id, value) {
  911. if (value == "") {
  912. return true;
  913. }
  914. var nftset = value.split(",")
  915. for (var i = 0; i < nftset.length; i++) {
  916. if (!nftset[i].match(/#[4|6]:[a-zA-Z0-9\-_]+#[a-zA-Z0-9\-_]+#[a-zA-Z0-9\-_]+$/)) {
  917. return _("NFTset name format error, format: [#[4|6]:[family#table#set]]");
  918. }
  919. }
  920. return true;
  921. }
  922. // other args
  923. so = ss.option(form.Value, "addition_flag", _("Additional Rule Flag"),
  924. _("Additional Flags for rules, read help on domain-rule for more information."))
  925. so.default = ""
  926. so.rempty = true
  927. so.modalonly = true;
  928. ///////////////////////////////////////
  929. // IP Blacklist;
  930. ///////////////////////////////////////
  931. // blacklist;
  932. o = s.taboption("blackip-list", form.TextValue, "blackip_ip_conf",
  933. "", _("Configure IP blacklists that will be filtered from the results of specific DNS server."));
  934. o.rows = 20;
  935. o.cfgvalue = function (section_id) {
  936. return fs.trimmed('/etc/smartdns/blacklist-ip.conf');
  937. };
  938. o.write = function (section_id, formvalue) {
  939. return this.cfgvalue(section_id).then(function (value) {
  940. if (value == formvalue) {
  941. return
  942. }
  943. return fs.write('/etc/smartdns/blacklist-ip.conf', formvalue.trim().replace(/\r\n/g, '\n') + '\n');
  944. });
  945. };
  946. ///////////////////////////////////////
  947. // domain address
  948. ///////////////////////////////////////
  949. o = s.taboption("domain-address", form.TextValue, "address_conf",
  950. "",
  951. _("Specify an IP address to return for any host in the given domains, Queries in the domains are never "
  952. + "forwarded and always replied to with the specified IP address which may be IPv4 or IPv6."));
  953. o.rows = 20;
  954. o.cfgvalue = function (section_id) {
  955. return fs.trimmed('/etc/smartdns/address.conf');
  956. };
  957. o.write = function (section_id, formvalue) {
  958. return this.cfgvalue(section_id).then(function (value) {
  959. if (value == formvalue) {
  960. return
  961. }
  962. return fs.write('/etc/smartdns/address.conf', formvalue.trim().replace(/\r\n/g, '\n') + '\n');
  963. });
  964. };
  965. ////////////////
  966. // Support
  967. ////////////////
  968. s = m.section(form.TypedSection, "smartdns", _("Technical Support"),
  969. _("If you like this software, please buy me a cup of coffee."));
  970. s.anonymous = true;
  971. o = s.option(form.Button, "web");
  972. o.title = _("SmartDNS official website");
  973. o.inputtitle = _("open website");
  974. o.inputstyle = "apply";
  975. o.onclick = function () {
  976. window.open("https://pymumu.github.io/smartdns", '_blank');
  977. };
  978. o = s.option(form.Button, "report");
  979. o.title = _("Report bugs");
  980. o.inputtitle = _("Report bugs");
  981. o.inputstyle = "apply";
  982. o.onclick = function () {
  983. window.open("https://github.com/pymumu/smartdns/issues", '_blank');
  984. };
  985. o = s.option(form.Button, "Donate");
  986. o.title = _("Donate to smartdns");
  987. o.inputtitle = _("Donate");
  988. o.inputstyle = "apply";
  989. o.onclick = function () {
  990. window.open("https://pymumu.github.io/smartdns/#donate", '_blank');
  991. };
  992. o = s.option(form.DummyValue, "_restart", _("Restart Service"));
  993. o.renderWidget = function () {
  994. return E('button', {
  995. 'class': 'btn cbi-button cbi-button-apply',
  996. 'id': 'btn_restart',
  997. 'click': ui.createHandlerFn(this, function () {
  998. return fs.exec('/etc/init.d/smartdns', ['restart'])
  999. .catch(function (e) { ui.addNotification(null, E('p', e.message), 'error') });
  1000. })
  1001. }, [_("Restart")]);
  1002. }
  1003. return m.render();
  1004. }
  1005. });