[*] android信令接入

This commit is contained in:
acgist
2023-03-29 08:16:04 +08:00
parent fcca3c90aa
commit f68119fd30
32 changed files with 397 additions and 128 deletions

View File

@@ -39,7 +39,7 @@ public class SocketClient extends ClientAdapter<AsynchronousSocketChannel> {
public SocketClient(SocketProperties socketProperties, AsynchronousSocketChannel instance) {
super(socketProperties.getTimeout(), instance);
this.ip = this.clientIp(instance);
this.cipher = CipherUtils.buildCipher(Cipher.ENCRYPT_MODE, socketProperties.getEncrypt(), socketProperties.getEncryptKey());
this.cipher = CipherUtils.buildCipher(Cipher.ENCRYPT_MODE, socketProperties.getEncrypt(), socketProperties.getEncryptSecret());
}
@Override

View File

@@ -66,7 +66,7 @@ public final class SocketSignalMessageHandler implements CompletionHandler<Integ
this.messageLength = 0;
this.bufferSize = socketProperties.getBufferSize();
this.maxBufferSize = socketProperties.getMaxBufferSize();
this.cipher = CipherUtils.buildCipher(Cipher.DECRYPT_MODE, socketProperties.getEncrypt(), socketProperties.getEncryptKey());
this.cipher = CipherUtils.buildCipher(Cipher.DECRYPT_MODE, socketProperties.getEncrypt(), socketProperties.getEncryptSecret());
this.buffer = ByteBuffer.allocateDirect(maxBufferSize);
this.channel = channel;
this.clientManager = clientManager;

View File

@@ -58,25 +58,25 @@ public class SocketSignalAutoConfiguration {
*/
private void buildSecret(SocketProperties socketProperties) {
log.info("Socket信令加密策略{}", socketProperties.getEncrypt());
if(socketProperties.getEncrypt() == null || StringUtils.isNotEmpty(socketProperties.getEncryptKey())) {
if(socketProperties.getEncrypt() == null) {
return;
}
final Random random = new Random();
switch (socketProperties.getEncrypt()) {
case AES -> {
final byte[] bytes = new byte[16];
random.nextBytes(bytes);
socketProperties.setEncryptKey(Base64.getMimeEncoder().encodeToString(bytes));
if(StringUtils.isNotEmpty(socketProperties.getEncryptSecret())) {
log.info("Socket信令加密密码固定{}", socketProperties.getEncryptSecret());
return;
}
case DES -> {
final byte[] bytes = new byte[8];
random.nextBytes(bytes);
socketProperties.setEncryptKey(Base64.getMimeEncoder().encodeToString(bytes));
final byte[] bytes = switch (socketProperties.getEncrypt()) {
case AES -> new byte[16];
case DES -> new byte[8];
default -> null;
};
if(bytes == null) {
final Random random = new Random();
random.nextBytes(bytes);
socketProperties.setEncryptSecret(Base64.getMimeEncoder().encodeToString(bytes));
log.info("Socket信令加密密码随机{}", socketProperties.getEncryptSecret());
} else {
log.warn("Socket信令加密密码算法不支持的算法{}", socketProperties.getEncrypt());
default -> {
// 其他情况使用明文
}
}
}
}

View File

@@ -0,0 +1,5 @@
package com.acgist.taoyao.signal.party.p2p;
public class Session {
}

View File

@@ -0,0 +1,5 @@
package com.acgist.taoyao.signal.party.p2p;
public class SessionManager {
}

View File

@@ -0,0 +1,5 @@
package com.acgist.taoyao.signal.protocol.p2p;
public class P2PAnswerProtocol {
}

View File

@@ -0,0 +1,5 @@
package com.acgist.taoyao.signal.protocol.p2p;
public class P2PCallProtocol {
}

View File

@@ -0,0 +1,5 @@
package com.acgist.taoyao.signal.protocol.p2p;
public class P2PCandidateProtocol {
}

View File

@@ -0,0 +1,5 @@
package com.acgist.taoyao.signal.protocol.p2p;
public class P2POfferProtocol {
}

View File

@@ -28,22 +28,22 @@ public class CipherUtils {
/**
* @param mode 模式
* @param encrypt 算法
* @param key 密钥
* @param secret 密钥
*
* @return 加密工具
*/
public static final Cipher buildCipher(int mode, Encrypt encrypt, String key) {
if(encrypt == null || encrypt == Encrypt.PLAINTEXT || StringUtils.isEmpty(key)) {
public static final Cipher buildCipher(int mode, Encrypt encrypt, String secret) {
if(encrypt == null || encrypt == Encrypt.PLAINTEXT || StringUtils.isEmpty(secret)) {
return null;
}
try {
final String algo = encrypt.getAlgo();
final String name = encrypt.name();
final Cipher cipher = Cipher.getInstance(algo);
cipher.init(mode, new SecretKeySpec(Base64.getMimeDecoder().decode(key), name));
cipher.init(mode, new SecretKeySpec(Base64.getMimeDecoder().decode(secret), name));
return cipher;
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException e) {
log.error("创建加密工具异常:{} - {} - {}", mode, encrypt, key, e);
log.error("创建加密工具异常:{} - {} - {}", mode, encrypt, secret, e);
}
return null;
}