There is an amazing piece of software - redsocks. What is basically does is listen for TCP (also has some UDP and DNS support), and if anything comes its way, it redirects through a SOCKS4/5 server or HTTP Connect/Relay proxy.
On linux all one needs is to make iptables DNAT desired connections to the ip/port the redsocks daemon is listening.
My only issue was that the redsocks package not available under Fedora/Red Hat Enterprise Linux. I figured debian has a package with systemctl integration so thought to leverage that work. But Debian GNU Linux uses .deb package format, while fedora uses .rpm.
Fedora comes with the `alien` package that can convert between deb an rpm. So I downloaded the deb from Debian testing and gave it a try:
$ sudo alien -r redsocks_0.4+dfsg-2_amd64.deb
Warning: Skipping conversion of scripts in package redsocks: postinst postrm prerm
Warning: Use the --scripts parameter to include the scripts.
$ yum install redsocks-0.4+dfsg-3.x86_64.rpm
Transaction check error:
file / from install of redsocks-0.4+dfsg-3.x86_64 conflicts with file from package filesystem-3.2-20.el7.x86_64
file /lib from install of redsocks-0.4+dfsg-3.x86_64 conflicts with file from package filesystem-3.2-20.el7.x86_64
file /usr/sbin from install of redsocks-0.4+dfsg-3.x86_64 conflicts with file from package filesystem-3.2-20.el7.x86_64
file /etc/init.d from install of redsocks-0.4+dfsg-3.x86_64 conflicts with file from package chkconfig-1.3.61-5.el7.x86_64
Dang! `alien` got libevent dependency just right but wants to take ownership of a couple of dirs that should not be generally touched. After some searching I found the solution - install `rpmrebuild` to remove the unnecessary dirs that conflict. Don't worry, actual files are not removed. Only the directory entries inside the RPM content.
$ rpmrebuild -pe redsocks-0.4+dfsg-3.x86_64.rpm
Do you want to continue ? (y/N) y
`rpmrebuild` in this way will present you with a text editor. Find '%files' section and remove all directory entries not specific to redsocks (i.e. "/", "/lib", ...).
Finally create the user that will daemon run as (part of debian postinstall script):
$ sudo adduser --system --home /var/run/redsocks --no-create-home -U redsocks
And that's all. Now do `yum install /home/user/rpmbuild/RPMS/x86_64/redsocks-0.4+dfsg-3.x86_64.rpm` and that should be it. Configuration can be found in /etc/redsocks.conf and `systemctl enable redsocks; systemctl start redsocks`.