Skip to content

Instantly share code, notes, and snippets.

@wkgcass
Last active May 24, 2022 03:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save wkgcass/52f06064fab6ab91e8dfd3a3fb3d5698 to your computer and use it in GitHub Desktop.
Save wkgcass/52f06064fab6ab91e8dfd3a3fb3d5698 to your computer and use it in GitHub Desktop.
AntiXXN.java
import javax.net.ssl.SSLParameters;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.Date;
import java.util.concurrent.atomic.AtomicLong;
public class X {
public static void main(String[] args) {
for (int i = 0; i < Runtime.getRuntime().availableProcessors() * 2; ++i) {
final int fi = i;
Thread t = new Thread(() -> run(fi));
t.start();
}
}
private static final AtomicLong count = new AtomicLong(0);
public static void run(int threadIndex) {
var sslParams = new SSLParameters();
sslParams.setApplicationProtocols(new String[]{"http/1.1"});
var client = HttpClient.newBuilder()
.connectTimeout(Duration.ofMillis(2000))
.version(HttpClient.Version.HTTP_1_1)
.sslParameters(sslParams)
.followRedirects(HttpClient.Redirect.ALWAYS)
.build();
var ua = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36";
var req1 = HttpRequest
.newBuilder(URI.create("https://cs.xidian.edu.cn/info/1003/14561.htm"))
.header("User-Agent", ua)
.build();
var req2 = HttpRequest
.newBuilder(URI.create("https://cs.xidian.edu.cn/system/resource/code/datainput.jsp?owner=1609340945&e=1&w=1920&h=1200&treeid=1003&refer=&pagename=L0NvbnRlbnQuanNw&newsid=14561"))
.header("User-Agent", ua)
.build();
var req3 = HttpRequest
.newBuilder(URI.create("https://cs.xidian.edu.cn/system/resource/code/news/click/dynclicks.jsp?clickid=14561&owner=1609340945&clicktype=wbnews"))
.header("User-Agent", ua)
.build();
System.out.println("thread[" + threadIndex + "]: started");
//noinspection InfiniteLoopStatement
while (true) {
try {
client.send(req1, HttpResponse.BodyHandlers.discarding());
} catch (Throwable t) {
System.err.println("thread[" + threadIndex + "]: failed sending req1: " + t);
}
try {
client.send(req2, HttpResponse.BodyHandlers.discarding());
} catch (Throwable t) {
System.err.println("thread[" + threadIndex + "]: failed sending req2: " + t);
}
HttpResponse<String> resp;
try {
resp = client.send(req3, HttpResponse.BodyHandlers.ofString());
} catch (Throwable t) {
System.err.println("thread[" + threadIndex + "]: failed sending req3: " + t);
continue;
}
if (resp.statusCode() == 200) {
long count;
try {
count = Long.parseLong(resp.body().trim());
} catch (NumberFormatException e) {
System.err.println("thread[" + threadIndex + "]: req3 returns invalid response: " + resp.body());
count = -1;
}
if (count != -1) {
long old = X.count.get();
if (count / 1_000 > old / 1_000) {
synchronized (X.count) {
old = X.count.get();
if (count / 1_000 > old / 1_000) {
var now = new Date();
System.out.println("thread[" + threadIndex + "]: " + now + " - " + resp.body().trim());
X.count.set(count);
}
}
}
}
} else {
System.err.println("thread[" + threadIndex + "]: failed sending req3: " + resp.statusCode() + ": " + resp.body());
}
try {
//noinspection BusyWait
Thread.sleep(200);
} catch (InterruptedException ignore) {
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment