import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.io.OutputStream;
import java.lang.reflect.Constructor;
import java.net.ServerSocket;
import java.net.Socket;
public class LANProxy extends Thread { // 操作实现代理服务器的类
protected Socket socket; // 传入数据用的Socket
private static String parent = null; // 上级代理服务器
private static int parentPort = -1;
public static int RETRIES = 5; // 在放弃之前尝试连接远程主机的次数
public static int PAUSE = 5; // 在两次连接尝试之间的暂停时间
public static int TIMEOUT = 50; // 等待Socket输入的等待时间
public static int BUFSIZ = 1024; // 输入的缓冲大小
public static boolean logging = false; // 是否要求代理服务器在日志中记录所有已传输的数据
public static OutputStream log = null; // 默认日志例程将向该OutputStream对象输出日志信息
public void run() { // 执行操作的线程
String line;
String host;
int port = 80;
Socket outbound = null;
try {
socket.setSoTimeout(TIMEOUT); // 设置超时时间
InputStream is = socket.getInputStream(); // 创建输入流
OutputStream os = null;
try {
line = ""; // 获取请求行的内容
host = "";
int state = 0;
boolean space;
while (true) { // 无限循环
int c = is.read(); // 读取输入流的信息
if (c == -1) // 没有读取信息
break;
if (logging)
proxyLog(c, true); // 将信息写入日志
space = Character.isWhitespace((char) c);// 判断是否为空白字符
switch (state) { // 判断状态
case 0:
if (space)
continue; // 跳过本次循环
state = 1; // 更改状态
case 1:
if (space) {
state = 2;
continue; // 跳过本次循环
}
line = line + (char) c; // 添加读取的信息
break;
case 2:
if (space)
continue; // 跳过空白字符
state = 3; // 更改状态
case 3:
if (space) { // 如果是空白字符
state = 4; // 更改状态
String host0 = host; // 取出网址
int n;
n = host.indexOf("//"); // 获取网址(不包括协议)
if (n != -1) // 没有找到
host = host.substring(n + 2);
n = host.indexOf('/');
if (n != -1) // 没有找到/
host = host.substring(0, n);
n = host.indexOf(":"); // 分析可能存在的端口号
if (n != -1) { // 没有找到:
port = Integer.parseInt(host.substring(n + 1));
host = host.substring(0, n);
}
host = outLog(host0, host, port, socket);// 获得网站域名
if (parent != null) {
host = parent;
port = parentPort;
}
int retry = RETRIES;
while (retry-- != 0) {
try { // 创建连接对象
outbound = new Socket(host, port);
break;
} catch (Exception e) {
System.out.println("无法创建连接:"
+ e.getMessage());
}
Thread.sleep(PAUSE); // 设置线程等待
}
if (outbound == null)
break;
outbound.setSoTimeout(TIMEOUT); // 设置超时时间
os = outbound.getOutputStream(); // 获得输出流对象
os.write(line.getBytes()); // 将信息写入流
os.write(' ');
os.write(host0.getBytes()); // 将信息写入流
os.write(' ');
outInfo(is, outbound.getInputStream(), os, socket
.getOutputStream()); // 调用方法将信息写入日志
break;
}
host = host + (char) c;
break;
}
}
} catch (IOException e) {
}
} catch (Exception e) {
} finally {
try {
socket.close(); // 释放资源
} catch (Exception e1) {
}
try {
outbound.close();
} catch (Exception e2) {
}
}
}
void outInfo(InputStream is0, InputStream is1, OutputStream os0,
OutputStream os1) throws IOException { // 读取流中信息写入日志
try {
int ir;
byte bytes[] = new byte[BUFSIZ]; // 创建字节数组,大小:1024
while (true) {
try {
if ((ir = is0.read(bytes)) > 0) { // 判断读取输入流的信息
os0.write(bytes, 0, ir); // 读取数据写入输出流对象中
if (logging)
proxyLog(bytes, 0, ir, true); // 写入日志
} else if (ir < 0) // 读取完毕
break;
} catch (InterruptedIOException e) { // 捕获中断IO流异常
}
try {
if ((ir = is1.read(bytes)) > 0) { // 判断读取输入流的信息
os1.write(bytes, 0, ir); // 读取数据写入输出流对象中
if (logging)
proxyLog(bytes, 0, ir, false); // 写入日志
} else if (ir < 0) // 读取完毕
break;
} catch (InterruptedIOException e) { // 捕获中断IO流异常
}
}
} catch (Exception e0) { // 捕获异常
}
}
// 用来把一个代理服务器链接到另一个代理服务器(需要指定另一个服务器的名称和端口)。
public static void setProxy(String name, int port) {
parent = name;
parentPort = port;
}
public LANProxy(Socket s) { // 创建一个代理线程
socket = s;
start(); // 启动线程
}
public void proxyLog(int c, boolean browser) throws IOException { // 写日志
log.write(c);
}
public void proxyLog(byte[] bytes, int offset, int len, boolean browser)
throws IOException { // 循环写日志
for (int i = 0; i < len; i++)
proxyLog((int) bytes[offset + i], browser);
}
// 默认情况下,日志信息输出到控制台或文件
public String outLog(String url, String host, int port, Socket sock) {
java.text.DateFormat cal = java.text.DateFormat.getDateTimeInstance();
System.out.println(cal.format(new java.util.Date()) + " - " + url + " "
+ sock.getInetAddress() + "\n");
return host;
}
public static void startProxy(int port, Class clobj) {
ServerSocket serverSocket;
try {
serverSocket = new ServerSocket(port); // 根据端口创建服务器端Socket对象
while (true) {
Class[] objClass = new Class[1]; // 创建类数组,大小为1
Object[] obj = new Object[1]; // 创建对象数组,大小为1
objClass[0] = Socket.class; // 添加Socket类
try { // 创建代理服务器实例
Constructor cons = clobj.getDeclaredConstructor(objClass);
obj[0] = serverSocket.accept(); // 挂起等待客户的请求
cons.newInstance(obj); // 创建HttpProxy或其派生类的实例
} catch (Exception e) {
Socket socket = (Socket) obj[0];// 对象强制转换
try {
socket.close(); // 释放资源
} catch (Exception ec) {
}
}
}
} catch (IOException e) {
}
}
public static void main(String args[]) { // java程序主入口处
System.out.println("LAN使用代理服务器连接成功......");
LANProxy.log = System.out; // 日志信息输出到控制台
LANProxy.logging = false;
LANProxy.startProxy(8080, LANProxy.class);// 调用方法
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
java LAN使用代理服务器
共2个文件
java:1个
jpg:1个
4星 · 超过85%的资源 需积分: 16 36 下载量 120 浏览量
2010-06-21
16:48:32
上传
评论
收藏 61KB ZIP 举报
温馨提示
java LAN使用代理服务器 java LAN使用代理服务器 java LAN使用代理服务器
资源推荐
资源详情
资源评论
收起资源包目录
java LAN使用代理服务器.zip (2个子文件)
Java.jpg 77KB
LANProxy.java 7KB
共 2 条
- 1
资源评论
- zsqat2013-08-13没有说明文档,不会用,谢谢共享!
- topgunqq2012-12-05不会用。需要说明文档。
- sccdry2014-04-08不会用。需要说明文档。
lihaiyang68
- 粉丝: 2
- 资源: 51
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功