1
0
mirror of https://github.com/mamoe/mirai.git synced 2025-03-02 22:40:43 +08:00

Updated robot & network structure

This commit is contained in:
Him188moe 2019-09-04 22:29:47 +08:00
parent 39330e4560
commit 99cf7e2f85
11 changed files with 39 additions and 48 deletions

View File

@ -2,7 +2,6 @@ package net.mamoe.mirai.network.packet
import lombok.Getter
import net.mamoe.mirai.network.Protocol
import net.mamoe.mirai.util.TestedSuccessfully
import net.mamoe.mirai.utils.*
import java.io.DataOutputStream
import java.io.IOException

View File

@ -1,7 +1,6 @@
package net.mamoe.mirai.network.packet
import net.mamoe.mirai.network.Protocol
import net.mamoe.mirai.util.TestedSuccessfully
import net.mamoe.mirai.utils.*
import java.io.DataInputStream

View File

@ -2,11 +2,7 @@ package net.mamoe.mirai.network.packet.login
import net.mamoe.mirai.network.Protocol
import net.mamoe.mirai.network.packet.*
import net.mamoe.mirai.util.TestedSuccessfully
import net.mamoe.mirai.utils.ByteArrayDataOutputStream
import net.mamoe.mirai.utils.TEA
import net.mamoe.mirai.utils.hexToBytes
import net.mamoe.mirai.utils.toUHexString
import net.mamoe.mirai.utils.*
import java.io.DataOutputStream
/**

View File

@ -2,8 +2,8 @@ package net.mamoe.mirai.network.packet.login
import net.mamoe.mirai.network.Protocol
import net.mamoe.mirai.network.packet.*
import net.mamoe.mirai.util.TestedSuccessfully
import net.mamoe.mirai.utils.TEA
import net.mamoe.mirai.utils.TestedSuccessfully
import net.mamoe.mirai.utils.toUHexString
import java.io.DataInputStream

View File

@ -3,7 +3,7 @@ package net.mamoe.mirai.network.packet.login
import net.mamoe.mirai.network.packet.PacketId
import net.mamoe.mirai.network.packet.ServerPacket
import net.mamoe.mirai.network.packet.goto
import net.mamoe.mirai.util.TestedSuccessfully
import net.mamoe.mirai.utils.TestedSuccessfully
import java.io.DataInputStream
/**

View File

@ -4,8 +4,8 @@ import net.mamoe.mirai.network.packet.ServerPacket
import net.mamoe.mirai.network.packet.cutTail
import net.mamoe.mirai.network.packet.dataInputStream
import net.mamoe.mirai.network.packet.goto
import net.mamoe.mirai.util.TestedSuccessfully
import net.mamoe.mirai.utils.TEA
import net.mamoe.mirai.utils.TestedSuccessfully
import net.mamoe.mirai.utils.hexToUBytes
import java.io.DataInputStream

View File

@ -4,15 +4,12 @@ package net.mamoe.mirai.task;
public interface MiraiTaskExceptionHandler {
void onHandle(Throwable e);
static MiraiTaskExceptionHandler byDefault(){
return byPrint();
}
static MiraiTaskExceptionHandler byIgnore(){
return a -> {};
}
static MiraiTaskExceptionHandler byPrint(){
static MiraiTaskExceptionHandler printing() {
return Throwable::printStackTrace;
}
static MiraiTaskExceptionHandler ignoring() {
return a -> {
};
}
}

View File

@ -35,7 +35,7 @@ public final class MiraiTaskManager {
*/
public void execute(Runnable runnable) {
this.execute(runnable, MiraiTaskExceptionHandler.byDefault());
this.execute(runnable, MiraiTaskExceptionHandler.printing());
}
public void execute(Runnable runnable, MiraiTaskExceptionHandler handler) {
@ -51,7 +51,7 @@ public final class MiraiTaskManager {
public <D> Future<D> submit(Callable<D> callable) {
return this.submit(callable, MiraiTaskExceptionHandler.byDefault());
return this.submit(callable, MiraiTaskExceptionHandler.printing());
}
public <D> Future<D> submit(Callable<D> callable, MiraiTaskExceptionHandler handler) {
@ -69,7 +69,7 @@ public final class MiraiTaskManager {
* 异步任务
*/
public <D> void ansycTask(Callable<D> callable, Consumer<D> callback) {
this.ansycTask(callable, callback, MiraiTaskExceptionHandler.byDefault());
this.ansycTask(callable, callback, MiraiTaskExceptionHandler.printing());
}
public <D> void ansycTask(Callable<D> callable, Consumer<D> callback, MiraiTaskExceptionHandler handler) {
@ -87,7 +87,7 @@ public final class MiraiTaskManager {
*/
public void repeatingTask(Runnable runnable, long intervalMillis) {
this.repeatingTask(runnable, intervalMillis, MiraiTaskExceptionHandler.byDefault());
this.repeatingTask(runnable, intervalMillis, MiraiTaskExceptionHandler.printing());
}
public void repeatingTask(Runnable runnable, long intervalMillis, MiraiTaskExceptionHandler handler) {
@ -95,7 +95,7 @@ public final class MiraiTaskManager {
}
public void repeatingTask(Runnable runnable, long intervalMillis, int times) {
this.repeatingTask(runnable, intervalMillis, times, MiraiTaskExceptionHandler.byDefault());
this.repeatingTask(runnable, intervalMillis, times, MiraiTaskExceptionHandler.printing());
}
public void repeatingTask(Runnable runnable, long intervalMillis, int times, MiraiTaskExceptionHandler handler) {

View File

@ -6,9 +6,11 @@ import java.nio.ByteBuffer;
import java.util.Random;
/**
* TEA 加密
*
* @author iweiz https://github.com/iweizime/StepChanger/blob/master/app/src/main/java/me/iweizi/stepchanger/qq/Cryptor.java
*/
public class TEA {
public final class TEA {
public static final TEA CRYPTOR_SHARE_KEY = new TEA(Protocol.Companion.hexToBytes(Protocol.shareKey));
public static final TEA CRYPTOR_0825KEY = new TEA(Protocol.Companion.hexToBytes(Protocol.key0825));
public static final TEA CRYPTOR_00BAKEY = new TEA(Protocol.Companion.hexToBytes(Protocol.key00BA));
@ -23,14 +25,12 @@ public class TEA {
private int mOutPos;
private int mPreOutPos;
private boolean isFirstBlock;
private boolean isRand;
public TEA(byte[] key) {
mKey = new long[4];
for (int i = 0; i < 4; i++) {
mKey[i] = pack(key, i * 4, 4);
}
isRand = true;
mRandom = new Random();
isFirstBlock = true;
}
@ -51,6 +51,7 @@ public class TEA {
return decrypt(source, UtilsKt.hexToBytes(keyHex));
}
@SuppressWarnings("SameParameterValue")
private static long pack(byte[] bytes, int offset, int len) {
long result = 0;
int max_offset = len > 8 ? offset + 8 : offset + len;
@ -61,11 +62,7 @@ public class TEA {
}
private int rand() {
return isRand ? mRandom.nextInt() : 0xff00ff;
}
public void enableRandom(boolean rand) {
isRand = rand;
return mRandom.nextInt();
}
private byte[] encode(byte[] bytes) {
@ -117,6 +114,7 @@ public class TEA {
isFirstBlock = false;
}
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
private boolean decodeOneBlock(byte[] ciphertext, int offset, int len) {
for (mIndexPos = 0; mIndexPos < 8; mIndexPos++) {
if (mOutPos + mIndexPos < len) {
@ -133,6 +131,7 @@ public class TEA {
}
@SuppressWarnings("SameParameterValue")
private byte[] encrypt(byte[] plaintext, int offset, int len) {
mInBlock = new byte[8];
mIV = new byte[8];
@ -183,11 +182,12 @@ public class TEA {
return mOutput;
}
private byte[] decrypt(byte[] ciphertext, int offset, int len) {
@SuppressWarnings("SameParameterValue")
private byte[] decrypt(byte[] cipherText, int offset, int len) {
if (len % 8 != 0 || len < 16) {
throw new IllegalArgumentException("must len % 8 == 0 && len >= 16");
}
mIV = decode(ciphertext, offset);
mIV = decode(cipherText, offset);
mIndexPos = mIV[0] & 7;
int plen = len - mIndexPos - 10;
isFirstBlock = true;
@ -206,7 +206,7 @@ public class TEA {
}
if (mIndexPos == 8) {
isFirstBlock = false;
if (!decodeOneBlock(ciphertext, offset, len)) {
if (!decodeOneBlock(cipherText, offset, len)) {
throw new RuntimeException("Unable to decode");
}
}
@ -216,20 +216,20 @@ public class TEA {
if (mIndexPos < 8) {
mOutput[outpos++] = isFirstBlock ?
mIV[mIndexPos] :
(byte) (ciphertext[mPreOutPos + offset + mIndexPos] ^ mIV[mIndexPos]);
(byte) (cipherText[mPreOutPos + offset + mIndexPos] ^ mIV[mIndexPos]);
++mIndexPos;
}
if (mIndexPos == 8) {
mPreOutPos = mOutPos - 8;
isFirstBlock = false;
if (!decodeOneBlock(ciphertext, offset, len)) {
if (!decodeOneBlock(cipherText, offset, len)) {
throw new RuntimeException("Unable to decode");
}
}
}
for (g = 0; g < 7; g++) {
if (mIndexPos < 8) {
if ((ciphertext[mPreOutPos + offset + mIndexPos] ^ mIV[mIndexPos]) != 0) {
if ((cipherText[mPreOutPos + offset + mIndexPos] ^ mIV[mIndexPos]) != 0) {
throw new RuntimeException();
} else {
++mIndexPos;
@ -238,7 +238,7 @@ public class TEA {
if (mIndexPos == 8) {
mPreOutPos = mOutPos;
if (!decodeOneBlock(ciphertext, offset, len)) {
if (!decodeOneBlock(cipherText, offset, len)) {
throw new RuntimeException("Unable to decode");
}
}

View File

@ -1,4 +1,4 @@
package net.mamoe.mirai.util
package net.mamoe.mirai.utils
/**
* @author Him188moe

View File

@ -6,14 +6,14 @@ import org.ini4j.Ini;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* Mirai Config
* Only support {INI} format
* Support MAP and LIST
* Thread safe
* Thread-safe Mirai Config <br>
* Only supports <code>INI</code> format <br>
* Supports {@link Map} and {@link List}
*/
public class MiraiSetting {
@ -42,12 +42,12 @@ public class MiraiSetting {
}
}
public void setSection(String key, MiraiSettingSection section){
public synchronized void setSection(String key, MiraiSettingSection section) {
cacheSection.put(key, section);
}
public MiraiSettingMapSection getMapSection(String key){
public synchronized MiraiSettingMapSection getMapSection(String key) {
if(!cacheSection.containsKey(key)) {
MiraiSettingMapSection section = new MiraiSettingMapSection();
if(ini.containsKey(key)){
@ -58,7 +58,7 @@ public class MiraiSetting {
return (MiraiSettingMapSection) cacheSection.get(key);
}
public MiraiSettingListSection getListSection(String key){
public synchronized MiraiSettingListSection getListSection(String key) {
if(!cacheSection.containsKey(key)) {
MiraiSettingListSection section = new MiraiSettingListSection();
if(ini.containsKey(key)){
@ -85,7 +85,7 @@ public class MiraiSetting {
}
}
public void clearCache(){
public synchronized void clearCache() {
cacheSection.clear();
}
}