mirror of
https://code.chipmunk.land/Parker2991/NitoriBot/
synced 2026-12-05 08:35:39 -05:00
refactor: does this count as 9.0.3
* refactor: get proxies better
This commit is contained in:
@@ -24,6 +24,7 @@ import org.geysermc.mcprotocollib.protocol.MinecraftProtocol;
|
||||
import org.geysermc.mcprotocollib.protocol.data.game.PlayerListEntry;
|
||||
import org.geysermc.mcprotocollib.protocol.data.game.PlayerListEntryAction;
|
||||
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.ClientboundPlayerInfoUpdatePacket;
|
||||
import org.jspecify.annotations.NonNull;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.EnumSet;
|
||||
@@ -38,7 +39,7 @@ public sealed class NitoriBot extends SessionAdapter implements MCProtocolLibMes
|
||||
public final ListenerManager listenerManager = new ListenerManager();
|
||||
public final ScheduledExecutorService scheduler = Main.scheduler;
|
||||
public final BotBuildInfo botBuildInfo = Main.botBuildInfo;
|
||||
public final RepoCommitInfo repoCommitInfo = Main.repoCommitInfo;
|
||||
public final RepoCommitInfo repoCommitInfo = Main.repoCommitInfo; // TODO
|
||||
public Config config;
|
||||
public Config.Options options;
|
||||
public List<NitoriBot> bots;
|
||||
@@ -133,7 +134,7 @@ public sealed class NitoriBot extends SessionAdapter implements MCProtocolLibMes
|
||||
options.host,
|
||||
options.port
|
||||
)
|
||||
.setProxy(new GetProxiesList().randomProxyIp())
|
||||
.setProxy(GetProxiesList.randomProxyIp())
|
||||
.setProtocol(protocol)
|
||||
.create();
|
||||
|
||||
@@ -156,7 +157,7 @@ public sealed class NitoriBot extends SessionAdapter implements MCProtocolLibMes
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRegistrationAvailable (MCProtocolLibMessenger messenger) {
|
||||
public void onRegistrationAvailable (@NonNull MCProtocolLibMessenger messenger) {
|
||||
// stub
|
||||
}
|
||||
|
||||
|
||||
+32
-34
@@ -5,42 +5,40 @@ import org.geysermc.mcprotocollib.network.ProxyInfo;
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.Iterator;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class GetProxiesList {
|
||||
public ProxyInfo randomProxyIp () throws IOException {
|
||||
Path proxiesPath = Paths.get("proxies.txt");
|
||||
public final class GetProxiesList {
|
||||
public static ProxyInfo randomProxyIp () throws IOException {
|
||||
String chosen = null;
|
||||
int valid = 0;
|
||||
try (Stream<String> lines = Files.lines(Paths.get("proxies.txt"))) {
|
||||
for (Iterator<String> it = lines.iterator(); it.hasNext(); ) {
|
||||
String line = it.next().trim();
|
||||
if (line.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
valid++;
|
||||
if (ThreadLocalRandom.current().nextInt(valid) == 0) {
|
||||
chosen = line;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (chosen == null) {
|
||||
throw new IOException("no proxies?");
|
||||
}
|
||||
return new ProxyInfo(ProxyInfo.Type.SOCKS5, toAddress(chosen));
|
||||
}
|
||||
|
||||
int countLines = Math.round(Files
|
||||
.lines(proxiesPath)
|
||||
.count());
|
||||
|
||||
Random random = new Random();
|
||||
|
||||
int randomIndex = random.nextInt(countLines);
|
||||
|
||||
List<String> lines = Files
|
||||
.lines(proxiesPath)
|
||||
.toList();
|
||||
|
||||
String[] ip = lines
|
||||
.stream()
|
||||
.skip(randomIndex)
|
||||
.findAny()
|
||||
.get()
|
||||
.split(":");
|
||||
|
||||
InetSocketAddress address = new InetSocketAddress(
|
||||
ip[0],
|
||||
Integer.parseInt(ip[1])
|
||||
);
|
||||
|
||||
return new ProxyInfo(
|
||||
ProxyInfo.Type.SOCKS5,
|
||||
address
|
||||
);
|
||||
private static InetSocketAddress toAddress (String addrPort) throws IllegalArgumentException {
|
||||
int colon = addrPort.lastIndexOf(':');
|
||||
if (colon == -1) {
|
||||
throw new IllegalArgumentException("invalid addr " + addrPort);
|
||||
}
|
||||
String host = addrPort.substring(0, colon);
|
||||
int port = Integer.parseInt(addrPort.substring(colon + 1));
|
||||
return new InetSocketAddress(host, port);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user