<commit_before>package org.lantern;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.SystemUtils;
import org.littleshoot.util.CommonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Configures Lantern. This can be either on the first run of the application
 * or through the user changing his or her configurations in the configuration
 * screen.
 */
public class Configurator {
    
    private final Logger log = LoggerFactory.getLogger(getClass());
    
    public void configure() {
        
        // We only want to configure the proxy if the user is in censored mode.
        if (SystemUtils.IS_OS_MAC_OSX) {
            configureOsxProxy();
        } else if (SystemUtils.IS_OS_WINDOWS) {
            configureWindowsProxy();
            // The firewall config is actually handled in a bat file from the
            // installer.
            //configureWindowsFirewall();
        }
    }

    private void configureWindowsFirewall() {
        final boolean oldNetSh;
        if (SystemUtils.IS_OS_WINDOWS_2000 ||
            SystemUtils.IS_OS_WINDOWS_XP) {
            oldNetSh = true;
        }
        else {
            oldNetSh = false;
        }

        if (oldNetSh) {
            final String[] commands = {
                "netsh", "firewall", "add", "allowedprogram", 
                "\""+SystemUtils.getUserDir()+"/Lantern.exe\"", "\"Lantern\"", 
                "ENABLE"
            };
            CommonUtils.nativeCall(commands);
        } else {
            final String[] commands = {
                "netsh", "advfirewall", "firewall", "add", "rule", 
                "name=\"Lantern\"", "dir=in", "action=allow", 
                "program=\""+SystemUtils.getUserDir()+"/Lantern.exe\"", 
                "enable=yes", "profile=any"
            };
            CommonUtils.nativeCall(commands);
        }
    }

    private void configureOsxProxy() {
        CommonUtils.nativeCall("networksetup -setwebproxy Airport 127.0.0.1 " + 
            LanternConstants.LANTERN_LOCALHOST_PORT);
        CommonUtils.nativeCall("networksetup -setwebproxy Ethernet 127.0.0.1 " + 
            LanternConstants.LANTERN_LOCALHOST_PORT);
    }

    private void configureWindowsProxy() {
        if (!SystemUtils.IS_OS_WINDOWS) {
            log.info("Not running on Windows");
            return;
        }
        final String key = 
            "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\" +
            "Internet Settings";
        
        final String ps = "ProxyServer";
        final String pe = "ProxyEnable";
        
        // We first want to read the start values so we can return the
        // registry to the original state when we shut down.
        final String proxyServerOriginal = WindowsRegistry.read(key, ps);
        final String proxyEnableOriginal = WindowsRegistry.read(key, pe);
        
        final String proxyServerUs = "127.0.0.1:"+
            LanternConstants.LANTERN_LOCALHOST_PORT;
        final String proxyEnableUs = "1";

        log.info("Setting registry to use MG as a proxy...");
        final int enableResult = 
            WindowsRegistry.writeREG_SZ(key, ps, proxyServerUs);
        final int serverResult = 
            WindowsRegistry.writeREG_DWORD(key, pe, proxyEnableUs);
        
        if (enableResult != 0) {<commit_msg>Added auto-unproxy on shutdown<commit_after>package org.lantern;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.SystemUtils;
import org.littleshoot.util.CommonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Configures Lantern. This can be either on the first run of the application
 * or through the user changing his or her configurations in the configuration
 * screen.
 */
public class Configurator {
    
    private final Logger log = LoggerFactory.getLogger(getClass());
    
    public void configure() {
        
        // We only want to configure the proxy if the user is in censored mode.
        if (SystemUtils.IS_OS_MAC_OSX) {
            configureOsxProxy();
        } else if (SystemUtils.IS_OS_WINDOWS) {
            configureWindowsProxy();
            // The firewall config is actually handled in a bat file from the
            // installer.
            //configureWindowsFirewall();
        }
    }

    private void configureWindowsFirewall() {
        final boolean oldNetSh;
        if (SystemUtils.IS_OS_WINDOWS_2000 ||
            SystemUtils.IS_OS_WINDOWS_XP) {
            oldNetSh = true;
        }
        else {
            oldNetSh = false;
        }

        if (oldNetSh) {
            final String[] commands = {
                "netsh", "firewall", "add", "allowedprogram", 
                "\""+SystemUtils.getUserDir()+"/Lantern.exe\"", "\"Lantern\"", 
                "ENABLE"
            };
            CommonUtils.nativeCall(commands);
        } else {
            final String[] commands = {
                "netsh", "advfirewall", "firewall", "add", "rule", 
                "name=\"Lantern\"", "dir=in", "action=allow", 
                "program=\""+SystemUtils.getUserDir()+"/Lantern.exe\"", 
                "enable=yes", "profile=any"
            };
            CommonUtils.nativeCall(commands);
        }
    }

    private void configureOsxProxy() {
        CommonUtils.nativeCall("networksetup -setwebproxy Airport 127.0.0.1 " + 
            LanternConstants.LANTERN_LOCALHOST_PORT);
        CommonUtils.nativeCall("networksetup -setwebproxy Ethernet 127.0.0.1 " + 
            LanternConstants.LANTERN_LOCALHOST_PORT);
        
        final Thread hook = new Thread(new Runnable() {
            public void run() {
                CommonUtils.nativeCall("networksetup -setwebproxystate Airport off");
                CommonUtils.nativeCall("networksetup -setwebproxystate Ethernet off");
            }
            
        }, "Unset-Web-Proxy-OSX");
        Runtime.getRuntime().addShutdownHook(hook);
    }

    private void configureWindowsProxy() {
        if (!SystemUtils.IS_OS_WINDOWS) {
            log.info("Not running on Windows");
            return;
        }
        final String key = 
            "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\" +
            "Internet Settings";
        
        final String ps = "ProxyServer";
        final String pe = "ProxyEnable";
        
        // We first want to read the start values so we can return the
        // registry to the original state when we shut down.
        final String proxyServerOriginal = WindowsRegistry.read(key, ps);
        final String proxyEnableOriginal = WindowsRegistry.read(key, pe);
        
        final String proxyServerUs = "127.0.0.1:"+
            LanternConstants.LANTERN_LOCALHOST_PORT;
        final String proxyEnableUs = "1";

        log.info("Setting registry to use MG as a proxy...");
        final int enableResult = 
            WindowsRegistry.writeREG_SZ(key, ps, proxyServerUs);
        final int serverResult = 
            WindowsRegistry.writeREG_DWORD(key, pe, proxyEnableUs);
        
        if (enableResult != 0) {
{'fraction_non_alphanumeric': 0.10210043121435526, 'fraction_numerical': 0.007233273056057866, 'mean_word_length': 2.0363175675675675, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 6, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4322307', 'n_tokens_mistral': 2011, 'n_tokens_neox': 1934, 'n_words': 486}