Controlling the bridge with the Stem library

The bridge is controlled using the Stem library, which communicates with the Tor process over the Tor control protocol. The setup is managed in the BeagleBridge class. After establishing a connection with the Tor process, this class registers two event listeners for the Bandwidth and Configuration changed event. The bandwidth event is triggered each second and reports, via the print_bw callback, the bytes used in the last second. This information is used to draw the bandwidth graph. The following callback function shows how the callback interacts with the LCD:

def make_bw_callback(test,lcd):
  '''Returns a callback function for the bandwidth event'''
  def print_bw(event):
    '''Obtains the bandwidth used from the last second from the
       bridge, normalizes it to the total bandwidth, and draw 
       that information to the display'''
    up = int(test.get_up_ratio(event.written))
    down = int(test.get_down_ratio(event.read))
    lcd.display_graph(up, down)

  return print_bw

Note

For those who want to dive deeper into the Stem library and the Tor control protocol, the Stem library has thorough online documentation and examples (https://stem.torproject.org/tutorials.html). The control protocol, for those who want an even more in-depth look, is located at https://gitweb.torproject.org/torspec.git?a=blob_plain;hb=HEAD;f=control-spec.txt.

The Configuration Changed event is the callback to inform the process that the bridge's configuration was changed. This occurs when the bandwidth knob is adjusted, which causes the update_rate method to be called, that sends a command to the bridge to update the configuration. The end result is that by adjusting the knob, you directly affect the bandwidth limits on your bridge. When the callback occurs, it will display the new bandwidth rates on the LCD so that you know that your limit has changed. This callback is shown as follows:

def make_conf_callback(lcd):
  '''Returns a callback function for the configuration changed
     event'''
  def conf_changed(event):
    '''Reads the new bandwidth rates from the bridge and draws
       that information to the display'''
    rate = str(int(event.config['RelayBandwidthRate']) / 1024)
    burst = str(int(event.config['RelayBandwidthBurst']) / 1024)
    lcd.display_rates(rate, burst)

  return conf_changed

The main section of the Python script performs the class instantiation and also displays a one-time splash screen to the LCD. It will show the total bytes transmitted by the bridge, the number of established circuits, and the last 24 bytes of the bridge's fingerprint. The bridge controller will run forever, while the bandwidth knob is at a nonzero value. When you dial the knob to zero, the program will exit and the LCD will be filled with blocks.

Finally, to run the bridge controller, execute the following command, with the first parameter being the location of the speed test results:

sudo python beaglebridge.py ~/speedtest.txt &

To avoid running as root, you'll have to manipulate user groups and permissions. The Tor process runs as the debian-tor user, and the Adafruit library, which enables device tree overlays on your behalf, needs to run at a user level that has the permission to enable these features. You can create a custom group and a user that is in the debian-tor group and then give that group permission to modify the device tree files to not run as root.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.74.66