Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

luci-app-smartdns: add new package #3413

Merged
merged 1 commit into from
Jul 2, 2020
Merged

luci-app-smartdns: add new package #3413

merged 1 commit into from
Jul 2, 2020

Conversation

pymumu
Copy link
Contributor

@pymumu pymumu commented Dec 15, 2019

This luci app supports smartdns.

Depends on openwrt/packages#10797.

Signed-off-by: Nick Peng pymumu@gmail.com

@feckert feckert added the depends on other pr pull request depends on another pull request label Dec 15, 2019
@pymumu pymumu force-pushed the master branch 4 times, most recently from 3db4ce5 to eab97d7 Compare December 21, 2019 03:58
@aparcar
Copy link
Member

aparcar commented Jan 2, 2020

Please rewrite this app in JavaScript instead of Lua. If you look at the commit history for other recently added apps they show required files. It is pretty straight forward to convert your existing Lua code to JavaScript.

@feckert feckert added the WIP pull request the author is still working on label Jan 9, 2020
@pymumu
Copy link
Contributor Author

pymumu commented Jan 30, 2020

@aparcar @feckert
Encountered two problems while rewriting, please help.

  1. Load external template html page, used to display service status, don't know how to rewrite.
m.section(SimpleSection).template = "smartdns/smartdns_status";
  1. The following code cannot read or write file(file exists with right permission), and prompts for invalid values ​​when entering some text after saving.
o = s.taboption("custom", form.TextValue, "_tmpl",_(""),_("smartdns custom settings"));
o.rows = 20;
o.cfgvalue = function(section_id) {
    return fs.trimmed('/etc/smartdns/custom.conf');
};
o.write = function (section_id, formvalue) {
   return fs.write('/etc/smartdns/custom.conf', formvalue.trim().replace(/\r\n/g, '\n') + '\n');
};

luci-app-smartdns.zip

@ysc3839
Copy link
Contributor

ysc3839 commented Jan 30, 2020

@pymumu
1.https://github.com/openwrt/luci/blob/master/applications/luci-app-ttyd/htdocs/luci-static/resources/view/ttyd/term.js
2.

o.cfgvalue = function(section_id) {
return fs.trimmed('/etc/ksmbd/smb.conf.template');
};
o.write = function(section_id, formvalue) {
return fs.write('/etc/ksmbd/smb.conf.template', formvalue.trim().replace(/\r\n/g, '\n') + '\n');
};

https://github.com/openwrt/luci/blob/master/applications/luci-app-ksmbd/root/usr/share/rpcd/acl.d/luci-app-ksmbd.json

@pymumu
Copy link
Contributor Author

pymumu commented Jan 31, 2020

@ysc3839 Problem 2 is solved, thanks.
but I think problem 1 is not the same as the example.

About problem 1, the feature is to display servie status, the status check function calls some shell commands (such as pidof, iptable) periodically and display the result.
for now, I can do is to make the page display some text periodically, but cannot get the results of these commands, because fs.exec is a async function.

the code is like this, dont't knonw how to write smartdnsServiceStatus()
Not familiar with javascript, request for help.

function smartdnsServiceStatus() {
        // how to get the following commands result with fs.exec() ?.
        // pidof smartdns >/dev/null  
        // iptables -t nat -nL PREROUTING 2>/dev/null | grep REDIRECT | grep dpt:53 | grep %q >/dev/null 2>&1  
        // uci get dhcp.@dnsmasq[0].server  
	return "return service status";
}

// section render
s.render = function (section_id) {
	L.Poll.add(function () {
		return L.resolveDefault(smartdnsServiceStatus()).then(function (res) {
			var content = res;
			var view = document.getElementById("service_status");
			view.innerHTML = res;
		});
	});

	return E('div', { class: 'cbi-map' },
		E('div', { class: 'cbi-section' }, [
			E('div', {id: 'service_status' },
				_('Collecting data ...'))
		])
	);
}

smartdns.js.zip

@feckert
Copy link
Member

feckert commented Jan 31, 2020

Maybe we can implement an RPC call in the ubus?

@jow-
Copy link
Contributor

jow- commented Jan 31, 2020

function smartdnsServiceStatus() {
        return Promise.all([
                fs.exec_direct('/bin/pidof', ['smartdns']),
                fs.exec_direct('/usr/sbin/iptables', ['-t', 'nat', '-n', '-L', 'PREROUTING']),
                uci.get_first('dhcp', 'dnsmasq', 'server')
        ]);
}

// section render
s.render = function (section_id) {
	L.Poll.add(function () {
		return L.resolveDefault(smartdnsServiceStatus()).then(function (res) {
			var pid = +res[0],
			    ipt = res[1],
			    srv = res[2];

			var matching_rules = (ipt || '').split(/\n/).filter(function(rule) {
			    return rule.match(/REDIRECT/) && rule.match(/dpt:53/);
			});

			var view = document.getElementById("service_status");
			view.innerHTML = _('PID of service is %s<br />Firewall rule is %s<br />DNS Server is %s').format(
			    isNaN(pid) ? '?' : pid, 
			    matching_rules.length ? 'present' : 'not present',
			    srv);
		});
	});

	return E('div', { class: 'cbi-map' },
		E('div', { class: 'cbi-section' }, [
			E('div', {id: 'service_status' },
				_('Collecting data ...'))
		])
	);
}

You need these ACL rules for the above code to work:

{
	"luci-app-smartdns": {
		"description": "...",
		"read": {
			"cgi-io": [ "exec" ],
			"file": {
				"/usr/sbin/iptables -t nat -n -L PREROUTING": [ "exec" ],
				"/bin/pidof smartdns": [ "exec" ]
			}
		}
	}
}

@ysc3839
Copy link
Contributor

ysc3839 commented Jan 31, 2020

smartdns is using procd to manage process. https://github.com/pymumu/smartdns/blob/744a5409e0fb65b98a8eeb09cdc48bacc0f2a10f/package/openwrt/files/etc/init.d/smartdns#L20
So pidof can be replaced with ubus service list.

var callServiceList = rpc.declare({
object: 'service',
method: 'list',
params: [ 'name' ],
expect: { '': {} }
});
L.Poll.add(function() {
return L.resolveDefault(callServiceList(conf), {})
.then(function(res) {
var instances = null;
try {
instances = res[conf]['instances'];
} catch (e) {}
if (!instances) return;

@pymumu
Copy link
Contributor Author

pymumu commented Feb 1, 2020

@jow- thanks, problem solved.

another problem, when only modify the content in the form.TextValue, and then click "Save&Apply", the page says "There are no changes to apply", how to solve this problem, the code is as follows.

o = s.taboption("custom", form.TextValue, "custom_conf",
	_(""),
	_("smartdns custom settings"));

o.rows = 20;
o.cfgvalue = function (section_id) {
	return fs.trimmed('/etc/smartdns/custom.conf');
};
o.write = function (section_id, formvalue) {
	return fs.write('/etc/smartdns/custom.conf', formvalue.trim().replace(/\r\n/g, '\n') + '\n');
};

@jow-
Copy link
Contributor

jow- commented Feb 1, 2020

another problem, when only modify the content in the form.TextValue, and then click "Save&Apply", the page says "There are no changes to apply"

Yeah, this is a known problem - ignore it for now, it needs to be fixed on the LuCI side.

@pymumu pymumu force-pushed the master branch 4 times, most recently from e666bb5 to 424895e Compare February 1, 2020 10:53
Signed-off-by: Nick Peng <pymumu@gmail.com>
@pymumu
Copy link
Contributor Author

pymumu commented Mar 20, 2020

@jow-
All changes are done and tested, please review.

@pymumu pymumu requested a review from jow- March 21, 2020 13:45
@feckert feckert merged commit 9f95d43 into openwrt:master Jul 2, 2020
@feckert
Copy link
Member

feckert commented Jul 2, 2020

@jow The way I see it, all changes have been implemented.
So that the pullrequest doesn't rot I will merge them now.
And give them a try.

@pymumu thanks for your contribution

@jmarcet
Copy link
Contributor

jmarcet commented Jul 3, 2020

I get the following error when trying this module:

Screenshot_20200703_134655

@feckert
Copy link
Member

feckert commented Jul 3, 2020

@jmarcet We have already an issue for that.
I will have a look on this next week if I have more time!
See #4226

But I think we have to add the uci permition read/write to the following uci file smartdns to https://github.com/openwrt/luci/blob/master/applications/luci-app-smartdns/root/usr/share/rpcd/acl.d/luci-app-smartdns.json
Please have a look for uci read/write permissions howto solve this to the luci-app-adblock.
Write:
https://github.com/openwrt/luci/blob/master/applications/luci-app-adblock/root/usr/share/rpcd/acl.d/luci-app-adblock.json#L5
Read:
https://github.com/openwrt/luci/blob/master/applications/luci-app-adblock/root/usr/share/rpcd/acl.d/luci-app-adblock.json#L28

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
depends on other pr pull request depends on another pull request WIP pull request the author is still working on
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants