File size: 1,058 Bytes
40cd288
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eefd1b7
 
40cd288
eefd1b7
40cd288
eefd1b7
 
40cd288
 
eefd1b7
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import requests
import socket

TOR_PROXY = "socks5h://127.0.0.1:9050"

def get(url, **kwargs):
    proxies = {"http": TOR_PROXY, "https": TOR_PROXY}
    return requests.get(url, proxies=proxies, **kwargs)

def post(url, **kwargs):
    proxies = {"http": TOR_PROXY, "https": TOR_PROXY}
    return requests.post(url, proxies=proxies, **kwargs)

def new_identity():
    """Minta IP baru dari Tor"""
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.connect(('127.0.0.1', 9051))
        s.sendall(b'AUTHENTICATE ""\r\nSIGNAL NEWNYM\r\n')

def patch_all():
    """
    Patch requests.Session secara global — semua requests otomatis lewat Tor.
    Tidak patch socket level untuk menghindari segfault.
    """
    _original_init = requests.Session.__init__

    def _patched_init(self, *args, **kwargs):
        _original_init(self, *args, **kwargs)
        self.proxies.update({"http": TOR_PROXY, "https": TOR_PROXY})

    requests.Session.__init__ = _patched_init
    print("[proxy] ✅ patch_all() aktif — semua requests lewat Tor")