Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,53 @@ python3 main.py --log-level DEBUG # Show detailed logs
python3 main.py -c /path/to/config.json # Use a different config file
python3 main.py --install-cert # Install MITM CA certificate and exit
python3 main.py --no-cert-check # Skip automatic CA install check on startup
python3 main.py --scan # Scan Google IPs and find the fastest one
```

> **Auto-install:** On startup (MITM mode), the proxy automatically checks if the CA certificate is trusted and attempts to install it. Use `--no-cert-check` to skip this. If auto-install fails (e.g. needs elevation), run `python main.py --install-cert` manually or follow Step 6 above.

### Scanning for the Fastest Google IP

If your current `google_ip` in `config.json` is blocked or slow, you can scan to find a faster one:

```bash
python3 main.py --scan
```

This will:
1. Probe 27 candidate Google IPs in parallel
2. Measure latency from your network
3. Display results in a table
4. Recommend the fastest IP
5. Exit with exit code 0 if at least one IP is reachable, 1 otherwise

**Example output:**
```
Scanning 27 Google frontend IPs
SNI: www.google.com
Timeout: 4s per IP
Concurrency: 8 parallel probes

IP LATENCY STATUS
-------------------- ------------ -------------------------
216.239.32.120 42ms OK
216.239.34.120 45ms OK
216.239.36.120 52ms OK
142.250.80.142 timeout timeout
...

Result: 15 / 27 reachable

Top 3 fastest IPs:
1. 216.239.32.120 (42ms)
2. 216.239.34.120 (45ms)
3. 216.239.36.120 (52ms)

Recommended: Set "google_ip": "216.239.32.120" in config.json
```

After scanning, update your `config.json` with the recommended IP and restart the proxy.

---

## Architecture
Expand Down Expand Up @@ -353,6 +396,7 @@ MasterHttpRelayVPN/
├── mitm.py # On-the-fly TLS interception
├── cert_installer.py # Cross-platform CA installer (Windows/macOS/Linux + Firefox)
├── codec.py # Content-Encoding decoder (gzip/deflate/br/zstd)
├── google_ip_scanner.py # Scanner to find the fastest reachable Google IP
├── constants.py # Tunable defaults and shared data
└── logging_utils.py # Colored, aligned log formatter
```
Expand Down
44 changes: 44 additions & 0 deletions README_FA.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,53 @@ python3 main.py --log-level DEBUG
python3 main.py -c /path/to/config.json
python3 main.py --install-cert # نصب گواهی CA و خروج
python3 main.py --no-cert-check # رد شدن از بررسی خودکار گواهی
python3 main.py --scan # اسکن IP های Google و یافتن سریع‌ترین
```

> **نصب خودکار:** هنگام اجرا در حالت `apps_script`، برنامه به‌طور خودکار بررسی می‌کند که آیا گواهی CA قابل اعتماد است یا نه و در صورت نیاز آن را نصب می‌کند. اگر نصب خودکار ناموفق بود (مثلاً نیاز به دسترسی مدیر دارد)، می‌توانید دستور `python main.py --install-cert` را اجرا کنید یا مراحل مرحله ۶ را دنبال کنید.

### اسکن کردن برای یافتن سریع‌ترین IP گوگل

اگر `google_ip` فعلی در `config.json` بلاک شده یا آهسته است، می‌توانید اسکن کنید تا سریع‌ترین آن را پیدا کنید:

```bash
python3 main.py --scan
```

این دستور:
1. ۲۷ IP برای fronting Google را به‌صورت موازی بررسی می‌کند
2. تأخیر (latency) از شبکه شما را اندازه می‌گیرد
3. نتایج را در جدول نمایش می‌دهد
4. سریع‌ترین IP را پیشنهاد می‌دهد
5. اگر حداقل یک IP در دسترس باشد کد خروج ۰، ورنه ۱ را برمی‌گرداند

**نمونه خروجی:**
```
Scanning 27 Google frontend IPs
SNI: www.google.com
Timeout: 4s per IP
Concurrency: 8 parallel probes

IP LATENCY STATUS
-------------------- ------------ -------------------------
216.239.32.120 42ms OK
216.239.34.120 45ms OK
216.239.36.120 52ms OK
142.250.80.142 timeout timeout
...

Result: 15 / 27 reachable

Top 3 fastest IPs:
1. 216.239.32.120 (42ms)
2. 216.239.34.120 (45ms)
3. 216.239.36.120 (52ms)

Recommended: Set "google_ip": "216.239.32.120" in config.json
```

پس از اسکن، مقدار `google_ip` در `config.json` را با IP پیشنهادی به‌روزرسانی کنید و پراکسی را دوباره راه‌اندازی کنید.

---

## معماری
Expand Down Expand Up @@ -297,6 +340,7 @@ MasterHttpRelayVPN/
├── mitm.py # ساخت و مدیریت گواهی‌ها
├── cert_installer.py # نصب خودکار CA در ویندوز/مک/لینوکس + فایرفاکس
├── codec.py # رمزگشای Content-Encoding (gzip/deflate/br/zstd)
├── google_ip_scanner.py # اسکنر IP های Google برای یافتن سریع‌ترین
├── constants.py # مقادیر پیش‌فرض قابل تنظیم
└── logging_utils.py # فرمت‌دهنده‌ی لاگ رنگی و منظم
```
Expand Down
42 changes: 42 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@

from cert_installer import install_ca, is_ca_trusted
from constants import __version__
from gas_stats_fetcher import create_fetcher as create_gas_stats_fetcher
from lan_utils import log_lan_access
from google_ip_scanner import scan_sync
from logging_utils import configure as configure_logging, print_banner
from mitm import CA_CERT_FILE
from proxy_server import ProxyServer
Expand Down Expand Up @@ -92,6 +94,16 @@ def parse_args():
action="store_true",
help="Skip the certificate installation check on startup.",
)
parser.add_argument(
"--scan",
action="store_true",
help="Scan Google IPs to find the fastest reachable one and exit.",
)
parser.add_argument(
"--gas-stats",
action="store_true",
help="Fetch and display Google Apps Script deployment stats, then exit.",
)
return parser.parse_args()


Expand Down Expand Up @@ -192,6 +204,36 @@ def main():
ok = install_ca(CA_CERT_FILE)
sys.exit(0 if ok else 1)

# ── Google IP Scanner ──────────────────────────────────────────────────
if args.scan:
setup_logging("INFO")
front_domain = config.get("front_domain", "www.google.com")
_log = logging.getLogger("Main")
_log.info(f"Scanning Google IPs (fronting domain: {front_domain})")
ok = scan_sync(front_domain)
sys.exit(0 if ok else 1)

# ── Google Apps Script Stats ────────────────────────────────────────
if args.gas_stats:
setup_logging("INFO")
_log = logging.getLogger("Main")
script_id = config.get("script_ids") or config.get("script_id")
if not script_id:
_log.error("No script_id configured in config.json")
sys.exit(1)
if isinstance(script_id, list):
_log.info("Fetching stats for %d script IDs…", len(script_id))
script_id = script_id[0] # Use first for stats
else:
_log.info("Fetching Google Apps Script deployment stats…")
try:
fetcher = create_gas_stats_fetcher(deployment_id=script_id)
fetcher.fetch_and_log_stats()
except ImportError as e:
_log.error(f"Cannot fetch stats: {e}")
sys.exit(1)
sys.exit(0)

setup_logging(config.get("log_level", "INFO"))
log = logging.getLogger("Main")

Expand Down
5 changes: 5 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ zstandard>=0.22.0

# Optional: Better network interface detection for LAN sharing
netifaces>=0.11.0

# Optional: Google Apps Script stats fetching (google_auth_stats)
google-auth>=2.25.0
google-auth-httplib2>=0.2.0
google-auth-oauthlib>=1.1.0
42 changes: 42 additions & 0 deletions src/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,48 @@
TCP_CONNECT_TIMEOUT = 10


# ── Google API settings ─────────────────────────────────────────────────────────
# Google Apps Script deployment stats
GAS_STATS_TIMEOUT = 10 # Timeout for GAS API requests (seconds)
GAS_STATS_API_BASE = "https://www.googleapis.com/apps_script/v1"
GAS_EXECUTION_API_BASE = "https://www.googleapis.com/execution/v1/projects"
GAS_CREDENTIALS_FILE = "credentials.json" # OAuth2 credentials file path


# ── Google IP Scanner settings ──────────────────────────────────────────────
GOOGLE_SCANNER_TIMEOUT = 4 # Timeout per IP probe (seconds)
GOOGLE_SCANNER_CONCURRENCY = 8 # Parallel probes
# Candidate Google frontend IPs for scanning (multiple ASNs and regions)
CANDIDATE_IPS: tuple[str, ...] = (
"216.239.32.120",
"216.239.34.120",
"216.239.36.120",
"216.239.38.120",
"142.250.80.142",
"142.250.80.138",
"142.250.179.110",
"142.250.185.110",
"142.250.184.206",
"142.250.190.238",
"142.250.191.78",
"172.217.1.206",
"172.217.14.206",
"172.217.16.142",
"172.217.22.174",
"172.217.164.110",
"172.217.168.206",
"172.217.169.206",
"34.107.221.82",
"142.251.32.110",
"142.251.33.110",
"142.251.46.206",
"142.251.46.238",
"142.250.80.170",
"142.250.72.206",
"142.250.64.206",
"142.250.72.110",
)

# ── Response cache ────────────────────────────────────────────────────────
CACHE_MAX_MB = 50
CACHE_TTL_STATIC_LONG = 3600 # images / fonts
Expand Down
Loading