Example #1
0
    def update_proxy_settings(self, *args, **kwargs):
        """
        When the network configuration changes, this updates the SOCKS proxy
        settings based the current IP address(es)
        """
        # Open a SystemConfiguration preferences session:
        sc_prefs = SCPreferences()

        # We want to enable the server when our hostname is not on the corporate network:
        # BUG: This does not handle multi-homed systems well:
        current_address = socket.gethostbyname(socket.getfqdn())
        new_state       = not current_address.startswith('10.0.1.')

        logging.info(
            "Current address is now %s: SOCKS proxy will be %s" % (
                current_address,
                "Enabled" if new_state else "Disabled"
            )
        )

        try:
            sc_prefs.set_proxy(
                enable=new_state,
                protocol='SOCKS',
                server=self.socks_server,
                port=self.socks_port
            )
            sc_prefs.save()

            logging.info("Successfully updated SOCKS proxy setting")
        except RuntimeError, e:
            logging.error("Unable to set SOCKS proxy setting: %s" % e.message)
    def network_changed(self, *args, **kwargs):
        # Open a SystemConfiguration preferences session:
        sc_prefs = SCPreferences()

        # We want to enable the server when our hostname is not on the corporate network:
        current_address = socket.gethostbyname(socket.getfqdn())
        new_state       = not current_address.startswith('128.36.')

        self.logger.info("Current address is now %s: SOCKS proxy will be %s" % (current_address, "Enabled" if new_state else "Disabled"))

        try:
            sc_prefs.set_proxy(enable=new_state, protocol='SOCKS', server=self.socks_server, port=self.socks_port)
            sc_prefs.save()
            os.system("killall ssh")
            self.logger.info("Successfully updated SOCKS proxy setting")
        except RuntimeError, e:
            self.logger.error("Unable to set SOCKS proxy setting: %s" % e.message)
Example #3
0
    def update_proxy_settings(self, *args, **kwargs):
        """
        When the network configuration changes, this updates the SOCKS proxy
        settings based the current IP address(es)
        """
        # Open a SystemConfiguration preferences session:
        sc_prefs = SCPreferences()

        # We want to enable the server when our hostname is not on the corporate network:
        # BUG: This does not handle multi-homed systems well:
        current_address = socket.gethostbyname(socket.getfqdn())
        new_state = not current_address.startswith('10.0.1.')

        logging.info("Current address is now %s: SOCKS proxy will be %s" %
                     (current_address, "Enabled" if new_state else "Disabled"))

        try:
            sc_prefs.set_proxy(enable=new_state,
                               protocol='SOCKS',
                               server=self.socks_server,
                               port=self.socks_port)
            sc_prefs.save()

            logging.info("Successfully updated SOCKS proxy setting")
        except RuntimeError, e:
            logging.error("Unable to set SOCKS proxy setting: %s" % e.message)
Example #4
0
def main():
    sc_prefs = SCPreferences()

    from optparse import OptionParser
    parser = OptionParser(__doc__.strip())

    parser.add_option('--enable',
                      dest='enable',
                      action="store_true",
                      default=True,
                      help='Enable proxy for the specified protocol')
    parser.add_option('--disable',
                      dest='enable',
                      action='store_false',
                      help='Disable proxy for the specified protocol')
    parser.add_option('--protocol',
                      choices=sc_prefs.proxy_protocols,
                      metavar='PROTOCOL',
                      help='Specify the protocol (%s)' %
                      ", ".join(sc_prefs.proxy_protocols))
    parser.add_option('--server',
                      metavar='SERVER',
                      help="Specify the proxy server's hostname")
    parser.add_option('--port',
                      type='int',
                      metavar='PORT',
                      help="Specify the proxy server's port")

    (options, args) = parser.parse_args()

    # optparser inexplicably lacks a require option due to extreme
    # pedanticism but it's not worth switching to argparse:
    if not options.protocol:
        print >> sys.stderr, "ERROR: You must specify a protocol to %s" % (
            "enable" if options.enable else "disable")
        sys.exit(1)

    if options.enable and not (options.server and options.port):
        print >> sys.stderr, "ERROR: You must specify a %s proxy server and port" % options.protocol
        sys.exit(1)

    if options.server:
        try:
            gethostbyname(options.server)
        except gaierror, exc:
            print >> sys.stderr, "ERROR: couldn't resolve server hostname %s: %s" % (
                options.server, exc.args[1]
            )  # e.message is broken in the standard socket.gaierror!
            sys.exit(1)