⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vpnclient.java

📁 这是linux下ssl vpn的实现程序
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
                int id = -1;
                if(idx != -1) {
                    try {
                        id = Integer.parseInt(tickets[i].substring(0, idx));
                    } catch (NumberFormatException nfe) {
                    }
                    String ticket = tickets[i].substring(idx + 1);
                }
                else {
                    id = Integer.parseInt(tickets[i]); 
                }
                if (id != -1) {
                    Tunnel tunnel = getPersistantForwardingTunnel(id);
                    if (tunnel == null) {
                        /* DEBUG */log.error("No forwarding tunnel with id " + id);
                        throw new Exception("No forwarding tunnel with id of " + id
                                        + ". If this is a new global tunnel, you should restart the SSL-Explorer Agent.");
                    } else {
                        switch (tunnel.getType()) {
                            case Tunnel.REMOTE_TUNNEL:
                                startRemoteForwarding(tunnel);
                                break;
                            default:
                                startListeningSocket(tunnel);
                                break;
                        }

                    }
                }
            }

            updateInformation();

        } catch (Exception e) {
            /* DEBUG */log.error("Failed to start listening socket.", e);
            throw new Exception("Failed to open tunnel. " + e.getMessage());
        }
        return redirect;
    }

    protected String closeTunnels(String redirect, String[] tickets) throws Exception {
        if (tickets == null || tickets.length == 0) {
            throw new Exception("You must select at least one tunnel to close.");
        }
        for (int i = 0; i < tickets.length; i++) {
            int idx = tickets[i].indexOf('/');
            int id = -1;
            try {
                id = Integer.parseInt(tickets[i].substring(0, idx));
            } catch (NumberFormatException nfe) {
            }
            String ticket = tickets[i].substring(idx + 1);
            stopListeningSocket(ticket);
        }

        updateInformation();
        return redirect;

    }

    private String addRedirectParameters(String redirect, String parameters) {
        StringBuffer buf = new StringBuffer(redirect);
        int idx = redirect.indexOf('?');
        if (idx == -1) {
            buf.append("?");
        } else {
            buf.append("&");
        }
        buf.append(parameters == null ? "" : parameters);
        return buf.toString();
    }

    private String addRedirectParameter(String redirect, String name, String value) {
        StringBuffer buf = new StringBuffer(redirect);
        int idx = redirect.indexOf('?');
        if (idx == -1) {
            buf.append("?");
        } else {
            buf.append("&");
        }
        buf.append(name);
        buf.append('=');
        buf.append(URLEncoder.encode(value));
        return buf.toString();
    }

    private void startShutdownProcedure(boolean deregister, boolean threadDeRegister) {
        /* DEBUG */log.info("Starting shutdown procedure " + (deregister ? "(degregistering)" : ""));

        
        AgentExtension ext;
        for(Enumeration e = extensions.elements(); e.hasMoreElements();) {
        	ext = (AgentExtension) e.nextElement();
        	
        	/* DEBUG */log.info("Shutting down " + ext.getName());
        	ext.exit();
        }
        
        Thread deregisterThread = null;
        if (deregister) {
            deregisterThread = new Thread() {
                public void run() {
                    try {
                        deregisterClient();
                    } catch (Exception e) {
                    }
                }
            };
            if (threadDeRegister) {
                deregisterThread.start();
            } else {
                deregisterThread.run();
            }
        }

        ClientCacheRemover.getInstance().cleanup();

        currentState = STATE_DISCONNECTED;

        gui.showDisconnected();
        gui.setInfo("SSL-Explorer Agent is shutting down");

        final Thread fDeregisterThread = deregisterThread;
        Thread t = new Thread() {
            public void run() {
                try {
                    Thread.sleep(SHUTDOWN_PERIOD);
                } catch (InterruptedException ex) {
                }
                /* DEBUG */log.info("Exiting JVM");
                if (fDeregisterThread != null) {
                    fDeregisterThread.interrupt();
                }
                System.exit(0);
            }
        };

        t.start();
    }

    protected Vector getTunnels() {
        return super.activeApplicationTunnels;
    }

    private String launchApplication(HTTPRequest request) throws IOException {

        /* DEBUG */log.info("Adding ticket "
        /* DEBUG */+ ticket
        /* DEBUG */+ " to request parameters");
        request.addParameter("ticket", ticket);
        /* DEBUG */request.addParameter("debug", "true");

        String name = request.getParameter("name");
        /* DEBUG */log.info("Starting application " + name);

        // FIXME by using a Hashtable we lose parameters that have the same name
        // as
        // another
        Application app = new Application(request.getParametersMap(), name);
        app.start();
        return app.getRedirectParameters();

    }

    private static String getCommandLineValue(String arg) {
        int idx = arg.indexOf('=');
        if (idx > -1)
            return arg.substring(idx + 1);
        else
            return arg;
    }

    protected void registerForwardingTunnel(VPNTunnel tunnel) {
        super.registerForwardingTunnel(tunnel);
        if(tunnel.getVPNConnectionListener() != null) {
            PORTS.getModel().updateItemAt(PORTS.getModel().getIndexForVPNConnectionListener(tunnel.getVPNConnectionListener()));
        }
        updateInformation();
    }

    protected void unregisterForwardingTunnel(VPNTunnel tunnel) {
        super.unregisterForwardingTunnel(tunnel);
        if(tunnel.getVPNConnectionListener() != null) {
            PORTS.getModel().updateItemAt(PORTS.getModel().getIndexForVPNConnectionListener(tunnel.getVPNConnectionListener()));
        }
        updateInformation();
    }

    public static void main(String[] args) {

        try {

            // Set a dynamic class loader so we can add more jars at runtime
            // Thread.currentThread().setContextClassLoader(new
            // DynamicClassLoader(VPNClient.class.getClassLoader()));

            /* DEBUG */org.apache.log4j.BasicConfigurator.configure();

            CONSOLE = new VPNClient.ConsoleOutputStream();
            PrintStream consolePrintStream = new PrintStream(CONSOLE);
            System.setErr(consolePrintStream);
            System.setOut(consolePrintStream);
            
            String hostname = null;
            int port = 443;
            String username = null;
            String ticket = null;
            String logProperties = null;
            int heartbeat = -1;
            int shutdown = -1;
            int webforwardInactivity = 300000;
            int tunnelInactivity = 600000;
            String browserCommand = null;
            String localProxyURL = null;
            String pluginProxyURL = null;
            String userAgent = null;
            String extensionClasses = null;

            for (int i = 0; i < args.length; i++) {
                if (args[i].startsWith("hostname")) {
                    hostname = getCommandLineValue(args[i]);
                } else if (args[i].startsWith("port")) {
                    try {
                        port = Integer.parseInt(getCommandLineValue(args[i]));
                    } catch (NumberFormatException ex) {
                    }
                } else if (args[i].startsWith("username")) {
                    username = getCommandLineValue(args[i]);
                } else if (args[i].startsWith("localProxyURL")) {
                    localProxyURL = getCommandLineValue(args[i]);
                } else if (args[i].startsWith("pluginProxyURL")) {
                    pluginProxyURL = getCommandLineValue(args[i]);
                } else if (args[i].startsWith("ticket")) {
                    ticket = getCommandLineValue(args[i]);
                } else if (args[i].startsWith("browserCommand")) {
                    browserCommand = getCommandLineValue(args[i]);
                } else if (args[i].startsWith("userAgent")) {
                    userAgent = getCommandLineValue(args[i]);
                } else if (args[i].startsWith("heartbeat")) {
                    try {
                        heartbeat = Integer.parseInt(getCommandLineValue(args[i]));
                    } catch (NumberFormatException ex) {
                    }
                } else if (args[i].startsWith("webforward.inactivity")) {
                    try {
                        webforwardInactivity = Integer.parseInt(getCommandLineValue(args[i]));
                    } catch (NumberFormatException ex) {
                    }
                } else if (args[i].startsWith("tunnel.inactivity")) {
                    try {
                        tunnelInactivity = Integer.parseInt(getCommandLineValue(args[i]));
                    } catch (NumberFormatException ex) {
                    }
                } else if (args[i].startsWith("shutdown")) {
                    try {
                        shutdown = Integer.parseInt(getCommandLineValue(args[i]));
                    } catch (NumberFormatException ex) {
                    }
                } else if (args[i].startsWith("log4j")) {
                    logProperties = getCommandLineValue(args[i]);
                } else if(args[i].startsWith("extensionClasses")) {
                	extensionClasses = getCommandLineValue(args[i]);
                }
            }

            /* DEBUG */if (logProperties != null) {
                /* DEBUG */File f = new File(logProperties);
                /* DEBUG */Properties props = new Properties();
                /* DEBUG */props.load(new FileInputStream(f));
                /* DEBUG */File logfile = new File(f.getParent(), "vpn-client.log");
                /* DEBUG */props.put("log4j.appender.logfile.File", logfile.getAbsolutePath());
                /* DEBUG */org.apache.log4j.PropertyConfigurator.configure(props);
                /* DEBUG */log = org.apache.commons.logging.LogFactory.getLog(VPNClient.class);
                /* DEBUG */log.info("Configured logging");
                /* DEBUG */}

            if (logProperties == null) {
                System.out.println("SSL-Explorer Agent debug mode is not enabled,\n" + "only serious errors will be displayed.");
            }

            if (port == -1 || hostname == null || username == null || ticket == null)
                throw new IOException("Expected hostname, port, username and ticket command line arguments");

            OptionDialog.INFORMATION_ICON = "/images/information.gif";
            OptionDialog.WARNING_ICON = "/images/warning.gif";
            OptionDialog.QUESTION_ICON = "/images/question.gif";
            OptionDialog.ERROR_ICON = "/images/error.gif";

            VPNClient vpn = new VPNClient();
            
            PORTS = new PortMonitor(vpn);
            PORTS.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent evt) {
                    PORTS.setVisible(false);
                }            
            });

            if (localProxyURL != null && !localProxyURL.equals("") && !localProxyURL.startsWith("browser://")) {
                // Use the user supplied proxy settings

                /* DEBUG */log.info("Setting user specified local proxy URL to " + obfuscateURL(localProxyURL));
                vpn.setLocalProxyURL(localProxyURL);
            } else {
                /* DEBUG */log.info("Attempting to detect proxy settings using platform specific methods");

                if (localProxyURL != null && localProxyURL.startsWith("browser://")) {
                    URI uri = new URI(localProxyURL);

                    /*
					 * Try to determine the proxy settings by first usng
					 * platform / browser specific method, then the proxy
					 * supplied by the Java plugin.
					 * 
					 * TODO be more intelligent about which browse to try first -
					 * use the userAgent parameter passed from the JSP
					 */
                    String proxyURL = null;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -