What's new

OpenVPN - Is it possible for only specfic ports to use VPN?

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

Weggy

Regular Contributor
Hi,

I've managed to setup my OpenVPN client settings so the specfic device I need connect to the internet via the VPN. However, what I would like to do is for that device to only use the VPN when using a specific port e.g. TCP 1080, UDP 6881.

Many Thanks for all help that might come my way :)
 
However, what I would like to do is for that device to only use the VPN when using a specific port e.g. TCP 1080, UDP 6881.

To ensure destination ports TCP 1080 and UDP 6881 from a specific LAN device are routed via say VPN Client 3, then ENABLE Selective Policy routing in the VPN Client 3 GUI, then create

/jffs/scripts/nat-start

Code:
#!/bin/sh

sleep 10

VPN_ID=3                    # <-- Edit for appropriate VPN Client
DEVICE="172.16.1.1"         # <-- Edit for appropriate LAN Device(s) i.e. CSV format
DPORTS_TCP="1080"           # <-- Edit Destination TCP port(s) i.e. CSV format
DPORTS_UDP="6881"           # <-- Edit Destination UDP port(s) i.e. CSV format

# Create required RPDB fwmark routing rule.....
ip rule del fwmark 0x${VPN_ID}000/0x${VPN_ID}000 2> /dev/null
ip rule add fwmark 0x${VPN_ID}000/0x${VPN_ID}000 table 11${VPN_ID} prio 999${VPN_ID}
ip route flush cache

# Add specific destination TCP ports to be routed via the VPN Client
iptables -t mangle -D PREROUTING -i br0 --src $DEVICE -p tcp -m multiport --dport $DPORTS_TCP -j MARK --set-mark 0x${VPN_ID}000/0x${VPN_ID}000
iptables -t mangle -A PREROUTING -i br0 --src $DEVICE -p tcp -m multiport --dport $DPORTS_TCP -j MARK --set-mark 0x${VPN_ID}000/0x${VPN_ID}000

# Add specific destination UDP ports to be routed via the VPN Client
iptables -t mangle -D PREROUTING -i br0 --src $DEVICE -p udp -m multiport --dport $DPORTS_UDP -j MARK --set-mark 0x${VPN_ID}000/0x${VPN_ID}000
iptables -t mangle -A PREROUTING -i br0 --src $DEVICE -p udp -m multiport --dport $DPORTS_UDP -j MARK --set-mark 0x${VPN_ID}000/0x${VPN_ID}000
 
Last edited:

Similar threads

Latest threads

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top