https://blog.tmlmt.com/hacking/deluge-vpn
I was able to co-opt this with a few changes for my needs.
Thanks OP

I am using OpenVPN on a headless Ubuntu server, but the procedure will be similar for any linux distro connecting through openvpn. My VPN provider is Private Internet Access (PIA).bluenote wrote:Could you post your procedure for binding deluge to the interface? I have this working (kind of) but it's very, very, manual.
I have to manually delete the default route for the openvpn tunnel as well which is a pain.
Thanks
Code: Select all
client
dev tun
proto udp
remote nl.privateinternetaccess.com 1194
resolv-retry infinite
nobind
persist-key
persist-tun
ca ca.crt
tls-client
remote-cert-tls server
auth-user-pass /etc/openvpn/login.conf
comp-lzo
verb 1
reneg-sec 0
auth-nocache
script-security 2
up /etc/openvpn/up.sh
Code: Select all
#!/bin/sh
/usr/sbin/service deluged stop
sudo -u vagrant -g vagrant sed -ie 's|\(\"listen_address\": \).*|\"listen_address\": \"'$4'\",|' /home/vagrant/.config/deluge/core.conf
sudo -u vagrant -g vagrant sed -ie 's|\(\"listen_interface\": \).*|\"listen_interface\": \"'$4'\",|' /home/vagrant/.config/deluge/core.conf
/usr/sbin/service deluged start
ScottyDelicious wrote:I am using OpenVPN on a headless Ubuntu server, but the procedure will be similar for any linux distro connecting through openvpn. My VPN provider is Private Internet Access (PIA).bluenote wrote:Could you post your procedure for binding deluge to the interface? I have this working (kind of) but it's very, very, manual.
I have to manually delete the default route for the openvpn tunnel as well which is a pain.
Thanks
I set up OpenVPN to connect on boot to the PIA Netherlands gateway. In my configuration file (/etc/openvpn/Netherlands.conf), there is a directive you can use called "up". This directive calls a script once the tunnel is up. My configuration file looks like this:The last line that says "up /etc/openvpn/up.sh" tells openvpn to run that script when the tunnel connection is up. I use this script to stop the deluged daemon, replace "listen_address" and "listen_interface" with the IP address assigned to me when the tunnel connected, then restart the deluged daemon.Code: Select all
client dev tun proto udp remote nl.privateinternetaccess.com 1194 resolv-retry infinite nobind persist-key persist-tun ca ca.crt tls-client remote-cert-tls server auth-user-pass /etc/openvpn/login.conf comp-lzo verb 1 reneg-sec 0 auth-nocache script-security 2 up /etc/openvpn/up.sh
Use vim or nano as sudo to edit /etc/openvpn/up.shMake sure you sudo chmod +x /etc/openvpn/up.sh to make it executable.Code: Select all
#!/bin/sh /usr/sbin/service deluged stop sudo -u vagrant -g vagrant sed -ie 's|\(\"listen_address\": \).*|\"listen_address\": \"'$4'\",|' /home/vagrant/.config/deluge/core.conf sudo -u vagrant -g vagrant sed -ie 's|\(\"listen_interface\": \).*|\"listen_interface\": \"'$4'\",|' /home/vagrant/.config/deluge/core.conf /usr/sbin/service deluged start
I am running deluge in a VM (using vagrant) for sandboxing and to ensure that the only connection to the VM is the VPN tunnel and the ports that vagrant exposes on the host machine to talk to the VM. I have setup the Upstart scripts to start the deluged daemon and deluge-web running as the user:group "vagrant". You would replace "vagrant" in the -u and -g flags with the user you have deluged running under, and of course point it to the correct location of the deluge configuration file "core.conf". For me, the configuration file for deluge is located at "/home/vagrant/.config/deluge/core.conf"
The script uses sed (Unix Stream Editor) to find a regular expression ("listen_address": {plus whatever follows to the end of this line}) and replace it with "listen_address": "the.IP.assigned.by.PIA", which is stored in the variable "$4" (an openvpn convention). "sed -ie" tells sed to do an inline edit, meaning it will write the changes to the same file.
I also have a cron job running every 5 minutes checking to see if the VPN is up. If not, it restarts the openvpn service, which in turn stops deluge, updates the config automatically, binding deluge to the new VPN IP address, and restarts deluged.
When the VPN is down and the IP address is no longer available, deluge completely stops, so there is no deluge traffic ever going in or out on my ISP assigned IP address.
Let me know if you need more clarification.
Code: Select all
curl -L https://github.com/slingamn/namespaced-openvpn/raw/master/namespaced-openvpn > ~/.bin/namespaced-openvpn; chmod +x ~/.bin/namespaced-openvpn
Code: Select all
sudo namespaced-openvpn --config /etc/openvpn/client/pia.conf --cd /etc/openvpn/client --daemon
sudo ip netns exec protected sudo -u "$USER" deluged
Code: Select all
sudo ip netns exec protected sudo -u "$USER"
Code: Select all
sudo ip netns exec protected sudo -u "$USER" deluge-console
Code: Select all
sudo pkill openvpn
Code: Select all
Sudo systemctl start openvpn-client@pia.conf
Code: Select all
sudo systemctl stop openvpn-client@pia.conf
https://github.com/slingamn/namespaced-openvpnThe network namespace functionality of Linux provides, in effect, additional isolated copies of the entire kernel networking stack. The idea behind namespaced-openvpn is this: the openvpn process itself can run in the root namespace, but its tunnel interface tun0 can be transferred into a new, protected network namespace. This new namespace will have the loopback adapter lo and tun0 as its only interfaces, and all non-localhost traffic will be routed over tun0. The openvpn process is not disrupted by this because it communicates with tun0 via the file descriptor it opened to /dev/net/tun, which is unaffected by the change of namespace.
As long as sensitive applications are correctly launched within the new, isolated namespace, all of the enumerated issues are systematically resolved:
Route injection is impossible because NetworkManager, dhclient, etc. are running in the root namespace, so they can respond to any change in the external network environment without affecting the protected namespace. In essence, this is a separation of concerns: the root namespace is tasked with maintaining connectivity, including interfacing with untrusted DHCP and DNS servers, while the protected namespace is tasked with maintaining privacy.
"Port Fail" is blocked because the protected namespace has no routing exception for the remote gateway: every packet must go to tun0.
Asymmetric routing attacks, IPv6 leaks, and DNS leaks are all blocked because the protected namespace has no access to any physical interface.
The openvpn process can freely restart because it runs in the root namespace, which has unmodified routes --- so its DNS request for the remote, and then its handshake with the resulting remote IP, all use eth0.
This approach has some further strengths:
It does not require any configuration changes to the root namespace, e.g., recreating eth0 as a virtual bridge.
Non-sensitive applications are free to use the physical interfaces, which may have better bandwidth or latency characteristics.
A namespaced-openvpn instance can peacefully coexist with another OpenVPN connection in the root namespace, without any concerns about conflicting private IPv4 addresses and routes. (Use the --nobind option to prevent the second openvpn process from trying and failing to reuse port 1194 in the root namespace.)
openvpn can be stopped and started without exposing processes in the protected namespace. If tun0 goes away, those processes don't revert to using a physical interface; instead, they have no connectivity at all.
Code: Select all
#!/bin/bash
sudo namespaced-openvpn --config /etc/openvpn/client/pia.conf --cd /etc/openvpn/client --daemon
sudo ip netns exec protected sudo -u "$USER" deluged
client_id=`head -n 100 /dev/urandom | sha256sum | tr -d " -"`
while ! [[ $(sudo ip netns exec protected sudo -u "$USER" ip addr | grep tun0) ]]; do sleep 1; done
json=`sudo ip netns exec protected sudo -u "$USER" curl "http://209.222.18.222:2000/?client_id=$client_id" 2>/dev/null`
if ! [ "$json" == "" ]; then
sudo ip netns exec protected sudo -u "$USER" deluge-console "config --set listen_ports ($(echo ${json:8:5}), $(echo ${json:8:5}))"
fi
x=1
while [[ "$json" == "" && $x -le 5 ]]; do
json=`sudo ip netns exec protected sudo -u "$USER" curl "http://209.222.18.222:2000/?client_id=$client_id" 2>/dev/null`
if ! [ "$json" == "" ]; then
sudo ip netns exec protected sudo -u "$USER" deluge-console "config --set listen_ports ($(echo ${json:8:5}), $(echo ${json:8:5}))"
fi
sleep 1
x=$(($x+1))
done
Code: Select all
sudo mkdir /etc/systemd/system/deluged.service.d
echo "[Service]
ExecStart=
ExecStart=sudo ip netns exec protected sudo -u deluge /usr/bin/deluged -d
ExecStartPre=sudo /home/martin/.bin/namespaced-openvpn --config /etc/openvpn/client/pia.conf --cd /etc/openvpn/client --daemon
ExecStartPre=/usr/bin/sleep 5
ExecStopPost=sudo /usr/bin/pkill openvpn" | sudo tee /etc/systemd/system/deluged.service.d/vpn.conf
sudo systemctl daemon-reload
Code: Select all
User=
User=martin