예제 #1
0
    def _run_worker_thread(self):
        """Runs the worker thread that waits for network events."""
        from Foundation import (
            CFRunLoopAddSource,
            CFRunLoopGetCurrent,
            kCFRunLoopCommonModes,
            CFRunLoopRun,
        )
        from SystemConfiguration import (
            SCDynamicStoreCreate,
            SCDynamicStoreSetNotificationKeys,
            SCDynamicStoreCreateRunLoopSource,
        )

        store = SCDynamicStoreCreate(None, "global-network-watcher",
                                     self._on_network_changed, None)
        SCDynamicStoreSetNotificationKeys(
            store, None,
            ["State:/Network/Global/IPv4", "State:/Network/Global/IPv6"])

        CFRunLoopAddSource(
            CFRunLoopGetCurrent(),
            SCDynamicStoreCreateRunLoopSource(None, store, 0),
            kCFRunLoopCommonModes,
        )

        CFRunLoopRun()
예제 #2
0
    def watchForProxyChanges(self):
        """ install a watcher for proxy changes """
        SCDynamicStoreSetNotificationKeys(self.store, None,
                                          ['State:/Network/Global/Proxies'])

        source = SCDynamicStoreCreateRunLoopSource(None, self.store, 0)
        loop = NSRunLoop.currentRunLoop().getCFRunLoop()
        CFRunLoopAddSource(loop, source, kCFRunLoopCommonModes)
예제 #3
0
def main():
    # Setup a dynamic store controller that monitors all keys:
    store = SCDynamicStoreCreate(None, "demo.controller", dynamicStoreChanged, None)
    SCDynamicStoreSetNotificationKeys(store, None, [".*"])
    source = SCDynamicStoreCreateRunLoopSource(None, store, 0)

    # Setup a preferences controller
    prefs = SCPreferencesCreate(None, "demo.prefs", None)
    SCPreferencesSetCallback(prefs, prefsChanged, None)

    # Set up a run loop and add all controllers to that.
    loop = CFRunLoopGetCurrent()
    CFRunLoopAddSource(loop, source, kCFRunLoopCommonModes)
    SCPreferencesScheduleWithRunLoop(prefs, loop, kCFRunLoopCommonModes)

    signal.signal(signal.SIGINT, sigint)
    CFRunLoopRun()
예제 #4
0
    # log("Initializing")
    run_at = None
    if connected():
        run_prey()

    sc_keys = ['State:/Network/Global/IPv4', 'State:/Network/Global/IPv6']

    store = SCDynamicStoreCreate(None, "global-network-change",
                                 network_state_changed, None)
    SCDynamicStoreSetNotificationKeys(store, None, sc_keys)

    CFRunLoopAddSource(
        # NSRunLoop.currentRunLoop().getCFRunLoop(),
        CFRunLoopGetCurrent(),
        SCDynamicStoreCreateRunLoopSource(None, store, 0),
        kCFRunLoopCommonModes)

    # signal.signal(signal.SIGHUP, partial(quit, "SIGHUP received"))

    # NOTE: This timer is basically a kludge around the fact that we can't reliably get
    #       signals or Control-C inside a runloop. This wakes us up often enough to
    #       appear tolerably responsive:
    CFRunLoopAddTimer(
        NSRunLoop.currentRunLoop().getCFRunLoop(),
        CFRunLoopTimerCreate(None, CFAbsoluteTimeGetCurrent(), timer_interval,
                             0, 0, timer_callback, None),
        kCFRunLoopCommonModes)

    try:
        AppHelper.runConsoleEventLoop(installInterrupt=True)