How to Access a Foreign API Blocked in China

Here’s a common scenario: your application needs to access an overseas API via curl, but the endpoint is unreachable from within China. How do you solve this?

This can be handled through a simple proxy setup. Below are several methods to achieve this:

Method 1: Using a Squid HTTP Proxy (Ideal for Browsers and HTTP Tools)
Steps (assuming the overseas server runs Ubuntu):

Install Squid

sudo apt update
sudo apt install squid -y

Edit the configuration file

sudo nano /etc/squid/squid.conf

Add the following lines at the end of the configuration file:

http_access allow all
http_port 3128

Restart Squid

sudo systemctl restart squid
sudo systemctl enable squid

Open port 3128 (firewall settings)

sudo ufw allow 3128

Access from China (using curl as an example)

curl -x http://<your-overseas-server-ip>:3128 https://v1.cnop.net

Method 2: Using socat for Port Forwarding (Ideal for Single Endpoint Forwarding)

If you only need to access a single API endpoint, such as https://v1.cnop.net, you can set up a direct forward on your overseas server.
Steps:

Install socat

sudo apt update
sudo apt install socat -y

Start the forward (maps a port on the overseas server to the API)

sudo socat TCP-LISTEN:8888,reuseaddr,fork TLS:api.cnop.net:443,verify=0

This maps https://v1.cnop.net to http://your-server-ip:8888

Access from China:

    curl -k http://your-server-ip:8888

Method 3: Using an SSH Dynamic Proxy (Ideal for Temporary Use, Handles All Traffic)

If you only access the endpoint occasionally, you can also set up a SOCKS proxy via SSH:
Run this command on your domestic computer (assuming you use Linux/Mac):

ssh -D 1080 user@overseas-server-ip

For Windows, you can configure a SOCKS5 proxy using tools like PuTTY or MobaXterm.

Then configure your domestic software (curl, Postman, browser) to use the SOCKS5 proxy:

socks5://127.0.0.1:1080

Advanced: Nginx Reverse Proxy (Ideal for API Mapping, Building a Self-Hosted Relay Service)

If you want to create a domestically accessible relay address for https://v1.cnop.net, you can set up a reverse proxy using NGINX on your overseas server:

Install NGINX:

sudo apt install nginx -y

Configure the proxy (/etc/nginx/sites-enabled/default or a new config file):

server {
listen 8080;
location / {
proxy_pass https://v1.cnop.net;
proxy_ssl_verify off;
}
}

Restart NGINX:

sudo systemctl restart nginx

Access from China:

curl http://your-server-ip:8080

Leave a Comment

Your email address will not be published.