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

📄 tcpinterceptor.java

📁 JAVA开源LDAP浏览器jxplorer的源码!
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
            textArea.append(text);

            // Shift saved bytes to the beginning
            for (i = 0; i < saved; i++)
                buffer[i] = buffer[bufferLen - saved + i];
            return saved;
        }

        private void finishUp()
        {
            done = true;
            try
            {
                if (out != null)
                {
                    out.flush();
                    if (null != outSocket)
                        outSocket.shutdownOutput();
                    else
                        out.close();
                    out = null;
                }
            }
            catch (Exception e)
            {
                ;
            }
            try
            {
                if (in != null)
                {
                    if (inSocket != null)
                        inSocket.shutdownInput();
                    else
                        in.close();
                    in = null;
                }
            }
            catch (Exception e)
            {
                ;
            }
            myConnection.wakeUp();
        }

        public void halt()
        {
            try
            {
                if (inSocket != null) inSocket.close();
                if (outSocket != null) outSocket.close();
                inSocket = null;
                outSocket = null;
                if (in != null) in.close();
                if (out != null) out.close();
                in = null;
                out = null;
                done = true;
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }


    class Connection extends Thread
    {
        Listener listener;
        boolean active;
        String fromHost;
        String time;
        JTextArea inputText = null;
        JScrollPane inputScroll = null;
        JTextArea outputText = null;
        JScrollPane outputScroll = null;
        Socket inSocket = null;
        Socket outSocket = null;
        Thread clientThread = null;
        Thread serverThread = null;
        SocketRR requestSocketRR = null;
        SocketRR ResponseSocketRR = null;
        InputStream inputStream = null;

        String HTTPProxyHost = null;
        int HTTPProxyPort = 80;

        public Connection(Listener l)
        {
            listener = l;
            HTTPProxyHost = l.HTTPProxyHost;
            HTTPProxyPort = l.HTTPProxyPort;
        }

        public Connection(Listener l, Socket s)
        {
            this(l);
            inSocket = s;
            start();
        }

        public Connection(Listener l, InputStream in)
        {
            this(l);
            inputStream = in;
            start();
        }

        public void run()
        {
            try
            {
                active = true;

                HTTPProxyHost = System.getProperty("http.proxyHost");
                if (HTTPProxyHost != null && HTTPProxyHost.equals(""))
                    HTTPProxyHost = null;

                if (HTTPProxyHost != null)
                {
                    String tmp = System.getProperty("http.proxyPort");

                    if (tmp != null && tmp.equals("")) tmp = null;
                    if (tmp == null)
                        HTTPProxyPort = 80;
                    else
                        HTTPProxyPort = Integer.parseInt(tmp);
                }

                if (inSocket != null)
                    fromHost = (inSocket.getInetAddress()).getHostName();
                else
                    fromHost = "resend";

                DateFormat df = new SimpleDateFormat("MM/dd/yy hh:mm:ss aa");

                time = df.format(new Date());

                int count = listener.connections.size();

                listener.tableModel.insertRow(count + 1, new Object[]{
                    getMessage("active00", "Active"),
                    time,
                    fromHost,
                    listener.hostField.getText(), ""
                }
                );
                listener.connections.add(this);
                inputText = new JTextArea(null, null, 20, 80);
                inputScroll = new JScrollPane(inputText);
                outputText = new JTextArea(null, null, 20, 80);
                outputScroll = new JScrollPane(outputText);

                ListSelectionModel lsm = listener.connectionTable.getSelectionModel();

                if (count == 0 || lsm.getLeadSelectionIndex() == 0)
                {
                    listener.outPane.setVisible(false);
                    int divLoc = listener.outPane.getDividerLocation();

                    listener.setLeft(inputScroll);
                    listener.setRight(outputScroll);

                    listener.removeButton.setEnabled(false);
                    listener.removeAllButton.setEnabled(true);
                    listener.saveButton.setEnabled(true);
                    listener.resendButton.setEnabled(true);
                    listener.outPane.setDividerLocation(divLoc);
                    listener.outPane.setVisible(true);
                }

                String targetHost = listener.hostField.getText();
                int targetPort = Integer.parseInt(listener.tPortField.getText());

                InputStream inSocketInputStream = inputStream;
                OutputStream inSocketOutputStream = null;

                InputStream tmpIn2 = null;
                OutputStream tmpOut2 = null;

                if (inSocketInputStream == null)
                    inSocketInputStream = inSocket.getInputStream();

                if (inSocket != null)
                    inSocketOutputStream = inSocket.getOutputStream();

                String bufferedData = null;
                StringBuffer buf = null;

                int index = listener.connections.indexOf(this);

                if (listener.isProxyBox.isSelected() || HTTPProxyHost != null)
                {
                    // Check if we're a proxy
                    byte[] b = new byte[1];

                    buf = new StringBuffer();
                    String s;

                    for (; ;)
                    {
                        int len;

                        len = inSocketInputStream.read(b, 0, 1);
                        if (len == -1) break;
                        s = new String(b);
                        buf.append(s);
                        if (b[0] != '\n') continue;
                        break;
                    }

                    bufferedData = buf.toString();
                    inputText.append(bufferedData);

                    if (bufferedData.startsWith("GET ") ||
                            bufferedData.startsWith("POST "))
                    {
                        int start, end;
                        URL url;

                        start = bufferedData.indexOf(' ') + 1;
                        while (bufferedData.charAt(start) == ' ') start++;
                        end = bufferedData.indexOf(' ', start);
                        String urlString = bufferedData.substring(start, end);

                        if (urlString.charAt(0) == '/') urlString = urlString.substring(1);
                        if (listener.isProxyBox.isSelected())
                        {
                            url = new URL(urlString);
                            targetHost = url.getHost();
                            targetPort = url.getPort();
                            if (targetPort == -1) targetPort = 80;

                            listener.tableModel.setValueAt(targetHost, index + 1,
                                    OUTHOST_COLUMN);
                            bufferedData = bufferedData.substring(0, start) +
                                    url.getFile() +
                                    bufferedData.substring(end);
                        }
                        else
                        {
                            url = new URL("http://" + targetHost + ":" +
                                    targetPort + "/" + urlString);

                            listener.tableModel.setValueAt(targetHost, index + 1,
                                    OUTHOST_COLUMN);
                            bufferedData = bufferedData.substring(0, start) +
                                    url.toExternalForm() +
                                    bufferedData.substring(end);

                            targetHost = HTTPProxyHost;
                            targetPort = HTTPProxyPort;
                        }

                    }
                }
                else
                /*
                 *     Change Host: header to point to correct host
                 *     keep reading a byte at a time until we find
                 *     a line (a data block ending in '\n') that starts with
                 *     'Host' - in which case we replace it with our own host.
                 */
                {
                    //
                    //
                    //
                    byte[] b1 = new byte[1];

                    buf = new StringBuffer();
                    String s1;
                    String lastLine = null;

                    for (; ;)
                    {
                        int len;
                        len = inSocketInputStream.read(b1, 0, 1);
                        if (len == -1)
                            break;
                        s1 = new String(b1);
                        buf.append(s1);
                        if (b1[0] != '\n')
                            continue;
                        // we have a complete line
                        String line = buf.toString();

                        buf.setLength(0);
                        // check to see if we have found Host: header
                        if (line.startsWith("Host: "))
                        {
                            // we need to update the hostname to target host
                            String newHost = "Host: " + targetHost + "\r\n";

                            bufferedData = bufferedData.concat(newHost);
                            break;
                        }
                        // add it to our headers so far
                        if (bufferedData == null)
                            bufferedData = line;
                        else
                            bufferedData = bufferedData.concat(line);

                        // failsafe
                        if (line.equals("\r\n")) break;
                        if ("\n".equals(lastLine) && line.equals("\n")) break;
                        lastLine = line;
                    }
                    if (bufferedData != null)
                    {
                        inputText.append(bufferedData);
                        int idx = bufferedData.length() < 50 ? bufferedData.length() : 50;
                        s1 = bufferedData.substring(0, idx);
                        int i = s1.indexOf('\n');

                        if (i > 0) s1 = s1.substring(0, i - 1);
                        s1 = s1 + "                           " +
                                "                       ";
                        s1 = s1.substring(0, 51);
                        listener.tableModel.setValueAt(s1, index + 1,
                                REQ_COLUMN);
                    }
                }

                if (targetPort == -1) targetPort = 80;
                outSocket = new Socket(targetHost, targetPort);

                tmpIn2 = outSocket.getInputStream();
                tmpOut2 = outSocket.getOutputStream();

                if (!listener.holdRequestBox.isSelected() && bufferedData != null)
                {
                    byte[] b = bufferedData.getBytes();
                    tmpOut2.write(b);
                }

                boolean format = listener.xmlFormatBox.isSelected();

                requestSocketRR = new SocketRR(this, inSocket, inSocketInputStream, outSocket,
                        tmpOut2, inputText, format,
                        listener.tableModel, index + 1, "request:",
                        listener.holdRequestBox.isSelected(), listener.sendButton);
                ResponseSocketRR = new SocketRR(this, outSocket, tmpIn2, inSocket,
                        inSocketOutputStream, outputText, format,
                        null, 0, "response:",
                        listener.holdResponseBox.isSelected(), listener.sendButton);

                while (requestSocketRR != null || ResponseSocketRR != null)

⌨️ 快捷键说明

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