📄 tcpmon.java
字号:
len = tmpIn1.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 " ) || bufferedData.startsWith( "PUT " ) || bufferedData.startsWith( "DELETE " ) ) { 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 // byte[] b1 = new byte[1]; buf = new StringBuffer(); String s1; String lastLine = null ; for ( ; ; ) { int len ; len = tmpIn1.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 + ":" + listenPort + "\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 ( bufferedData != null ) { byte[] b = bufferedData.getBytes(); tmpOut2.write( b ); slowLink.pump(b.length); } boolean format = listener.xmlFormatBox.isSelected(); boolean numeric = listener.numericBox.isSelected(); //this is the channel to the endpoint rr1 = new SocketRR(this, inSocket, tmpIn1, outSocket, tmpOut2, inputText, format, numeric, listener.tableModel, index + 1, "request:", slowLink); //create the response slow link from the inbound slow link SlowLinkSimulator responseLink = new SlowLinkSimulator(slowLink); //this is the channel from the endpoint rr2 = new SocketRR( this, outSocket, tmpIn2, inSocket, tmpOut1, outputText, format, numeric, null, 0, "response:", responseLink); while ( rr1 != null || rr2 != null ) { // 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( !rr2.isDone() ) if (null != rr1 && rr1.isDone()) { if ( index >= 0 && rr2 != null) { listener.tableModel.setValueAt(getMessage("resp00", "Resp"), 1 + index, STATE_COLUMN ); } rr1 = null; } if (null != rr2 && rr2.isDone()) { if ( index >= 0 && rr1 != null ) { listener.tableModel.setValueAt(getMessage("req00", "Req"), 1 + index, STATE_COLUMN ); } rr2 = null; } // Thread.sleep( 10 ); synchronized ( this) { this.wait(1000); //Safety just incase we're not told to wake up. } } // System.out.println("Done "); // rr1.halt(); // rr2.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(); if(outputText!=null) { outputText.append( st.toString() ); } else { //something went wrong before we had the output area System.out.println(st.toString()); } halt(); } } synchronized void wakeUp() { this.notifyAll(); } public void halt() { try { if ( rr1 != null ) { rr1.halt(); } if ( rr2 != null ) { rr2.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(); } } } /** * this is one of the tabbed panels that acts as the actual proxy */ 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 JButton stopButton = null ; public JButton removeButton = null ; public JButton removeAllButton = null ; public JCheckBox xmlFormatBox = null ; public JCheckBox numericBox = null ; public JButton saveButton = null ; public JButton resendButton = 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 ; public int delayBytes = 0; public int delayTime = 0; public SlowLinkSimulator slowLink; public final Vector connections = new Vector(); /** * create a listener * @param _notebook * @param name * @param listenPort * @param host * @param targetPort * @param isProxy * @param slowLink optional reference to a slow connection */ public Listener(JTabbedPane _notebook, String name, int listenPort, String host, int targetPort, boolean isProxy, SlowLinkSimulator slowLink) { notebook = _notebook ; if ( name == null ) { name = getMessage("port01", "Port") + " " + listenPort ; } //set the slow link to the passed down link if(slowLink!=null) { this.slowLink=slowLink; } else { //or make up a no-op one. this.slowLink=new SlowLinkSimulator(0,0); } this.setLayout( new BorderLayout() ); // 1st component is just a row of labels and 1-line entry fields ///////////////////////////////////////////////////////////////////// JPanel top = new JPanel();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -