Created
September 9, 2012 08:23
-
-
Save yunazuno/3683319 to your computer and use it in GitHub Desktop.
Switch the proxy automatically
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
echo $_SERVER["REMOTE_ADDR"]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# | |
# ssh-autoproxy.py : Switch the proxy automatically | |
# | |
# When you are in the inside of a firewall, I connect to the server | |
# directly. But you are in the outside of a firewall, I connect to the | |
# server over proxy. | |
# | |
# Usage: | |
# $HOME/.ssh/config: | |
# | |
# # foo.example.com need to connect via proxy.example.com | |
# # if you are in the outside of 192.0.2.0/24 or 198.51.100.0/24 | |
# Host foo | |
# HostName foo.example.com | |
# ProxyCommand ssh-autoproxy.py proxy.example.com %h %p 192.0.2.0/24 198.51.100.0/24 | |
# | |
# | |
# ipaddr::branches/3144 is needed. Stable version of ipaddr is not | |
# suitable for pep 3144 | |
# @see http://code.google.com/p/ipaddr-py/wiki/Using3144 | |
# | |
import ipaddr | |
import urllib2 | |
import subprocess | |
import argparse | |
def get_wan_ip(check_url = "http://yunazuno.net/misc/ipaddr/ipaddr.php"): | |
"""Get the own global IP address | |
Get the own global IP address via specific url. | |
""" | |
handle = urllib2.urlopen(check_url, timeout=2) | |
data = handle.read(16) | |
try: | |
wanip = ipaddr.ip_address(data) | |
except ValueError: | |
wanip = ipaddr.ip_address("0.0.0.0") | |
return wanip | |
def is_inside_network(networks): | |
wanipaddr = get_wan_ip() | |
is_inside = False | |
for network_str in networks: | |
network = ipaddr.ip_network(network_str) | |
if wanipaddr in network: | |
is_inside = True | |
break | |
return is_inside | |
def main(): | |
## Parse arguments | |
parser = argparse.ArgumentParser() | |
parser.add_argument("proxy", help="proxy") | |
parser.add_argument("hostname", help="hostname") | |
parser.add_argument("port", help="port") | |
parser.add_argument('network', nargs='+', default=[], | |
help="Networks") | |
args = parser.parse_args() | |
networks = args.network | |
proxy = args.proxy | |
hostname = args.hostname | |
port = args.port | |
## Check the wan ipaddr: is inside or not | |
is_inside = is_inside_network(networks) | |
## connect | |
if is_inside: | |
# connect directly | |
cmd = "nc {0} {1}".format(hostname, port) | |
else: | |
# connect over proxy | |
cmd = "ssh {0} nc {1} {2}".format(proxy, hostname, port) | |
subprocess.call(cmd, shell=True) | |
return | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment