如何解决如何使用BufferedReader监视HTTP请求?
我正在制作类似于Burp的HTTP代理来学习Java。
在这里,我正在使用Socket
和ServerSocket
来监听请求。我试图让它连续监听请求,同时能够拦截多个单独的请求,即使这些请求是在大约同一时间发送的(主要是噪音)。
import java.io.*;
import java.net.*;
import java.util.*;
public class notBurp {
public static void main(String[] args) {
Thread proxy = new Thread(new Proxy("notBurp",9000));
proxy.start();
}
}
abstract class Tab implements Runnable {
public void run() {}
// ...
}
class Proxy extends Tab {
private String hostName;
private int port;
private boolean isIntercept;
public Proxy(String hostName,int port) {
System.out.println("[+] new Proxy");
this.hostName = hostName;
this.port = port;
this.isIntercept = true;
}
@Override
public void run() {
try {
System.out.println("[+] Running Proxy @ " + Thread.currentThread().getName());
ServerSocket serverSocket = new ServerSocket(this.port);
Socket clientSocket = serverSocket.accept();
String request = interceptRequest(clientSocket);
System.out.println(request);
forwardRequest(request);
} catch (Exception e) {
e.printStackTrace();
}
}
private String interceptRequest(Socket clientSocket) {
String request = "";
try {
String line;
int countBlankLines = 0;
int countThreshold = 0;
String[] methodsWith1BlankLine = {"GET","HEAD","DELETE","CONNECT","OPTIONS","TRACE"};
String[] methodsWith2BlankLines = {"POST","PUT"};
String requestMethod = "";
BufferedReader reader =
new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
while ((line = reader.readLine()) != null) {
if(requestMethod.isBlank()){
requestMethod = line.split(" ")[0];
if(Arrays.asList(methodsWith1BlankLine).contains(requestMethod)){
countThreshold = 1;
} else if(Arrays.asList(methodsWith2BlankLines).contains(requestMethod)){
countThreshold = 2;
} else {
System.out.println("[!] Invalid HTTP Method");
return "";
}
} else if(line.isBlank()){
countBlankLines++;
}
request += line + "\n";
if(countBlankLines == countThreshold){
break;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
return request;
}
}
private void forwardRequest(String request) {}
}
使用Socket
和BufferedReader
,我试图捕获那些单独的请求。但是,reader
不知道每个请求何时何地结束。我尝试了一种计算空白行的解决方案,但是POST
和PUT
请求实际上没有设置一定数量的空白行来标记每个请求的结束。因此,reader
不会在没有正文发送时完成阅读,并且当正文中有多个以空行分隔的段落时,它们将无法捕获并转发整个正文。我想允许将正文包含在其方法实际上并不包含在正文中的请求中。
如果可能的话,我想避免使用某种超时方法来处理大约在同一时间发送多个请求的情况,并避免减慢捕获速度。
让BufferedReader
知道HTTP请求何时/何地结束的好方法是什么?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。