Tor là mạng ẩn danh phổ biến nhất. Kết hợp với Privoxy để chuyển SOCKS5 proxy thành HTTP proxy, bạn có thể dùng Tor cho mọi ứng dụng. Bài viết này hướng dẫn setup Tor + Privoxy proxy bằng Docker chỉ trong vài phút.

Tor và Privoxy là gì?

Tor

Tor (The Onion Router) là mạng ẩn danh, mã hóa traffic qua nhiều node. Mỗi request đi qua 3 relay ngẫu nhiên, không ai có thể theo dõi bạn từ đầu đến cuối.

Privoxy

Privoxy là web proxy có tính năng filtering. Tor chỉ hỗ trợ SOCKS5, nhưng nhiều ứng dụng chỉ hiểu HTTP proxy. Privoxy đóng vai trò chuyển đổi: HTTP proxy → SOCKS5 → Tor.

Kiến trúc

Application → Privoxy (HTTP :8118) → Tor (SOCKS5 :9050) → Tor Network → Internet
  • Port 8118: Privoxy HTTP Proxy
  • Port 9050: Tor SOCKS5 Proxy
  • Port 9051: Tor Control Port

1. Cài đặt nhanh bằng Docker

Pull image

# Pull từ Docker Hub
docker pull dockage/tor-privoxy

# Hoặc build từ source
docker build -t tor-privoxy https://github.com/Quochung1st-dev/tor-privoxy.git#main

Chạy bằng Docker Compose

# docker-compose.yml
version: "3.8"

services:
  tor-privoxy:
    image: dockage/tor-privoxy:latest
    container_name: tor-privoxy
    ports:
      - "9050:9050"   # SOCKS5
      - "9051:9051"   # Control port
      - "8118:8118"   # HTTP proxy
    restart: unless-stopped
# Khởi động
docker-compose up -d

# Xem logs
docker-compose logs -f tor-privoxy

Chạy bằng Docker Run

docker run --name tor-privoxy -d \
  -p 9050:9050 \
  -p 9051:9051 \
  -p 8118:8118 \
  --restart unless-stopped \
  dockage/tor-privoxy:latest

2. Kiểm tra Proxy hoạt động

Test bằng curl

# Qua HTTP proxy (Privoxy)
curl -x http://127.0.0.1:8118 https://check.torproject.org/api/ip

# Qua SOCKS5 (Tor trực tiếp)
curl --socks5 127.0.0.1:9050 https://check.torproject.org/api/ip

# Kiểm tra IP thật vs IP qua Tor
echo "Real IP:" && curl -s https://api.ipify.org
echo "Tor IP:" && curl -x http://127.0.0.1:8118 -s https://api.ipify.org

Nếu 2 IP khác nhau → proxy hoạt động đúng.

Kiểm tra Tor status

# Check Tor bootstrap status
docker exec tor-privoxy sh -c "curl -s http://127.0.0.1:9051"

# Hoặc xem logs
docker logs tor-privoxy --tail 20

3. Sử dụng Proxy

Trong Terminal / Shell

# Set environment variables
export http_proxy="http://127.0.0.1:8118"
export https_proxy="http://127.0.0.1:8118"

# Hoặc cho SOCKS5
export ALL_PROXY="socks5://127.0.0.1:9050"

# Test
wget -q -O - https://api.ipify.org

# Unset
unset http_proxy https_proxy ALL_PROXY

Trong Python

import requests

# HTTP proxy
proxies = {
    'http': 'http://127.0.0.1:8118',
    'https': 'http://127.0.0.1:8118',
}

# SOCKS5 (cần install PySocks: pip install pysocks)
proxies_socks = {
    'http': 'socks5://127.0.0.1:9050',
    'https': 'socks5://127.0.0.1:9050',
}

response = requests.get('https://api.ipify.org', proxies=proxies)
print(f"Tor IP: {response.text}")

Trong Node.js

const axios = require('axios');
const { SocksProxyAgent } = require('socks-proxy-agent');

// SOCKS5
const agent = new SocksProxyAgent('socks5://127.0.0.1:9050');

axios.get('https://api.ipify.org', { httpsAgent: agent })
  .then(res => console.log('Tor IP:', res.data));

// HTTP proxy
const HttpProxyAgent = require('http-proxy-agent');
const httpAgent = new HttpProxyAgent('http://127.0.0.1:8118');

axios.get('https://api.ipify.org', { httpsAgent: httpAgent })
  .then(res => console.log('Tor IP:', res.data));

Trên Browser (Firefox)

  • Settings → General → Network Settings → Settings
  • Chọn Manual proxy configuration
  • HTTP Proxy: 127.0.0.1 Port: 8118
  • SOCKS Host: 127.0.0.1 Port: 9050
  • Check Proxy DNS when using SOCKS v5

4. Đổi IP (New Identity)

Tor tự động đổi IP mỗi 10 phút. Nhưng nếu muốn đổi ngay:

# Cách 1: Restart container
docker restart tor-privoxy

# Cách 2: Gửi signal NEWNYM qua control port
docker exec tor-privoxy sh -c \
  'echo -e "AUTHENTICATE \"\"\nSIGNAL NEWNYM\nQUIT" | nc 127.0.0.1 9051'

# Cách 3: Script tự động đổi IP
#!/bin/bash
# rotate-tor-ip.sh
docker exec tor-privoxy sh -c \
  'echo -e "AUTHENTICATE \"\"\nSIGNAL NEWNYM\nQUIT" | nc 127.0.0.1 9051'
sleep 5
NEW_IP=$(curl -x http://127.0.0.1:8118 -s https://api.ipify.org)
echo "New Tor IP: $NEW_IP"

5. Custom Tor Configuration

Mở rộng cấu hình Tor bằng cách mount file vào /etc/torrc.d/:

# docker-compose.yml
version: "3.8"

services:
  tor-privoxy:
    image: dockage/tor-privoxy:latest
    container_name: tor-privoxy
    ports:
      - "9050:9050"
      - "9051:9051"
      - "8118:8118"
    volumes:
      - ./torrc.d:/etc/torrc.d:ro
    restart: unless-stopped
# ./torrc.d/custom.conf

# Chỉ đi qua relay ở specific countries
ExitNodes {us},{de},{nl}
StrictNodes 1

# Exclude nodes
ExcludeNodes {cn},{ru}

# Log level
Log notice stdout

# Bandwidth rate (KB/s)
RelayBandwidthRate 500 KB
RelayBandwidthBurst 1000 KB

6. Security Hardening

  • Giới hạn access: Chỉ bind proxy trên internal network.
  • Firewall: Block port 8118, 9050 từ outside.
  • Không dùng cho P2P: Tor không thiết kế cho BitTorrent.
# Chỉ bind localhost
docker run --name tor-privoxy -d \
  -p 127.0.0.1:9050:9050 \
  -p 127.0.0.1:9051:9051 \
  -p 127.0.0.1:8118:8118 \
  --restart unless-stopped \
  dockage/tor-privoxy:latest

# Hoặc dùng UFW
ufw deny 8118
ufw deny 9050
ufw deny 9051

7. Monitoring

# Xem logs
docker logs -f tor-privoxy

# Check container status
docker ps | grep tor-privoxy

# Resource usage
docker stats tor-privoxy

# Test connection
curl -x http://127.0.0.1:8118 -s https://check.torproject.org/ | grep "Congratulations"

8. Troubleshooting

Proxy không kết nối được

# Kiểm tra container đang chạy
docker ps | grep tor

# Xem logs
docker logs tor-privoxy --tail 50

# Restart
docker restart tor-privoxy

Tor chậm

  • Tor chậm hơn internet thường — đó là trade-off của ẩn danh.
  • Dùng ExitNodes để chọn exit gần target server.
  • Tránh download file lớn qua Tor.

Không đổi được IP

# Kiểm tra control port
docker exec tor-privoxy sh -c "nc -z 127.0.0.1 9051 && echo OK"

# NEWNYM có rate limit: 10 giây giữa mỗi lần đổi
# Đợi 10 giây rồi thử lại

Kết Luận

Tor + Privoxy trong Docker là cách nhanh nhất để có proxy ẩn danh. Chỉ cần pull image và chạy.

  • HTTP proxy qua Privoxy (port 8118).
  • SOCKS5 qua Tor trực tiếp (port 9050).
  • Dùng cho curl, Python, Node.js, browser.
  • Đổi IP bằng SIGNAL NEWNYM.

Source code: Quochung1st-dev/tor-privoxy trên GitHub.

Chào các bạn mình là Quốc Hùng , mình sinh ra thuộc cung song tử ,song tử luôn khẳng định chính mình ,luôn luôn phấn đấu vượt lên phía trước ,mình sinh ra và lớn lên tại vùng đất võ cổ truyền ,đam mê của mình là coder ,ngày đi học tối về viết blog ...