Skip to content

Commit 119d288

Browse files
committed
server ssl testing
1 parent 5cacbb0 commit 119d288

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

src/main/java/one/xio/AsioVisitor.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package one.xio;
22

3+
import one.xio.AsioVisitor.FSM.sslBacklog;
4+
import one.xio.AsioVisitor.Helper.F;
5+
36
import javax.net.ssl.SSLEngine;
47
import javax.net.ssl.SSLEngineResult;
58
import javax.net.ssl.SSLEngineResult.HandshakeStatus;
@@ -26,7 +29,7 @@
2629
* Time: 11:50 PM
2730
*/
2831
public interface AsioVisitor {
29-
boolean $DBG = null != System.getenv("DEBUG_VISITOR_ORIGINS");
32+
boolean $DBG = "true".equals(Config.get("DEBUG_VISITOR_ORIGINS", "false"));
3033
Map<Impl, String> $origins = $DBG
3134
? new WeakHashMap<Impl, String>()
3235
: null;
@@ -93,6 +96,7 @@ public void run() {
9396

9497
try {
9598
key.interestOps(0);
99+
assert executorService!=null:"must install FSM executorService!";
96100
executorService.invokeAll(runnables);
97101
} catch (InterruptedException e) {
98102
e.printStackTrace();
@@ -108,11 +112,12 @@ public static void setExecutorService(ExecutorService svc) {
108112
* this is a beast.
109113
*/
110114
public static void handShake(final Pair<SelectionKey, SSLEngine> state) {
111-
115+
if(!state.getA().isValid())return;
112116
SSLEngine sslEngine = state.getB();
113117

114118
HandshakeStatus handshakeStatus = sslEngine.getHandshakeStatus();
115119
System.err.println("hs: " + handshakeStatus);
120+
116121
switch (handshakeStatus) {
117122
case NEED_TASK:
118123
delegateTasks(state, new Runnable() {
@@ -185,12 +190,13 @@ private static void needNetUnwrap(final Pair<SelectionKey, SSLEngine> state) {
185190
final ByteBuffer fromNet = sslBacklog.fromNet.resume(state);
186191
final ByteBuffer toApp = sslBacklog.toApp.resume(state);
187192

188-
Helper.toRead(key, new Helper.F() {
193+
Helper.toRead(key, new F() {
189194
public void apply(SelectionKey key) throws Exception {
190195
int read = ((SocketChannel) key.channel()).read(fromNet);
191196
System.err.println("hsread: " + read);
192197
SSLEngine sslEngine = state.getB();
193-
198+
System.err.println("nunwrap1a: " + fromNet.toString());
199+
System.err.println("?<< https://"+UTF_8.decode((ByteBuffer) fromNet.duplicate().flip()));//fromNet.flip()
194200
SSLEngineResult unwrap = sslEngine.unwrap((ByteBuffer) fromNet.flip(), toApp);
195201
System.err.println("nunwrap2:" + unwrap);
196202
System.err.println("nunwrap2a: " + fromNet.toString());
@@ -451,6 +457,7 @@ public static int read(Channel channel, ByteBuffer fromNet) throws IOException {
451457
}
452458

453459
public static int read(SelectionKey key, ByteBuffer dest) throws IOException {
460+
if(!key.isValid())return -1;
454461
SocketChannel channel = (SocketChannel) key.channel();
455462
int origin = dest.position();
456463

@@ -459,9 +466,10 @@ public static int read(SelectionKey key, ByteBuffer dest) throws IOException {
459466
if (null != sslEngine) {
460467

461468

462-
ByteBuffer fromNet = FSM.sslBacklog.fromNet.resume(pair(key, sslEngine));
463-
ByteBuffer toApp = FSM.sslBacklog.toApp.resume(pair(key, sslEngine));
469+
ByteBuffer fromNet = sslBacklog.fromNet.resume(pair(key, sslEngine));
470+
ByteBuffer toApp = sslBacklog.toApp.resume(pair(key, sslEngine));
464471
int read1 = channel.read(fromNet);
472+
if(-1==read1)key.cancel();
465473
System.err.println("r1: "+read1);
466474
System.err.println("r1a: from "+fromNet);
467475
System.err.println("r1b: dest "+dest);
@@ -522,7 +530,7 @@ public static int write(SelectionKey key, ByteBuffer fromApp) throws IOException
522530
int write = 0;
523531
if (null != sslEngine) {
524532
try {
525-
ByteBuffer toNet = FSM.sslBacklog.toNet.resume(pair(key, sslEngine));
533+
ByteBuffer toNet = sslBacklog.toNet.resume(pair(key, sslEngine));
526534
System.err.println("w:fa:0 " + fromApp);
527535
System.err.println("w:tn:0 " + toNet);
528536
SSLEngineResult sslEngineResult = sslEngine.wrap(fromApp, toNet);
@@ -540,12 +548,11 @@ public static int write(SelectionKey key, ByteBuffer fromApp) throws IOException
540548
switch (handshakeStatus) {
541549
case NOT_HANDSHAKING:
542550
case FINISHED:
543-
write = ((SocketChannel) key.channel()).write((ByteBuffer) toNet.flip());
544-
System.err.println("w:w:2 " + write);
551+
write=sslEngineResult.bytesConsumed();
552+
System.err.println("w:w:2 " + ((SocketChannel) key.channel()).write((ByteBuffer) toNet.flip()));
545553
System.err.println("w:tn:2 " + toNet);
546554
toNet.compact();
547555
System.err.println("w:tn:3 " + toNet);
548-
549556
break;
550557
case NEED_TASK:
551558
case NEED_WRAP:

src/main/java/one/xio/Config.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package one.xio;
22

3-
public class Config {
4-
3+
/**
4+
* non-public config class for FSM specific configs
5+
*/
6+
final class Config {
57
public static String get(String ox_var, String... defaultVal) {
68
String javapropname = "1xio."
79
+ ox_var.toLowerCase().replaceAll("^1xio_", "").replace('_',

src/main/java/one/xio/Pair.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public Pair(A a, B b) {
1515
this.b = b;
1616
}
1717

18-
public static<A,B> Pair <A,B> pair(A a,B b){return new Pair<>(a, b);}
18+
public static<A,B > Pair <A,B> pair(A a,B b){return new Pair<>(a, b);}
1919
public A getA() {
2020
return a;
2121
}

0 commit comments

Comments
 (0)