<commit_before>package org.lantern;

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

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.SystemUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.eclipse.swt.SWT;
import org.lantern.win.WinProxy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.eventbus.Subscribe;
import com.google.common.io.Files;

/**
 * Class that handles turning proxying on and off for all platforms.
 */
public class Proxifier {
    
    public static class ProxyConfigurationError extends Exception {}
    public static class ProxyConfigurationCancelled extends ProxyConfigurationError {};
    
    private static final Logger LOG = 
        LoggerFactory.getLogger(Proxifier.class);
    
    /**
     * File external processes can use to determine if Lantern is currently
     * proxying traffic. Useful for things like the FireFox extensions.
     */
    private static final File LANTERN_PROXYING_FILE =
        new File(LanternConstants.CONFIG_DIR, "lanternProxying");
    
    private static boolean interactiveUnproxyCalled;

    private static final MacProxyManager mpm = 
        new MacProxyManager("testId", 4291);
    
    private static final File PROXY_ON = 
        new File(LanternHub.jettyLauncher().getResourceBaseFile(), "proxy_on.pac");
    private static final File PROXY_OFF = 
        new File(LanternHub.jettyLauncher().getResourceBaseFile(), "proxy_off.pac");
    private static final File PROXY_ALL = 
        new File(LanternHub.jettyLauncher().getResourceBaseFile(), "proxy_all.pac");
    
    static {
        copyFromLocal(PROXY_ON);
        copyFromLocal(PROXY_ALL);
        copyFromLocal(PROXY_OFF);
    }
    private static final WinProxy WIN_PROXY = 
        new WinProxy(LanternConstants.CONFIG_DIR);
    
    static {
        final class Subscriber {
            @Subscribe
            public void onQuit(final QuitEvent quit) {
                LOG.info("Got quit event!");
                interactiveUnproxy();
            }
        }
        LanternHub.register(new Subscriber());
        
        if (SystemUtils.IS_OS_MAC_OSX) {
            final File Lantern = new File("Lantern");
            if (!Lantern.isFile()) {
                LOG.info("Creating hard link to osascript...");
                try {
                    final String result = 
                        mpm.runScript("ln", "/usr/bin/osascript", "Lantern");
                    LOG.info("Result of script is: {}", result);
                } catch (final IOException e) {
                    LOG.warn("Error creating hard link!!", e);
                }
            } else {
                LOG.info("Appears to already be a link to osascript");
            }
        }
        LANTERN_PROXYING_FILE.delete();
        LANTERN_PROXYING_FILE.deleteOnExit();
        if (!PROXY_OFF.isFile()) {
            final String msg = 
                "No pac at: "+PROXY_OFF.getAbsolutePath() +"\nfrom: " +
                new File(".").getAbsolutePath();
            LOG.error(msg);
            throw new IllegalStateException(msg);
        }
        LOG.info("Both pac files are in their expected locations");

        // We always want to stop proxying on shutdown -- doesn't hurt 
        // anything in the case where we never proxied in the first place.
        // If there is a UI we let the UI handle it. 
        final Thread hook = new Thread(new Runnable() {
            @Override
            public void run() {
                interactiveUnproxy();
            }
        }, "Unset-Web-Proxy-Thread");
        Runtime.getRuntime().addShutdownHook(hook);
    }
    
    
    private static void copyFromLocal(final File dest) {
        final File all = new File(dest.getName());
        try {
            Files.copy(all, dest);
        } catch (final IOException e) {
            LOG.error("Could not copy from {} to {}", all, dest);
        }
    }
    
    /**<commit_msg>Better logging and error checking for #444<commit_after>package org.lantern;

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

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.SystemUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.eclipse.swt.SWT;
import org.lantern.win.WinProxy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.eventbus.Subscribe;
import com.google.common.io.Files;

/**
 * Class that handles turning proxying on and off for all platforms.
 */
public class Proxifier {
    
    public static class ProxyConfigurationError extends Exception {}
    public static class ProxyConfigurationCancelled extends ProxyConfigurationError {};
    
    private static final Logger LOG = 
        LoggerFactory.getLogger(Proxifier.class);
    
    /**
     * File external processes can use to determine if Lantern is currently
     * proxying traffic. Useful for things like the FireFox extensions.
     */
    private static final File LANTERN_PROXYING_FILE =
        new File(LanternConstants.CONFIG_DIR, "lanternProxying");
    
    private static boolean interactiveUnproxyCalled;

    private static final MacProxyManager mpm = 
        new MacProxyManager("testId", 4291);
    
    private static final File PROXY_ON = 
        new File(LanternHub.jettyLauncher().getResourceBaseFile(), "proxy_on.pac");
    private static final File PROXY_OFF = 
        new File(LanternHub.jettyLauncher().getResourceBaseFile(), "proxy_off.pac");
    private static final File PROXY_ALL = 
        new File(LanternHub.jettyLauncher().getResourceBaseFile(), "proxy_all.pac");
    
    static {
        copyFromLocal(PROXY_ON);
        copyFromLocal(PROXY_ALL);
        copyFromLocal(PROXY_OFF);
    }
    private static final WinProxy WIN_PROXY = 
        new WinProxy(LanternConstants.CONFIG_DIR);
    
    static {
        final class Subscriber {
            @Subscribe
            public void onQuit(final QuitEvent quit) {
                LOG.info("Got quit event!");
                interactiveUnproxy();
            }
        }
        LanternHub.register(new Subscriber());
        
        if (SystemUtils.IS_OS_MAC_OSX) {
            final File Lantern = new File("Lantern");
            if (!Lantern.isFile()) {
                LOG.info("Creating hard link to osascript...");
                try {
                    final String result = 
                        mpm.runScript("ln", "/usr/bin/osascript", "Lantern");
                    LOG.info("Result of script is: {}", result);
                } catch (final IOException e) {
                    LOG.warn("Error creating hard link!!", e);
                }
            } else {
                LOG.info("Appears to already be a link to osascript");
            }
        }
        LANTERN_PROXYING_FILE.delete();
        LANTERN_PROXYING_FILE.deleteOnExit();
        if (!PROXY_OFF.isFile()) {
            final String msg = 
                "No pac at: "+PROXY_OFF.getAbsolutePath() +"\nfrom: " +
                new File(".").getAbsolutePath();
            LOG.error(msg);
            throw new IllegalStateException(msg);
        }
        LOG.info("Both pac files are in their expected locations");

        // We always want to stop proxying on shutdown -- doesn't hurt 
        // anything in the case where we never proxied in the first place.
        // If there is a UI we let the UI handle it. 
        final Thread hook = new Thread(new Runnable() {
            @Override
            public void run() {
                interactiveUnproxy();
            }
        }, "Unset-Web-Proxy-Thread");
        Runtime.getRuntime().addShutdownHook(hook);
    }
    
    
    private static void copyFromLocal(final File dest) {
        final File local = new File(dest.getName());
        if (!local.isFile()) {
            LOG.error("No file at: {}", local);
            return;
        }
        try {
            Files.copy(local, dest);
        } catch (final IOException e) {
            LOG.error("Error copying file from "+local+" to "+ dest, e);
        }
    }
    
    /**
{'fraction_non_alphanumeric': 0.09724793286437122, 'fraction_numerical': 0.0018511662347278786, 'mean_word_length': 2.1037916507085406, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '7236413', 'n_tokens_mistral': 2270, 'n_tokens_neox': 2207, 'n_words': 590}