diff --git a/AsyncSocket/src/main/kotlin/cn/tursom/socket/security/SecurityNioServer.kt b/AsyncSocket/src/main/kotlin/cn/tursom/socket/security/SecurityNioServer.kt index 7fe2c57..30dba3a 100644 --- a/AsyncSocket/src/main/kotlin/cn/tursom/socket/security/SecurityNioServer.kt +++ b/AsyncSocket/src/main/kotlin/cn/tursom/socket/security/SecurityNioServer.kt @@ -10,17 +10,19 @@ class SecurityNioServer( port: Int, backlog: Int = 50, coroutineScope: CoroutineScope = GlobalScope, + autoCloseSocket: Boolean = true, @Suppress("MemberVisibilityCanBePrivate") val rsa: RSA = RSA(), val handler: suspend AsyncSocket.() -> Unit -) : NioServer(port, backlog, coroutineScope, { +) : NioServer(port, backlog, coroutineScope, autoCloseSocket, { AsyncSocketSecurityUtil.initActiveAESSocket(this, rsa) handler() }) { constructor( port: Int, + keySize: Int, backlog: Int = 50, coroutineScope: CoroutineScope = GlobalScope, - keySize: Int, + autoCloseSocket: Boolean = true, handler: suspend AsyncSocket.() -> Unit - ) : this(port, backlog, coroutineScope, RSA(keySize), handler) + ) : this(port, backlog, coroutineScope, autoCloseSocket, RSA(keySize), handler) } \ No newline at end of file diff --git a/AsyncSocket/src/test/kotlin/cn/tursom/niothread/EchoServer.java b/AsyncSocket/src/test/kotlin/cn/tursom/niothread/EchoServer.java index 5e0ec10..73a3ce9 100644 --- a/AsyncSocket/src/test/kotlin/cn/tursom/niothread/EchoServer.java +++ b/AsyncSocket/src/test/kotlin/cn/tursom/niothread/EchoServer.java @@ -75,7 +75,7 @@ public class EchoServer implements Closeable, Runnable { } @Override - public void handleRead(@NotNull SelectionKey key, @NotNull NioThread nioThread) throws Throwable { + public void handleRead(@NotNull SelectionKey key, @NotNull NioThread nioThread) { ByteBuffer buffer = (ByteBuffer) key.attachment(); SocketChannel channel = (SocketChannel) key.channel(); ByteBufferExtensionKt.read(channel, buffer); @@ -83,7 +83,7 @@ public class EchoServer implements Closeable, Runnable { } @Override - public void handleWrite(@NotNull SelectionKey key, @NotNull NioThread nioThread) throws Throwable { + public void handleWrite(@NotNull SelectionKey key, @NotNull NioThread nioThread) { ByteBuffer buffer = (ByteBuffer) key.attachment(); SocketChannel channel = (SocketChannel) key.channel(); ByteBufferExtensionKt.write(channel, buffer); diff --git a/AsyncSocket/src/test/kotlin/cn/tursom/socket/server/NioLoopServerTest.java b/AsyncSocket/src/test/kotlin/cn/tursom/socket/server/NioLoopServerTest.java new file mode 100644 index 0000000..107c391 --- /dev/null +++ b/AsyncSocket/src/test/kotlin/cn/tursom/socket/server/NioLoopServerTest.java @@ -0,0 +1,80 @@ +package cn.tursom.socket.server; + +import cn.tursom.core.buffer.ByteBuffer; +import cn.tursom.core.buffer.ByteBufferExtensionKt; +import cn.tursom.core.buffer.impl.DirectByteBuffer; +import cn.tursom.niothread.NioProtocol; +import cn.tursom.niothread.NioThread; +import cn.tursom.niothread.WorkerLoopNioThread; +import org.jetbrains.annotations.NotNull; + +import java.io.Closeable; +import java.io.IOException; +import java.nio.channels.SelectionKey; +import java.nio.channels.Selector; +import java.nio.channels.SocketChannel; + +public class NioLoopServerTest implements Runnable, Closeable { + private static final NioProtocol protocol = new NioProtocol() { + @Override + public void handleConnect(@NotNull SelectionKey key, @NotNull NioThread nioThread) { + key.interestOps(SelectionKey.OP_READ); + key.attach(new DirectByteBuffer(1024)); + } + + @Override + public void handleRead(@NotNull SelectionKey key, @NotNull NioThread nioThread) { + ByteBuffer buffer = (ByteBuffer) key.attachment(); + SocketChannel channel = (SocketChannel) key.channel(); + ByteBufferExtensionKt.read(channel, buffer); + key.interestOps(SelectionKey.OP_WRITE); + } + + @Override + public void handleWrite(@NotNull SelectionKey key, @NotNull NioThread nioThread) { + ByteBuffer buffer = (ByteBuffer) key.attachment(); + SocketChannel channel = (SocketChannel) key.channel(); + ByteBufferExtensionKt.write(channel, buffer); + key.interestOps(SelectionKey.OP_WRITE); + } + + @Override + public void exceptionCause(@NotNull SelectionKey key, @NotNull NioThread nioThread, @NotNull Throwable e) throws Throwable { + e.printStackTrace(); + key.cancel(); + key.channel().close(); + } + }; + private final int port; + private final NioLoopServer server; + + public NioLoopServerTest(int port) { + this.port = port; + server = new NioLoopServer(port, protocol, 50, (threadName, workLoop) -> { + try { + return new WorkerLoopNioThread(threadName, Selector.open(), true, 3000, workLoop); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); + } + + @Override + public void close() { + server.close(); + } + + @Override + public void run() { + server.run(); + } + + public int getPort() { + return port; + } + + public static void main(String[] args) { + NioLoopServerTest server = new NioLoopServerTest(12345); + server.run(); + } +} diff --git a/src/main/kotlin/cn/tursom/core/encrypt/DSA.kt b/src/main/kotlin/cn/tursom/core/encrypt/DSA.kt index f04271d..cae3f82 100644 --- a/src/main/kotlin/cn/tursom/core/encrypt/DSA.kt +++ b/src/main/kotlin/cn/tursom/core/encrypt/DSA.kt @@ -1,10 +1,10 @@ package cn.tursom.core.encrypt -import sun.security.provider.DSAPublicKey import java.security.KeyFactory import java.security.KeyPair import java.security.KeyPairGenerator import java.security.interfaces.DSAPrivateKey +import java.security.interfaces.DSAPublicKey import java.security.spec.X509EncodedKeySpec @Suppress("unused", "MemberVisibilityCanBePrivate") diff --git a/src/main/kotlin/cn/tursom/core/encrypt/ECC.kt b/src/main/kotlin/cn/tursom/core/encrypt/ECC.kt index c6ef4dd..f881ddf 100644 --- a/src/main/kotlin/cn/tursom/core/encrypt/ECC.kt +++ b/src/main/kotlin/cn/tursom/core/encrypt/ECC.kt @@ -39,21 +39,34 @@ class ECC( generator.generateKeyPair() }) - constructor(keySize: Int = 256, standardCurveLine: String = StandardCurveLine.secp256k1.name) : this(keySize, ECGenParameterSpec(standardCurveLine)) - constructor(keySize: Int = 256, standardCurveLine: StandardCurveLine) : this(keySize, standardCurveLine.name) - constructor(publicKey: ByteArray) : this(KeyFactory.getInstance("EC").generatePublic(X509EncodedKeySpec(publicKey)) as ECPublicKey) + constructor( + keySize: Int = 256, + standardCurveLine: String = StandardCurveLine.secp256k1.name.replace('_', ' ') + ) : this( + keySize, + ECGenParameterSpec(standardCurveLine) + ) + + constructor(keySize: Int = 256, standardCurveLine: StandardCurveLine) : this( + keySize, + standardCurveLine.name.replace('_', ' ') + ) + + constructor(publicKey: ByteArray) : this( + KeyFactory.getInstance("EC").generatePublic(X509EncodedKeySpec(publicKey)) as ECPublicKey + ) override fun signature(digest: String): String = "${digest}withECDSA" @Suppress("EnumEntryName", "SpellCheckingInspection") enum class StandardCurveLine { - secp224r1, `NIST B-233`, secp160r1, secp160r2, `NIST K-233`, sect163r2, secp128r1, sect163r1, `NIST P-256`, - sect409r1, `NIST B-163`, `NIST B-283`, secp128r2, brainpoolP192r1, secp192r1, brainpoolP256r1, `NIST K-283`, - secp256r1, `NIST P-384`, sect113r2, sect163k1, `NIST K-163`, `NIST B-409`, secp224k1, sect239k1, sect193r2, - `NIST K-409`, secp112r2, sect113r1, brainpoolP320r1, secp112r1, secp160k1, `NIST P-224`, sect193r1, sect233k1, - sect571r1, `NIST P-192`, sect409k1, `NIST B-571`, brainpoolP224r1, sect233r1, sect571k1, brainpoolP160r1, - `NIST K-571`, secp256k1, secp192k1, sect283k1, sect283r1, secp384r1, secp521r1, sect131r1, sect131r2, - brainpoolP384r1, brainpoolP512r1, `NIST P-521` + secp224r1, `NIST_B-233`, secp160r1, secp160r2, `NIST_K-233`, sect163r2, secp128r1, sect163r1, `NIST_P-256`, + sect409r1, `NIST_B-163`, `NIST_B-283`, secp128r2, brainpoolP192r1, secp192r1, brainpoolP256r1, `NIST_K-283`, + secp256r1, `NIST_P-384`, sect113r2, sect163k1, `NIST_K-163`, `NIST_B-409`, secp224k1, sect239k1, sect193r2, + `NIST_K-409`, secp112r2, sect113r1, brainpoolP320r1, secp112r1, secp160k1, `NIST_P-224`, sect193r1, sect233k1, + sect571r1, `NIST_P-192`, sect409k1, `NIST_B-571`, brainpoolP224r1, sect233r1, sect571k1, brainpoolP160r1, + `NIST_K-571`, secp256k1, secp192k1, sect283k1, sect283r1, secp384r1, secp521r1, sect131r1, sect131r2, + brainpoolP384r1, brainpoolP512r1, `NIST_P-521` } companion object {