Why Bypass my VPN and how to solve it automatically? Using VPN can make your life easier and more complicated at the same time. Find out how to bypass, tunnel-split, or make specific exceptions to your current VPN connection.
Why Bypass VPN? I have had troubles with email traffic in the past and my email address was blacklisted in some occasions because I was using a VPN connection. Keep in mind some ports that are used by your email provider might not be supported by your VPN service and thus cause problems when using email whilst you are connected to your VPN. I wanted to bypass the VPN connection for exactly that reason so that the email traffic is routed directly to the email server and does not use the VPN tunnel. Depending on the VPN service you are using, this may be an option already built-in but in my case my VPN service does not offer exclusions, VPN bypass, or alike.
I was frustrated that my otherwise very neatly VPN service does not offer anything to bypass certain traffic that I want to exclude from the VPN tunnel. That made me write this automated bypass route.
Some of you may find this useful. It is written in AppleScript to make use of the on-board tools that Mac OSX is equipped with-no additional software needed.
You can use and adopt the following code with the native applescript environment your MacBook is equipped with. Open the AppleScript Editor and paste the code in the editor and amend the variables myURL and MyEmailApp to your needs. This code will beep if it adds the route so I that have an audible feedback of what is/was happening. Delete the beep if you do not like this.
`
(* BYPASS VPN and start Email App
By UweTheSailor on 25. Nov. 2022
This script will add a route to your Mac to bypass the VPN for this URL (email, web, or else) and opens an app after
it added the exception. The script automatically resolves the IP address from a specific URL and the Gateway IP from current
settings and adds automatically a route to the routing table of your machine to bypass the VPN connection for this specific URL and
opens my email client app (Airmail).
*)
-- Put your URL that you want to bypass your VPN connection here:
set myURL to "hotmail-or-else.com"
-- Put the name of your App that you want to open here:
set myEmailApp to "YourEmailAppOrElse"
-- Resolve the URL into IP Adress using a shell script:
set myip3 to do shell script "host " & myURL & " |grep address"
-- no error handling if DNS does not work!
set AppleScript's text item delimiters to " "
set MyURL_IP to (text item 4 of myip3)
-- The Variable "MyURL_IP" is your correct IP of the current URL!
-- return MyURL_IP
-- Get the router / gateway IP address from shell script:
set GatewayAdr to (do shell script "ipconfig getpacket en0 | grep 'router'")
-- Extract Gatway IP Adress from string - use trim with delimiters:
set AppleScript's text item delimiters to "{"
set IP1 to (text item 2 of GatewayAdr)
set AppleScript's text item delimiters to "}"
set IPAdrGatway to (text item 1 of IP1)
-- The Variable "IPAdrGatway" is your correct IP of the current gateway!
-- return IPAdrGatway
set RouteTable to (do shell script "netstat -rn") -- refresh the variable with actual data from current routing table!
if RouteTable does not contain MyURL_IP & " " & IPAdrGatway then
do shell script "sudo route -nv add " & MyURL_IP & " " & IPAdrGatway
beep
delay 1 -- wait one sec to take over the new settings
-- Refresh the routing table entries:
set RouteTable to (do shell script "netstat -rn")
-- Check if route was added and open Airmail App:
if RouteTable contains MyURL_IP & " " & IPAdrGatway then
-- open Arimail App only if not running yet:
tell application myEmailApp
activate
end tell
else
-- Error handling
display dialog "The route could not be verified and was not added!" with icon caution with title "Cannot verify the route!"
end if
else if RouteTable contains MyURL_IP & " " & IPAdrGatway then
tell application myEmailApp
activate
end tell
end if
`
Yours truly, Uwe
Not much to be honest!