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() {
suspend fun doLogin() {
channel = PlatformSocket()
channel.connect("113.96.13.208", 8080)
launch(CoroutineName("Incoming Packet Receiver")) { processReceive() }
channel = PlatformSocket()
channel.connect("113.96.13.208", 8080)
launch(CoroutineName("Incoming Packet Receiver")) { processReceive() }
bot.logger.info("Trying login")
var response: LoginPacket.LoginPacketResponse = LoginPacket.SubCommand9(bot.client).sendAndExpect()
mainloop@ while (true) {
when (response) {
is LoginPacket.LoginPacketResponse.UnsafeLogin -> {
bot.configuration.loginSolver.onSolveUnsafeDeviceLoginVerify(bot, response.url)
response = LoginPacket.SubCommand9(bot.client).sendAndExpect()
}
bot.logger.info("Trying login")
var response: LoginPacket.LoginPacketResponse = LoginPacket.SubCommand9(bot.client).sendAndExpect()
mainloop@ while (true) {
when (response) {
is LoginPacket.LoginPacketResponse.UnsafeLogin -> {
bot.configuration.loginSolver.onSolveUnsafeDeviceLoginVerify(bot, response.url)
response = LoginPacket.SubCommand9(bot.client).sendAndExpect()
}
is LoginPacket.LoginPacketResponse.Captcha -> when (response) {
is LoginPacket.LoginPacketResponse.Captcha.Picture -> {
var result = response.data.withUse {
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 -> when (response) {
is LoginPacket.LoginPacketResponse.Captcha.Picture -> {
var result = response.data.withUse {
bot.configuration.loginSolver.onSolvePicCaptcha(bot, this)
}
is LoginPacket.LoginPacketResponse.Captcha.Slider -> {
var ticket = bot.configuration.loginSolver.onSolveSliderCaptcha(bot, response.url)
if (ticket == null) {
ticket = ""
}
response = LoginPacket.SubCommand2.SubmitSliderCaptcha(bot.client, ticket).sendAndExpect()
continue@mainloop
if (result == null || result.length != 4) {
//refresh captcha
result = "ABCD"
}
}
is LoginPacket.LoginPacketResponse.Error -> error(response.toString())
is LoginPacket.LoginPacketResponse.DeviceLockLogin -> {
response = LoginPacket.SubCommand20(
bot.client,
response.t402,
response.t403
).sendAndExpect()
response = LoginPacket.SubCommand2.SubmitPictureCaptcha(bot.client, response.sign, result).sendAndExpect()
continue@mainloop
}
is LoginPacket.LoginPacketResponse.Success -> {
bot.logger.info("Login successful")
break@mainloop
is LoginPacket.LoginPacketResponse.Captcha.Slider -> {
var ticket = bot.configuration.loginSolver.onSolveSliderCaptcha(bot, response.url)
if (ticket == null) {
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() {
//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())
}
println("d2key=${bot.client.wLoginSigInfo.d2Key.toUHexString()}")
StatSvc.Register(bot.client).sendAndExpect<StatSvc.Register.Response>()
}
doLogin()
doInit()
suspend fun init() {
//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
final override val account: BotAccount = account
@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 } }
init {
@ -98,16 +99,28 @@ abstract class BotImpl<N : BotNetworkHandler> constructor(
}
_network = createNetworkHandler(this.coroutineContext)
while (true){
loginLoop@ while (true) {
try {
return _network.login()
} catch (e: Exception){
_network.login()
break@loginLoop
} catch (e: Exception) {
e.logStacktrace()
_network.dispose(e)
}
logger.warning("Login failed. Retrying in 3s...")
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

View File

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