I configured the tor client on the router and it worked very well. Now I need to make a simple UI for it where the user can configure and view the status.
I have no way to get the status of the connection to tor, I can get the following log
Nov 04 09:11:24.000 [notice] Tor 0.3.2.10 (git-31cc63deb69db819) opening new log file.
Nov 04 09:11:53.000 [notice] {CONTROL} Bootstrapped 0%: Starting
Nov 04 09:12:27.000 [notice] {CONTROL} Bootstrapped 80%: Connecting to the Tor network
Nov 04 09:15:16.000 [notice] {CONTROL} Bootstrapped 85%: Finishing handshake with first hop
Nov 04 09:15:17.000 [notice] {CONTROL} Bootstrapped 90%: Establishing a Tor circuit
Nov 04 09:15:18.000 [notice] {CONTROL} Bootstrapped 100%: Done
But this is not reliable, because my network will be disconnected at any time, the corresponding log will not be updated effectively, I will get an error status.
Through https://check.torproject.org/ access to the state is reliable, but I don't want to use this way, because he will consume my data traffic.
Is there any other way I can get to the right state?
You can use the local Tor Control API on port 9051 to get that information (and a lot more).
Typically something like calling AUTHENTICATE and GETINFO network-liveness will give you something similar to the log content you're seeing in syslog.
It's as "real-time" as you're going to get it from my experience. It is a view as to the actual status within the Tor process. All the network connection/reconnection is handled inside Tor which means it takes up to a couple of minutes for the process to realise that it is no longer connected to the Tor network.You could build additional logic on your own if you want to attempt to preempt disconnection - for example, you could assume that if the main internet connection is gone, Tor is as good as disconnected (aka will change to disconnected over the next while).
There's a lot of information in this file, but what criteria do I use to determine if tor is connected correctly?
Maybe this is a very rookie problem, hope to get some help.
I think OP is trying to detect the situation when the device has no internet at all (and TOR is obviously not functioning), but TOR insists that "everything is all right".
In this situation, I will probably script a periodic wget/curl execution to test that an external page can be reached.
I tried to get the status from port 9051, but it wasn't accurate.
Perhaps tor has no connected or disconnected status.
Anyway, thank you very much for your advice.
I have a device that I need to use an icon on the LCD to indicate whether the router provides tor service, and it only has two states is UP or DOWN.
My device using LTE data, so I don't want to request network ways to obtain state, such as through https://check.torproject.org/ , this will consume my data traffic, and is likely to data leakage.
@VincentR gave me some great suggestions, but the state I picked up from network socket is not a real-time state. Sometimes it takes more than ten minutes to update.
Maybe it takes this long for a status update, but I want it to be faster.
I can get some information through /tmp/lib/tor/state. There is a lot of information here, but it is not what I want. I only need two states, UP or DOWN
when both tor and your internet interface(s) say "up" - consider your service is "up"
if either tor or your internet interface(s) say "down" - consider your service is "down"
reload/restart tor when your internet interface(s) comes back up to force a re-initialisation of the tor connections and state
potentially stop tor when the internet interface(s) is not up anyway (typically the latest working configuration stays cached to speed things up when it restarts). Otherwise, tor can use quite a lot of resources (eg. CPU) when attempting to reconnect.
Therefore, if you have valid Guard in list with their valid key hashes, you are "connected". You can search and validate these hashes (or the server nickname) here: https://metrics.torproject.org/rs.html#search (BTW, here's the link to the Metrics .onion page, and other TorProject sites: https://onion.torproject.org/)
Is this an OpenWrt device!?!?
If not, how does it use OpenWrt to connect to Tor?
Are you 100% sure that you have an OpenWrt running Tor?
What is the make/model of your device?
I find that nearly impossible.
You're saying:
You've seen the status information; and
You know it to be inaccurate...
How?
Please verify you're connected to the correct Tor instance on OpenWrt; and not an instance running on your laptop/PC for example. Also make sure you're not running nested Tor (e.g. on your LTE modem and OpenWrt too). I'm not sure how your modem sees Tor (or was programmed to check) if the device doesn't have it installed already. Also, I wouldn't trust anything with closed course firmware from a Chinese agency to run Tor (or check an .onion) for me.
This doesn't obtain a state, it makes a virtual circuit and uses Tor to reach the website. I think you confuse that green onion on the web page for "Up" - when you should be verifying connectivity to your current guard nodes.