Commit e85014e7 authored by liujiahua123123's avatar liujiahua123123

Merge remote-tracking branch 'origin/master'

parents eeed2859 43595db0
package net.mamoe.mirai.network;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author Him188moe @ Mirai Project
*/
public interface Protocol {
List<String> SERVER_IP = new ArrayList<>() {{
add("183.60.56.29");
List.of(
"sz2.tencent.com",
"sz3.tencent.com",
"sz4.tencent.com",
"sz5.tencent.com",
"sz6.tencent.com",
"sz8.tencent.com",
"sz9.tencent.com"
).forEach(s -> {
try {
SERVER_IP.add(InetAddress.getByName(s).getHostAddress());
} catch (UnknownHostException ignored) {
}
});
}};
String head = "02";
String ver = "37 13 ";
String fixVer = "03 00 00 00 01 2E 01 00 00 68 52 00 00 00 00 ";
String tail = " 03";
String _fixVer = "02 00 00 00 01 01 01 00 00 68 20 ";
String _0825data0 = "00 18 00 16 00 01 ";
String _0825data2 = "00 00 04 53 00 00 00 01 00 00 15 85 ";
String _0825key = "A4 F1 91 88 C9 82 14 99 0C 9E 56 55 91 23 C8 3D";
String redirectionKey = "A8 F2 14 5F 58 12 60 AF 07 63 97 D6 76 B2 1A 3B";
String publicKey = "02 6D 28 41 D2 A5 6F D2 FC 3E 2A 1F 03 75 DE 6E 28 8F A8 19 3E 5F 16 49 D3";
String shareKey = "1A E9 7F 7D C9 73 75 98 AC 02 E0 80 5F A9 C6 AF";
String _0836fix = "06 A9 12 97 B7 F8 76 25 AF AF D3 EA B4 C8 BC E7 ";
String _00BaKey = "C1 9C B8 C8 7B 8C 81 BA 9E 9E 7A 89 E1 7A EC 94";
String _00BaFixKey = "69 20 D1 14 74 F5 B3 93 E4 D5 02 B3 71 1A CD 2A";
String encryptKey = "“BA 42 FF 01 CF B4 FF D2 12 F0 6E A7 1B 7C B3 08”";
static byte[] hexToBytes(String hex) {
var list = Arrays.stream(hex.split(" ")).map(String::trim).map(s -> Byte.valueOf(s, 16)).collect(Collectors.toList());
var buff = new byte[list.size()];
for (int i = 0; i < list.size(); i++) {
buff[i] = list.get(i);
}
return buff;
}
}
package net.mamoe.mirai.network.packet.client; package net.mamoe.mirai.network.packet.client;
import lombok.Data;
import lombok.EqualsAndHashCode;
import net.mamoe.mirai.network.Protocol;
import net.mamoe.mirai.network.packet.PacketId; import net.mamoe.mirai.network.packet.PacketId;
import net.mamoe.mirai.util.TEAEncryption;
import java.io.IOException; import java.io.IOException;
/** /**
* @author Him188moe @ Mirai Project * @author Him188moe @ Mirai Project
*/ */
@PacketId(0x0058) @EqualsAndHashCode(callSuper = true)
@Data
@PacketId(0x00_58)
public class ClientHeartbeatPacket extends ClientPacket { public class ClientHeartbeatPacket extends ClientPacket {
public long qq;
public byte[] sessionKey;//登录后获得
@Override @Override
public void encode() throws IOException { public void encode() throws IOException {
this.writeRandom(2);
this.writeQQ(qq);
this.writeHex(Protocol.fixVer);
this.write(TEAEncryption.encrypt(new byte[]{0x00, 0x01, 0x00, 0x01}, sessionKey));
} }
} }
package net.mamoe.mirai.network.packet.client; package net.mamoe.mirai.network.packet.client;
import lombok.Data;
import lombok.EqualsAndHashCode;
import net.mamoe.mirai.network.Protocol;
import net.mamoe.mirai.network.packet.PacketId; import net.mamoe.mirai.network.packet.PacketId;
import net.mamoe.mirai.util.TEAEncryption;
import java.io.IOException;
/** /**
* @author Him188moe @ Mirai Project * @author Him188moe @ Mirai Project
*/ */
@PacketId(0x08_25_31_01)// TODO: 2019/8/8 @EqualsAndHashCode(callSuper = true)
@Data
@PacketId(0x08_25_31_01)
public class ClientLoginPacket extends ClientPacket { public class ClientLoginPacket extends ClientPacket {
public long qq;
@Override @Override
public void encode() { public void encode() throws IOException {
this.writeQQ(qq);
this.writeHex(Protocol.fixVer);
this.writeHex(Protocol._0825key);
//TEA 加密
var data = new ClientPacket() {
@Override
public void encode() throws IOException {
this.writeHex(Protocol._0825data0);
this.writeHex(Protocol._0825data2);
this.writeQQ(qq);
this.writeHex("00 00 00 00 03 09 00 08 00 01");
this.writeIp(Protocol.SERVER_IP.get(2));
this.writeHex("00 02 00 36 00 12 00 02 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 14 00 1D 01 02 00 19");
this.writeHex(Protocol.publicKey);
}
};
data.encode();
this.write(TEAEncryption.encrypt(data.toByteArray(), Protocol.hexToBytes(Protocol._0825key)));
} }
} }
package net.mamoe.mirai.network.packet.client; package net.mamoe.mirai.network.packet.client;
import lombok.Getter; import lombok.Getter;
import net.mamoe.mirai.network.Protocol;
import net.mamoe.mirai.network.packet.Packet; import net.mamoe.mirai.network.packet.Packet;
import net.mamoe.mirai.network.packet.PacketId; import net.mamoe.mirai.network.packet.PacketId;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays;
/** /**
* @author Him188moe @ Mirai Project * @author Him188moe @ Mirai Project
...@@ -25,60 +25,38 @@ public abstract class ClientPacket extends DataOutputStream implements Packet { ...@@ -25,60 +25,38 @@ public abstract class ClientPacket extends DataOutputStream implements Packet {
packageId = annotation.value(); packageId = annotation.value();
try { try {
writeHead(); this.writeHex(Protocol.head);
writeVersion(); this.writeHex(Protocol.ver);
writePacketId(); writePacketId();
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
public static void main(String[] args) throws IOException { protected void writeIp(String ip) throws IOException {
var pk = new ClientPacket() { for (String s : ip.split("\\.")) {
@Override this.writeInt(Integer.parseInt(s));
public void encode() throws IOException { }
writeHead();
}
};
pk.encode();
System.out.println(Arrays.toString(((ByteArrayOutputStream) pk.out).toByteArray()));
}
protected void writeHead() throws IOException {
this.writeByte(0x02);
}
protected void writeVersion() throws IOException {
this.writeByte(0x37_13);
} }
protected void writePacketId() throws IOException { protected void writePacketId() throws IOException {
this.writeByte(this.packageId); this.writeInt(this.packageId);
} }
protected void writeFixVer() throws IOException { protected void writeHex(String hex) throws IOException {
this.writeByte(0x03); for (String s : hex.split(" ")) {
this.writeByte(0x00); s = s.trim();
this.writeByte(0x00); if (s.isEmpty()) {
this.writeByte(0x00); continue;
this.writeByte(0x01); }
this.writeByte(0x2E); this.writeByte(Byte.valueOf(s, 16));
this.writeByte(0x01); }
this.writeByte(0x00);
this.writeByte(0x00);
this.writeByte(0x68);
this.writeByte(0x52);
this.writeByte(0x00);
this.writeByte(0x00);
this.writeByte(0x00);
this.writeByte(0x00);
} }
protected void write0825Key() throws IOException { protected void writeRandom(int length) throws IOException {
this.writeLong(0xA4_F1_91_88); for (int i = 0; i < length; i++) {
this.writeLong(0xC9_82_14_99); this.writeByte((byte) (int) (Math.random() * 255));
this.writeLong(0x0C_9E_56_55); }
this.writeLong(0x91_23_C8_3D);
} }
protected void writeQQ(long qq) throws IOException { protected void writeQQ(long qq) throws IOException {
...@@ -87,7 +65,13 @@ public abstract class ClientPacket extends DataOutputStream implements Packet { ...@@ -87,7 +65,13 @@ public abstract class ClientPacket extends DataOutputStream implements Packet {
/** /**
* Encode this packet * Encode this packet.
*/ * <p>
* Before sending the packet, an {@linkplain Protocol#tail tail} will be added.
*/// TODO: 2019/8/9 添加 tail
public abstract void encode() throws IOException; public abstract void encode() throws IOException;
public byte[] toByteArray() {
return ((ByteArrayOutputStream) this.out).toByteArray();
}
} }
package net.mamoe.mirai.util; package net.mamoe.mirai.util;
/** /**
* TEA encryption
*
* @author Him188moe @ Mirai Project * @author Him188moe @ Mirai Project
*/ */
public final class TEAEncryption { public final class TEAEncryption {
public static byte[] encrypt(byte[] source, byte[] key) {
return new _TEAEncryption().encrypt(source, key);
}
public byte[] decrypt(byte[] source, byte[] key) {
return new _TEAEncryption().decrypt(source, key);
}
public byte[] decrypt(byte[] source, int offset, int length, byte[] key) {
return new _TEAEncryption().decrypt(source, offset, length, key);
}
} }
package net.mamoe.mirai.util;
/**
* @author Him188moe @ Mirai Project
*/
public final class TeaEncryption {
public static native int Decrypt();
}
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