toripchanger


Nametoripchanger JSON
Version 1.2.0 PyPI version JSON
download
home_pagehttps://github.com/DusanMadar/TorIpChanger
SummaryPython powered way to get a unique Tor IP
upload_time2024-03-24 13:55:55
maintainerNone
docs_urlNone
authorDusan Madar
requires_python>=3.5
licenseMIT
keywords change tor ip
VCS
bugtrack_url
requirements requests stem
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![Build Status](https://github.com/DusanMadar/TorIpChanger/actions/workflows/main.yml/badge.svg)](https://github.com/DusanMadar/TorIpChanger/actions/workflows/main.yml)
[![Coverage Status](https://coveralls.io/repos/github/DusanMadar/TorIpChanger/badge.svg?branch=master)](https://coveralls.io/github/DusanMadar/TorIpChanger?branch=master)
[![PyPI version](https://badge.fury.io/py/toripchanger.svg)](https://badge.fury.io/py/toripchanger)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black)


# TorIpChanger
A simple workaround for [Tor IP changing behavior](https://stem.torproject.org/faq.html#how-do-i-request-a-new-identity-from-tor):

> An important thing to note is that a new circuit does not necessarily mean a new IP address. Paths are randomly selected based on heuristics like speed and stability. There are only so many large exits in the Tor network, so it's not uncommon to reuse an exit you have had previously.

## Installation
```bash
pip install toripchanger
```


## Dependencies
TorIpChanger *assumes you have installed and setup Tor and Privoxy*, for example following steps mentioned in these tutorials:

* [A step-by-step guide how to use Python with Tor and Privoxy](https://gist.github.com/DusanMadar/8d11026b7ce0bce6a67f7dd87b999f6b)
* [Crawling anonymously with Tor in Python](http://sacharya.com/crawling-anonymously-with-tor-in-python/)
  * [Alternative link (Gist)](https://gist.github.com/KhepryQuixote/46cf4f3b999d7f658853) for "Crawling anonymously with Tor in Python"

Or, when using Docker, simply use https://github.com/dperson/torproxy. Refer to
[Dockerfile](Dockerfile) and [docker-compose.yaml](docker-compose.yaml) for more details.

```console
dm@lnx:~/code/toripchanger$ docker-compose up -d
Starting toripchanger_tor-proxy_1 ... done
Starting toripchanger_tor-ip-changer_1 ... done
dm@lnx:~/code/toripchanger$ curl http://localhost:8080/changeip/
{"error":"","newIp":"1.2.3.4"}
```


## Usage
### Basic examples
With TorIpChanger you can define how often a Tor IP can be reused:
```python
from toripchanger import TorIpChanger

# Tor IP reuse is prohibited.
tor_ip_changer_0 = TorIpChanger(reuse_threshold=0)
current_ip = tor_ip_changer_0.get_new_ip()

# Current Tor IP address can be reused after one other IP was used (default setting).
tor_ip_changer_1 = TorIpChanger(local_http_proxy='127.0.0.1:8888')
current_ip = tor_ip_changer_1 .get_new_ip()

# Current Tor IP address can be reused after 5 other Tor IPs were used.
tor_ip_changer_5 = TorIpChanger(tor_address="localhost", reuse_threshold=5)
current_ip = tor_ip_changer_5.get_new_ip()
```

### Remote Tor control
Sometimes, typically while using Docker, you may want to control a Tor instance
which doesn't run on localhost. To do this, you have two options.

#### Use `0.0.0.0` as control address
Set `ControlPort` to `0.0.0.0:9051` in your `torrc` file  and set `tor_address` when initializing TorIpChanger
```python
from toripchanger import TorIpChanger

tor_ip_changer = TorIpChanger(tor_address="172.17.0.2")
current_ip = tor_ip_changer.get_new_ip()
```

Though, Tor is not very happy about it (and rightly so) and will warn you
>You have a ControlPort set to accept connections from a non-local address. This means that programs not running on your computer can reconfigure your Tor. That's pretty bad, since the controller protocol isn't encrypted! Maybe you should just listen on 127.0.0.1 and use a tool like stunnel or ssh to encrypt remote connections to your control port.

Also, you have to set either `CookieAuthentication` or `HashedControlPassword` otherwise `ControlPort` will be closed
>You have a ControlPort set to accept unauthenticated connections from a non-local address. This means that programs not running on your computer can reconfigure your Tor, without even having to guess a password. That's so bad that I'm closing your ControlPort for you. If you need to control your Tor remotely, try enabling authentication and using a tool like stunnel or ssh to encrypt remote access.

Please note `ControlListenAddress` config is **OBSOLETE** and Tor (tested with 0.3.3.7) will ignore it and log the following message
> ```
> [warn] Skipping obsolete configuration option 'ControlListenAddress'
> ```

While the config itself is obsolte, its [documentation](https://people.torproject.org/~sysrqb/webwml/docs/tor-manual.html.en#ControlListenAddress) (**not the official documentation!**) concerning the risks related to exposing `ControlPort` on `0.0.0.0` is still valid
> We strongly recommend that you leave this alone unless you know what you’re doing, since giving attackers access to your control listener is really dangerous.

#### Use `toripchanger_server`
[toripchanger_server](scripts/toripchanger_server) script starts a simple web server which allows you to change Tor' IP remotely using an HTTP get request to `/changeip/`. The response body is always

```
{
    "newIp": "1.2.3.4",
    "error": ""
}
```
with an appropriate status (`error` is an empty string when all is good).

Changing Tor' IP may not be instantaneous (especially when combined with a high `reuse_threshold`) and hence your client should use a reasonable timeout (e.g. at least 60s).

`toripchanger_server` takes all arguments required to initialize `TorIpChanger` plus `--server-host` and `--server-port`, for more details see the usage below.

```
usage: toripchanger_server [-h] [--server-host SERVER_HOST]
                           [--server-port SERVER_PORT]
                           [--reuse-threshold REUSE_THRESHOLD]
                           [--local-http-proxy LOCAL_HTTP_PROXY]
                           [--tor-password TOR_PASSWORD]
                           [--tor-address TOR_ADDRESS] [--tor-port TOR_PORT]
                           [--new-ip-max-attempts NEW_IP_MAX_ATTEMPTS]

optional arguments:
  -h, --help            show this help message and exit
  --server-host SERVER_HOST
                        TorIpChanger server host (default: 0.0.0.0)
  --server-port SERVER_PORT
                        TorIpChanger server port (default: 8080)
  --reuse-threshold REUSE_THRESHOLD
                        Number of IPs to use before reusing the current one
                        (default: 1)
  --local-http-proxy LOCAL_HTTP_PROXY
                        Local proxy IP and port (default: 127.0.0.1:8118)
  --tor-password TOR_PASSWORD
                        Tor controller password (default: "")
  --tor-address TOR_ADDRESS
                        IP address or resolvable hostname of the Tor
                        controller (default: 127.0.0.1)
  --tor-port TOR_PORT   Port number of the Tor controller (default: 9051)
  --new-ip-max-attempts NEW_IP_MAX_ATTEMPTS
                        Get new IP attempts limit (default: 10)

```

To be able to change Tor IP remotely with `toripchanger_server`

  1. run `pip install toripchanger[server]` in your container
  2. start `toripchanger_server` (on the same host where Tor runs)
  3. expose the port `toripchanger_server` runs on to Docker host (or other containers)
  4. test changing IP works, e.g. `curl http://localhost:8080/changeip/`

An example [docker-compose.yaml](docker-compose.yaml) can be used for testing
as instructed in section [Dependencies](#dependencies).

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/DusanMadar/TorIpChanger",
    "name": "toripchanger",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.5",
    "maintainer_email": null,
    "keywords": "change tor ip",
    "author": "Dusan Madar",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/38/18/c5d968b00017fae3896ea9da59875d9b7893214432e7cead71c0460b212e/toripchanger-1.2.0.tar.gz",
    "platform": "linux",
    "description": "[![Build Status](https://github.com/DusanMadar/TorIpChanger/actions/workflows/main.yml/badge.svg)](https://github.com/DusanMadar/TorIpChanger/actions/workflows/main.yml)\n[![Coverage Status](https://coveralls.io/repos/github/DusanMadar/TorIpChanger/badge.svg?branch=master)](https://coveralls.io/github/DusanMadar/TorIpChanger?branch=master)\n[![PyPI version](https://badge.fury.io/py/toripchanger.svg)](https://badge.fury.io/py/toripchanger)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black)\n\n\n# TorIpChanger\nA simple workaround for [Tor IP changing behavior](https://stem.torproject.org/faq.html#how-do-i-request-a-new-identity-from-tor):\n\n> An important thing to note is that a new circuit does not necessarily mean a new IP address. Paths are randomly selected based on heuristics like speed and stability. There are only so many large exits in the Tor network, so it's not uncommon to reuse an exit you have had previously.\n\n## Installation\n```bash\npip install toripchanger\n```\n\n\n## Dependencies\nTorIpChanger *assumes you have installed and setup Tor and Privoxy*, for example following steps mentioned in these tutorials:\n\n* [A step-by-step guide how to use Python with Tor and Privoxy](https://gist.github.com/DusanMadar/8d11026b7ce0bce6a67f7dd87b999f6b)\n* [Crawling anonymously with Tor in Python](http://sacharya.com/crawling-anonymously-with-tor-in-python/)\n  * [Alternative link (Gist)](https://gist.github.com/KhepryQuixote/46cf4f3b999d7f658853) for \"Crawling anonymously with Tor in Python\"\n\nOr, when using Docker, simply use https://github.com/dperson/torproxy. Refer to\n[Dockerfile](Dockerfile) and [docker-compose.yaml](docker-compose.yaml) for more details.\n\n```console\ndm@lnx:~/code/toripchanger$ docker-compose up -d\nStarting toripchanger_tor-proxy_1 ... done\nStarting toripchanger_tor-ip-changer_1 ... done\ndm@lnx:~/code/toripchanger$ curl http://localhost:8080/changeip/\n{\"error\":\"\",\"newIp\":\"1.2.3.4\"}\n```\n\n\n## Usage\n### Basic examples\nWith TorIpChanger you can define how often a Tor IP can be reused:\n```python\nfrom toripchanger import TorIpChanger\n\n# Tor IP reuse is prohibited.\ntor_ip_changer_0 = TorIpChanger(reuse_threshold=0)\ncurrent_ip = tor_ip_changer_0.get_new_ip()\n\n# Current Tor IP address can be reused after one other IP was used (default setting).\ntor_ip_changer_1 = TorIpChanger(local_http_proxy='127.0.0.1:8888')\ncurrent_ip = tor_ip_changer_1 .get_new_ip()\n\n# Current Tor IP address can be reused after 5 other Tor IPs were used.\ntor_ip_changer_5 = TorIpChanger(tor_address=\"localhost\", reuse_threshold=5)\ncurrent_ip = tor_ip_changer_5.get_new_ip()\n```\n\n### Remote Tor control\nSometimes, typically while using Docker, you may want to control a Tor instance\nwhich doesn't run on localhost. To do this, you have two options.\n\n#### Use `0.0.0.0` as control address\nSet `ControlPort` to `0.0.0.0:9051` in your `torrc` file  and set `tor_address` when initializing TorIpChanger\n```python\nfrom toripchanger import TorIpChanger\n\ntor_ip_changer = TorIpChanger(tor_address=\"172.17.0.2\")\ncurrent_ip = tor_ip_changer.get_new_ip()\n```\n\nThough, Tor is not very happy about it (and rightly so) and will warn you\n>You have a ControlPort set to accept connections from a non-local address. This means that programs not running on your computer can reconfigure your Tor. That's pretty bad, since the controller protocol isn't encrypted! Maybe you should just listen on 127.0.0.1 and use a tool like stunnel or ssh to encrypt remote connections to your control port.\n\nAlso, you have to set either `CookieAuthentication` or `HashedControlPassword` otherwise `ControlPort` will be closed\n>You have a ControlPort set to accept unauthenticated connections from a non-local address. This means that programs not running on your computer can reconfigure your Tor, without even having to guess a password. That's so bad that I'm closing your ControlPort for you. If you need to control your Tor remotely, try enabling authentication and using a tool like stunnel or ssh to encrypt remote access.\n\nPlease note `ControlListenAddress` config is **OBSOLETE** and Tor (tested with 0.3.3.7) will ignore it and log the following message\n> ```\n> [warn] Skipping obsolete configuration option 'ControlListenAddress'\n> ```\n\nWhile the config itself is obsolte, its [documentation](https://people.torproject.org/~sysrqb/webwml/docs/tor-manual.html.en#ControlListenAddress) (**not the official documentation!**) concerning the risks related to exposing `ControlPort` on `0.0.0.0` is still valid\n> We strongly recommend that you leave this alone unless you know what you\u2019re doing, since giving attackers access to your control listener is really dangerous.\n\n#### Use `toripchanger_server`\n[toripchanger_server](scripts/toripchanger_server) script starts a simple web server which allows you to change Tor' IP remotely using an HTTP get request to `/changeip/`. The response body is always\n\n```\n{\n    \"newIp\": \"1.2.3.4\",\n    \"error\": \"\"\n}\n```\nwith an appropriate status (`error` is an empty string when all is good).\n\nChanging Tor' IP may not be instantaneous (especially when combined with a high `reuse_threshold`) and hence your client should use a reasonable timeout (e.g. at least 60s).\n\n`toripchanger_server` takes all arguments required to initialize `TorIpChanger` plus `--server-host` and `--server-port`, for more details see the usage below.\n\n```\nusage: toripchanger_server [-h] [--server-host SERVER_HOST]\n                           [--server-port SERVER_PORT]\n                           [--reuse-threshold REUSE_THRESHOLD]\n                           [--local-http-proxy LOCAL_HTTP_PROXY]\n                           [--tor-password TOR_PASSWORD]\n                           [--tor-address TOR_ADDRESS] [--tor-port TOR_PORT]\n                           [--new-ip-max-attempts NEW_IP_MAX_ATTEMPTS]\n\noptional arguments:\n  -h, --help            show this help message and exit\n  --server-host SERVER_HOST\n                        TorIpChanger server host (default: 0.0.0.0)\n  --server-port SERVER_PORT\n                        TorIpChanger server port (default: 8080)\n  --reuse-threshold REUSE_THRESHOLD\n                        Number of IPs to use before reusing the current one\n                        (default: 1)\n  --local-http-proxy LOCAL_HTTP_PROXY\n                        Local proxy IP and port (default: 127.0.0.1:8118)\n  --tor-password TOR_PASSWORD\n                        Tor controller password (default: \"\")\n  --tor-address TOR_ADDRESS\n                        IP address or resolvable hostname of the Tor\n                        controller (default: 127.0.0.1)\n  --tor-port TOR_PORT   Port number of the Tor controller (default: 9051)\n  --new-ip-max-attempts NEW_IP_MAX_ATTEMPTS\n                        Get new IP attempts limit (default: 10)\n\n```\n\nTo be able to change Tor IP remotely with `toripchanger_server`\n\n  1. run `pip install toripchanger[server]` in your container\n  2. start `toripchanger_server` (on the same host where Tor runs)\n  3. expose the port `toripchanger_server` runs on to Docker host (or other containers)\n  4. test changing IP works, e.g. `curl http://localhost:8080/changeip/`\n\nAn example [docker-compose.yaml](docker-compose.yaml) can be used for testing\nas instructed in section [Dependencies](#dependencies).\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Python powered way to get a unique Tor IP",
    "version": "1.2.0",
    "project_urls": {
        "Homepage": "https://github.com/DusanMadar/TorIpChanger"
    },
    "split_keywords": [
        "change",
        "tor",
        "ip"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3cde4231a1a0eaa4d566c07536fe4b86d15946103915b557fb99418114ad8c6b",
                "md5": "22e4cd8f31930bfabc5811174163a1ff",
                "sha256": "54b747557ae94b89dcafafb45d9bd6b541de56a9ed32e449b1195e24ed7a56ea"
            },
            "downloads": -1,
            "filename": "toripchanger-1.2.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "22e4cd8f31930bfabc5811174163a1ff",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.5",
            "size": 8854,
            "upload_time": "2024-03-24T13:55:53",
            "upload_time_iso_8601": "2024-03-24T13:55:53.154293Z",
            "url": "https://files.pythonhosted.org/packages/3c/de/4231a1a0eaa4d566c07536fe4b86d15946103915b557fb99418114ad8c6b/toripchanger-1.2.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3818c5d968b00017fae3896ea9da59875d9b7893214432e7cead71c0460b212e",
                "md5": "fb27005d29f59f8c0e356d7c4ecda923",
                "sha256": "0bd8061d05d792c36fda96ce3c9c47e615d119d5dd9e1f46a003668d42141691"
            },
            "downloads": -1,
            "filename": "toripchanger-1.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "fb27005d29f59f8c0e356d7c4ecda923",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.5",
            "size": 12825,
            "upload_time": "2024-03-24T13:55:55",
            "upload_time_iso_8601": "2024-03-24T13:55:55.613910Z",
            "url": "https://files.pythonhosted.org/packages/38/18/c5d968b00017fae3896ea9da59875d9b7893214432e7cead71c0460b212e/toripchanger-1.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-24 13:55:55",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "DusanMadar",
    "github_project": "TorIpChanger",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "requests",
            "specs": []
        },
        {
            "name": "stem",
            "specs": []
        }
    ],
    "lcname": "toripchanger"
}
        
Elapsed time: 0.22467s