wiki:doc/TorToggle

Version 23 (modified by karsten, 6 years ago) (diff)

Name changed from TorToggle to doc/TorToggle

There are numerous ways to en- and disable surfing via Privoxy, like any other proxy.

Some, however, might like to always use Privoxy as a proxy and just toggle forwarding to Tor on and off.

For users of UNIXish operating systems there is a script which performs this task.

Windows users had to manually edit Privoxy's config files (or at least I am not aware of a more convenient method).

So I wrote these few lines of code to automate the task. Hopefully this is useful for some of you.

Compile using any D compiler, or use the executable.

/*  tortoggle.d

    a tool to toggle forwarding to tor via privoxy for windows

    written 2007 by Sascha Othman

    These piece of software is in the public domain.
    Permission to use, copy, modify, and distribute this software for any purpose
    and without fee is hereby granted, without any conditions or restrictions.
    This software is provided 'as is' without express or implied warranty.

*/

import std.file;
import std.c.windows.windows;
import std.windows.registry;

version(D_Version2) {}
else {
    alias char[] string;
}

enum Action { enable=1, disable, toggle }

int main(string[] args) {
    int action;
    if (args.length > 1) {
        switch (args[1][0]) {
        case 'e': action=Action.enable; break;
        case 'd': action=Action.disable; break;
        case 't': action=Action.toggle; break;
        default:
            return MessageBoxA(null, cast(char*)"command line options:\n\ne[anble]\nd[isable]\nt[oggle on/off]",
                               cast(char*)args[0], MB_OK | MB_ICONEXCLAMATION);;
        }
    }

    char* title=cast(char*)"TorToggle";
    string confile;
    try {
        confile=Registry.currentUser().getKey("Software\\Privoxy").getValue("Install_Dir").value_SZ();
    } catch (Exception e)
        return MessageBoxA(null, cast(char*)e.toString(), title, MB_OK | MB_ICONEXCLAMATION);
    confile~="\\privoxy\\config.txt";
    char en='\n', dis='#';
    bool status=false;

    if (!std.file.exists(confile)) return MessageBoxA(null, "Privoxy config not found", title, MB_OK | MB_ICONEXCLAMATION);
    string conf=cast(string)std.file.read(confile);

    int i=std.regexp.find(conf, "forward.*:9050")-1;

    if (i && conf[i]==en) status=true;
    else if (!i || conf[i]!=dis) return MessageBoxA(null, "Unable to detect Tor status", title, MB_OK | MB_ICONEXCLAMATION);

    if (!action) action=MessageBoxA(null, cast(char*)("Tor is "~(status?"en":"dis")~"abled. "~(status?"Dis":"En")~"able?"),
                                title, MB_YESNO | MB_ICONINFORMATION);
    if ( (action==IDYES) || (action==Action.toggle) || ((action!=IDNO) && ((action==Action.enable) ^ status)) ) {
        char[] tmp=conf.dup;
        tmp[i]=(status?dis:en);
        std.file.write(confile, tmp);
    }
    return 0;
}