
First, hand to teach you to use Java to build an intranet penetration agent
Let's first figure out how to play with Java's own socket proxy service. Here's akey trick: Don't operate the underlying socket directly, use ServerSocket to listen to the port is more economical. Look at the core of this code:
// Note that it's easy to misspell ServerSocket(8080) and forget to add the parameter
ServerSocket server = new ServerSocket(0, 50, InetAddress.getByName("0.0.0.0")); while (true) { {
while (true) {
Socket client = server.accept(); // Remember to create a new thread here.
// Remember to thread the new thread here, otherwise it will block
new Thread(new ProxyHandler(client)).start();
}
Note that there is a hole in the port settings: if you use new ServerSocket(8080) directly, you may not be able to connect to the external network. You have to add0.0.0.0 this special addressTo allow external access. Many newbies planted in this, debugging half a day to find that it is the binding address of the problem.
Second, to the proxy service to wear a HTTP jacket
It's not enough to have a proxy, it has to be recognized by the browser. Here to deal with the HTTP protocol's CONNECT method, focus on this response header processing:
// Note the two newlines at the end of the response header
String response = "HTTP/1.1 200 Connection Establishedrr";
OutputStream out = client.getOutputStream();
OutputStream out = client.getOutputStream(); out.write(response.getBytes()); out.flush(); out.write(response.getBytes())
out.write(response.getBytes()); out.flush();
Here's one.Easy to roll over: The response header must be formatted strictly according to the HTTP protocol, and the browser will get stuck if there is a newline character missing. It is recommended to use WireShark packet capture comparison to ensure that the format is completely correct.
Third, the ipipgo agent real-world matching value
Here's the point! Our proxy service is going to access ipipgo's premium line. Configure the proxy in the code ofcorrect postureIt should be:
// Be careful to replace the authentication information with your own
System.setProperty("http.proxyHost", "gateway.ipipgo.com");
System.setProperty("http.proxyPort", "9021");
System.setProperty("http.proxyUser", "your_username"); System.setProperty("http.proxyUser", "your_username");
System.setProperty("http.proxyPassword", "your_password"); System.setProperty("http.proxyPassword", "your_password");
Here's a recommendation to use theirStatic Home Package, especially suitable for scenarios that require stable IPs. For example, crawler projects or systems that require fixed IP login, 35 dollars a month is much cheaper than buying a server.
IV. Guidelines for avoiding pitfalls (lessons learned through blood and tears)
Name a few potholes I've stepped in:
1. Time-out settings: If you don't set socketTimeout, the whole service hangs when there is a network fluctuation.
2. Memory leakage: Each connection must be closed in a timely manner, using the try-with-resources syntax is the safest way to do this.
3. Accreditation issues: ipipgo's TK line requires a special authentication header, remember to ask customer service for sample code!
V. What you must want to ask
Q: What should I do if my agent is slow?
A:优先选ipipgo的跨境专线,实测能降60%。别贪便宜用标准套餐,企业版贵2块但带宽更稳
Q: How do I choose a package?
A: Dynamic standard edition for development and testing, enterprise edition for formal business, fixed IP option for static residence is required. Their customer service can give program advice
Q: Does it support multi-threading?
A: code to open the thread pool on the line, ipipgo API default support for 500 concurrent, not enough to apply for capacity expansion
Finally, a big truth: write your own agent although practicing, but the production environment or directly on the ipipgo ready-made program is more reliable. Their SERP API to do data collection is really fragrant, who uses who knows. Where the code is stuck, remember to go to the official website to find sample code, much stronger than blindly looking for tutorials everywhere.

