📄 simpleproxy.java
字号:
/*
* 代理服务器
*/
import java.net.*;
import java.io.*;
import java.util.*;
import java.text.*;
import java.text.DateFormat.*;
import java.lang.reflect.*;
public class SimpleProxy extends Thread {
static public int Retry_Num=3;
static public int Connect_Delay=3;
static public int TimeOut=20;
static public int BufferSize=1024;
static public boolean Passport = false;
static public OutputStream Message=null;
// 传入数据用Socket
protected Socket socket;
// 上级代理服务器
static private String Parent=null;
static private int ParentPort=-100;
static public void ParentProxy(String parentname, int portnum) {
Parent=parentname;
ParentPort=portnum;
}
// 在给定Socket上创建一个代理线程。
public SimpleProxy(Socket sock) {
socket=sock;
start();
}
public void recordLog(int c) throws IOException {
Message.write(c);
}
//记录、输出日志
public void recordLog(byte[] bytes,int c, int lenth) throws IOException {
for (int i=0;i<lenth;i++)
recordLog((int)bytes[c+i]);
}
// 默认情况下,日志信息输出到标准输出设备
public String getHostName(String url, String host, int portnum, Socket sock) {
DateFormat ware=DateFormat.getDateTimeInstance();
System.out.println(ware.format(new Date()) + " - " + url + " "
+ sock.getInetAddress()+"\n");
return host;
}
// 执行操作的线程
public void run() {
String strline;
String strhost;
int port=80;
Socket out=null;
try {
socket.setSoTimeout(TimeOut);
InputStream instr=socket.getInputStream();
OutputStream osstr=null;
try {
// 获取请求行的内容
strline="";
strhost="";
int state=0;
boolean blank;
while (true) {
int l=instr.read();
if (l==-1) break;
if (Passport) recordLog(l);
blank=Character.isWhitespace((char)l);
switch (state) {
case 0:
if (blank) continue;
state=1;
case 1:
if (blank) {
state=2;
continue;
}
strline=strline+(char)l;
break;
case 2:
if (blank) continue;
// 跳过空白字符
state=3;
case 3:
if (blank) {
state=4;
// 只取出主机名称部分
String host=strhost;
int n;
n=strhost.indexOf("//");
if (n!=-1) strhost=strhost.substring(n+2);
n=strhost.indexOf('/');
if (n!=-1) strhost=strhost.substring(0,n);
// 分析可能存在的端口号
n=strhost.indexOf(":");
if (n!=-1) {
port=Integer.parseInt(strhost.substring(n+1));
strhost=strhost.substring(0,n);
}
strhost=getHostName(host,strhost,port,socket);
if (Parent!=null) {
strhost=Parent;
port=ParentPort;
//处理上级代理
}
int retry=Retry_Num;
while (retry--!=0) {
try {
out=new Socket(strhost,port);
break;
} catch (Exception e) { }
// 等待
Thread.sleep(Connect_Delay);
}
if (out==null) break;
out.setSoTimeout(TimeOut);
osstr=out.getOutputStream();
osstr.write(strline.getBytes());
osstr.write(' ');
osstr.write(host.getBytes());
osstr.write(' ');
check(instr,out.getInputStream(),osstr,socket.getOutputStream());
break;
}
strhost=strhost+(char)l;
break;
}
}
}
catch (IOException e) {
}
} catch (Exception e) {
}
finally {
try {
socket.close();
} catch (Exception e1) {
}
//关闭socket
try {
out.close();
} catch (Exception e2) {
}
//关闭socket
}
}
void check(InputStream instr0, InputStream instr1,
OutputStream outstr0, OutputStream outstr1) throws IOException {
try {
int len;
byte bytes[]=new byte[BufferSize];
while (true) {
try {
if ((len=instr0.read(bytes))>0) {
outstr0.write(bytes,0,len);
if (Passport)
recordLog(bytes,0,len);
}
else if (len<0)
break;
} catch (InterruptedIOException e) {
}
try {
if ((len=instr1.read(bytes))>0) {
outstr1.write(bytes,0,len);
if (Passport)
recordLog(bytes,0,len);
}
else if (len<0)
break;
} catch (InterruptedIOException e) {
}
}
} catch (Exception e0) {
System.out.println("异常: " + e0);
}
}
static public void startProxy(int port,Class test) {
ServerSocket sersock;
Socket sock;
try {
sersock=new ServerSocket(port);
while (true) {
Class [] sarg = new Class[1];
Object [] arg= new Object[1];
sarg[0]=Socket.class;
try {
Constructor layer = test.getDeclaredConstructor(sarg);
arg[0]=sersock.accept();
layer.newInstance(arg);
// 创建SimpleProxy及其派生类的实例
} catch (Exception e) {
Socket esock = (Socket)arg[0];
try { esock.close();
} catch (Exception ec) {}
}
}
} catch (IOException e) {
}
}
// main方法,用于简单测试
static public void main(String args[]) {
System.out.println("在端口808启动代理服务器\n");
SimpleProxy.Message=System.out;
SimpleProxy.Passport=false;
SimpleProxy.startProxy(808,SimpleProxy.class);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -