How to route internet traffic via `Clash for Windows` (Ping from Python code is not working)

Viewed 2631
from os import system
system("ping www.twitter.com")
system("ping www.yahoo.com")
system("ping www.facebook.com")

I am in China, and Twitter and Facebook are banned here. I can open them in the browser using Clash for Windows software.

I have to download tweets from Twitter. So I need to ping the websites using Python to get tweets. I cannot ping the websites though.

How do I make my Python code use the Clash for Windows.

Output of the above code:

Pinging www.twitter.com [108.160.169.186] with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Request timed out.

Ping statistics for 108.160.169.186:
    Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),

Pinging new-fp-shed.wg1.b.yahoo.com [180.222.102.201] with 32 bytes of data:
Reply from 180.222.102.201: bytes=32 time=258ms TTL=42
Reply from 180.222.102.201: bytes=32 time=229ms TTL=42
Reply from 180.222.102.201: bytes=32 time=230ms TTL=42
Request timed out.

Ping statistics for 180.222.102.201:
    Packets: Sent = 4, Received = 3, Lost = 1 (25% loss),
Approximate round trip times in milli-seconds:
    Minimum = 229ms, Maximum = 258ms, Average = 239ms

Pinging www.facebook.com [69.63.184.14] with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Request timed out.

Ping statistics for 69.63.184.14:
    Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),

OS: Windows 10 (updated to latest edition). Using PyCharm as my IDE.

7 Answers

You said in comment that you are using Clash, however Clash is not a VPN:

Clash - A rule-based tunnel in Go.

Features:

  • Local HTTP/HTTPS/SOCKS server with authentication support
  • VMess, Shadowsocks, Trojan, Snell protocol support for remote connections
  • Built-in DNS server that aims to minimize DNS pollution attack impact, supports DoH/DoT upstream and fake IP.
  • Rules based off domains, GEOIP, IP CIDR or ports to forward packets to different nodes
  • Remote groups allow users to implement powerful rules. Supports automatic fallback, load balancing or auto select node based off latency
  • Remote providers, allowing users to get node lists remotely instead of hardcoding in config
  • Netfilter TCP redirecting. Deploy Clash on your Internet gateway with iptables.
  • Comprehensive HTTP RESTful API controller

source: https://github.com/Dreamacro/clash

I'm not sure exactly how it works, my current understanding is that it allows you to use proxies.

Although it also has TUN mode as a "Premium Feature" (not sure what does it mean, I see no option to buy "Premium") which may work similarly to VPN, but I'm not sure:

Premium Features:

  • TUN mode on macOS, Linux and Windows. Doc
  • Match your tunnel by Script
  • Rule Provider

Clash for Windows which you try to use, is GUI for Clash.

Documentation is available, but it is only in Chinese:

https://github.com/Fndroid/clash-win-docs-new

screenshot:

Clash for Windows

I tried to use it, but I don't know how to configure it. It didn't work at all.


I see many possible solutions:

  1. You can switch to proper VPN. Paid VPNs are more recommended than free ones. Cost is typically 3-10$/month depending on offer. Alternatively you can try to setup OpenVPN on your own VPS, it may be a bit cheaper, but not necessarily.
  2. You can configure your script to use proxy. Libraries like requests support it: Proxies with Python 'Requests' module
  3. You can try to read Clash for Windows documentation to see if it is possible what you try to achieve. Maybe it is enough to turn on System Proxy as visible on screenshot above?
  4. You can try to configure Clash in TUN mode. In my opinion it may be more difficult than solutions 1 and 2. If you prefer this way, I suggest to read Clash documentation thoroughly: https://github.com/Dreamacro/clash/wiki/premium-core-features#tun-device

If you can afford it, I recommend solution 1 in most use-cases. I understand that you prefer free solution, however servers are not free, somebody needs to incur costs of running them (hardware, electricity etc.)

There is a variety of Python libraries that can help you. openpyn is one of them. Firstly, call this command in your terminal for setup:

sudo openpyn --init

After which all your Internet traffic can be redirected to a VPN server using the following command:

openpyn us

The command above is the default, to transfer all the traffic to the US. Other locations can be chosen, see the link above for more info.

As soon as your traffic is redirected, you are free to see banned sites as you did:

from os import system
system("ping www.twitter.com")
system("ping www.facebook.com")

When you have VPN running and active then it should redirect all traffic via VPN server. If it is not redirecting all traffic, then maybe it was configured to redirect only webrowser traffic. Also it is possible that you aren't using true VPN, but a proxy.

Please share which VPN are you using, it will help us to help you.


You can just start VPN manually and then try executing your Python code.

Alternatively you can control when your VPN is running from Python. It requires different library for each VPN provider. Please tell us which VPN do you use.

This is working example for NordVPN using nordvpn_switcher package:


import time 
from nordvpn_switcher import initialize_VPN,rotate_VPN,terminate_VPN

initialize_VPN(save=1,area_input=['complete rotation'])

for i in range(1):
    rotate_VPN()

    from os import system
    system("ping www.twitter.com")
    system("ping www.yahoo.com")
    system("ping www.facebook.com")


    print('\nDo whatever you want here (e.g. pinging). Pausing for 10 seconds...\n')
    time.sleep(10)

terminate_VPN()

Tor uses SOCKS 5 proxy server to give anonymity. In case you're using Tor, there are two ways to use SOCKS 5 proxy, one is to configure it at application level(browser, python code etc..), another to configure at Network Interface level. If you're using Tor, simply follow this answer, socks server is running at localhost:9050 by default - How to make python Requests work via socks proxy Since you haven't done anything and it's already working, I guess you're using tunnel-based VPN. In this case, it should work automatically. In your case, ping could be blocked by the VPN provider.

Ping uses ICMP protocol while HTTP/HTTPS uses TCP protocol in the Transport layer (in OSI model). They are very different things, and one working doesn't guarantee other working. In many cases, ping is blocked, by the server or other middlewares that don't support ICMP protocols. Most cloud providers don't support ICMP protocols in their networking components, for ex. Azure doesn't, for their Load Balancers. So, instead of trying ping, you should try the real http request. Following is sample code

import requests
r = requests.get("www.google.com")

You can use this code:

from ping3 import ping

p=ping("example.com")

print(p)

Let's try to isolate this problem if it's Python or network-related.

Does it work if you run ping in the shell directly?

ping twitter.com

It's possible that your VPN has a setting that blocks Pings from the internet. It should be one of the configurations in the Firewall.

Related