|
22 | 22 |
|
23 | 23 | import com.ayakacraft.authlibproxyforserver.AuthlibProxyForServer; |
24 | 24 | import com.ayakacraft.authlibproxyforserver.ProxyConfig; |
25 | | -import com.ayakacraft.authlibproxyforserver.utils.PreprocessPattern; |
| 25 | +import com.ayakacraft.authlibproxyforserver.mixin.YggdrasilAuthenticationServiceAccessor; |
| 26 | +import com.ayakacraft.authlibproxyforserver.utils.NetworkUtils; |
| 27 | +import com.ayakacraft.authlibproxyforserver.utils.preprocess.PreprocessPattern; |
26 | 28 | import com.mojang.brigadier.CommandDispatcher; |
27 | 29 | import com.mojang.brigadier.arguments.IntegerArgumentType; |
28 | 30 | import com.mojang.brigadier.arguments.StringArgumentType; |
29 | 31 | import com.mojang.brigadier.context.CommandContext; |
30 | 32 | import net.minecraft.server.command.ServerCommandSource; |
31 | 33 | import net.minecraft.text.Text; |
| 34 | +import net.minecraft.util.Formatting; |
| 35 | +import net.minecraft.util.Pair; |
32 | 36 |
|
33 | 37 | import java.io.IOException; |
| 38 | +import java.net.URI; |
| 39 | +import java.util.LinkedList; |
| 40 | +import java.util.List; |
34 | 41 |
|
35 | 42 | import static com.ayakacraft.authlibproxyforserver.AuthlibProxyForServer.LOGGER; |
36 | 43 | import static com.ayakacraft.authlibproxyforserver.AuthlibProxyForServer.config; |
|
40 | 47 |
|
41 | 48 | public final class AuthProxyCommand { |
42 | 49 |
|
| 50 | + private static final int TCPING_TIMES = 5; |
| 51 | + |
43 | 52 | private static int display(CommandContext<ServerCommandSource> context) { |
44 | 53 | sendFeedback( |
45 | 54 | context.getSource(), |
@@ -153,6 +162,27 @@ private static int type(CommandContext<ServerCommandSource> context, String type |
153 | 162 | return 1; |
154 | 163 | } |
155 | 164 |
|
| 165 | + private static int ping(CommandContext<ServerCommandSource> context) { |
| 166 | + List<String> hosts = new LinkedList<>(); |
| 167 | + //#if MC>=12006 |
| 168 | + com.mojang.authlib.Environment env = YggdrasilAuthenticationServiceAccessor.determineEnvironment(); |
| 169 | + hosts.add(env.sessionHost()); |
| 170 | + hosts.add(env.servicesHost()); |
| 171 | + //#elseif MC>=11600 |
| 172 | + //$$ com.mojang.authlib.Environment env = YggdrasilAuthenticationServiceAccessor.determineEnvironment(); |
| 173 | + //$$ hosts.add(env.getAuthHost()); |
| 174 | + //$$ hosts.add(env.getAccountsHost()); |
| 175 | + //$$ hosts.add(env.getSessionHost()); |
| 176 | + //$$ hosts.add(env.getServicesHost()); |
| 177 | + //#else |
| 178 | + //$$ hosts.add(com.ayakacraft.authlibproxyforserver.mixin.YggdrasilGameProfileRepositoryAccessor.getBaseUrl()); |
| 179 | + //$$ hosts.add(com.ayakacraft.authlibproxyforserver.mixin.YggdrasilMinecraftSessionServiceAccessor.getBaseUrl()); |
| 180 | + //$$ hosts.add(com.ayakacraft.authlibproxyforserver.mixin.YggdrasilUserAuthenticationAccessor.getBaseUrl()); |
| 181 | + //#endif |
| 182 | + new TcpingThread(hosts, context.getSource()).start(); |
| 183 | + return 1; |
| 184 | + } |
| 185 | + |
156 | 186 | /** |
157 | 187 | * @return true if failed |
158 | 188 | */ |
@@ -211,7 +241,76 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) { |
211 | 241 | .then(literal("http").executes(it -> AuthProxyCommand.type(it, "HTTP"))) |
212 | 242 | .then(literal("socks").executes(it -> AuthProxyCommand.type(it, "SOCKS"))) |
213 | 243 | ) |
| 244 | + .then(literal("ping").executes(AuthProxyCommand::ping)) |
214 | 245 | ); |
215 | 246 | } |
216 | 247 |
|
| 248 | + private static class TcpingThread extends Thread { |
| 249 | + |
| 250 | + private static Text packetLossStatusText(int packetsReceived) { |
| 251 | + double packetLoss = 1d - (double) packetsReceived / AuthProxyCommand.TCPING_TIMES; |
| 252 | + Formatting colour; |
| 253 | + if (packetLoss >= 0.8d) { |
| 254 | + colour = Formatting.RED; |
| 255 | + } else if (packetLoss >= 0.3d) { |
| 256 | + colour = Formatting.YELLOW; |
| 257 | + } else { |
| 258 | + colour = Formatting.GREEN; |
| 259 | + } |
| 260 | + return Text.literal(String.format("%d packets transmitted, %d packets received, ", TCPING_TIMES, packetsReceived)) |
| 261 | + .append(Text.literal(String.format("%.1f%%", packetLoss * 100)).formatted(colour)) |
| 262 | + .append(Text.literal(" packet loss")); |
| 263 | + } |
| 264 | + |
| 265 | + private static Text averagePingText(long ping) { |
| 266 | + Formatting colour; |
| 267 | + if (ping >= 800) { |
| 268 | + colour = Formatting.RED; |
| 269 | + } else if (ping >= 400) { |
| 270 | + colour = Formatting.YELLOW; |
| 271 | + } else { |
| 272 | + colour = Formatting.GREEN; |
| 273 | + } |
| 274 | + return Text.literal("Average ping: ") |
| 275 | + .append(Text.literal(ping + "ms").formatted(colour)); |
| 276 | + } |
| 277 | + |
| 278 | + private final List<String> hosts; |
| 279 | + |
| 280 | + private final ServerCommandSource source; |
| 281 | + |
| 282 | + public TcpingThread(List<String> hosts, ServerCommandSource source) { |
| 283 | + this.hosts = hosts; |
| 284 | + this.source = source; |
| 285 | + } |
| 286 | + |
| 287 | + private void tcping(String host) { |
| 288 | + Pair<Long, Integer> res = NetworkUtils.tcpingMultiple(URI.create(host), proxy, TCPING_TIMES); |
| 289 | + sendFeedback( |
| 290 | + source, |
| 291 | + Text.literal(String.format("Ping for '%s':", host)), |
| 292 | + false |
| 293 | + ); |
| 294 | + sendFeedback( |
| 295 | + source, |
| 296 | + packetLossStatusText(res.getRight()), |
| 297 | + false |
| 298 | + ); |
| 299 | + if (res.getRight() > 0) { |
| 300 | + sendFeedback( |
| 301 | + source, |
| 302 | + averagePingText(res.getLeft() / TCPING_TIMES), |
| 303 | + false |
| 304 | + ); |
| 305 | + } |
| 306 | + } |
| 307 | + |
| 308 | + public void run() { |
| 309 | + sendFeedback(source, Text.literal("Ping started"), false); |
| 310 | + hosts.forEach(this::tcping); |
| 311 | + sendFeedback(source, Text.literal("Ping finished"), false); |
| 312 | + } |
| 313 | + |
| 314 | + } |
| 315 | + |
217 | 316 | } |
0 commit comments