Connecting to an IRC server behind a firewall? I feel your pain!
So I use Xchat daily and connect to a private IRC server to talk with my colleagues. I also have a BIP server in the office to record all of the IRC transcripts, this way I never miss any conversations regardless of the time of day. Because the BIP server is behind a firewall on the companies network I can’t access it from the outside. For the past year I’ve been working around this by connecting to my companies firewall via ssh and creating a SOCKS tunnel then simply directing xchat to talk through my local SOCKS proxy.
To do this , open a terminal and issue:
ssh -CND <LOCAL_IP_ADDRESS>:<PORT> <USER>@<SSH HOST>
Ex: ssh -CND 192.168.1.44:9999 email@example.com
Starting ssh with -CND:
‘D’ Specifies a local “dynamic” application-level port forwarding. This works by allocating a socket to listen to port on the local side, optionally bound to the specified bind_address. It also adds compression to the datastream ‘C’ and the ‘N’ is a safeguard which protects the user from executing remote commands.
192.168.1.44 is my IPv4 address
9999 is the local port i’m going to open and direct traffic through
After the SSH tunnel is open I now need to launch xchat, navigate to Settings -> Preferences -> Network Setup and configure xchat to use my local IP (192.168.1.44) and local port (9999) then press OK then Reconnect.
I should now be able to connect to the IRC server behind the firewall. Usually I run through this process a few times a day, so it becomes somewhat of a tedious annoyance after a while.
Recently I finished a cool python3 script that does all of this in quick command.
The following code will do the following:
1.) identify the ipv4 address of the interface device you specify
2.) configure xchat.conf to use the new ipv4 address and port specified by the user
3.) open the ssh tunnel using the SSH -CND command from above
4.) launch xchat and connect to your server (assuming you have it set to auto connect)
To use it simply run
$./xchat.py -i <interface> -p <port>
ex: $./xchat.py -i wlan0 -p 9999
the user can select wlan0 or eth0 and of course their desired port. When your done with the tunnel simply issue <Ctrl-C> to kill it and wala!
#!/usr/bin/env python3 #Sean Feole 2012, # #xchat proxy wrapper, for those of you that are constantly on the go: # -------------- What does it do? ------------------ # Creates a SSH Tunnel to Proxy through and updates your xchat config # so that the user does not need to muddle with program settings import signal import shutil import sys import subprocess import argparse import re import time proxyhost = "myhost.company.com" proxyuser = "sfeole" localusername = "sfeole" def get_net_info(interface): """ Obtains your IPv4 address """ myaddress = subprocess.getoutput("/sbin/ifconfig %s" % interface)\ .split("\n").split()[5:] if myaddress == "CAST": print ("Please Confirm that your Network Device is Configured") sys.exit() else: return (myaddress) def configure_xchat_config(Proxy_ipaddress, Proxy_port): """ Reads your current xchat.conf and creates a new one in /tmp """ in_file = open("/home/%s/.xchat2/xchat.conf" % localusername, "r") output_file = open("/tmp/xchat.conf", "w") for line in in_file.readlines(): line = re.sub(r'net_proxy_host.+', 'net_proxy_host = %s' % Proxy_ipaddress, line) line = re.sub(r'net_proxy_port.+', 'net_proxy_port = %s' % Proxy_port, line) output_file.write(line) output_file.close() in_file.close() shutil.copy("/tmp/xchat.conf", "/home/%s/.xchat2/xchat.conf" % localusername) def ssh_proxy(ProxyAddress, ProxyPort, ProxyUser, ProxyHost): """ Create SSH Tunnel and Launch Xchat """ ssh_address = "%s:%i" % (ProxyAddress, ProxyPort) user_string = "%s@%s" % (ProxyUser, ProxyHost) ssh_open = subprocess.Popen(["/usr/bin/ssh", "-CND", ssh_address, user_string], stdout=subprocess.PIPE, stdin=subprocess.PIPE) time.sleep(1) print ("") print ("Kill this tunnel with Ctrl-C") time.sleep(2) subprocess.call("xchat") stat = ssh_open.poll() while stat is None: stat = ssh_open.poll() def main(): """ Core Code """ parser = argparse.ArgumentParser() parser.add_argument('-i', '--interface', help="Select the interface you wish to use", choices=['eth0', 'wlan0'], required=True) parser.add_argument('-p', '--port', help="Select the internal port you wish to bind to", required=True, type=int) args = parser.parse_args() proxyip = (get_net_info("%s" % args.interface)) configure_xchat_config(proxyip, args.port) print (proxyip, args.port, proxyuser, proxyhost) ssh_proxy(proxyip, args.port, proxyuser, proxyhost) if __name__ == "__main__": sys.exit(main())
Refer to the launchpad address above for more info.