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

📄 vpnclient.java

📁 这是linux下ssl vpn的实现程序
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
                        }

                    } catch (Throwable t) {
                        /* DEBUG */log.error("Failed to check state of tunnels.", t);
                    }
                }
                /* DEBUG */log.info("Disconnected, so stopping tunnel inactivity monitor");
            }
        };
        t.setDaemon(true);
        t.start();
    }

    private void installHTTPSSupport() throws IOException {
        /* DEBUG */log.info("Installing Maverick SSL support for HTTPSURLStreamHandler");
        HttpsURLStreamHandlerFactory.addHTTPSSupport();
    }

    public boolean promptForCredentials(HttpAuthenticator authenticator) {
        return AuthenticationDialog.promptForCredentials(authenticator);
    }

    private void startupHTTPD() {

        /* DEBUG */log.info("Starting HTTPD");
        try {
            randomPort = selectRandomPort();
            httpd = new HTTPServer(randomPort);
            /* DEBUG */log.info("HTTPD created on port " + randomPort);

            httpd.addListener(this);

        } catch (IOException ex) {
            /* DEBUG */log.info("Failed to start HTTPD", ex);
            /* DEBUG */log.info("SSL-Explorer Agent must now exit as its useless without the HTTPD");
            System.exit(1);
        }

    }
    
    public VPNClientGUI getGUI() {
    	return gui;
    }

    private boolean heartbeat() {

        try {
            /* DEBUG */log.info("Sending client heartbeat");

            HttpClient client = getHttpClient();
            GetMethod get = new GetMethod("/clientHeartbeat.do");
            get.setParameter("ticket", getTicket());
            HttpResponse response = client.execute(get);
            try {
                InputStream in = response.getInputStream();
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                byte[] buf = new byte[4096];
                int read;

                while ((read = in.read(buf)) > -1) {
                    out.write(buf, 0, read);

                }
                in.close();

                /* DEBUG */log.info("Received response from server");

                // Parse the result to determine if everything is ok
                XMLElement root = new XMLElement();
                root.parseString(new String(out.toByteArray()));

                if (root.getName().equals("success")) {
                    /* DEBUG */log.info("Server is alive");

                    Vector messages = root.getChildren();
                    for (Enumeration e = messages.elements(); e.hasMoreElements();) {
                        XMLElement el = (XMLElement) e.nextElement();
                        if (el.getName().equals("message")) {
                            String title = (String) el.getAttribute("title");
                            int type = Integer.parseInt((String) el.getAttribute("type"));
                            String message = el.getContent();
                            ;
                            OptionDialog.prompt(gui.getGUIComponent(), type, "SSL-Explorer Message "
                                            + (title.equals("") ? "" : (" - " + title)), "Received: "
                                            + SimpleDateFormat.getDateTimeInstance().format(new Date()) + "\n\n" + message,
                                OptionDialog.CHOICES_OK);
                            // Frame owner =
                            // UIUtil.getFrameAncestor(gui.getGUIComponent());
                            // if(owner == null) {
                            // owner = UIUtil.getSharedFrame();
                            // }
                            // final Dialog dialog = new Dialog(owner, title);
                            // dialog.setLayout(new BorderLayout());
                            // dialog.add(new Label(message),
                            // BorderLayout.CENTER);
                            // Panel b = new Panel(new
                            // FlowLayout(FlowLayout.CENTER));
                            // Button close = new Button("Close");
                            // b.add(close);
                            // close.addActionListener(new ActionListener() {
                            // public void actionPerformed(ActionEvent evt) {
                            // dialog.dispose();
                            // }
                            // });
                            // dialog.add(b, BorderLayout.SOUTH);
                            // dialog.pack();
                            // dialog.setVisible(true);
                            //
                            // // TODO popup a message dialog
                        } else {
                            /* DEBUG */log.info("Unrecognised element " + el.getName() + " in heartbeat response.");

                        }
                    }

                    return true;
                } else if (root.getName().equals("shutdown")) {
                    /* DEBUG */log.info("Received shutdown instruction during heartbeat");
                    startShutdownProcedure(false, true);
                } else {
                    // See what is wrong? maybe our ticket has been invalidated?
                    /* DEBUG */log.info("Server responded with error");
                    /* DEBUG */log.info(root.getContent());
                }
            } finally {
                response.close();
            }

        } catch (XMLParseException ex) {
            /* DEBUG */log.info("Heartbeat failed.", ex);
        } catch (SSLIOException ex) {
            /* DEBUG */log.info("Heartbeat failed.", ex.getRealException());
        } catch (Exception ex) {
            /* DEBUG */log.info("Heartbeat failed.", ex);
        }

        currentState = STATE_DISCONNECTED;
        return false;
    }

    private void registerClient() throws IOException, HttpException, UnsupportedAuthenticationException,
                    AuthenticationCancelledException {

        HttpResponse response = null;
        for (int i = 0; i < 3; i++) {

            /* DEBUG */log.info("Registering with the SSL-Explorer server");
            /* DEBUG */log.info("Server is " + getSSLExplorerHost() + ":" + getSSLExplorerPort());

            HttpClient client = getHttpClient();
            PostMethod post = new PostMethod("/registerClient.do");
            post.setParameter("ticket", ticket);
            post.setParameter("type", "1");
            post.setParameter("port", String.valueOf(randomPort));
            Properties sysProps = getSystemPropertiesToSend();
            for (Enumeration e = sysProps.keys(); e.hasMoreElements();) {
                String key = (String) e.nextElement();
                String val = sysProps.getProperty(key);
                post.setParameter(key, val);
            }
            response = client.execute(post);

            if (response.getStatus() == 302) {
                // Reset the client
                this.client = null;

                URL url = new URL(response.getHeaderField("Location"));
                sslexplorerHostname = url.getHost();
                if (url.getPort() > 0)
                    sslexplorerPort = url.getPort();
                continue;
            }

            try {
                InputStream in = response.getInputStream();
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                byte[] buf = new byte[4096];
                int read;

                while ((read = in.read(buf)) > -1)
                    out.write(buf, 0, read);

                in.close();

                // Parse the result to determine if everything is ok - we will
                // get a
                // new ticket through the secure connection for subsequent
                // connections
                XMLElement root = new XMLElement();

                String xml = new String(out.toByteArray());
                root.parseString(xml);

                /* DEBUG */log.info("Got response from server");
                if (root.getName().equalsIgnoreCase("success")) {
                    this.ticket = (String) root.getAttribute("ticket");
                    /* DEBUG */log.info("Got ticket: " + ticket);
                    return;
                } else {
                    throw new IOException("Registration failed. " + root.getContent());
                    // /* DEBUG */log.info("Failed to register: " +
                    // root.getContent());
                    // /* DEBUG */log.info(
                    // /* DEBUG */"VPN client must now exit as it must be
                    // registered
                    // with the server");
                    // OptionDialog.error(gui.getGUIComponent(), "Error", "The
                    // VPN
                    // client failed to register with the server. The reason
                    // give
                    // was
                    // :-\n\n" +
                    // root.getContent());
                    // /* DEBUG */log.info("User dismissed dialog");
                    // System.exit(2);
                }

            } finally {
                response.close();
            }
        }

        throw new IOException("Failed to register with server.. too many redirects!");

    }

    private void deregisterClient() throws IOException, HttpException, UnsupportedAuthenticationException,
                    AuthenticationCancelledException {

        /* DEBUG */log.info("Deregistering from the SSL-Explorer server");
        /* DEBUG */log.info("Server is " + getSSLExplorerHost() + ":" + getSSLExplorerPort());

        HttpClient client = getHttpClient();
        GetMethod get = new GetMethod("/deregisterClient.do");
        get.setParameter("ticket", getTicket());
        get.setParameter("port", String.valueOf(randomPort));
        HttpResponse response = client.execute(get);
        InputStream in = response.getInputStream();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buf = new byte[4096];
        int read;

        while ((read = in.read(buf)) > -1)
            out.write(buf, 0, read);

        in.close();

        // Parse the result to determine if everything is ok - we will get a
        // new ticket through the secure connection for subsequent connections
        XMLElement root = new XMLElement();
        root.parseString(new String(out.toByteArray()));

        /* DEBUG */log.info("Got response from server");
        if (root.getName().equalsIgnoreCase("success")) {
            return;
        } else {
            /* DEBUG */log.info("Failed to deregister: " + root.getContent());
        }

    }

    private void updateInformation() {

        String msg = "";
        int count = getActiveDirectTunnels().size() + activeForwardingTunnels.size();

        for (Enumeration e = remoteListeners.elements(); e.hasMoreElements();) {
            MultiplexedConnection con = (MultiplexedConnection) e.nextElement();
            count += con.getActiveChannels().length;
        }
        int ports = activeListeners.size() + remoteListeners.size();
        if (ports > 0) {
            if (count > 0) {
                msg = "Client has " + ports
                                + (ports > 1 ? " tunnels open " : " tunnel open ") + "with " + count
                                + (count > 1 ? " active connections." : " active connection.");
            } else {
                msg = "Client has "
                                + ports
                                + (ports > 1 ? " tunnels open but no active connections."
                                                : " tunnel open but no active connections.");
            }
        } else if (count > 0) {

            msg = "There are no open tunnels but " + count + ((count > 1) ? " active connections remain." : " active connection remains.");

        }

        if (msg.equals("")) {
            msg = "The SSL-Explorer Agent is idle.";
        }

        gui.setInfo(msg);
    }

    private void configureSystemTray() {

        /* DEBUG */log.info("Configuring system tray");

        if (ApplicationLauncher.checkVersion("1.2") && System.getProperty("os.name").startsWith("Windows")) {

        } else {
            /**
			 * The system tray cannot be used. What alternatives do we have??
			 */
            /* DEBUG */log.info("System tray disabled");
            /* DEBUG */log.info("Java Version is " + System.getProperty("java.version"));
            /* DEBUG */log.info("Operating system is " + System.getProperty("os.name"));
        }

    }

    public IOStreamConnectorListener getTXIOListener() {
        if (txIo == null) {
            txIo = new TXIOStreamConnectorListener();
        }
        return txIo;

⌨️ 快捷键说明

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