⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 socksserver.java

📁 java HTTP代理多线程监听和处理连接 java HTTP代理多线程监听和处理连接
💻 JAVA
字号:
package proxy;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.InterruptedIOException;
import java.io.OutputStream;
import java.net.Socket;
import java.io.PrintWriter;

/**
 * Socket 代理线程
 * 
 * @author liwen
 * 
 */
public class SocksServer implements Service {
	protected UserAuthorized userAuthorized;

	private Socket socket;

	public void serve(Socket soc) throws Exception {
		this.socket = soc;
		//if (this.userAuthorized == null)
		//	this.Authorized(socket);
		this.doProxy(socket);
	}

	public UserAuthorized getUserAuthorized() throws Exception {
		return userAuthorized;
	}

	

	private void doProxy(Socket socket) throws Exception {
		InputStream in = socket.getInputStream();
		OutputStream out = socket.getOutputStream();

		try {
			int ir;
			byte bytes[] = new byte[ProxyServer.BUFER_SIZE];
			byte[] rtnBuf = null;
			String UserStr = "";
			while (true) {
				try {
					if ((ir = in.read(bytes)) > 0) {
						for (int i = 0; i < ir; i++) {
							int c = bytes[i];
							UserStr = UserStr + (char) c;
						}
						System.out.println(UserStr);
						out.write(bytes, 0, ir);
						out.flush();
						UserStr= "";
					} else if (ir < 0) {
						break;
					}
				} catch (InterruptedIOException e) {
					// System.out.println(" put server InterruptedIOException: "
					// + e);
				}

			}
		} catch (Exception e0) {
			Logs.log("Pipe exception: " + e0);
		}

	}

	
/*
	private void pipe(InputStream clientIn, InputStream serverIn,
			OutputStream serverOut, OutputStream clientOut) throws IOException {
		try {
			int ir;
			byte bytes[] = new byte[ProxyServer.BUFER_SIZE];
			boolean isfirst = true;
			String testRtnStr = "";
			while (true) {
				try {
					if ((ir = clientIn.read(bytes)) > 0) {
						if (ProxyServer._DEBUG_) {
							for (int i = 0; i < ir; i++) {
								int c = bytes[i];
								testRtnStr = testRtnStr + (char) c;
							}
							System.out.println("Client RTN:\n" + testRtnStr);
						}
						
						// 给QQ客户端返回认证信息
						if (isfirst) {
							clientOut
									.write("http/1.1 200 Connection established"
											.getBytes());
							clientOut.flush();
							isfirst = false;
						} else {
							serverOut.write(bytes, 0, ir);
							serverOut.flush();
						}
						serverOut.write(bytes, 0, ir);
						serverOut.flush();
					} else if (ir < 0) {
						break;
					}
				} catch (InterruptedIOException e) {
					// System.out.println(" put server InterruptedIOException: "
					// + e);
				}

				try {
					if ((ir = serverIn.read(bytes)) > 0) {
						if (ProxyServer._DEBUG_) {
							// 查看返回字符
							for (int i = 0; i < ir; i++) {
								int c = bytes[i];
								testRtnStr = testRtnStr + (char) c;
							}
							System.out.println("Server RTN:\n" + testRtnStr);
						}
						clientOut.write(bytes, 0, 5);
						clientOut.flush();
						clientOut.write(bytes, 6, ir);
						clientOut.flush();
					} else if (ir < 0) {
						break;
					}
				} catch (InterruptedIOException e) {
					// System.out.println(" from server InterruptedIOException:
					// " + e);
				}
			}
		} catch (Exception e0) {
			Logs.log("Pipe exception: " + e0);
		}

	}*/
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -