i@mn0t.dev:~#

get_relays.sh

A simple bash script to get informations about tor relays.

This script call the Onionoo api.

Why ?

Because I dont want to use other technologies than html/css.

But in web environment to call an api, it's easy to think about js or php
but there is no php on this server and dont want js.

Finally I decide to use an external solution, a simple bash script who will get the informations
and rendering it into an html page, tor.html.

The script in simply excecuted from cron every 10min.

How ?

There is one interesting route in the Onionoo api, /details,
plus some parameters are availaible, here I juste need the parameter lookup.

lookup take the fingerprint of the tor relay to return a json formatted response.

The jq command will be used to easily parse the document.

Code !

The script is very simple, there is no need a lot of explication.

The head of html page (title, nav etc) and the foot page are stored in variables.

I send the content of these vars into an html file.
The main part is sent when the script start and the footer variable at the end.

The main content is generated inside a while loop through the tor relays fingerprints.


#!/bin/bash 

lastupdate=$(echo `date +%d/%m/%Y` at `date +%H:%M:%S`)
html_head="
<html>
  <head>
   <link href='./css/base.css' rel='stylesheet' />
   <link href='./css/corps.css' rel='stylesheet' />
  </head>
  <body id='body'>
    <h1 class='title'><a href='index.html'>i@mn0t.dev:~#</a></h1>
    <!--menu nav-->
    <nav id='navigation'>
      <ul>
        <li><a href='./system/system.html'>System</a></li>
        <li>|</li>
        <li><a href='./code/index.html'>Code</a></li>
        <li>|</li>
        <li><a href='./about.html'>About</a></li>
        <li>|</li>
        <li><a href='./tor.html'>Tor</a></li>
      </ul>
    </nav>
    <br /><br />
    <p>This page is generate from simple <a href='#'>bash script</a></p>
    <p>Last update: $lastupdate</p>"

html_foot='
  </body>
</html>'

relays_metrics="https://metrics.torproject.org/rs.html#details"

relays_url="https://onionoo.torproject.org/details"
tr="tr -d '\"'"

##declare -a relays=('DDF6B1924744E9CE89DE1E834E21F25E06CC33F4' '22F0AA861A74EBB7B4A3EC3EA9E28D6750F477E0') 

## relays var is pseudo list, one fingerprint by line
## this var is treated as file with the duck <()
relays="DDF6B1924744E9CE89DE1E834E21F25E06CC33F4
3C5303669B20D09FF27434010A6B97320BF655C8
599F6D54BB5DC8DAF6F335699AF669B21FD1AF32"

tor_html="../tor.html"
rm $tor_html
cat <<EOF >> $tor_html
    $html_head
EOF

while read relay
do
  content=`curl -s $relays_url?lookup=$relay`
  relay_name=`echo $content |jq '.relays[] | .nickname' |$tr`
  relay_flags=`echo $content |jq '.relays[].flags[]' |$tr`
  relay_address=`echo $content | jq '.relays[].or_addresses[]' |$tr | cut -d: -f1`
  relay_port=`echo $content | jq '.relays[].or_addresses[]' |$tr | cut -d: -f2`
  relay_first_seen=`echo $content | jq '.relays[] | .first_seen' |$tr`
  relay_last_seen=`echo $content | jq '.relays[] | .last_seen' |$tr`
  relay_exit_policy=`echo $content | jq '.relays[].exit_policy[]' |$tr`
  relay_bandwidth_rate=`echo $content | jq '.relays[] | .bandwidth_rate' |$tr`
  relay_advertised_bandwidth=`echo $content | jq '.relays[] | .advertised_bandwidth' |$tr`
  bandwidth_rate=$(($relay_bandwidth_rate/1024**2))
  advertised_bandwidth=$(($relay_advertised_bandwidth/1024**2))
  cat <<EOF >> $tor_html
    <div class="tor_relay">
    <details style="border: none">
    <summary><h3>Relay: <a href="$relays_metrics/$relay" target="_blank">$relay</a></h3></summary>
    Relay Name: <span class="surligne"><code>$relay_name</code></span><br />
    Relay Flags: <span class="surligne"><code>$relay_flags</code></span><br />
    OR Address: <span class="surligne"><code>$relay_address</code></span><br />
    OR Port: <span class="surligne"><code>$relay_port</code></span><br />
    Relay Started: <span class="surligne"><code>$relay_first_seen</code></span><br />
    Relay Last update: <span class="surligne"><code>$relay_last_seen</code></span><br />
    Exit Policy: <span class="surligne"><code>$relay_exit_policy</code></span><br />
    Relay Bandwidth Rate: <span class="surligne"><code>$bandwidth_rate MB/s</code></span><br />
    Relay Advertised Bandwidth: <span class="surligne"><code>$advertised_bandwidth MB/s</code></span><br />
    </details>
    </div>
EOF
done < <(echo "$relays")

cat <<EOF >> ../tor.html
  $html_foot
EOF
            

Simply install the cron rule:
echo "*/10 * * * * (cd <html_document_root>; bash get_relays.sh)" >> /etc/crontab