bridgedb.proxy

Classes for finding and managing lists of open proxies.

downloadTorExits(proxyList, ipaddress, port=443, protocol=None)[source]

Run a script which downloads a list of Tor exit relays which allow their clients to exit to the given ipaddress and port.

Parameters:
Return type:

Deferred

Returns:

A deferred which will callback with a list, each item in the list is a string containing an IP of a Tor exit relay.

loadProxiesFromFile(filename, proxySet=None, removeStale=False)[source]

Load proxy IP addresses from a list of files.

Parameters:
  • filename (str) –

    A filename whose path can be either absolute or relative to the current working directory. The file should contain the IP addresses of various open proxies, one per line, for example:

    11.11.11.11
    22.22.22.22
    123.45.67.89
    
  • proxySet (None or ProxySet.) – If given, load the addresses read from the files into this ProxySet.
  • removeStale (bool) – If True, remove proxies from the proxySet which were not listed in any of the files. (default: False)
Returns:

A list of all the proxies listed in the **files* (regardless of whether they were added or removed).

class ProxySet(proxies={})[source]

Bases: _abcoll.MutableSet

A collections.MutableSet for storing validated IP addresses.

Inheritance diagram of ProxySet

Initialise a ProxySet.

Parameters:proxies (A tuple, list, dict, or set.) –

Optionally, initialise with an iterable, proxies. For each item in that iterable, item must either:

  1. be a string or int representing an IP address, or,
  2. be another iterable, whose first item satisfies #1.
_exitTag = 'exit_relay'

A tag to apply to IP addresses within this ProxySet which are known Tor exit relays.

proxies

All proxies in this set, regardless of tags.

exitRelays

Get all proxies in this ProxySet tagged as Tor exit relays.

Return type:set
Returns:A set of all known Tor exit relays which are contained within this ProxySet.
_getErrorMessage(x=None, y=None)[source]

Make an error message describing how this class works.

addProxies(proxies, tag=None)[source]

Add proxies to this set.

This calls add() for each item in the iterable proxies. Each proxy, if added, will be tagged with a current timestamp.

Parameters:
  • proxies (A tuple, list, dict, or set.) –

    An iterable. For each item in that iterable, item must either:

    1. be a string or int representing an IP address, or,
    2. be another iterable, whose first item satisfies #1.
  • tag – An optional value to link to all untagged proxies. If None, it will be a timestamp (seconds since epoch, as a float) for when the proxy was first added to this set.
addExitRelays(relays)[source]

Add proxies to this set.

This calls add() for each item in the iterable proxies. Each proxy, if added, will be tagged with a current timestamp.

Parameters:
  • proxies (A tuple, list, dict, or set.) –

    An iterable. For each item in that iterable, item must either:

    1. be a string or int representing an IP address, or,
    2. be another iterable, whose first item satisfies #1.
  • tag – An optional value to link to all untagged proxies. If None, it will be a timestamp (seconds since epoch, as a float) for when the proxy was first added to this set.
getTag(ip)[source]

Get the tag for an ip in this ProxySet, if available.

Parameters:ip (basestring or int) – The IP address to obtain the tag for.
Return type:None or basestring or int
Returns:The tag for that ip, iff ip exists in this ProxySet and it has a tag.
getAllWithTag(tag)[source]

Get all proxies in this ProxySet with a given tag.

Parameters:tag (basestring) – A tag to search for.
Return type:set
Returns:A set of all proxies which are contained within this ProxySet which are also tagged with tag.
firstSeen(ip)[source]

Get the timestamp when ip was first seen, if available.

Parameters:ip (basestring or int) – The IP address to obtain a timestamp for.
Return type:float or None
Returns:The timestamp (in seconds since epoch) if available. Otherwise, returns None.
isExitRelay(ip)[source]

Check if ip is a known Tor exit relay.

Parameters:ip (basestring or int) – The IP address to check.
Return type:boolean
Returns:True if ip is a known Tor exit relay; False otherwise.
replaceProxyList(proxies, tag=None)[source]

Clear everything and add all proxies.

Parameters:proxies (A tuple, list, dict, or set.) –

An iterable. For each item in that iterable, item must either:

  1. be a string or int representing an IP address, or,
  2. be another iterable, whose first item satisfies #1.
_assigned = ('__name__', '__doc__')
add(ip, tag=None)[source]

Add an ip to this set, with an optional tag.

This has no effect if the ip is already present. The ip is only added if it passes the checks in isIPAddress().

Parameters:
  • ip (basestring or int) – The IP address to add.
  • tag – An optional value to link to ip. If not given, it will be a timestamp (seconds since epoch, as a float) for when ip was first added to this set.
Return type:

bool

Returns:

True if ip is in this set; False otherwise.

copy()[source]

Initialise a ProxySet.

Parameters:proxies (A tuple, list, dict, or set.) –

Optionally, initialise with an iterable, proxies. For each item in that iterable, item must either:

  1. be a string or int representing an IP address, or,
  2. be another iterable, whose first item satisfies #1.
contains(ip)[source]

x.__contains__(y) <==> y in x.

Parameters:ip (basestring or int) – The IP address to check.
Return type:boolean
Returns:True if ip is in this set; False otherwise.
discard(ip)[source]

Entirely remove ip from this set.

Parameters:ip (basestring or int) – The IP address to remove.
remove(other)[source]

Entirely remove ip from this set.

Parameters:ip (basestring or int) – The IP address to remove.
difference(other)[source]

Return the difference of two or more sets as a new set.

(i.e. all elements that are in this set but not the others.)

issubset(other)[source]

Report whether another set contains this set.

issuperset(other)[source]

Report whether this set contains another set.

intersection(other)[source]

Return the intersection of two or more sets as a new set.

(i.e. elements that are common to all of the sets.)

symmetric_difference(other)[source]

Return the symmetric difference of two sets as a new set.

(i.e. all elements that are in exactly one of the sets.)

union(other)[source]

Return the union of sets as a new set.

(i.e. all elements that are in either set.)

_abc_cache = <_weakrefset.WeakSet object>
_abc_negative_cache = <_weakrefset.WeakSet object>
_abc_negative_cache_version = 25
_abc_registry = <_weakrefset.WeakSet object>
class ExitListProtocol[source]

Bases: twisted.internet.protocol.ProcessProtocol

A Protocol for get-exit-list.

Attr boolean connected:
 True if our transport is connected.
Attr transport:If twisted.internet.reactor.spawnProcess() is called with an instance of this class as it’s protocol, then spawnProcess() will return this transport.

Create a protocol for downloading a list of current Tor exit relays.

Variables:
  • exitlist – A MutableSet containing the IP addresses of known Tor exit relays which can reach our public IP address.
  • data (list) – A list containing a bytes object for each chuck of data received from the transport.
  • deferred – A deferred which will callback with the exitlist when the process has ended.
Parameters:

script (string) – The full pathname of the script to run.

childConnectionLost(childFD)[source]

See t.i.protocol.ProcessProtocol.childConnectionLost().

connectionMade()[source]

Called when a connection is made.

This may be considered the initializer of the protocol, because it is called when the connection is completed. For clients, this is called once the connection to the server has been established; for servers, this is called after an accept() call stops blocking and a socket has been received. If you need to send any greeting or initial message, do it here.

errReceived(data)[source]

Some data was received from stderr.

outReceived(data)[source]

Some data was received from stdout.

outConnectionLost()[source]

This will be called when stdout is closed.

parseData()[source]

Parse all data received so far into our <bridgedb.proxy.ProxySet> exitlist.

processEnded(reason)[source]

Called when the child process exits and all file descriptors associated with it have been closed.

processExited(reason)[source]

This will be called when the subprocess exits.