📄 httpproxy.java
字号:
package section18;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class HttpProxy extends Thread {
static public int CONNECT_RETRIES=5;
static public int CONNECT_PAUSE=5;
static public int TIMEOUT=500;
static public int BUFSIZE=1024;
static public boolean logging=true;
static public OutputStream log=null;
public String head="";
protected Socket socket;
public HttpProxy(Socket s)
{
socket=s;
}
public void writeLog(byte[] bytes,int offset,int len)throws IOException
{
for(int i=offset;i<len;i++)
{
log.write((int)bytes[i]);
}
}
public void run()
{
String line;
String host;
int port=80;
Socket outbound=null;
try
{
socket.setSoTimeout(TIMEOUT);
InputStream is=socket.getInputStream();
host=getHost(is);
try
{
while(true)
{
int n=-1;
n=host.indexOf(":");
if(n!=-1)
{
port=Integer.parseInt(host.substring(n+1));
host=host.substring(0,n);
}
int retry=CONNECT_RETRIES;
System.out.println("host is "+host);
while(retry--!=0)
{
try{
outbound=new Socket(host,port);
break;
}catch(Exception e)
{
e.printStackTrace();
}
try{
Thread.sleep(CONNECT_PAUSE);
}catch(InterruptedException e){e.printStackTrace();}
}
if(outbound==null)break;
outbound.setSoTimeout(TIMEOUT);
OutputStream os1=outbound.getOutputStream();
byte[] bytes=head.getBytes();
os1.write(bytes,0,bytes.length);
pipe(socket.getInputStream(),outbound.getInputStream(),socket.getOutputStream(),os1);
break;
}
}catch(IOException e){}
}catch(IOException e){}
finally
{
try{socket.close();}catch(Exception e1){e1.printStackTrace();}
try{outbound.close();}catch(Exception e2){e2.printStackTrace();}
}
}
public String getHost(InputStream is)throws IOException
{
String host=null;
DataInputStream dis=new DataInputStream(is);
host=dis.readLine();
head=host;
int n=host.indexOf("//");
if(n!=-1)host=host.substring(n+2);
n=host.indexOf('/');
if(n!=-1)host=host.substring(0,n);
return host;
}
void pipe(InputStream is0,InputStream is1,OutputStream os0,OutputStream os1)throws IOException
{
try{
int i;
byte bytes[]=new byte[BUFSIZE];
while(true)
{
try{
if((i=is0.read(bytes))>0)
{
os1.write(bytes,0,i);
if(logging)writeLog(bytes,0,i);
}
else if(i<0)
break;
}
catch(InterruptedIOException e){}
try
{
if((i=is1.read(bytes))>0)
{
os0.write(bytes,0,i);
if(logging)writeLog(bytes,0,i);
}
else if(i<0)break;
}catch(InterruptedIOException e)
{
e.printStackTrace();
}
}
}catch(Exception e)
{
System.out.println("Pipe Exception:"+e);
e.printStackTrace();
}
}
static public void main(String args[])
{
System.out.println("Starting proxy on port 808");
int port=808;
HttpProxy.log=System.out;
HttpProxy.logging=true;
ServerSocket ssocket;
Socket socket;
try{
ssocket=new ServerSocket(port);
while(true)
{
socket=ssocket.accept();
new HttpProxy(socket).start();
}
}catch(Exception e)
{
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -