leekspin.server

Functions for creating mock [bridge-]server-descriptors.

generateServerDescriptor(nick, fingerprint, timestamp, ipv4, ipv6, port, vers, protocols, uptime, bandwidth, extraInfoHexDigest, onionKeyLine, signingKeyLine, publicNTORKey, bridge=True)[source]

Generate an @type [bridge-]server-descriptor.

Parameters:
  • nick (str) – The router’s nickname.
  • fingerprint (str) – The SHA-1 digest of the router’s public identity key.
  • timestamp (str) – An ISO 8601 timestamp, with a space as the separator.
  • ipv4 (str) – The IP address for router’s main ORAddress.
  • ipv6 (str) – Any IPv6 ORAddress es for this router.
  • port (str) – The port for the router’s main ORAddress.
  • vers (str) – One of SERVER_VERSIONS.
  • protocols (str) – The OR protocol versions supported by this router.
  • uptime (str) – The current uptime for this router.
  • bandwidth (str) – A bandwidth history line for this router.
  • extraInfoHexDigest (str) – The SHA-1 digest of the router’s @type [bridge-]extrainfo descriptor, before the extrainfo descriptor is signed.
  • onionKeyLine (str) – An onion-key line.
  • signingKeyLine (str) – A signing-key line.
  • publicNTORKey (str) – An ntor-onion-key.
  • bridge (bool) – If True, create a Bridge descriptor. If False, create a Relay descriptor.
makeProtocolsLine(version=None)[source]

Generate an appropriate [bridge-]server-descriptor protocols line.

Parameters:version (str) – One of SERVER_VERSIONS.
Return type:str
Returns:An @type [bridge-]server-descriptor protocols line.
makeExtraInfoDigestLine(hexdigest, version)[source]

Create a line to embed the hex SHA-1 digest of the extrainfo.

Parameters:
  • hexdigest (str) – Should be the hex-encoded (uppercase) output of the SHA-1 digest of the generated extrainfo document (this is the extra-info descriptor, just without the signature at the end). This is the same exact digest which gets signed by the OR server identity key, and that signature is appended to the extrainfo document to create the extra-info descriptor.
  • version (str) – One of SERVER_VERSIONS.
Return type:

str

Returns:

An @type [bridge-]server-descriptor extra-info-digest line.

makeFingerprintLine(fingerprint, version=None)[source]

Generate an appropriate [bridge-]server-descriptor fingerprint line.

For example, for tor-0.2.3.25 and prior versions, this would look like:

opt fingerprint D4BB C339 2560 1B7F 226E 133B A85F 72AF E734 0B29

Parameters:
  • fingerprint (str) – A public key fingerprint in groups of four, separated by spaces.
  • version (str) – One of SERVER_VERSIONS.
Return type:

str

Returns:

An @type [bridge-]server-descriptor fingerprint line.

makeBandwidthLine(variance=30)[source]

Create a random bandwidth line with some plausible bandwidth burst variance.

From dir-spec.txt §2.1 “Router descriptors”:

"bandwidth" bandwidth-avg bandwidth-burst bandwidth-observed NL

[Exactly once]

  Estimated bandwidth for this router, in bytes per second.  The
  "average" bandwidth is the volume per second that the OR is willing
  to sustain over long periods; the "burst" bandwidth is the volume
  that the OR is willing to sustain in very short intervals.  The
  "observed" value is an estimate of the capacity this relay can
  handle.  The relay remembers the max bandwidth sustained output over
  any ten second period in the past day, and another sustained input.
  The "observed" value is the lesser of these two numbers.

The observed bandwidth, in this function, is taken as some random value, bounded between 20KB/s and 2MB/s. For example, say:

>>> import math
>>> variance = 25
>>> observed = 180376
>>> percentage = float(variance) / 100.
>>> percentage
0.25

The variance in this context is the percentage of the observed bandwidth, which will be added to the observed bandwidth, and becomes the value for the burst bandwidth:

>>> burst = observed + math.ceil(observed * percentage)
>>> assert burst > observed

This doesn’t do much, since the burst bandwidth in a real @type [bridge-]server-descriptor is reported by the OR; this function mostly serves to avoid generating completely-crazy, totally-implausible bandwidth values. The average bandwidth value is then just the mean value of the other two.

Parameters:variance (int) – The percent of the fake observed bandwidth to increase the burst bandwidth by.
Return type:str
Returns:A bandwidth line for an @type [bridge-]server-descriptor.
makeHSDirLine(version)[source]

This line doesn’t do much… all the cool kids are HSDir s these days.

Parameters:version (str) – One of SERVER_VERSIONS.
Return type:str
Returns:An @type [bridge-]server-descriptor hidden-service-dir line.