-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor StatSvc ip recording, fix #2349 #2351
base: dev
Are you sure you want to change the base?
Changes from all commits
03d3ad8
aa846a5
a89da69
40516d9
040bc16
68a4958
54cd19b
0fb8886
e831be6
6a07617
df762ce
fe8b1c9
e5707ed
b91e4e5
7a61f04
bb44c2f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ import kotlin.jvm.JvmName | |
*/ | ||
internal expect class PlatformSocket : Closeable, HighwayProtocolChannel { | ||
val isOpen: Boolean | ||
val connectedIp: Long | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 给特殊意义值留下注释 |
||
|
||
override fun close() | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,14 +30,15 @@ internal abstract class AbstractCommonNHTestWithSelector : | |
overrideComponents[BotOfflineEventMonitor] = BotOfflineEventMonitorImpl() | ||
} | ||
|
||
val conn = PlatformConn() | ||
val conn get() = PlatformConn(createAddress()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. by lazy? |
||
|
||
val selector = TestSelector<TestCommonNetworkHandler> { | ||
object : TestCommonNetworkHandler(bot, createContext(), createAddress()) { | ||
// override suspend fun createConnection(decodePipeline: PacketDecodePipeline): PlatformConn = channel | ||
override suspend fun createConnection(): PlatformConn { | ||
return conn | ||
} | ||
|
||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2019-2022 Mamoe Technologies and contributors. | ||
* | ||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. | ||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. | ||
* | ||
* https://github.com/mamoe/mirai/blob/dev/LICENSE | ||
*/ | ||
|
||
package net.mamoe.mirai.internal.network.framework.test | ||
|
||
import net.mamoe.mirai.internal.network.framework.PlatformConn | ||
import net.mamoe.mirai.internal.network.handler.createSocketAddress | ||
import net.mamoe.mirai.internal.test.runBlockingUnit | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertTrue | ||
|
||
class IpConversionTest { | ||
private fun String.toIpV4Long(): Long { | ||
PlatformConn(address = createSocketAddress(host = this, port = 80)).getConnectedIPPlatform().run { | ||
return if (this in 1..2) { | ||
-this | ||
} else { | ||
this | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun `test bad ipAddress`() = runBlockingUnit { | ||
assertEquals(-2, "some^ting%bad".toIpV4Long()) | ||
assertEquals(-2, "another_bad".toIpV4Long()) | ||
assertEquals(-2, " ".toIpV4Long()) | ||
assertEquals(-2, "w.a.c.d".toIpV4Long()) | ||
assertEquals(-2, "the..anotherbad......".toIpV4Long()) | ||
assertEquals(-2, "错误的IP地址".toIpV4Long()) | ||
} | ||
|
||
@Test | ||
fun `test good ipAddress`() = runBlockingUnit { | ||
assertTrue("www.baidu.com".toIpV4Long() > 0) | ||
assertTrue("www.qq.com".toIpV4Long() > 0) | ||
assertTrue("www.sohu.com".toIpV4Long() > 0) | ||
assertTrue("www.weibo.com".toIpV4Long() > 0) | ||
} | ||
|
||
@Test | ||
fun `test plain ipAddress`() = runBlockingUnit { | ||
assertEquals(16885952L, "192.168.1.1".toIpV4Long()) | ||
assertEquals(4294967295L, "255.255.255.255".toIpV4Long()) | ||
assertEquals(0L, "0.0.0.0".toIpV4Long()) | ||
assertEquals(1869573999L, "111.111.111.111".toIpV4Long()) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,8 +24,8 @@ import net.mamoe.mirai.internal.network.handler.CommonNetworkHandler | |
import net.mamoe.mirai.internal.network.handler.NetworkHandler.State | ||
import net.mamoe.mirai.internal.network.handler.NetworkHandlerContext | ||
import net.mamoe.mirai.internal.network.protocol.packet.OutgoingPacket | ||
import net.mamoe.mirai.utils.cast | ||
import net.mamoe.mirai.utils.debug | ||
import net.mamoe.mirai.utils.* | ||
import java.net.InetSocketAddress | ||
import java.net.SocketAddress | ||
import io.netty.channel.Channel as NettyChannel | ||
|
||
|
@@ -131,6 +131,16 @@ internal open class NettyNetworkHandler( | |
return contextResult.await() | ||
} | ||
|
||
override fun io.netty.channel.Channel.getConnectedIP(): Long = this.remoteAddress().let { address -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 为什么要有这个lambda? |
||
{ | ||
if (this.isActive && address is InetSocketAddress) { | ||
address.address?.address?.copyOf()?.also { it.reverse() }?.toInt()?.toLongUnsigned() ?: 2L | ||
} else { | ||
0L | ||
} | ||
} | ||
}.invoke() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 创建lambda立即invoke只会增加复杂度 |
||
|
||
@Suppress("EXTENSION_SHADOWED_BY_MEMBER") | ||
override fun io.netty.channel.Channel.close() { | ||
this.close() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,12 @@ package net.mamoe.mirai.internal.network.framework | |
|
||
import kotlinx.coroutines.ExecutorCoroutineDispatcher | ||
import net.mamoe.mirai.internal.network.handler.NetworkHandlerFactory | ||
import net.mamoe.mirai.internal.network.handler.SocketAddress | ||
import net.mamoe.mirai.internal.network.handler.getHost | ||
import net.mamoe.mirai.utils.toInt | ||
import net.mamoe.mirai.utils.toLongUnsigned | ||
import java.net.InetAddress | ||
import java.net.UnknownHostException | ||
import kotlin.test.AfterTest | ||
|
||
/** | ||
|
@@ -51,7 +57,16 @@ internal actual abstract class AbstractCommonNHTest actual constructor() : | |
} | ||
} | ||
|
||
actual val conn: PlatformConn = NettyNHTestChannel() | ||
actual val conn: PlatformConn get() = PlatformConn(address = createAddress()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. by lazy? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JVM那边的测试也是get() 而且这里是创建连接才会去get这个 get()这里应该没问题 |
||
} | ||
|
||
internal actual class PlatformConn actual constructor(actual val address: SocketAddress) : NettyNHTestChannel() { | ||
actual fun getConnectedIPPlatform(): Long { | ||
return (address.address ?: try { | ||
InetAddress.getByName(address.getHost()) | ||
} catch (e: UnknownHostException) { | ||
null | ||
})?.address?.copyOf()?.also { it.reverse() }?.toInt()?.toLongUnsigned() ?: 2L | ||
} | ||
} | ||
|
||
internal actual typealias PlatformConn = NettyNHTestChannel |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这部分应该在提供这个 ip 的地方处理