时间:2021-05-19
复制代码 代码如下:
package cn.liangjintang.httpproxy;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class HttpProxy {
static long threadCount = 0;
int myTcpPort = 8080;
private ServerSocket myServerSocket;
private Thread myThread;
public HttpProxy(int port) throws IOException {
myTcpPort = port;
myServerSocket = new ServerSocket(myTcpPort);
myThread = new Thread(new Runnable() {
public void run() {
try {
while (true)
new HTTPSession(myServerSocket.accept());
} catch (IOException ioe) {
}
}
});
myThread.setDaemon(true);
myThread.start();
}
/**
* Stops the server.
*/
public void stop() {
try {
myServerSocket.close();
myThread.join();
} catch (IOException ioe) {
} catch (InterruptedException e) {
}
}
public class HTTPSession implements Runnable {
private Socket mySocket;
public HTTPSession(Socket s) {
mySocket = s;
Thread t = new Thread(this);
t.setDaemon(true);
t.start();
}
@Override
public void run() {
try {
++threadCount;
InputStream is = mySocket.getInputStream();
if (is == null)
return;
final int bufsize = 8192;
byte[] buf = new byte[bufsize];
int splitbyte = 0;
int rlen = 0;
{
int read = is.read(buf, 0, bufsize);
while (read > 0) {
rlen += read;
splitbyte = findHeaderEnd(buf, rlen);
if (splitbyte > 0)
break;
read = is.read(buf, rlen, bufsize - rlen);
}
ByteArrayInputStream hbis = new ByteArrayInputStream(buf,
0, rlen);
BufferedReader hin = new BufferedReader(
new InputStreamReader(hbis));
Host host = new Host();
{
String string;
boolean flag = false;
while ((string = hin.readLine()) != null) {
if (string.toLowerCase().startsWith("host:")) {
host.host = string;
flag = true;
}
System.out.println(string);
}
if (!flag) {
mySocket.getOutputStream().write(
"error!".getBytes());
mySocket.close();
return;
}
}
host.cal();
System.out.println("address:[" + host.address + "]port:"
+ host.port + "\n-------------------\n");
try {
pipe(buf, rlen, mySocket, mySocket.getInputStream(),
mySocket.getOutputStream(), host);
} catch (Exception e) {
System.out.println("Run Exception!");
e.printStackTrace();
}
}
} catch (Exception e) {
}
System.out.println("threadcount:" + --threadCount);
}
/**
* finad http header
**/
private int findHeaderEnd(final byte[] buf, int rlen) {
int splitbyte = 0;
while (splitbyte + 3 < rlen) {
if (buf[splitbyte] == '\r' && buf[splitbyte + 1] == '\n'
&& buf[splitbyte + 2] == '\r'
&& buf[splitbyte + 3] == '\n')
return splitbyte + 4;
splitbyte++;
}
return 0;
}
void pipe(byte[] request, int requestLen, Socket client,
InputStream clientIS, OutputStream clientOS, Host host)
throws Exception {
byte bytes[] = new byte[1024 * 32];
Socket socket = new Socket(host.address, host.port);
socket.setSoTimeout(3000);
OutputStream os = socket.getOutputStream();
InputStream is = socket.getInputStream();
try {
do {
os.write(request, 0, requestLen);
int resultLen = 0;
try {
while ((resultLen = is.read(bytes)) != -1
&& !mySocket.isClosed() && !socket.isClosed()) {
clientOS.write(bytes, 0, resultLen);
}
} catch (Exception e) {
System.out.println("target Socket exception:"
+ e.toString());
}
System.out.println("proxy requset-connect broken,socket:"
+ socket.hashCode());
} while (!mySocket.isClosed()
&& (requestLen = clientIS.read(request)) != -1);
} catch (Exception e) {
System.out.println("client Socket exception:" + e.toString());
}
System.out.println("end,socket:" + socket.hashCode());
os.close();
is.close();
clientIS.close();
clientOS.close();
socket.close();
mySocket.close();
}
// target Host info
final class Host {
public String address;
public int port;
public String host;
public boolean cal() {
if (host == null)
return false;
int start = host.indexOf(": ");
if (start == -1)
return false;
int next = host.indexOf(':', start + 2);
if (next == -1) {
port = 80;
address = host.substring(start + 2);
} else {
address = host.substring(start + 2, next);
port = Integer.valueOf(host.substring(next + 1));
}
return true;
}
}
}
public static void main(String[] args) {
try {
new HttpProxy(8580);
} catch (IOException ioe) {
System.err.println("Couldn't start server:\n" + ioe);
System.exit(-1);
}
System.out.println("start!");
try {
System.in.read();
} catch (Throwable t) {
}
System.out.println("stop!");
}
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
对于http代理服务器,很多朋友对它不是很了解,不知道如何去设置,那么http代理服务器地址如何设置?http代理服务器功能有哪些呢?下面就给大家详细的来谈谈关
一、功能说明:1.多线程方式抓取代理服务器,并多线程验证代理服务器ps代理服务器是从http:///proxy8.html..........总共抓取了800个
Nginx是一个免费、开源、高性能、可伸缩、可靠、功能全面且流行的HTTP和反向代理服务器,一个邮件代理服务器,以及通用的TCP/UDP代理服务器。Nginx的
简介Nginx是一个高性能的HTTP服务器和反向代理服务器.最常用的两个功能是反向代理和负载均衡.反向代理反向代理是正向代理的反面.普通的代理服务器是需要用户主
对于站长来说选择代理服务器很重要,现在很多网络运营公司都会提供代理服务器服务,代理服务器网站比较多,有的人在选择时会比较困惑,下面就和大家介绍下代理服务器有哪些