📄 httpproxy.java
字号:
import java.net.*;
import java.io.*;
//代理服务的线程服务类
public class HttpProxy extends Thread
{
//尝试对远程连接的次数
static public int CONNECT_RETRIES=5;
//间隔多少时间尝试一次对远程连接
static public int CONNECT_PAUSE=5;
//等待用户从浏览器发布数据的时间
static public int TIMEOUT=500;
//socket的输入缓冲
static public int BUFSIZ=1024;
//log标识
static public boolean logging = true;
//log信息的输出流
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
{
//超时时间为TIMEOUT毫秒
socket.setSoTimeout(TIMEOUT);
//得到socket输入流
InputStream is=socket.getInputStream();
host=getHost(is);
//OutputStream os=socket.getOutputStream();
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();
}
//没间隔一段时间,尝试一次连接
Thread.sleep(CONNECT_PAUSE);
}
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 (Exception 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);
//jdk1.3API中readLine方法已经被BufferedReader替换
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[BUFSIZ];
while (true)
{
try
{
//将客户端输入流写入服务端输出流
//如果inputstream没有数据read方法阻塞
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
{
//将服务端输入流写入客户端输出流
//如果inputstream没有数据,read方法阻塞
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 ssock;
Socket sock;
try
{
ssock=new ServerSocket(port);
while (true)
{
sock =ssock.accept();
(new HttpProxy(sock)).start();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -