📄 tcpinterceptor.java
字号:
notebook.addTab(name, this);
notebook.repaint();
notebook.setSelectedIndex(notebook.getTabCount() - 1);
}
}
class SocketWaiter extends Thread
{
ServerSocket sSocket = null;
Listener listener;
int port;
boolean pleaseStop = false;
public SocketWaiter(Listener l, int p)
{
listener = l;
port = p;
start();
}
public void run()
{
try
{
listener.setLeft(new JLabel(getMessage("wait00", " Waiting for Connection...")));
listener.repaint();
sSocket = new ServerSocket(port);
for (; ;)
{
Socket inSocket = sSocket.accept();
if (pleaseStop) break;
new Connection(listener, inSocket);
inSocket = null;
}
}
catch (Exception exp)
{
if (!"socket closed".equals(exp.getMessage()))
{
JLabel tmp = new JLabel(exp.toString());
tmp.setForeground(Color.red);
listener.setLeft(tmp);
listener.setRight(new JLabel(""));
listener.stop();
}
}
}
public void halt()
{
try
{
pleaseStop = true;
new Socket("127.0.0.1", port);
if (sSocket != null) sSocket.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
class SocketRR extends Thread
{
Socket inSocket = null;
Socket outSocket = null;
JTextArea textArea;
JButton sendXMLButton;
InputStream in = null;
OutputStream out = null;
boolean xmlFormat;
volatile boolean done = false;
TableModel tableModel = null;
int tableIndex = 0;
String type = null;
Connection myConnection = null;
boolean holdXMLUntilEdited = false;
/**
*
* @param c
* @param inputSocket
* @param inputStream
* @param outputSocket
* @param outputStream
* @param _textArea
* @param format
* @param tModel
* @param index
* @param type
* @param hold Whether to hold on to the XML message until the user has
* edited it - if false, message is sent on immediately (as per
* original TcpMon behaviour).
* @param send a button which allows the user to send the message on manually,
* but is only active if 'hold' is true.
*/
public SocketRR(Connection c, Socket inputSocket, InputStream inputStream,
Socket outputSocket, OutputStream outputStream,
JTextArea _textArea, boolean format,
TableModel tModel, int index, final String type,
boolean hold, JButton send)
{
inSocket = inputSocket;
in = inputStream;
outSocket = outputSocket;
out = outputStream;
textArea = _textArea;
xmlFormat = format;
tableModel = tModel;
tableIndex = index;
this.type = type;
myConnection = c;
holdXMLUntilEdited = hold;
sendXMLButton = send;
start();
}
public boolean isDone()
{
return (done);
}
public void run()
{
try
{
// fancy pants hold-and-forward stuff dies if these buffer lengths are exceeded for a
// single http request or response... - CB
byte[] buffer = new byte[32768];
byte[] tmpbuffer = new byte[65536];
int saved = 0;
int len;
// int i1, i2;
int i;
int reqSaved = 0;
int tabWidth = 3;
boolean atMargin = true;
//if ( inSocket != null ) inSocket.setSoTimeout( 10 );
//if ( outSocket != null ) outSocket.setSoTimeout( 10 );
if (tableModel != null)
{
String tmpStr = (String) tableModel.getValueAt(tableIndex,
REQ_COLUMN);
if (!"".equals(tmpStr))
reqSaved = tmpStr.length();
}
//XXX clear out old listeners?
if (holdXMLUntilEdited)
sendXMLButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
sendXMLButton.setEnabled(false);
String text = textArea.getText();
text = fixContentLengthHeaders(text);
textArea.setText(text);
byte[] buffer = text.getBytes(); // XXX do we need to force "UTF-8" ?
out.write(buffer, 0, buffer.length);
}
catch (Exception e2)
{
e2.printStackTrace();
}
finally
{
finishUp();
}
}
});
readWriteLoop(buffer, saved, reqSaved, tmpbuffer, tabWidth);
// this.sleep(3); // Let other threads have a chance to run
// halt();
// Only set the 'done' flag if we were reading from a
// Socket - if we were reading from an input stream then
// we'll let the other side control when we're done
// if ( inSocket != null ) done = true ;
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (!holdXMLUntilEdited)
finishUp();
}
}
private void readWriteLoop(byte[] buffer, int saved, int reqSaved, byte[] tmpbuffer, int tabWidth) throws IOException
{
int len;
int i;
readloop:
for (; ;)
{
if (done) break;
len = buffer.length;
// Used to be 1, but if we block it doesn't matter
// however 1 will break with some servers, including apache
if (len == 0) len = buffer.length;
if (saved + len > buffer.length) len = buffer.length - saved;
int len1 = 0;
while (len1 == 0)
{
try
{
len1 = in.read(buffer, saved, len);
}
catch (Exception ex)
{
if (done && saved == 0) break readloop;
len1 = -1;
break;
}
}
len = len1;
if (len == -1 && saved == 0) break;
if (len == -1) done = true;
// No matter how we may (or may not) format it, send it
// on unformatted - we don't want to mess with how its
// sent to the other side, just how its displayed
if (!holdXMLUntilEdited && out != null && len > 0)
{
out.write(buffer, saved, len);
}
if (tableModel != null && reqSaved < 50)
{
String old = (String) tableModel.getValueAt(tableIndex,
REQ_COLUMN);
old = old + new String(buffer, saved, len);
if (old.length() > 50)
old = old.substring(0, 50);
reqSaved = old.length();
if ((i = old.indexOf('\n')) > 0)
{
old = old.substring(0, i - 1);
reqSaved = 50;
}
tableModel.setValueAt(old, tableIndex, REQ_COLUMN);
}
if (xmlFormat)
{
saved = xmlFormatTextArea(saved, len, buffer, tmpbuffer, tabWidth);
}
else
{
textArea.append(new String(buffer, 0, len));
}
if (holdXMLUntilEdited)
{
sendXMLButton.setEnabled(true);
}
// this.sleep(3); // Let other threads have a chance to run
}
}
private int xmlFormatTextArea(int saved, int len, byte[] buffer, byte[] tmpbuffer, int tabWidth)
{
int i;
boolean atMargin;
// Do XML Formatting
boolean inXML = false;
int bufferLen = saved;
if (len != -1) bufferLen += len;
int i1 = 0;
int i2 = 0;
int thisIndent = -1, nextIndent = -1, previousIndent = -1;
saved = 0;
for (; i1 < bufferLen; i1++)
{
// Except when we're at EOF, saved last char
if (len != -1 && i1 + 1 == bufferLen)
{
if (buffer[i1] > 32) // CB - But ONLY do that if it isn't a character we want!!!
{
tmpbuffer[i2++] = buffer[i1];
saved = 0;
}
else
saved = 1;
break;
}
thisIndent = -1;
if (buffer[i1] == '<' && buffer[i1 + 1] != '/')
{
previousIndent = nextIndent++;
thisIndent = nextIndent;
inXML = true;
}
if (buffer[i1] == '<' && buffer[i1 + 1] == '/')
{
if (previousIndent > nextIndent)
thisIndent = nextIndent;
previousIndent = nextIndent--;
inXML = true;
}
if (buffer[i1] == '/' && buffer[i1 + 1] == '>')
{
previousIndent = nextIndent--;
inXML = true;
}
if (thisIndent != -1)
{
if (thisIndent > 0) tmpbuffer[i2++] = (byte) '\n';
for (i = tabWidth * thisIndent; i > 0; i--)
tmpbuffer[i2++] = (byte) ' ';
}
atMargin = (buffer[i1] == '\n' || buffer[i1] == '\r');
if (!inXML || !atMargin)
{
tmpbuffer[i2++] = buffer[i1];
}
}
String text = new String(tmpbuffer, 0, i2);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -