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

📄 tcpinterceptor.java

📁 JAVA开源LDAP浏览器jxplorer的源码!
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
                {
                    // Only loop as long as the connection to the target
                    // machine is available - once that's gone we can stop.
                    // The old way, loop until both are closed, left us
                    // looping forever since no one closed the 1st one.
                    // while( !ResponseSocketRR.isDone() )
                    if (null != requestSocketRR && requestSocketRR.isDone())
                    {
                        if (index >= 0 && ResponseSocketRR != null)
                        {
                            listener.tableModel.setValueAt(getMessage("resp00", "Resp"),
                                    1 + index, STATE_COLUMN);
                        }
                        requestSocketRR = null;
                    }
                    if (null != ResponseSocketRR && ResponseSocketRR.isDone())
                    {
                        if (index >= 0 && requestSocketRR != null)
                        {
                            listener.tableModel.setValueAt(getMessage("req00", "Req"),
                                    1 + index, STATE_COLUMN);
                        }
                        ResponseSocketRR = null;
                    }

                    //  Thread.sleep( 10 );
                    synchronized (this)
                    {
                        this.wait(1000); //Safety just incase we're not told to wake up.
                    }
                }

                //  System.out.println("Done ");
                // requestSocketRR.halt();
                // ResponseSocketRR.halt();


                active = false;

                /*
                 if ( inSocket != null ) {
                 inSocket.close();
                 inSocket = null ;
                 }
                 outSocket.close();
                 outSocket = null ;
                 */

                if (index >= 0)
                {
                    listener.tableModel.setValueAt(getMessage("done00", "Done"),
                            1 + index, STATE_COLUMN);

                }
            }
            catch (Exception e)
            {
                StringWriter st = new StringWriter();
                PrintWriter wr = new PrintWriter(st);
                int index = listener.connections.indexOf(this);

                if (index >= 0)
                    listener.tableModel.setValueAt(getMessage("error00", "Error"), 1 + index, STATE_COLUMN);
                e.printStackTrace(wr);
                wr.close();
                outputText.append(st.toString());
                halt();
            }
        }

        synchronized void wakeUp()
        {
            this.notifyAll();
        }

        public void halt()
        {
            try
            {
                if (requestSocketRR != null) requestSocketRR.halt();
                if (ResponseSocketRR != null) ResponseSocketRR.halt();
                if (inSocket != null) inSocket.close();
                inSocket = null;
                if (outSocket != null) outSocket.close();
                outSocket = null;
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }

        public void remove()
        {
            int index = -1;

            try
            {
                halt();
                index = listener.connections.indexOf(this);
                listener.tableModel.removeRow(index + 1);
                listener.connections.remove(index);
            }
            catch (Exception e)
            {
                System.err.println("index:=" + index + this);
                e.printStackTrace();
            }
        }
    }


    class Listener extends JPanel
    {
        public Socket inputSocket = null;
        public Socket outputSocket = null;
        public JTextField portField = null;
        public JTextField hostField = null;
        public JTextField tPortField = null;
        public JCheckBox isProxyBox = null;
        public JCheckBox holdRequestBox = null;
        public JCheckBox holdResponseBox = null;
        public JButton stopButton = null;
        public JButton removeButton = null;
        public JButton removeAllButton = null;
        public JCheckBox xmlFormatBox = null;
        public JButton saveButton = null;
        public JButton resendButton = null;
        public JButton sendButton = null;
        public JButton switchButton = null;
        public JButton closeButton = null;
        public JTable connectionTable = null;
        public DefaultTableModel tableModel = null;
        public JSplitPane outPane = null;
        public ServerSocket sSocket = null;
        public SocketWaiter sw = null;
        public JPanel leftPanel = null;
        public JPanel rightPanel = null;
        public JTabbedPane notebook = null;
        public String HTTPProxyHost = null;
        public int HTTPProxyPort = 80;

        final public Vector connections = new Vector();

        public Listener(JTabbedPane _notebook, String name,
                        int listenPort, String host, int targetPort,
                        boolean isProxy)
        {
            notebook = _notebook;
            if (name == null) name = getMessage("port01", "Port") + " " + listenPort;

            this.setLayout(new BorderLayout());

            // 1st component is just a row of labels and 1-line entry fields
            /////////////////////////////////////////////////////////////////////
            JPanel top = new JPanel();

            top.setLayout(new BoxLayout(top, BoxLayout.X_AXIS));
            top.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
            final String start = getMessage("start00", "Start");

            top.add(stopButton = new JButton(start));
            top.add(Box.createRigidArea(new Dimension(5, 0)));
            top.add(new JLabel("  " + getMessage("listenPort01", "Listen Port:") + " ", SwingConstants.RIGHT));
            top.add(portField = new JTextField("" + listenPort, 4));
            top.add(new JLabel("  " + getMessage("host00", "Host:"), SwingConstants.RIGHT));
            top.add(hostField = new JTextField(host, 30));
            top.add(new JLabel("  " + getMessage("port02", "Port:") + " ", SwingConstants.RIGHT));
            top.add(tPortField = new JTextField("" + targetPort, 4));
            top.add(Box.createRigidArea(new Dimension(5, 0)));
            top.add(isProxyBox = new JCheckBox(getMessage("proxy00", "Proxy")));
            top.add(holdRequestBox = new JCheckBox(getMessage("holdRequest", "Hold Request")));
            top.add(holdResponseBox = new JCheckBox(getMessage("holdResponse", "Hold Response")));

            isProxyBox.addChangeListener(new BasicButtonListener(isProxyBox)
            {
                public void stateChanged(ChangeEvent event)
                {
                    JCheckBox box = (JCheckBox) event.getSource();
                    boolean state = box.isSelected();

                    tPortField.setEnabled(!state);
                    hostField.setEnabled(!state);
                }
            }
            );

            isProxyBox.setSelected(isProxy);

            portField.setEditable(false);
            portField.setMaximumSize(new Dimension(50, Short.MAX_VALUE));
            hostField.setEditable(false);
            hostField.setMaximumSize(new Dimension(85, Short.MAX_VALUE));
            tPortField.setEditable(false);
            tPortField.setMaximumSize(new Dimension(50, Short.MAX_VALUE));

            stopButton.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    if (getMessage("stop00", "Stop").equals(event.getActionCommand())) stop();
                    if (start.equals(event.getActionCommand())) start();
                }
            }
            );

            this.add(top, BorderLayout.NORTH);

            // 2nd component is a split pane with a table on the top
            // and the request/response text areas on the bottom
            /////////////////////////////////////////////////////////////////////

            tableModel = new DefaultTableModel(new String[]{
                getMessage("state00", "State"),
                getMessage("time00", "Time"),
                getMessage("requestHost00", "Request Host"),
                getMessage("targetHost", "Target Host"),
                getMessage("request00", "Request...")
            }, 0);

            connectionTable = new JTable(1, 2);
            connectionTable.setModel(tableModel);
            connectionTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
            // Reduce the STATE column and increase the REQ column
            TableColumn col;

            col = connectionTable.getColumnModel().getColumn(STATE_COLUMN);
            col.setMaxWidth(col.getPreferredWidth() / 2);
            col = connectionTable.getColumnModel().getColumn(REQ_COLUMN);
            col.setPreferredWidth(col.getPreferredWidth() * 2);


            ListSelectionModel sel = connectionTable.getSelectionModel();


            tableModel.addRow(new Object[]{
                "---", getMessage("mostRecent00", "Most Recent"), "---", "---", "---"
            }
            );

            JPanel tablePane = new JPanel();

            tablePane.setLayout(new BorderLayout());

            JScrollPane tableScrollPane = new JScrollPane(connectionTable);

            tablePane.add(tableScrollPane, BorderLayout.CENTER);
            JPanel buttons = new JPanel();

            buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
            buttons.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
            final String removeSelected = getMessage("removeSelected00", "Remove Selected");

            buttons.add(removeButton = new JButton(removeSelected));
            buttons.add(Box.createRigidArea(new Dimension(5, 0)));
            final String removeAll = getMessage("removeAll00", "Remove All");

            buttons.add(removeAllButton = new JButton(removeAll));
            tablePane.add(buttons, BorderLayout.SOUTH);

            removeButton.setEnabled(false);
            removeButton.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    if (removeSelected.equals(event.getActionCommand())) remove();
                }
            }
            );

            removeAllButton.setEnabled(false);
            removeAllButton.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    if (removeAll.equals(event.getActionCommand())) removeAll();
                }
            }
            );

            // Add Response Section
            /////////////////////////////////////////////////////////////////////
            JPanel pane2 = new JPanel();

            pane2.setLayout(new BorderLayout());

            leftPanel = new JPanel();
            leftPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
            leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
            leftPanel.add(new JLabel("  " + getMessage("request01", "Request")));
            leftPanel.add(new JLabel(" " + getMessage("wait01", "Waiting for connection")));

            rightPanel = new JPanel();
            rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
            rightPanel.add(new JLabel("  " + getMessage("response00", "Response")));
            rightPanel.add(new JLabel(""));

            outPane = new JSplitPane(0, leftPanel, rightPanel);
            outPane.setDividerSize(4);
            pane2.add(outPane, BorderLayout.CENTER);

            JPanel bottomButtons = new JPanel();

            bottomButtons.setLayout(new BoxLayout(bottomButtons, BoxLayout.X_AXIS));
            bottomButtons.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
            bottomButtons.add(xmlFormatBox = new JCheckBox(getMessage("xmlFormat00", "XML Format")));
            bottomButtons.add(Box.createRigidArea(new Dimension(5, 0)));

            final String save = getMessage("save00", "Save");
            bottomButtons.add(saveButton = new JButton(save));
            bottomButtons.add(Box.createRigidArea(new Dimension(5, 0)));

            final String resend = getMessage("resend00", "Resend");
            bottomButtons.add(resendButton = new JButton(resend));
            bottomButtons.add(Box.createRigidArea(new Dimension(5, 0)));

            final String sendStr = getMessage("send", "Send");
            bottomButtons.add(sendButton = new JButton(sendStr));
            bottomButtons.add(Box.createRigidArea(new Dimension(5, 0)));

            final String switchStr = getMessage("switch00", "Switch Layout");
            bottomButtons.add(switchButton = new JButton(switchStr));
            bottomButtons.add(Box.createHorizontalGlue());

            final String close = getMessage("close00", "Close");
            bottomButtons.add(closeButton = new JButton(close));
            pane2.add(bottomButtons, BorderLayout.SOUTH);

            saveButton.setEnabled(false);
            saveButton.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    if (save.equals(event.getActionCommand())) save();
                }
            }
            );

            resendButton.setEnabled(false);
            resendButton.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    if (resend.equals(event.getActionCommand())) resend();
                }
            }
            );

⌨️ 快捷键说明

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