This commit is contained in:
liujiahua123123 2019-09-07 14:13:53 +08:00
commit 7682f2100c
13 changed files with 195 additions and 140 deletions

View File

@ -183,9 +183,9 @@ public class MiraiServer {
//robot.network.tryLogin$mirai_core().whenComplete((state, e) -> {
if (state == LoginState.SUCCEED) {
Robot.instances.add(robot);
getLogger().info(" Succeed");
getLogger().success(" Login Succeed");
} else {
getLogger().error(" Failed with error " + state);
getLogger().error(" Login Failed with error " + state);
robot.close();
}
// }).get();

View File

@ -22,6 +22,7 @@ class Group(robot: Robot, number: Long) : Contact(robot, number), Closeable {
}
companion object {
@JvmStatic
fun groupNumberToId(number: Long): Long {
val left: Long = number.toString().let {
if (it.length < 6) {
@ -59,6 +60,7 @@ class Group(robot: Robot, number: Long) : Contact(robot, number), Closeable {
}
}
@JvmStatic
fun groupIdToNumber(id: Long): Long {
var left: Long = id.toString().let {
if (it.length < 6) {

View File

@ -1,7 +1,9 @@
@file:JvmMultifileClass
@file:JvmName("RobotNetworkHandler")
package net.mamoe.mirai.network
import net.mamoe.mirai.MiraiServer
import net.mamoe.mirai.Robot
import net.mamoe.mirai.contact.Group
import net.mamoe.mirai.contact.QQ
@ -27,7 +29,7 @@ import java.util.*
import java.util.concurrent.CompletableFuture
import java.util.concurrent.ScheduledFuture
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException
import javax.imageio.ImageIO
import kotlin.reflect.KClass
@ -83,40 +85,32 @@ class RobotNetworkHandler(private val robot: Robot) : Closeable {
//private | internal
internal fun tryLogin(): CompletableFuture<LoginState> = this.tryLogin(500, TimeUnit.MILLISECONDS)
internal fun tryLogin(): CompletableFuture<LoginState> = this.tryLogin(200)
/**
* 仅当 [LoginState] [LoginState.UNKNOWN] 且非 [LoginState.TIMEOUT] 才会调用 [loginHook].
* 如果要输入验证码, 那么会以参数 [LoginState.VERIFICATION_CODE] 调用 [loginHandler], 登录完成后再以 [LoginState.SUCCEED] 调用 [loginHandler]
*
* @param connectingTimeout 连接每个服务器的 timeout
* @param touchingTimeoutMillis 连接每个服务器的 timeout
*/
internal fun tryLogin(connectingTimeout: Long, unit: TimeUnit = TimeUnit.MILLISECONDS): CompletableFuture<LoginState> {
internal fun tryLogin(touchingTimeoutMillis: Long): CompletableFuture<LoginState> {
val ipQueue: LinkedList<String> = LinkedList(Protocol.SERVER_IP)
val future = CompletableFuture<LoginState>()
fun login() {
val ip = ipQueue.poll()
if (ip != null) {
// val future = this@RobotNetworkHandler.socketHandler.touch(ip)
this@RobotNetworkHandler.socketHandler.touch(ip).runCatching {
this@runCatching.get(connectingTimeout, unit).let { state ->
if (state == LoginState.UNKNOWN) {
login()
} else {
future.complete(state)
}
}
}.onFailure {
when (it) {
is TimeoutException -> login()
else -> throw it
}
}
} else {
if (ip == null) {
future.complete(LoginState.UNKNOWN)//所有服务器均返回 UNKNOWN
return
}
this@RobotNetworkHandler.socketHandler.touch(ip, touchingTimeoutMillis).get().let { state ->
if (state == LoginState.UNKNOWN || state == LoginState.TIMEOUT) {
login()
} else {
future.complete(state)
}
}
}
login()
@ -181,12 +175,15 @@ class RobotNetworkHandler(private val robot: Robot) : Closeable {
/**
* Start network and touch the server
*/
internal fun touch(serverAddress: String): CompletableFuture<LoginState> {
internal fun touch(serverAddress: String, timeoutMillis: Long): CompletableFuture<LoginState> {
MiraiLogger.info("Connecting server: $serverAddress")
this.loginFuture = CompletableFuture()
socketHandler.serverIP = serverAddress
sendPacket(ClientTouchPacket(robot.account.qqNumber, socketHandler.serverIP))
waitForPacket(ServerTouchResponsePacket::class, timeoutMillis) {
loginFuture!!.complete(LoginState.TIMEOUT)
}
return this.loginFuture!!
}
@ -196,7 +193,10 @@ class RobotNetworkHandler(private val robot: Robot) : Closeable {
*/
@ExperimentalUnsignedTypes
internal fun sendPacket(packet: ClientPacket) {
checkNotNull(socket) { "socket closed" }
checkNotNull(socket) { "network closed" }
if (socket!!.isClosed) {
return
}
try {
packet.encodePacket()
@ -296,9 +296,8 @@ class RobotNetworkHandler(private val robot: Robot) : Closeable {
*/
private lateinit var sessionResponseDecryptionKey: ByteArray
private var verificationCodeSequence: Int = 0//这两个验证码使用
private var verificationCodeSequence: Int = 0
private var verificationCodeCache: ByteArray? = null//每次包只发一部分验证码来
private var verificationCodeCacheCount: Int = 1//
private lateinit var verificationToken: ByteArray
@ -337,18 +336,23 @@ class RobotNetworkHandler(private val robot: Robot) : Closeable {
}
}
is ServerVerificationCodeRepeatPacket -> {//todo 这个名字正确么
this.tgtgtKey = packet.tgtgtKeyUpdate
is ServerVerificationCodeCorrectPacket -> {
this.tgtgtKey = getRandomByteArray(16)
this.token00BA = packet.token00BA
sendPacket(ClientLoginResendPacket3105(robot.account.qqNumber, robot.account.password, this.loginTime, this.loginIP, this.tgtgtKey!!, this.token0825, this.token00BA))
}
is ServerVerificationCodeTransmissionPacket -> {
if (packet is ServerVerificationCodeWrongPacket) {
this.verificationCodeSequence = 0
this.verificationCodeCache = byteArrayOf()
}
this.verificationCodeSequence++
this.verificationCodeCache = this.verificationCodeCache!! + packet.verificationCodePartN
this.verificationToken = packet.verificationToken
this.verificationCodeCacheCount++
this.token00BA = packet.token00BA
@ -356,10 +360,11 @@ class RobotNetworkHandler(private val robot: Robot) : Closeable {
//todo 看易语言 count 和 sequence 是怎样变化的
if (packet.transmissionCompleted) {
this.verificationCodeCache
(MiraiServer.getInstance().parentFolder + "VerificationCode.png").writeBytes(this.verificationCodeCache!!)
println(CharImageUtil.createCharImg(ImageIO.read(this.verificationCodeCache!!.inputStream())))
TODO("验证码好了")
} else {
sendPacket(ClientVerificationCodeTransmissionRequestPacket(this.verificationCodeCacheCount, robot.account.qqNumber, this.token0825, this.verificationCodeSequence, this.token00BA))
sendPacket(ClientVerificationCodeTransmissionRequestPacket(packet.count + 1, robot.account.qqNumber, this.token0825, this.verificationCodeSequence, this.token00BA))
}
}

View File

@ -73,7 +73,7 @@ abstract class ServerPacket(val input: DataInputStream) : Packet {
"00 58" -> ServerHeartbeatResponsePacket(stream)
"00 BA" -> ServerVerificationCodePacket.Encrypted(stream)
"00 BA" -> ServerVerificationCodePacket.Encrypted(stream, idHex)
"00 CE", "00 17" -> ServerEventPacket.Raw.Encrypted(stream, idHex.hexToBytes())

View File

@ -40,12 +40,20 @@ class ClientVerificationCodeTransmissionRequestPacket(
}
}
/**
* 验证码输入错误
*/
class ServerVerificationCodeWrongPacket(input: DataInputStream, dataSize: Int, packetId: ByteArray) : ServerVerificationCodeTransmissionPacket(input, dataSize, packetId) {
}
/**
* 服务器发送验证码图片文件一部分过来
*
* @author Him188moe
*/
class ServerVerificationCodeTransmissionPacket(input: DataInputStream, private val dataSize: Int, private val packetId: ByteArray) : ServerVerificationCodePacket(input) {
@PacketId("00 BA 31")
open class ServerVerificationCodeTransmissionPacket(input: DataInputStream, private val dataSize: Int, private val packetId: ByteArray) : ServerVerificationCodePacket(input) {
lateinit var verificationCodePartN: ByteArray
lateinit var verificationToken: ByteArray//56bytes
@ -58,46 +66,62 @@ class ServerVerificationCodeTransmissionPacket(input: DataInputStream, private v
this.verificationToken = this.input.readNBytesAt(10, 56)
val length = this.input.readShortAt(66)
this.input.skip(2)
this.verificationCodePartN = this.input.readNBytes(length)
this.input.skip(2)
this.transmissionCompleted = this.input.readBoolean().not()
this.input.skip(1)
//val byte = this.input.readByte().toInt()
val byte = this.input.readByteAt(70 + length).toInt()
MiraiLogger.debug("transmissionCompleted=$byte")
MiraiLogger.debug("verificationCodePartN=" + this.verificationCodePartN.toUHexString())
this.transmissionCompleted = byte == 0
this.token00BA = this.input.readNBytesAt(dataSize - 57, 40)
this.count = byteArrayOf(0, 0, packetId[2], packetId[3]).toUHexString().hexToInt()
this.token00BA = this.input.readNBytesAt(dataSize - 56, 40)
this.count = packetId[3].toInt()
}
}
fun main() {
val data = "FC 40 C0 57 0F 15 A4 1F 09 32 39 C9 52 05 44 D5 BA C4 78 B8 70 D7 C0 74 91 A4 7E 44 A5 A7 FD D2 E3 A7 10 3E E4 73 D8 13 E2 A2 0B A4 38 9F AB D3 4A D1 01 0E AB 37 11 84 52 08 DC 85 53 7E 75 08 D1 BA 2A 05 76 0F 84 7C A0 70 25 A4 4E E6 C1 9A C9 71 E7 10 48 F0 9D AA 27 87 3C 99 38 5A AE AE C1 58 17 FC A4 C6 9E 25 68 C0 F7 20 04 CA 98 91 1D 88 83 A7 74 D0 05 DD E9 28 57 46 CA 93 A1 F4 C0 83 4E 18 CE 57 0C 4F 1F 96 20 8F 62 4D E5 90 D2 6A AA E5 45 8B A1 B1 97 32 B5 38 97 9D 43 E9 28 65 5D B4 09 73 44 52 DE 2B C3 5B 18 F1 4A 0C 36 CC DE 31 B2 24 19 C2 19 A4 30 A2 8C 87 B2 12 E2 78 9A 52 9C 40 7F 47 0A 40 90 84 69 84 84 86 8B F8 FE 30 8E C3 30 C0 7D 3F 73 38 89 D4 6F 56 91 9B 04 7D 94 25 5E C4 8D EB E2 18 02 CC 8D 98 07 28 0E CE 05 4E 11 25 B9 27 2C E9 3E 49 71 76 E7 BC C2 02 8D D3 85 49 66 BA F0 87 31 C2 93 0D 88 F8 39 04 37 2F 2C 63 F2 55 96 8F 32 D1 CE 51 F0 D4 0A 0C F0 23 3B 63 06 28 80 41 E9 9E E1 CC AE 00 9E 20 6F CB 3C B3 50 D7 02 CC 5A F0 D1 97 C8 DC 3D F8 1B C6 6D A3 1B C3 B6 55 7A B2 44 D5 47 A7 F0 96 46 4C 3B AC 9C 2E E6 58 D1 FF 48 5C A2 30 35 B2 97 89 62 19 42 6A 81 60 C4 DC B6 6D 03 47 75 AD 26 B0 30 67 57 C6 C3 05 3F FB 3A B6 51 C1 4C 24 AC FC AC 94 C7 A7 B8 82 BC E0 64 4C A5 E9 8F 86 85 CA B0 52 F5 13 33 55 D9 18 DA 70 C3 FE 78 D7 68 8D 96 0D A3 76 0F 70 61 46 94 86 61 B4 9F EA 72 0A 72 96 66 F9 B0 DE 32 A2 80 66 8C 6A 5C 4D 13 25 06 94 80 52 A9 00 29 95 05 B0 FB A6 32 60 41 1D 06 9A 1A 36 B8 C0 4C CD BE 82 7D F5 8C 83 6B 2E F0 C4 38 40 33 45 7F B4 AF 57 8E 90 B4 B0 0F 7D A0 F2 A1 DA 6A 5E 2A 14 A7 35 07 0B CB 17 3A 43 A2 71 CE 77 A4 0B A8 6E 50 6A 46 A5 39 40 14 C0 11 BA F0 D7 EF 0A E0 F6 BB 40 3E 89 D1 2A 0E D8 86 22 C8 C0 52 A1 72 40 7A 08 A9 B8 42 39 27 8A 66 5E 2C F3 CB D0 1E 3E CC 42 82 C2 39 A6 E3 EE 02 A8 40 6D E8 98 C0 23 50 5A 1F B3 FE 68 59 84 E4 26 AD A0 64 B2 56 D4 08 56 0A BC AF 15 DD 67 51 CA 20 D5 0F C2 BD 22 E9 BB 0B A3 CB B8 00 98 66 26 C0 6E 73 18 67 F2 78 27 E7 38 F8 F4 51 9E 5B 15 BE E8 13 F3 CC D9 80 B6 E2 D7 F2 DE 91 55 05 0C 58 93 2D 50 56 34 C5 14 4F 7F B8 80 F6 D5 0A 2B 4F 0C 67 20 66 4D 57 17 96 4B CB 25 29 FD 00 42 B6 BA 0F DF".hexToBytes()
ServerVerificationCodeTransmissionPacket(data.dataInputStream(), data.size, "00 BA 31 01".hexToBytes()).let {
it.decode()
println(it)
}
}
/**
* 暂不了解意义
* 验证码正确
*
* @author Him188moe
*/
class ServerVerificationCodeRepeatPacket(input: DataInputStream) : ServerVerificationCodePacket(input) {
class ServerVerificationCodeCorrectPacket(input: DataInputStream) : ServerVerificationCodePacket(input) {
lateinit var token00BA: ByteArray//56 bytes
lateinit var tgtgtKeyUpdate: ByteArray
@ExperimentalUnsignedTypes
override fun decode() {
token00BA = this.input.readNBytesAt(10, 56)
tgtgtKeyUpdate = getRandomByteArray(16)
}
}
abstract class ServerVerificationCodePacket(input: DataInputStream) : ServerPacket(input) {
@PacketId("00 BA")
class Encrypted(input: DataInputStream) : ServerPacket(input) {
class Encrypted(input: DataInputStream, val idHex: String) : ServerPacket(input) {
@ExperimentalUnsignedTypes
fun decrypt(): ServerVerificationCodePacket {
this.input goto 14
val data = TEA.decrypt(this.input.readAllBytes().let { it.copyOfRange(0, it.size - 1) }, Protocol.key00BA.hexToBytes())
return if (data.size == 95) {
ServerVerificationCodeRepeatPacket(data.dataInputStream())
} else {
ServerVerificationCodeTransmissionPacket(data.dataInputStream(), data.size, this.input.readNBytesAt(3, 4))
val data = TEA.decrypt(this.input.readAllBytes().cutTail(1), Protocol.key00BA.hexToBytes())
if (idHex.startsWith("00 BA 32")) {
if (data.size == 95) {
ServerVerificationCodeCorrectPacket(data.dataInputStream())
} else {
return ServerVerificationCodeWrongPacket(data.dataInputStream(), data.size, this.input.readNBytesAt(3, 4))
}
}
return ServerVerificationCodeTransmissionPacket(data.dataInputStream(), data.size, this.input.readNBytesAt(3, 4))
}
}
}

View File

@ -43,4 +43,9 @@ enum class LoginState {
* 未知. 更换服务器或等几分钟再登录可能解决.
*/
UNKNOWN,
/**
* 超时
*/
TIMEOUT,
}

View File

@ -0,0 +1,55 @@
package net.mamoe.mirai.utils;
import java.awt.*;
import java.awt.image.BufferedImage;
/**
* 图片转字符图片, 来自 CSDN 开源
*
* @author zhoujie https://blog.csdn.net/qq_37902949/article/details/81228566
*/
public final class CharImageUtil {
public static String createCharImg(BufferedImage image) {
return createCharImg(image, 100, 20);
}
public static String createCharImg(BufferedImage image, int sizeWeight, int sizeHeight) {
//生成字符图片
image = resize(image, sizeWeight, sizeHeight);
int width = image.getWidth();
int height = image.getHeight();
StringBuilder output = new StringBuilder();
for (int i = 0; i < height; i++) {
StringBuilder line = new StringBuilder();
for (int j = 0; j < width; j++) {
int rgb = image.getRGB(j, i);
int R = (rgb & 0xff0000) >> 16;
int G = (rgb & 0x00ff00) >> 8;
int B = rgb & 0x0000ff;
int gray = (R * 30 + G * 59 + B * 11 + 50) / 100;
int index = 31 * gray / 255;
line.append(asc[index]); //添加每个字符
}
output.append(line).append("\n");
}
return output.toString();
}
public static BufferedImage resize(BufferedImage img, int newW, int newH) {
Image tmp = img.getScaledInstance(newW, newH, Image.SCALE_SMOOTH);
BufferedImage dimg = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = dimg.createGraphics();
g2d.drawImage(tmp, 0, 0, null);
g2d.dispose();
return dimg;
}
private final static char[] asc = {' ', '`', '.', '^', ',', ':', '~', '"',
'<', '!', 'c', 't', '+', '{', 'i', '7', '?', 'u', '3', '0', 'p', 'w',
'4', 'A', '8', 'D', 'X', '%', '#', 'H', 'W', 'M'};
}

View File

@ -16,6 +16,8 @@ object MiraiLogger {
infix fun notice(o: Any?) = this.print(o.toString(), LoggerTextFormat.LIGHT_BLUE)
infix fun success(o: Any?) = this.print(o.toString(), LoggerTextFormat.GREEN)
infix fun debug(o: Any?) = this.print(o.toString(), LoggerTextFormat.YELLOW)
infix fun catching(e: Throwable) {

View File

@ -35,7 +35,7 @@ public class MiraiSettings {
}
this.file = file;
try {
if(file.exists()){
if (!file.exists()) {
if (!file.createNewFile()) {
throw new RuntimeException("cannot create config file " + file);
}

View File

@ -6,99 +6,61 @@ import net.mamoe.mirai.utils.RobotAccount
import java.util.*
/**
* 筛选掉无法登录(冻结/设备锁/UNKNOWN) qq
*
* @author Him188moe
*/
val qqList = "2258868346----123456789.\n" +
"1545483785----yuk7k1dxnf3jn5\n" +
"2948786488----123123123\n" +
"3059674084----qq123456\n" +
"1918079979----123456789.\n" +
"3050478794----18872590321\n" +
"3331537204----123456789.\n" +
"2128659972----123456789.\n" +
"3435376516----abc123456\n" +
"2980527804----a123456\n" +
"2752195782----qq123456789\n" +
"3130257966----13415986622\n" +
"1802730396----123456789\n" +
"3021732783----15866103923\n" +
"306499606----abc123456\n" +
"2893904328----abc123456\n" +
"1765904806----123456789\n" +
"3254202261----15223045268\n" +
"2947707697----abc123456\n" +
"3500959200----123456789.\n" +
"2169513531----123456789.\n" +
"2983688661----a123456\n" +
"1246882194----pz49779866\n" +
"2315275635----147258369\n" +
"2802294904----123456789\n" +
"2955364492----1234567890\n" +
"1753325115----123456789\n" +
"2642725191----qq123456\n" +
"2152972686----123456789.\n" +
"2845953617----123456789.\n" +
"3329641753----123456789.\n" +
"1458302685----123456789a\n" +
"2351156352----987654321\n" +
"2304786984----fkhwt53787\n" +
"3322756212----123456789.\n" +
"3187253283----123456789.\n" +
"3168715730----147258369\n" +
"2189916732----18831892323\n" +
"2965337631----123456789.\n" +
"1901802165----123456789.\n" +
"414015319----abc123456\n" +
"3400636089----123456789a\n" +
"3530336304----seoua80060\n" +
"3147312971----123456789.\n" +
"3011083526----yp70y9\n" +
"286888078----abc123456\n" +
"3126754112----1234567890\n" +
"2924643025----123123123\n" +
"341870356----ncvhZtQD\n" +
"3358177328----123456789a\n" +
"1396419201----eakuj14475\n" +
"3541159580----123456789.\n" +
"2540245592----1234567890\n" +
"2024802855----123456789.\n" +
"2578309660----1234567890\n" +
"1934965091----123456789.\n" +
"3449408956----a123456789\n" +
"2509348670----123456789.\n" +
"2305961679----123456789.\n" +
"3532858521----123456789.\n" +
"3308276898----123456789a\n" +
"1760897490----123456789\n" +
"2920800012----123123123\n" +
"2923942248----123123123\n" +
"3216600579----13882755274\n" +
"3100259299----qq123456\n" +
"3242723735----1234567890\n" +
"2142733062----123456789.\n" +
"1557689693----123456789\n" +
"3505693439----sb2662vqy6q\n" +
"3231125974----123456789.\n" +
"3433048975----13893690883\n" +
"3168017129----18780999209\n" +
"2922045831----123123123\n" +
"3578152022----a123456789\n" +
"2116254935----147258369\n" +
"3158479284----1234567890\n" +
"3149394424----qq123456789\n" +
"2829521712----123456789.\n" +
"3218671461----123456789.\n" +
"3035873094----123456789a\n" +
"2224518667----147258369\n" +
"3175801590----123456789.\n" +
"3203228181----123456789a\n" +
"3213497536----123456789a\n" +
"3377317115----123456789\n" +
"2672537341----qq123456789\n" +
"2945957617----123123123\n" +
"2763390197----123456789.\n" +
"3322711709----123456789."
val qqList = "2535777366----abc123456\n" +
"2535815148----abc123456\n" +
"2535704896----abc123456\n" +
"2535744882----abc123456\n" +
"2535656918----abc123456\n" +
"2535679286----abc123456\n" +
"2535606374----abc123456\n" +
"2535647743----abc123456\n" +
"2535543049----abc123456\n" +
"2535583893----abc123456\n" +
"2535508338----abc123456\n" +
"2535524178----abc123456\n" +
"2535363077----abc123456\n" +
"2535469090----abc123456\n" +
"2535263758----abc123456\n" +
"2535258328----abc123456\n" +
"2535175332----abc123456\n" +
"2535175855----abc123456\n" +
"2535126490----abc123456\n" +
"2535169081----abc123456\n" +
"2535054551----abc123456\n" +
"2535085068----abc123456\n" +
"2535041182----abc123456\n" +
"2535055583----abc123456\n" +
"2534883752----abc123456\n" +
"2534909231----abc123456\n" +
"2534715278----abc123456\n" +
"2534766467----abc123456\n" +
"2534696956----abc123456\n" +
"2534703892----abc123456\n" +
"2534597961----abc123456\n" +
"2534687923----abc123456\n" +
"2534573690----abc123456\n" +
"2534596747----abc123456\n" +
"2534467863----abc123456\n" +
"2534480141----abc123456\n" +
"2534377951----abc123456\n" +
"2534418547----abc123456\n" +
"2534315990----abc123456\n" +
"2534318348----abc123456\n" +
"2534220616----abc123456\n" +
"2534288430----abc123456\n" +
"2534205633----abc123456\n" +
"2534226589----abc123456\n" +
"2534182470----abc123456\n" +
"2534194558----abc123456\n" +
"2534106061----abc123456\n" +
"2534108283----abc123456\n" +
"2534026460----abc123456\n" +
"2534037598----abc123456\n"
fun main() {