Skip to content

Commit 9c2a32f

Browse files
committed
implement support to proxy a minecraft session
1 parent 44ce00c commit 9c2a32f

25 files changed

+1049
-329
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<groupId>systems.kinau</groupId>
1313
<artifactId>FishingBot</artifactId>
14-
<version>2.7.18</version>
14+
<version>2.8.0-experimental</version>
1515
<name>FishingBot</name>
1616
<description>An AFK fishing bot for Minecraft</description>
1717

src/main/java/systems/kinau/fishingbot/Bot.java

Lines changed: 135 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import systems.kinau.fishingbot.network.ping.ServerPinger;
3131
import systems.kinau.fishingbot.network.protocol.NetworkHandler;
3232
import systems.kinau.fishingbot.network.protocol.ProtocolConstants;
33+
import systems.kinau.fishingbot.network.proxy.ServerProxyHandler;
3334
import systems.kinau.fishingbot.network.realms.Realm;
3435
import systems.kinau.fishingbot.network.realms.RealmsAPI;
3536

@@ -53,6 +54,7 @@ public class Bot {
5354
@Getter @Setter private int serverPort;
5455
@Getter @Setter private AuthData authData;
5556
@Getter @Setter private boolean wontConnect = false;
57+
@Getter private final boolean proxyMode;
5658

5759
@Getter private EventManager eventManager;
5860
@Getter private CommandRegistry commandRegistry;
@@ -68,8 +70,9 @@ public class Bot {
6870
@Getter private File logsFolder = new File("logs");
6971
@Getter private File accountFile = new File("account.json");
7072

71-
public Bot(CommandLine cmdLine) {
73+
public Bot(CommandLine cmdLine, boolean proxyMode) {
7274
FishingBot.getInstance().setCurrentBot(this);
75+
this.proxyMode = proxyMode;
7376
this.eventManager = new EventManager();
7477
this.moduleManager = new ModuleManager();
7578
if (!cmdLine.hasOption("nogui"))
@@ -123,7 +126,7 @@ public Bot(CommandLine cmdLine) {
123126
FishingBot.getI18n().info("config-loaded-from", new File(getConfig().getPath()).getAbsolutePath());
124127

125128
// error if credentials are default credentials
126-
if (getConfig().getUserName().equals("[email protected]")) {
129+
if (!proxyMode && getConfig().getUserName().equals("[email protected]")) {
127130
FishingBot.getI18n().warning("credentials-not-set");
128131
if (!cmdLine.hasOption("nogui"))
129132
Dialogs.showCredentialsNotSet();
@@ -242,10 +245,8 @@ public Bot(CommandLine cmdLine) {
242245
port = Integer.parseInt(ipAndPort.split(":")[1]);
243246
}
244247

245-
//Ping server
246-
FishingBot.getI18n().info("server-pinging", ip, String.valueOf(port), getConfig().getDefaultProtocol());
247-
ServerPinger sp = new ServerPinger(ip, port);
248-
sp.ping();
248+
setServerHost(ip);
249+
setServerPort(port);
249250
}
250251

251252
public FishingModule getFishingModule() {
@@ -260,16 +261,54 @@ public DiscordModule getDiscordModule() {
260261
return (DiscordModule) getModuleManager().getLoadedModule(DiscordModule.class).orElse(null);
261262
}
262263

263-
public void start(CommandLine cmdLine) {
264+
public void ping() {
265+
if (!isProxyMode())
266+
setServerProtocol(ProtocolConstants.getProtocolId(FishingBot.getInstance().getCurrentBot().getConfig().getDefaultProtocol()));
267+
FishingBot.getI18n().info("server-pinging", getServerHost(), String.valueOf(getServerPort()), ProtocolConstants.getVersionString(getServerProtocol()));
268+
ServerPinger sp = new ServerPinger(getServerHost(), getServerPort());
269+
sp.ping();
270+
}
271+
272+
private boolean canStart(CommandLine cmdLine) {
264273
if (isRunning() || isPreventStartup()) {
265274
FishingBot.getInstance().setCurrentBot(null);
266275
if (cmdLine.hasOption("nogui"))
267-
return;
276+
return false;
268277
FishingBot.getInstance().getMainGUIController().updateStartStop();
269278
FishingBot.getInstance().getMainGUIController().enableStartStop();
279+
return false;
280+
}
281+
return true;
282+
}
283+
284+
public void start(CommandLine cmdLine) {
285+
if (canStart(cmdLine))
286+
connect();
287+
}
288+
289+
public void injectProxy(CommandLine cmdLine, ServerProxyHandler proxyConnection) {
290+
if (!canStart(cmdLine)) {
291+
try {
292+
if (proxyConnection.getServerSocket() != null && !proxyConnection.getServerSocket().isClosed())
293+
proxyConnection.getServerSocket().close();
294+
if (proxyConnection.getServerSocket() != null && !proxyConnection.getServerSocket().isClosed())
295+
proxyConnection.getClientSocket().close();
296+
} catch (Exception ex) {
297+
ex.printStackTrace();
298+
}
270299
return;
271300
}
272-
connect();
301+
302+
setRunning(true);
303+
this.socket = proxyConnection.getServerSocket();
304+
this.net = new NetworkHandler(proxyConnection);
305+
startup(new LootHistory());
306+
}
307+
308+
public void shutdownProxyConnection() {
309+
setRunning(false);
310+
reset();
311+
shutdown();
273312
}
274313

275314
private boolean authenticate(File accountFile) {
@@ -299,10 +338,87 @@ private void registerCommands() {
299338
getCommandRegistry().registerCommand(new SwapCommand());
300339
}
301340

302-
private void connect() {
303-
String serverName = getServerHost();
304-
int port = getServerPort();
341+
private void enableModules(LootHistory savedLootHistory) {
342+
getModuleManager().enableModule(new HandshakeModule(getServerHost(), getServerPort()));
343+
getModuleManager().enableModule(new LoginModule());
344+
getModuleManager().enableModule(new ClientDefaultsModule());
345+
getModuleManager().enableModule(new FishingModule(savedLootHistory));
346+
getModuleManager().enableModule(new ChatProxyModule());
305347

348+
if (getConfig().isStartTextEnabled())
349+
getModuleManager().enableModule(new ChatCommandModule());
350+
351+
if (getConfig().isWebHookEnabled())
352+
getModuleManager().enableModule(new DiscordModule());
353+
354+
if (getConfig().isAutoLootEjectionEnabled())
355+
getModuleManager().enableModule(new EjectionModule());
356+
357+
if (getConfig().isTimerEnabled())
358+
getModuleManager().enableModule(new TimerModule());
359+
}
360+
361+
private void startup(LootHistory savedLootHistory) {
362+
registerCommands();
363+
364+
if (FishingBot.getInstance().getMainGUIController() != null && !getEventManager().isRegistered(FishingBot.getInstance().getMainGUIController()))
365+
getEventManager().registerListener(FishingBot.getInstance().getMainGUIController());
366+
367+
// enable required modules
368+
369+
enableModules(savedLootHistory);
370+
371+
// init item handler & player
372+
373+
new ItemHandler(getServerProtocol());
374+
this.player = new Player();
375+
}
376+
377+
private void reset() {
378+
if (getPlayer() != null)
379+
getEventManager().unregisterListener(getPlayer());
380+
getEventManager().getRegisteredListener().clear();
381+
getEventManager().getClassToInstanceMapping().clear();
382+
getModuleManager().disableAll();
383+
this.socket = null;
384+
this.net = null;
385+
this.player = null;
386+
}
387+
388+
private boolean canAutoReconnect() {
389+
if (getConfig().isAutoReconnect() && !isPreventReconnect()) {
390+
FishingBot.getI18n().info("bot-automatic-reconnect", String.valueOf(getConfig().getAutoReconnectTime()));
391+
392+
try {
393+
Thread.sleep(getConfig().getAutoReconnectTime() * 1000);
394+
} catch (InterruptedException ignore) { }
395+
396+
if (getAuthData() == null) {
397+
if (getConfig().isOnlineMode())
398+
authenticate(accountFile);
399+
else {
400+
FishingBot.getI18n().info("credentials-using-offline-mode", getConfig().getUserName());
401+
authData = new AuthData(null, null, null, getConfig().getUserName());
402+
}
403+
}
404+
return true;
405+
}
406+
return false;
407+
}
408+
409+
private void shutdown() {
410+
FishingBot.getInstance().setCurrentBot(null);
411+
if (FishingBot.getInstance().getMainGUIController() != null) {
412+
FishingBot.getInstance().getMainGUIController().updateStartStop();
413+
FishingBot.getInstance().getMainGUIController().updatePlayPaused();
414+
try {
415+
Thread.sleep(1000);
416+
} catch (InterruptedException ignore) { }
417+
FishingBot.getInstance().getMainGUIController().enableStartStop();
418+
}
419+
}
420+
421+
private void connect() {
306422
LootHistory savedLootHistory = new LootHistory();
307423

308424
do {
@@ -321,41 +437,11 @@ private void connect() {
321437
continue;
322438
}
323439
}
324-
this.socket = new Socket(serverName, port);
440+
this.socket = new Socket(getServerHost(), getServerPort());
325441

326442
this.net = new NetworkHandler();
327443

328-
registerCommands();
329-
if (FishingBot.getInstance().getMainGUIController() != null)
330-
getEventManager().registerListener(FishingBot.getInstance().getMainGUIController());
331-
332-
if (FishingBot.getInstance().getMainGUIController() != null && !getEventManager().isRegistered(FishingBot.getInstance().getMainGUIController()))
333-
getEventManager().registerListener(FishingBot.getInstance().getMainGUIController());
334-
335-
// enable required modules
336-
337-
getModuleManager().enableModule(new HandshakeModule(serverName, port));
338-
getModuleManager().enableModule(new LoginModule(getAuthData().getUsername()));
339-
getModuleManager().enableModule(new ClientDefaultsModule());
340-
getModuleManager().enableModule(new FishingModule(savedLootHistory));
341-
getModuleManager().enableModule(new ChatProxyModule());
342-
343-
if (getConfig().isStartTextEnabled())
344-
getModuleManager().enableModule(new ChatCommandModule());
345-
346-
if (getConfig().isWebHookEnabled())
347-
getModuleManager().enableModule(new DiscordModule());
348-
349-
if (getConfig().isAutoLootEjectionEnabled())
350-
getModuleManager().enableModule(new EjectionModule());
351-
352-
if (getConfig().isTimerEnabled())
353-
getModuleManager().enableModule(new TimerModule());
354-
355-
// init item handler & player
356-
357-
new ItemHandler(getServerProtocol());
358-
this.player = new Player();
444+
startup(savedLootHistory);
359445

360446
// add shutdown hook
361447

@@ -372,7 +458,7 @@ private void connect() {
372458

373459
while (running) {
374460
try {
375-
net.readData();
461+
net.readDataFromServer();
376462
} catch (Exception ex) {
377463
ex.printStackTrace();
378464
FishingBot.getI18n().warning("packet-could-not-be-received");
@@ -384,48 +470,17 @@ private void connect() {
384470
FishingBot.getI18n().severe("bot-could-not-be-started", e.getMessage());
385471
} finally {
386472
try {
387-
if (socket != null)
473+
if (socket != null && !socket.isClosed())
388474
this.socket.close();
389475
} catch (IOException e) {
390476
e.printStackTrace();
391477
}
392478

393-
if (getPlayer() != null)
394-
getEventManager().unregisterListener(getPlayer());
395-
getEventManager().getRegisteredListener().clear();
396-
getEventManager().getClassToInstanceMapping().clear();
397479
if (getFishingModule() != null)
398480
savedLootHistory = getFishingModule().getLootHistory();
399-
getModuleManager().disableAll();
400-
this.socket = null;
401-
this.net = null;
402-
this.player = null;
403-
}
404-
if (getConfig().isAutoReconnect() && !isPreventReconnect()) {
405-
FishingBot.getI18n().info("bot-automatic-reconnect", String.valueOf(getConfig().getAutoReconnectTime()));
406-
407-
try {
408-
Thread.sleep(getConfig().getAutoReconnectTime() * 1000);
409-
} catch (InterruptedException ignore) { }
410-
411-
if (getAuthData() == null) {
412-
if (getConfig().isOnlineMode())
413-
authenticate(accountFile);
414-
else {
415-
FishingBot.getI18n().info("credentials-using-offline-mode", getConfig().getUserName());
416-
authData = new AuthData(null, null, null, getConfig().getUserName());
417-
}
418-
}
481+
reset();
419482
}
420-
} while (getConfig().isAutoReconnect() && !isPreventReconnect());
421-
FishingBot.getInstance().setCurrentBot(null);
422-
if (FishingBot.getInstance().getMainGUIController() != null) {
423-
FishingBot.getInstance().getMainGUIController().updateStartStop();
424-
FishingBot.getInstance().getMainGUIController().updatePlayPaused();
425-
try {
426-
Thread.sleep(1000);
427-
} catch (InterruptedException ignore) { }
428-
FishingBot.getInstance().getMainGUIController().enableStartStop();
429-
}
483+
} while (canAutoReconnect());
484+
shutdown();
430485
}
431486
}

0 commit comments

Comments
 (0)