Commit 99cf7e2f authored by Him188moe's avatar Him188moe

Updated robot & network structure

parent 39330e45
...@@ -2,7 +2,6 @@ package net.mamoe.mirai.network.packet ...@@ -2,7 +2,6 @@ package net.mamoe.mirai.network.packet
import lombok.Getter import lombok.Getter
import net.mamoe.mirai.network.Protocol import net.mamoe.mirai.network.Protocol
import net.mamoe.mirai.util.TestedSuccessfully
import net.mamoe.mirai.utils.* import net.mamoe.mirai.utils.*
import java.io.DataOutputStream import java.io.DataOutputStream
import java.io.IOException import java.io.IOException
......
package net.mamoe.mirai.network.packet package net.mamoe.mirai.network.packet
import net.mamoe.mirai.network.Protocol import net.mamoe.mirai.network.Protocol
import net.mamoe.mirai.util.TestedSuccessfully
import net.mamoe.mirai.utils.* import net.mamoe.mirai.utils.*
import java.io.DataInputStream import java.io.DataInputStream
......
...@@ -2,11 +2,7 @@ package net.mamoe.mirai.network.packet.login ...@@ -2,11 +2,7 @@ package net.mamoe.mirai.network.packet.login
import net.mamoe.mirai.network.Protocol import net.mamoe.mirai.network.Protocol
import net.mamoe.mirai.network.packet.* import net.mamoe.mirai.network.packet.*
import net.mamoe.mirai.util.TestedSuccessfully import net.mamoe.mirai.utils.*
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 java.io.DataOutputStream import java.io.DataOutputStream
/** /**
......
...@@ -2,8 +2,8 @@ package net.mamoe.mirai.network.packet.login ...@@ -2,8 +2,8 @@ package net.mamoe.mirai.network.packet.login
import net.mamoe.mirai.network.Protocol import net.mamoe.mirai.network.Protocol
import net.mamoe.mirai.network.packet.* import net.mamoe.mirai.network.packet.*
import net.mamoe.mirai.util.TestedSuccessfully
import net.mamoe.mirai.utils.TEA import net.mamoe.mirai.utils.TEA
import net.mamoe.mirai.utils.TestedSuccessfully
import net.mamoe.mirai.utils.toUHexString import net.mamoe.mirai.utils.toUHexString
import java.io.DataInputStream import java.io.DataInputStream
......
...@@ -3,7 +3,7 @@ package net.mamoe.mirai.network.packet.login ...@@ -3,7 +3,7 @@ package net.mamoe.mirai.network.packet.login
import net.mamoe.mirai.network.packet.PacketId import net.mamoe.mirai.network.packet.PacketId
import net.mamoe.mirai.network.packet.ServerPacket import net.mamoe.mirai.network.packet.ServerPacket
import net.mamoe.mirai.network.packet.goto import net.mamoe.mirai.network.packet.goto
import net.mamoe.mirai.util.TestedSuccessfully import net.mamoe.mirai.utils.TestedSuccessfully
import java.io.DataInputStream import java.io.DataInputStream
/** /**
......
...@@ -4,8 +4,8 @@ import net.mamoe.mirai.network.packet.ServerPacket ...@@ -4,8 +4,8 @@ import net.mamoe.mirai.network.packet.ServerPacket
import net.mamoe.mirai.network.packet.cutTail import net.mamoe.mirai.network.packet.cutTail
import net.mamoe.mirai.network.packet.dataInputStream import net.mamoe.mirai.network.packet.dataInputStream
import net.mamoe.mirai.network.packet.goto import net.mamoe.mirai.network.packet.goto
import net.mamoe.mirai.util.TestedSuccessfully
import net.mamoe.mirai.utils.TEA import net.mamoe.mirai.utils.TEA
import net.mamoe.mirai.utils.TestedSuccessfully
import net.mamoe.mirai.utils.hexToUBytes import net.mamoe.mirai.utils.hexToUBytes
import java.io.DataInputStream import java.io.DataInputStream
......
...@@ -4,15 +4,12 @@ package net.mamoe.mirai.task; ...@@ -4,15 +4,12 @@ package net.mamoe.mirai.task;
public interface MiraiTaskExceptionHandler { public interface MiraiTaskExceptionHandler {
void onHandle(Throwable e); void onHandle(Throwable e);
static MiraiTaskExceptionHandler byDefault(){ static MiraiTaskExceptionHandler printing() {
return byPrint(); return Throwable::printStackTrace;
}
static MiraiTaskExceptionHandler byIgnore(){
return a -> {};
} }
static MiraiTaskExceptionHandler byPrint(){ static MiraiTaskExceptionHandler ignoring() {
return Throwable::printStackTrace; return a -> {
};
} }
} }
...@@ -35,7 +35,7 @@ public final class MiraiTaskManager { ...@@ -35,7 +35,7 @@ public final class MiraiTaskManager {
*/ */
public void execute(Runnable runnable) { public void execute(Runnable runnable) {
this.execute(runnable, MiraiTaskExceptionHandler.byDefault()); this.execute(runnable, MiraiTaskExceptionHandler.printing());
} }
public void execute(Runnable runnable, MiraiTaskExceptionHandler handler) { public void execute(Runnable runnable, MiraiTaskExceptionHandler handler) {
...@@ -51,7 +51,7 @@ public final class MiraiTaskManager { ...@@ -51,7 +51,7 @@ public final class MiraiTaskManager {
public <D> Future<D> submit(Callable<D> callable) { 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) { public <D> Future<D> submit(Callable<D> callable, MiraiTaskExceptionHandler handler) {
...@@ -69,7 +69,7 @@ public final class MiraiTaskManager { ...@@ -69,7 +69,7 @@ public final class MiraiTaskManager {
* 异步任务 * 异步任务
*/ */
public <D> void ansycTask(Callable<D> callable, Consumer<D> callback) { 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) { public <D> void ansycTask(Callable<D> callable, Consumer<D> callback, MiraiTaskExceptionHandler handler) {
...@@ -87,7 +87,7 @@ public final class MiraiTaskManager { ...@@ -87,7 +87,7 @@ public final class MiraiTaskManager {
*/ */
public void repeatingTask(Runnable runnable, long intervalMillis) { 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) { public void repeatingTask(Runnable runnable, long intervalMillis, MiraiTaskExceptionHandler handler) {
...@@ -95,7 +95,7 @@ public final class MiraiTaskManager { ...@@ -95,7 +95,7 @@ public final class MiraiTaskManager {
} }
public void repeatingTask(Runnable runnable, long intervalMillis, int times) { 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) { public void repeatingTask(Runnable runnable, long intervalMillis, int times, MiraiTaskExceptionHandler handler) {
......
...@@ -6,9 +6,11 @@ import java.nio.ByteBuffer; ...@@ -6,9 +6,11 @@ import java.nio.ByteBuffer;
import java.util.Random; import java.util.Random;
/** /**
* TEA 加密
*
* @author iweiz https://github.com/iweizime/StepChanger/blob/master/app/src/main/java/me/iweizi/stepchanger/qq/Cryptor.java * @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_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_0825KEY = new TEA(Protocol.Companion.hexToBytes(Protocol.key0825));
public static final TEA CRYPTOR_00BAKEY = new TEA(Protocol.Companion.hexToBytes(Protocol.key00BA)); public static final TEA CRYPTOR_00BAKEY = new TEA(Protocol.Companion.hexToBytes(Protocol.key00BA));
...@@ -23,14 +25,12 @@ public class TEA { ...@@ -23,14 +25,12 @@ public class TEA {
private int mOutPos; private int mOutPos;
private int mPreOutPos; private int mPreOutPos;
private boolean isFirstBlock; private boolean isFirstBlock;
private boolean isRand;
public TEA(byte[] key) { public TEA(byte[] key) {
mKey = new long[4]; mKey = new long[4];
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
mKey[i] = pack(key, i * 4, 4); mKey[i] = pack(key, i * 4, 4);
} }
isRand = true;
mRandom = new Random(); mRandom = new Random();
isFirstBlock = true; isFirstBlock = true;
} }
...@@ -51,6 +51,7 @@ public class TEA { ...@@ -51,6 +51,7 @@ public class TEA {
return decrypt(source, UtilsKt.hexToBytes(keyHex)); return decrypt(source, UtilsKt.hexToBytes(keyHex));
} }
@SuppressWarnings("SameParameterValue")
private static long pack(byte[] bytes, int offset, int len) { private static long pack(byte[] bytes, int offset, int len) {
long result = 0; long result = 0;
int max_offset = len > 8 ? offset + 8 : offset + len; int max_offset = len > 8 ? offset + 8 : offset + len;
...@@ -61,11 +62,7 @@ public class TEA { ...@@ -61,11 +62,7 @@ public class TEA {
} }
private int rand() { private int rand() {
return isRand ? mRandom.nextInt() : 0xff00ff; return mRandom.nextInt();
}
public void enableRandom(boolean rand) {
isRand = rand;
} }
private byte[] encode(byte[] bytes) { private byte[] encode(byte[] bytes) {
...@@ -117,6 +114,7 @@ public class TEA { ...@@ -117,6 +114,7 @@ public class TEA {
isFirstBlock = false; isFirstBlock = false;
} }
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
private boolean decodeOneBlock(byte[] ciphertext, int offset, int len) { private boolean decodeOneBlock(byte[] ciphertext, int offset, int len) {
for (mIndexPos = 0; mIndexPos < 8; mIndexPos++) { for (mIndexPos = 0; mIndexPos < 8; mIndexPos++) {
if (mOutPos + mIndexPos < len) { if (mOutPos + mIndexPos < len) {
...@@ -133,6 +131,7 @@ public class TEA { ...@@ -133,6 +131,7 @@ public class TEA {
} }
@SuppressWarnings("SameParameterValue")
private byte[] encrypt(byte[] plaintext, int offset, int len) { private byte[] encrypt(byte[] plaintext, int offset, int len) {
mInBlock = new byte[8]; mInBlock = new byte[8];
mIV = new byte[8]; mIV = new byte[8];
...@@ -183,11 +182,12 @@ public class TEA { ...@@ -183,11 +182,12 @@ public class TEA {
return mOutput; 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) { if (len % 8 != 0 || len < 16) {
throw new IllegalArgumentException("must 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; mIndexPos = mIV[0] & 7;
int plen = len - mIndexPos - 10; int plen = len - mIndexPos - 10;
isFirstBlock = true; isFirstBlock = true;
...@@ -206,7 +206,7 @@ public class TEA { ...@@ -206,7 +206,7 @@ public class TEA {
} }
if (mIndexPos == 8) { if (mIndexPos == 8) {
isFirstBlock = false; isFirstBlock = false;
if (!decodeOneBlock(ciphertext, offset, len)) { if (!decodeOneBlock(cipherText, offset, len)) {
throw new RuntimeException("Unable to decode"); throw new RuntimeException("Unable to decode");
} }
} }
...@@ -216,20 +216,20 @@ public class TEA { ...@@ -216,20 +216,20 @@ public class TEA {
if (mIndexPos < 8) { if (mIndexPos < 8) {
mOutput[outpos++] = isFirstBlock ? mOutput[outpos++] = isFirstBlock ?
mIV[mIndexPos] : mIV[mIndexPos] :
(byte) (ciphertext[mPreOutPos + offset + mIndexPos] ^ mIV[mIndexPos]); (byte) (cipherText[mPreOutPos + offset + mIndexPos] ^ mIV[mIndexPos]);
++mIndexPos; ++mIndexPos;
} }
if (mIndexPos == 8) { if (mIndexPos == 8) {
mPreOutPos = mOutPos - 8; mPreOutPos = mOutPos - 8;
isFirstBlock = false; isFirstBlock = false;
if (!decodeOneBlock(ciphertext, offset, len)) { if (!decodeOneBlock(cipherText, offset, len)) {
throw new RuntimeException("Unable to decode"); throw new RuntimeException("Unable to decode");
} }
} }
} }
for (g = 0; g < 7; g++) { for (g = 0; g < 7; g++) {
if (mIndexPos < 8) { if (mIndexPos < 8) {
if ((ciphertext[mPreOutPos + offset + mIndexPos] ^ mIV[mIndexPos]) != 0) { if ((cipherText[mPreOutPos + offset + mIndexPos] ^ mIV[mIndexPos]) != 0) {
throw new RuntimeException(); throw new RuntimeException();
} else { } else {
++mIndexPos; ++mIndexPos;
...@@ -238,7 +238,7 @@ public class TEA { ...@@ -238,7 +238,7 @@ public class TEA {
if (mIndexPos == 8) { if (mIndexPos == 8) {
mPreOutPos = mOutPos; mPreOutPos = mOutPos;
if (!decodeOneBlock(ciphertext, offset, len)) { if (!decodeOneBlock(cipherText, offset, len)) {
throw new RuntimeException("Unable to decode"); throw new RuntimeException("Unable to decode");
} }
} }
......
package net.mamoe.mirai.util package net.mamoe.mirai.utils
/** /**
* @author Him188moe * @author Him188moe
......
...@@ -6,14 +6,14 @@ import org.ini4j.Ini; ...@@ -6,14 +6,14 @@ import org.ini4j.Ini;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
/** /**
* Mirai Config * Thread-safe Mirai Config <br>
* Only support {INI} format * Only supports <code>INI</code> format <br>
* Support MAP and LIST * Supports {@link Map} and {@link List}
* Thread safe
*/ */
public class MiraiSetting { public class MiraiSetting {
...@@ -42,12 +42,12 @@ 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); cacheSection.put(key, section);
} }
public MiraiSettingMapSection getMapSection(String key){ public synchronized MiraiSettingMapSection getMapSection(String key) {
if(!cacheSection.containsKey(key)) { if(!cacheSection.containsKey(key)) {
MiraiSettingMapSection section = new MiraiSettingMapSection(); MiraiSettingMapSection section = new MiraiSettingMapSection();
if(ini.containsKey(key)){ if(ini.containsKey(key)){
...@@ -58,7 +58,7 @@ public class MiraiSetting { ...@@ -58,7 +58,7 @@ public class MiraiSetting {
return (MiraiSettingMapSection) cacheSection.get(key); return (MiraiSettingMapSection) cacheSection.get(key);
} }
public MiraiSettingListSection getListSection(String key){ public synchronized MiraiSettingListSection getListSection(String key) {
if(!cacheSection.containsKey(key)) { if(!cacheSection.containsKey(key)) {
MiraiSettingListSection section = new MiraiSettingListSection(); MiraiSettingListSection section = new MiraiSettingListSection();
if(ini.containsKey(key)){ if(ini.containsKey(key)){
...@@ -85,7 +85,7 @@ public class MiraiSetting { ...@@ -85,7 +85,7 @@ public class MiraiSetting {
} }
} }
public void clearCache(){ public synchronized void clearCache() {
cacheSection.clear(); cacheSection.clear();
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment