From 0632a02beb78705dc93b760c7bfa86ecedacb970 Mon Sep 17 00:00:00 2001 From: shikeying <pxzsky@163.com> Date: 星期四, 22 二月 2024 11:10:34 +0800 Subject: [PATCH] 增加注释 --- walker-tcp/src/main/java/com/walker/tcp/netty/WebSocketServerInitializer.java | 44 ++++++++++++++ walker-tcp/src/main/java/com/walker/tcp/netty/WebSocketNettyHandler.java | 58 ++++++++++++------ walker-ml-openocr/src/main/java/com/walker/openocr/idcard/IdCardObject.java | 36 ++++++++++++ 3 files changed, 118 insertions(+), 20 deletions(-) diff --git a/walker-ml-openocr/src/main/java/com/walker/openocr/idcard/IdCardObject.java b/walker-ml-openocr/src/main/java/com/walker/openocr/idcard/IdCardObject.java index 4c224a3..436e840 100644 --- a/walker-ml-openocr/src/main/java/com/walker/openocr/idcard/IdCardObject.java +++ b/walker-ml-openocr/src/main/java/com/walker/openocr/idcard/IdCardObject.java @@ -33,6 +33,42 @@ private String dateStart; private String dateEnd; + public void setName(String name) { + this.name = name; + } + + public void setSex(String sex) { + this.sex = sex; + } + + public void setNation(String nation) { + this.nation = nation; + } + + public void setBirthday(String birthday) { + this.birthday = birthday; + } + + public void setAddress(String address) { + this.address = address; + } + + public void setCardNo(String cardNo) { + this.cardNo = cardNo; + } + + public void setSignOffice(String signOffice) { + this.signOffice = signOffice; + } + + public void setDateStart(String dateStart) { + this.dateStart = dateStart; + } + + public void setDateEnd(String dateEnd) { + this.dateEnd = dateEnd; + } + public String getName() { return name; } diff --git a/walker-tcp/src/main/java/com/walker/tcp/netty/WebSocketNettyHandler.java b/walker-tcp/src/main/java/com/walker/tcp/netty/WebSocketNettyHandler.java index 68d0670..ffa3481 100644 --- a/walker-tcp/src/main/java/com/walker/tcp/netty/WebSocketNettyHandler.java +++ b/walker-tcp/src/main/java/com/walker/tcp/netty/WebSocketNettyHandler.java @@ -26,58 +26,76 @@ public class WebSocketNettyHandler extends DefaultLongHandler { private WebSocketServerHandshaker handshaker; - + private String uri; - + public String getUri() { return uri; } + /** + * 璁剧疆澶栭儴閰嶇疆鐨剋ebsocket璁块棶鍦板潃銆� + * <pre> + * 1) 璇ュ湴鍧�鏄祻瑙堝櫒鍙闂殑鏈�缁堝湴鍧�锛屽锛歸s://my.com/websocket 鎴� wss://my.com/wss + * 2) 寮曟搸鏈韩骞朵笉浼氱敓鎴愬湴鍧�锛屼唬鐮佷腑log鏃ュ織涓瓀ebsocket浠呮槸璋冭瘯骞堕潪鐪熸璺緞 + * 3) 寮曟搸浠呯洃鍚缃殑绔彛锛堥粯璁�60000锛夛紝閫氬父鍦� nginx 閰嶇疆涓紝浠g悊璇ョ鍙e嵆鍙紝濡傦細 + * + * ... + * location /wss { + * proxy_pass http://my.com:60000; + * proxy_http_version 1.1; + * proxy_set_header Upgrade $http_upgrade; + * proxy_set_header Connection "Upgrade"; + * } + * + * </pre> + * @param uri + */ public void setUri(String uri) { this.uri = uri; } @Override protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { - + String textMessage = null; - + if(msg instanceof FullHttpRequest){ //-----------> http://xxxx handleHttpRequest(ctx,(FullHttpRequest)msg); textMessage = WebSocketUtils.WEB_SOCKET_HTTP_MSG; msgThreadLocal.set(textMessage); - + }else if(msg instanceof WebSocketFrame){ - //-----------> ws://xxxx + //-----------> ws://xxxx textMessage = handlerWebSocketFrame(ctx,(WebSocketFrame)msg); String id = ctx.channel().id().asLongText() ; LongConnection conn = (LongConnection)connectionManager.getConnection(id); - + msgThreadLocal.set(textMessage); tcpServerHandler.onRead(conn, textMessage); - + } else { throw new UnsupportedOperationException("涓嶆敮鎸佽娑堟伅绫诲瀷锛�" + msg.getClass().getName()); } } - + @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { // System.out.println("鏈嶅姟绔鍙栧畬鎴愭暟鎹竴娆°��" + ctx.channel().remoteAddress() ); - + String id = ctx.channel().id().asLongText() ; // String msg = msgThreadLocal.get(); Object msg = msgThreadLocal.get(); if(msg != null && msg.toString().equals(WebSocketUtils.WEB_SOCKET_HTTP_MSG)){ return; } - + tcpServerHandler.onReadComplete(msg, id, ctx); msgThreadLocal.set(null); super.channelReadComplete(ctx); } - + private String handlerWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) { // 鍒ゆ柇鏄惁鍏抽棴閾捐矾鐨勬寚浠� if (frame instanceof CloseWebSocketFrame) { @@ -85,7 +103,7 @@ handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain()); return null; } - + // 鍒ゆ柇鏄惁ping娑堟伅 if (frame instanceof PingWebSocketFrame) { ctx.channel().write(new PongWebSocketFrame(frame.content().retain())); @@ -95,14 +113,14 @@ //ctx.channel().write(new PongWebSocketFrame(frame.content().retain())); return null; } - - + + // 鏈緥绋嬩粎鏀寔鏂囨湰娑堟伅锛屼笉鏀寔浜岃繘鍒舵秷鎭� if (!(frame instanceof TextWebSocketFrame)) { //System.out.println("鏈緥绋嬩粎鏀寔鏂囨湰娑堟伅锛屼笉鏀寔浜岃繘鍒舵秷鎭�"); throw new UnsupportedOperationException(String.format("%s frame types not supported", frame.getClass().getName())); } - + // 杩斿洖搴旂瓟娑堟伅 return ((TextWebSocketFrame) frame).text(); //System.out.println("鏈嶅姟绔敹鍒帮細" + request); @@ -113,7 +131,7 @@ // 杩斿洖銆愯皝鍙戠殑鍙戠粰璋併�� // ctx.channel().writeAndFlush(tws); } - + /** * 绗竴娆¤姹傛槸http璇锋眰锛岃姹傚ご鍖呮嫭ws鐨勪俊鎭� * @param ctx @@ -127,12 +145,12 @@ sendHttpResponse(ctx,req, new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST)); return; } - + logger.debug("================ " + "ws://"+ctx.channel()+ "/websocket"); // WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory("ws://"+ctx.channel()+ "/websocket",null,false); WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(uri,null,false); handshaker = wsFactory.newHandshaker(req); - + if(handshaker == null){ //涓嶆敮鎸� WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel()); @@ -140,7 +158,7 @@ handshaker.handshake(ctx.channel(), req); } } - + private static void sendHttpResponse(ChannelHandlerContext ctx,FullHttpRequest req,DefaultFullHttpResponse res){ // 杩斿洖搴旂瓟缁欏鎴风 if (res.status().code() != 200) diff --git a/walker-tcp/src/main/java/com/walker/tcp/netty/WebSocketServerInitializer.java b/walker-tcp/src/main/java/com/walker/tcp/netty/WebSocketServerInitializer.java index 2700971..e97e4b4 100644 --- a/walker-tcp/src/main/java/com/walker/tcp/netty/WebSocketServerInitializer.java +++ b/walker-tcp/src/main/java/com/walker/tcp/netty/WebSocketServerInitializer.java @@ -6,7 +6,15 @@ import io.netty.handler.codec.http.HttpServerCodec; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; +import io.netty.handler.ssl.SslHandler; import io.netty.handler.stream.ChunkedWriteHandler; + +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLEngine; +import java.io.FileInputStream; +import java.io.InputStream; +import java.security.KeyStore; public class WebSocketServerInitializer extends DefaultServerInitializer { @@ -38,4 +46,40 @@ pipeline.addLast("handler", this.getHandler()); } + /** + * 鍒涘缓鏀寔wss鍙潬杩炴帴鐨勯�氫俊銆傛殏鏃朵笉浣跨敤銆� + * <pre> + * 1) 涓嶈繃鐩墠鎯呭喌鐪嬶紝涓�鑸槸鍦� nginx 绔唬鐞嗚缃嵆鍙紝涓�鑸笉蹇呭湪绋嬪簭涓敮鎸� + * 2) 纭疄闇�瑕佸湪绋嬪簭涓敮鎸侊紝闇�瑕侀厤缃瘉涔︼紝杩欏湪闆嗙兢鐜涓嬪緢闅剧淮鎶ゃ�� + * </pre> + * @param pipeline + * @param password + * @throws Exception + * @date 2024-01-31 + */ + private void createSSLHandler(ChannelPipeline pipeline, String password) throws Exception{ + KeyStore ks = KeyStore.getInstance("JKS"); + InputStream ksInputStream = new FileInputStream("/Users/liukun/ca/demo.liukun.com.keystore.jks"); + ks.load(ksInputStream, password.toCharArray()); + KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); + kmf.init(ks, password.toCharArray()); + SSLContext sslContext = SSLContext.getInstance("TLS"); + sslContext.init(kmf.getKeyManagers(), null, null); + SSLEngine sslEngine = sslContext.createSSLEngine(); + sslEngine.setUseClientMode(false); + sslEngine.setNeedClientAuth(false); + // 闇�鎶奡slHandler娣诲姞鍦ㄧ涓�浣� + pipeline.addFirst("ssl", new SslHandler(sslEngine)); + } + + /** + * 鏄惁鍏佽浣跨敤锛歸ss鍙潬閫氶亾 + * @param enableSSL + * @date 2024-01-31 + */ + public void setEnableSSL(boolean enableSSL) { + this.enableSSL = enableSSL; + } + + private boolean enableSSL = false; } -- Gitblit v1.9.1