Created
May 7, 2021 12:24
-
-
Save nqxcode/33dbe1686ed4b7d4d7d1c6de79a95971 to your computer and use it in GitHub Desktop.
Tor tab for openwrt (luci integration)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- /usr/lib/lua/luci/view/tor.htm | |
<%+header%> | |
<div> | |
<h2 name="content"><%:Tor%></h2> | |
<div id="view"> | |
<div> | |
<div class="tor_status_wrapper"> | |
<span>Состояние соединения</span>: <span id="tor_status" class="tor_status"></span><span id="tor_status_spinner" | |
class="tor_status_spinner"></span> | |
</div> | |
<button id="tor_restart_button" type="button" class="btn cbi-button cbi-button-apply pull-right" | |
title="<%:Перезапустить Tor%>" | |
aria-label="<%:Перезапустить Tor%>" onclick="restart(event)">Перезапустить Tor | |
</button> | |
</div> | |
</div> | |
</div> | |
<style type="text/css"> | |
.tor_status_wrapper { | |
display: inline-block; | |
font-size: 18px; | |
line-height: 30px; | |
} | |
.tor_status_wrapper .tor_status.complete { | |
color: green; | |
} | |
.tor_status_wrapper .tor_status.in-progress { | |
color: #b9b90d; | |
} | |
.tor_status_wrapper .tor_status_spinner { | |
display: inline-block; | |
width: 0px; | |
height: 16px; | |
margin: 0 0px; | |
} | |
</style> | |
<script type="text/javascript"> | |
var tor_status_spinner = document.getElementById("tor_status_spinner"); | |
var tor_status = document.getElementById('tor_status') | |
var tor_restart_button = document.getElementById('tor_restart_button') | |
function spinner(element, state) { | |
if (state === 1) { | |
element.style.width = "16px"; | |
element.innerHTML = '<img src="<%=resource%>/icons/loading.gif" alt="<%:Loading%>" width="16" height="16" style="vertical-align:middle" />'; | |
} else { | |
element.style.width = "0px"; | |
element.innerHTML = ''; | |
} | |
} | |
function percentage(value) { | |
tor_status.setAttribute('class', 'tor_status') | |
if (value === 0 || value > 0) { | |
if (value === 100) { | |
tor_status.classList.add('complete'); | |
tor_status.innerHTML = 'подключен' | |
} else { | |
tor_status.classList.add('in-progress'); | |
tor_status.innerHTML = 'подключается - ' + value + '%' | |
} | |
spinner(tor_status_spinner, 0); | |
} else { | |
tor_status.innerHTML = '' | |
spinner(tor_status_spinner, 1); | |
} | |
} | |
function get_status() { | |
XHR.get('<%=luci.dispatcher.build_url("admin", "services", "tor", "status")%>', null, function (x, st) { | |
if (st.status === 'success') { | |
percentage(st.percentage) | |
tor_restart_button.disabled = st.percentage !== 100 | |
} else { | |
console.error(st) | |
} | |
}); | |
} | |
function restart() { | |
tor_restart_button.disabled = true; | |
XHR.get('<%=luci.dispatcher.build_url("admin", "services", "tor", "restart")%>', null, function (x, st) { | |
if (st.status === 'success') { | |
percentage(st.percentage) | |
} else { | |
console.error(st) | |
} | |
}); | |
} | |
function get_external_ip() { | |
XHR.get('<%=luci.dispatcher.build_url("admin", "services", "tor", "get_external_ip")%>', null, function (x, st) { | |
if (st.status === 'success') { | |
console.info(st.ip) | |
} else { | |
console.error(st) | |
} | |
}); | |
} | |
spinner(tor_status_spinner, 1); | |
setInterval(function () { | |
get_status() | |
}, 2000) | |
</script> | |
<%+footer%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- /usr/lib/lua/luci/controller/tor.lua | |
module("luci.controller.tor", package.seeall) | |
function index() | |
entry({ "admin", "services", "tor", }, template("tor"), "Tor", 60).dependent = true | |
entry({ "admin", "services", "tor", "status" }, call("action_status")) | |
entry({ "admin", "services", "tor", "restart" }, call("action_restart")) | |
entry({ "admin", "services", "tor", "get_external_ip" }, call("action_get_external_ip")) | |
end | |
function action_status() | |
luci.http.prepare_content("application/json") | |
luci.http.write_json({ status = 'success', percentage = get_tor_status() }); | |
end | |
function action_restart() | |
luci.http.prepare_content("application/json") | |
luci.sys.init.restart("tor") | |
luci.http.write_json({ status = 'success', percentage = get_tor_status() }); | |
end | |
function action_get_external_ip() | |
--local ip = luci.util.exec('curl --socks5 127.0.0.1:9100 https://check.torproject.org |& grep -Po "(?<=strong>)[\d\.]+(?=</strong)"'):gsub("\n", "") | |
--luci.http.write_json({ status = 'success', ip = ip }); | |
end | |
function get_tor_status() | |
if is_tor_running() then | |
local output = luci.util.exec("tail -n 10 /var/log/tor/notices.log | grep Bootstrapped | tail -n 1 | awk '{print $6}' | tr -d '%'"):gsub("\n", "") | |
return tonumber(output) | |
end | |
end | |
function is_tor_running() | |
pid = luci.util.exec("cat /var/lib/tor/tor.pid"):gsub("\n", "") | |
return is_dir_exists("/proc/" .. pid) | |
end | |
function is_dir_exists(path) | |
local os = require 'os' | |
return os.execute('[[ -d ' .. path .. ' ]]') == 0 | |
end | |
function log(message) | |
luci.util.exec("logger " .. tostring(message)) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment