Skip to content

Commit

Permalink
update code
Browse files Browse the repository at this point in the history
update code
  • Loading branch information
tywo45 committed Nov 23, 2021
1 parent 5012ad7 commit d5601d2
Show file tree
Hide file tree
Showing 27 changed files with 292 additions and 247 deletions.
4 changes: 2 additions & 2 deletions src/core/pom.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
Expand All @@ -8,7 +9,7 @@
<parent>
<groupId>org.t-io</groupId>
<artifactId>tio-parent</artifactId>
<version>3.7.0.v20201010-RELEASE</version>
<version>3.7.5.v20211028-RELEASE</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>

Expand All @@ -18,7 +19,6 @@
<artifactId>tio-utils</artifactId>
</dependency>

<!-- redisson, tio内置集群会用到 -->
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
Expand Down
30 changes: 9 additions & 21 deletions src/core/src/main/java/org/tio/core/ssl/facade/SSLFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,29 +205,21 @@ recommend that a file or class name and description of purpose be included on
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;
import org.tio.core.ssl.SslVo;
import org.tio.core.utils.ByteBufferUtils;

/**
* 一个
* @author tanyaowu
*
*/
public class SSLFacade implements ISSLFacade {
@SuppressWarnings("unused")
private static final String TAG = "SSLFascade";
private static final Logger log = LoggerFactory.getLogger(SSLFacade.class);

private static final String TAG = "SSLFascade";
private static final Logger log = LoggerFactory.getLogger(SSLFacade.class);
private AtomicLong sslSeq = new AtomicLong();

private Handshaker _handshaker;
private IHandshakeCompletedListener _hcl;
private final Worker _worker;
private boolean _clientMode;
private ChannelContext channelContext;
private Handshaker _handshaker;
private IHandshakeCompletedListener _hcl;
private final Worker _worker;
private boolean _clientMode;
private ChannelContext channelContext;

public SSLFacade(ChannelContext channelContext, SSLContext context, boolean client, boolean clientAuthRequired, ITaskHandler taskHandler) {
this.channelContext = channelContext;
//Currently there is no support for SSL session reuse,
// so no need to take a peerHost or port from the host application
final String who = client ? "client" : "server";
SSLEngine engine = makeSSLEngine(context, client, clientAuthRequired);
Buffers buffers = new Buffers(engine.getSession(), channelContext);
Expand All @@ -236,10 +228,6 @@ public SSLFacade(ChannelContext channelContext, SSLContext context, boolean clie
_clientMode = client;
}

// private void debug(final String message, final String... args) {
// SSLLog.debug(TAG, message, args);
// }

@Override
public boolean isClientMode() {
return _clientMode;
Expand Down Expand Up @@ -276,7 +264,7 @@ public void encrypt(SslVo sslVo) throws SSLException {
long seq = sslSeq.incrementAndGet();

ByteBuffer src = sslVo.getByteBuffer();
ByteBuffer[] byteBuffers = org.tio.core.utils.ByteBufferUtils.split(src, 1024 * 8);
ByteBuffer[] byteBuffers = ByteBufferUtils.split(src, 2048);
if (byteBuffers == null) {
log.debug("{}, 准备, SSL加密{}, 明文:{}", channelContext, channelContext.getId() + "_" + seq, sslVo);
SSLEngineResult result = _worker.wrap(sslVo, sslVo.getByteBuffer());
Expand Down
6 changes: 3 additions & 3 deletions src/core/src/main/java/org/tio/core/task/DecodeRunnable.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ recommend that a file or class name and description of purpose be included on
import org.tio.core.ChannelContext.CloseCode;
import org.tio.core.Tio;
import org.tio.core.TioConfig;
import org.tio.core.exception.AioDecodeException;
import org.tio.core.exception.TioDecodeException;
import org.tio.core.intf.Packet;
import org.tio.core.stat.ChannelStat;
import org.tio.core.stat.IpStat;
Expand Down Expand Up @@ -346,7 +346,7 @@ public void decode() {
int per = readableLength / channelStat.decodeFailCount;
if (per < Math.min(channelContext.getReadBufferSize() / 2, 256)) {
String str = "连续解码" + channelStat.decodeFailCount + "次都不成功,并且平均每次接收到的数据为" + per + "字节,有慢攻击的嫌疑";
throw new AioDecodeException(str);
throw new TioDecodeException(str);
}
}
}
Expand Down Expand Up @@ -413,7 +413,7 @@ public void decode() {

channelContext.setPacketNeededLength(null);

if (e instanceof AioDecodeException) {
if (e instanceof TioDecodeException) {
List<Long> list = tioConfig.ipStats.durationList;
if (list != null && list.size() > 0) {
try {
Expand Down
29 changes: 29 additions & 0 deletions src/core/src/main/java/org/tio/core/utils/ByteBufferUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ recommend that a file or class name and description of purpose be included on

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -482,6 +483,34 @@ public static String readString(ByteBuffer buffer, String charset, char endChar,
return null;
}

/**
* 获取utf-8字符串
* @param bytes
* @return
*
*/
public static String getUtf8(byte[] bytes) {
return new String(bytes, StandardCharsets.UTF_8);
}

/**
* 读取byte
* @param buffer
* @return
*/
public static byte read(ByteBuffer buffer) {
return buffer.get();
}

/**
* 读取short
* @param buffer
* @return
*/
public static short readShort(ByteBuffer buffer) {
return buffer.getShort();
}

public static int readUB1(ByteBuffer buffer) {
int ret = buffer.get() & 0xff;
return ret;
Expand Down
1 change: 1 addition & 0 deletions src/core/src/main/java/org/tio/server/ServerTioConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ public void share(ServerTioConfig tioConfig) {
this.clientNodes = tioConfig.clientNodes;
this.connections = tioConfig.connections;
this.groups = tioConfig.groups;
this.groupStat = tioConfig.groupStat;
this.users = tioConfig.users;
this.tokens = tioConfig.tokens;
this.ids = tioConfig.ids;
Expand Down
64 changes: 0 additions & 64 deletions src/core/src/main/java/org/tio/server/TioServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,12 @@ recommend that a file or class name and description of purpose be included on
package org.tio.server;

import java.io.IOException;
import java.io.InputStream;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.StandardSocketOptions;
import java.net.URL;
import java.nio.channels.AsynchronousChannelGroup;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
Expand All @@ -212,7 +208,6 @@ recommend that a file or class name and description of purpose be included on
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.Node;
import org.tio.utils.IoUtils;
import org.tio.utils.SysConst;
import org.tio.utils.hutool.DateUtil;
import org.tio.utils.hutool.StrUtil;
Expand Down Expand Up @@ -353,8 +348,6 @@ public void start(String serverIp, int serverPort) throws IOException {
} else {
System.out.println(printStr);
}

checkLastVersion();
}

/**
Expand Down Expand Up @@ -401,63 +394,6 @@ public boolean stop() {
return ret;
}

private void checkLastVersion() {
if (checkLastVersion) {
serverTioConfig.groupExecutor.execute(new Runnable() {
@Override
public void run() {
try {
URL url = new URL(SysConst.CHECK_LASTVERSION_URL_1);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(10 * 1000);
connection.connect();
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream inputStream = connection.getInputStream();
String result = IoUtils.streamToString(inputStream);

connection.disconnect();

url = new URL(SysConst.CHECK_LASTVERSION_URL_2 + result);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(10 * 1000);
connection.connect();
responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
inputStream = connection.getInputStream();
result = IoUtils.streamToString(inputStream);

if (SysConst.TIO_CORE_VERSION.equals(result)) {
log.info("The version you are using is the latest");
} else {
log.info("t-io latest version:{},your version:{}", result, SysConst.TIO_CORE_VERSION);
//3.6.2.v20200808-RELEASE
String myVersionDateStr = SysConst.TIO_CORE_VERSION.substring(SysConst.TIO_CORE_VERSION.length() - 16, SysConst.TIO_CORE_VERSION.length() - 8);
String latestVersionDateStr = result.substring(result.length() - 16, result.length() - 8);

SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");

Date myVersionDate = format.parse(myVersionDateStr);
Date latestVersionDate = format.parse(latestVersionDateStr);
Integer days = DateUtil.daysBetween(myVersionDate, latestVersionDate);

log.info("You haven't upgraded in {} days", days);
}
}

connection.disconnect();

}
} catch (Exception e) {
// log.error("", e);
}
}
});
}
}

public boolean isCheckLastVersion() {
return checkLastVersion;
}
Expand Down
Loading

0 comments on commit d5601d2

Please sign in to comment.