Improve login and init

This commit is contained in:
Him188 2020-01-30 23:53:28 +08:00
parent 288d0df738
commit fb3fe2bcca
3 changed files with 85 additions and 70 deletions

View File

@ -37,84 +37,79 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
override suspend fun login() { override suspend fun login() {
suspend fun doLogin() { channel = PlatformSocket()
channel = PlatformSocket() channel.connect("113.96.13.208", 8080)
channel.connect("113.96.13.208", 8080) launch(CoroutineName("Incoming Packet Receiver")) { processReceive() }
launch(CoroutineName("Incoming Packet Receiver")) { processReceive() }
bot.logger.info("Trying login") bot.logger.info("Trying login")
var response: LoginPacket.LoginPacketResponse = LoginPacket.SubCommand9(bot.client).sendAndExpect() var response: LoginPacket.LoginPacketResponse = LoginPacket.SubCommand9(bot.client).sendAndExpect()
mainloop@ while (true) { mainloop@ while (true) {
when (response) { when (response) {
is LoginPacket.LoginPacketResponse.UnsafeLogin -> { is LoginPacket.LoginPacketResponse.UnsafeLogin -> {
bot.configuration.loginSolver.onSolveUnsafeDeviceLoginVerify(bot, response.url) bot.configuration.loginSolver.onSolveUnsafeDeviceLoginVerify(bot, response.url)
response = LoginPacket.SubCommand9(bot.client).sendAndExpect() response = LoginPacket.SubCommand9(bot.client).sendAndExpect()
} }
is LoginPacket.LoginPacketResponse.Captcha -> when (response) { is LoginPacket.LoginPacketResponse.Captcha -> when (response) {
is LoginPacket.LoginPacketResponse.Captcha.Picture -> { is LoginPacket.LoginPacketResponse.Captcha.Picture -> {
var result = response.data.withUse { var result = response.data.withUse {
bot.configuration.loginSolver.onSolvePicCaptcha(bot, this) bot.configuration.loginSolver.onSolvePicCaptcha(bot, this)
}
if (result == null || result.length != 4) {
//refresh captcha
result = "ABCD"
}
response = LoginPacket.SubCommand2.SubmitPictureCaptcha(bot.client, response.sign, result).sendAndExpect()
continue@mainloop
} }
is LoginPacket.LoginPacketResponse.Captcha.Slider -> { if (result == null || result.length != 4) {
var ticket = bot.configuration.loginSolver.onSolveSliderCaptcha(bot, response.url) //refresh captcha
if (ticket == null) { result = "ABCD"
ticket = ""
}
response = LoginPacket.SubCommand2.SubmitSliderCaptcha(bot.client, ticket).sendAndExpect()
continue@mainloop
} }
} response = LoginPacket.SubCommand2.SubmitPictureCaptcha(bot.client, response.sign, result).sendAndExpect()
is LoginPacket.LoginPacketResponse.Error -> error(response.toString())
is LoginPacket.LoginPacketResponse.DeviceLockLogin -> {
response = LoginPacket.SubCommand20(
bot.client,
response.t402,
response.t403
).sendAndExpect()
continue@mainloop continue@mainloop
} }
is LoginPacket.LoginPacketResponse.Captcha.Slider -> {
is LoginPacket.LoginPacketResponse.Success -> { var ticket = bot.configuration.loginSolver.onSolveSliderCaptcha(bot, response.url)
bot.logger.info("Login successful") if (ticket == null) {
break@mainloop ticket = ""
}
response = LoginPacket.SubCommand2.SubmitSliderCaptcha(bot.client, ticket).sendAndExpect()
continue@mainloop
} }
} }
is LoginPacket.LoginPacketResponse.Error -> error(response.toString())
is LoginPacket.LoginPacketResponse.DeviceLockLogin -> {
response = LoginPacket.SubCommand20(
bot.client,
response.t402,
response.t403
).sendAndExpect()
continue@mainloop
}
is LoginPacket.LoginPacketResponse.Success -> {
bot.logger.info("Login successful")
break@mainloop
}
} }
println("d2key=${bot.client.wLoginSigInfo.d2Key.toUHexString()}")
StatSvc.Register(bot.client).sendAndExpect<StatSvc.Register.Response>()
} }
suspend fun doInit() { println("d2key=${bot.client.wLoginSigInfo.d2Key.toUHexString()}")
//start updating friend/group list StatSvc.Register(bot.client).sendAndExpect<StatSvc.Register.Response>()
bot.logger.info("Start updating friend/group list") }
/*
val data = FriendList.GetFriendGroupList(
bot.client,
0,
1,
0,
2
).sendAndExpect<FriendList.GetFriendGroupList.Response>()
*/
val data = FriendList.GetTroopList(
bot.client
).sendAndExpect<FriendList.GetFriendGroupList.Response>()
println(data.contentToString())
}
doLogin() suspend fun init() {
doInit() //start updating friend/group list
bot.logger.info("Start updating friend/group list")
/*
val data = FriendList.GetFriendGroupList(
bot.client,
0,
1,
0,
2
).sendAndExpect<FriendList.GetFriendGroupList.Response>()
*/
val data = FriendList.GetTroopList(
bot.client
).sendAndExpect<FriendList.GetFriendGroupList.Response>()
println(data.contentToString())
} }

View File

@ -27,7 +27,8 @@ abstract class BotImpl<N : BotNetworkHandler> constructor(
@Suppress("CanBePrimaryConstructorProperty") // for logger @Suppress("CanBePrimaryConstructorProperty") // for logger
final override val account: BotAccount = account final override val account: BotAccount = account
@UseExperimental(RawAccountIdUse::class) @UseExperimental(RawAccountIdUse::class)
override val uin: Long get() = account.id override val uin: Long
get() = account.id
final override val logger: MiraiLogger by lazy { configuration.logger ?: DefaultLogger("Bot($uin)").also { configuration.logger = it } } final override val logger: MiraiLogger by lazy { configuration.logger ?: DefaultLogger("Bot($uin)").also { configuration.logger = it } }
init { init {
@ -98,16 +99,28 @@ abstract class BotImpl<N : BotNetworkHandler> constructor(
} }
_network = createNetworkHandler(this.coroutineContext) _network = createNetworkHandler(this.coroutineContext)
while (true){ loginLoop@ while (true) {
try { try {
return _network.login() _network.login()
} catch (e: Exception){ break@loginLoop
} catch (e: Exception) {
e.logStacktrace() e.logStacktrace()
_network.dispose(e) _network.dispose(e)
} }
logger.warning("Login failed. Retrying in 3s...") logger.warning("Login failed. Retrying in 3s...")
delay(3000) delay(3000)
} }
while (true) {
try {
return _network.init()
} catch (e: Exception) {
e.logStacktrace()
_network.dispose(e)
}
logger.warning("Init failed. Retrying in 3s...")
delay(3000)
}
} }
protected abstract fun createNetworkHandler(coroutineContext: CoroutineContext): N protected abstract fun createNetworkHandler(coroutineContext: CoroutineContext): N

View File

@ -47,6 +47,13 @@ abstract class BotNetworkHandler : CoroutineScope {
@MiraiInternalAPI @MiraiInternalAPI
abstract suspend fun login() abstract suspend fun login()
/**
* 初始化获取好友列表等值.
*/
@MiraiInternalAPI
open suspend fun init() {
}
/** /**
* 等待直到与服务器断开连接. 若未连接则立即返回 * 等待直到与服务器断开连接. 若未连接则立即返回
*/ */