2019-07-08 17:55:14 +00:00
|
|
|
/*
|
2020-01-09 03:05:42 +00:00
|
|
|
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
|
2019-07-08 17:55:14 +00:00
|
|
|
*
|
2019-07-11 21:30:35 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2019-07-08 17:55:14 +00:00
|
|
|
*
|
2019-07-11 21:30:35 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
2019-07-08 17:55:14 +00:00
|
|
|
*
|
|
|
|
* @author GeyserMC
|
|
|
|
* @link https://github.com/GeyserMC/Geyser
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.geysermc.connector;
|
|
|
|
|
2019-07-08 19:13:31 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.BedrockPacketCodec;
|
2019-07-08 23:35:32 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.BedrockServer;
|
2020-04-17 16:34:09 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.v390.Bedrock_v390;
|
2019-10-31 02:14:23 +00:00
|
|
|
|
2019-07-08 19:19:41 +00:00
|
|
|
import lombok.Getter;
|
2019-12-21 17:35:48 +00:00
|
|
|
|
2020-01-04 05:58:58 +00:00
|
|
|
import org.geysermc.common.AuthType;
|
2020-04-17 16:38:18 +00:00
|
|
|
import org.geysermc.common.IGeyserConfiguration;
|
2019-12-21 17:53:19 +00:00
|
|
|
import org.geysermc.common.PlatformType;
|
2019-12-01 21:16:52 +00:00
|
|
|
import org.geysermc.common.bootstrap.IGeyserBootstrap;
|
2019-12-21 17:35:48 +00:00
|
|
|
import org.geysermc.common.logger.IGeyserLogger;
|
2020-04-19 23:08:14 +00:00
|
|
|
import org.geysermc.connector.command.CommandManager;
|
2019-08-06 01:59:54 +00:00
|
|
|
import org.geysermc.connector.metrics.Metrics;
|
2019-07-10 06:34:10 +00:00
|
|
|
import org.geysermc.connector.network.ConnectorServerEventHandler;
|
2019-12-21 17:35:48 +00:00
|
|
|
import org.geysermc.connector.network.remote.RemoteServer;
|
2019-10-16 18:55:05 +00:00
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
2020-03-24 04:03:33 +00:00
|
|
|
import org.geysermc.connector.network.translators.Translators;
|
2019-07-21 23:52:30 +00:00
|
|
|
import org.geysermc.connector.thread.PingPassthroughThread;
|
2019-11-06 00:55:59 +00:00
|
|
|
import org.geysermc.connector.utils.Toolbox;
|
2019-07-08 17:55:14 +00:00
|
|
|
|
2019-07-08 23:35:32 +00:00
|
|
|
import java.net.InetSocketAddress;
|
2019-09-15 23:46:05 +00:00
|
|
|
import java.text.DecimalFormat;
|
2019-09-21 16:00:13 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2020-04-06 13:33:36 +00:00
|
|
|
import java.util.concurrent.CompletableFuture;
|
2019-07-08 17:55:14 +00:00
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
import java.util.concurrent.ScheduledExecutorService;
|
2019-07-21 23:52:30 +00:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2019-07-08 17:55:14 +00:00
|
|
|
|
2019-08-09 01:42:55 +00:00
|
|
|
@Getter
|
2019-12-21 17:35:48 +00:00
|
|
|
public class GeyserConnector {
|
2019-07-08 17:55:14 +00:00
|
|
|
|
2020-04-17 16:34:09 +00:00
|
|
|
public static final BedrockPacketCodec BEDROCK_PACKET_CODEC = Bedrock_v390.V390_CODEC;
|
2019-07-08 19:13:31 +00:00
|
|
|
|
2019-09-21 16:00:13 +00:00
|
|
|
public static final String NAME = "Geyser";
|
|
|
|
public static final String VERSION = "1.0-SNAPSHOT";
|
2019-07-08 17:55:14 +00:00
|
|
|
|
2020-04-09 14:26:14 +00:00
|
|
|
private final Map<InetSocketAddress, GeyserSession> players = new HashMap<>();
|
2019-08-09 01:42:55 +00:00
|
|
|
|
2019-07-08 17:55:14 +00:00
|
|
|
private static GeyserConnector instance;
|
|
|
|
|
2019-12-21 17:35:48 +00:00
|
|
|
private RemoteServer remoteServer;
|
2020-01-04 05:58:58 +00:00
|
|
|
private AuthType authType;
|
2019-07-08 19:19:41 +00:00
|
|
|
|
|
|
|
private boolean shuttingDown = false;
|
|
|
|
|
2019-07-08 17:55:14 +00:00
|
|
|
private final ScheduledExecutorService generalThreadPool;
|
2019-07-21 23:52:30 +00:00
|
|
|
private PingPassthroughThread passthroughThread;
|
|
|
|
|
2019-12-21 17:38:45 +00:00
|
|
|
private BedrockServer bedrockServer;
|
2019-12-21 17:53:19 +00:00
|
|
|
private PlatformType platformType;
|
2020-03-17 16:43:09 +00:00
|
|
|
private IGeyserBootstrap bootstrap;
|
2019-12-21 17:38:45 +00:00
|
|
|
|
2019-08-09 01:42:55 +00:00
|
|
|
private Metrics metrics;
|
2019-08-06 01:59:54 +00:00
|
|
|
|
2020-03-17 16:43:09 +00:00
|
|
|
private GeyserConnector(PlatformType platformType, IGeyserBootstrap bootstrap) {
|
2019-09-15 23:46:05 +00:00
|
|
|
long startupTime = System.currentTimeMillis();
|
2019-09-14 23:21:55 +00:00
|
|
|
|
2019-07-08 17:55:14 +00:00
|
|
|
instance = this;
|
|
|
|
|
2020-03-17 16:43:09 +00:00
|
|
|
this.bootstrap = bootstrap;
|
|
|
|
|
|
|
|
IGeyserLogger logger = bootstrap.getGeyserLogger();
|
|
|
|
IGeyserConfiguration config = bootstrap.getGeyserConfig();
|
|
|
|
|
2019-12-21 17:53:19 +00:00
|
|
|
this.platformType = platformType;
|
2019-07-13 19:00:51 +00:00
|
|
|
|
|
|
|
logger.info("******************************************");
|
2019-07-08 17:55:14 +00:00
|
|
|
logger.info("");
|
2019-11-28 04:33:45 +00:00
|
|
|
logger.info("Loading " + NAME + " version " + VERSION);
|
2019-07-08 17:55:14 +00:00
|
|
|
logger.info("");
|
|
|
|
logger.info("******************************************");
|
|
|
|
|
2019-09-22 22:15:27 +00:00
|
|
|
this.generalThreadPool = Executors.newScheduledThreadPool(config.getGeneralThreadPool());
|
2019-09-22 22:08:12 +00:00
|
|
|
|
2019-07-21 21:18:20 +00:00
|
|
|
logger.setDebug(config.isDebugMode());
|
|
|
|
|
2019-11-06 00:55:59 +00:00
|
|
|
Toolbox.init();
|
2020-03-24 04:03:33 +00:00
|
|
|
Translators.start();
|
2019-07-11 22:39:28 +00:00
|
|
|
|
2019-12-21 17:35:48 +00:00
|
|
|
remoteServer = new RemoteServer(config.getRemote().getAddress(), config.getRemote().getPort());
|
2019-11-30 12:34:45 +00:00
|
|
|
authType = AuthType.getByName(config.getRemote().getAuthType());
|
2019-07-08 23:35:32 +00:00
|
|
|
|
2019-07-21 23:52:30 +00:00
|
|
|
passthroughThread = new PingPassthroughThread(this);
|
|
|
|
if (config.isPingPassthrough())
|
|
|
|
generalThreadPool.scheduleAtFixedRate(passthroughThread, 1, 1, TimeUnit.SECONDS);
|
|
|
|
|
2019-12-21 17:38:45 +00:00
|
|
|
bedrockServer = new BedrockServer(new InetSocketAddress(config.getBedrock().getAddress(), config.getBedrock().getPort()));
|
2019-07-10 06:34:10 +00:00
|
|
|
bedrockServer.setHandler(new ConnectorServerEventHandler(this));
|
2019-07-08 23:35:32 +00:00
|
|
|
bedrockServer.bind().whenComplete((avoid, throwable) -> {
|
|
|
|
if (throwable == null) {
|
2019-09-15 23:46:05 +00:00
|
|
|
logger.info("Started Geyser on " + config.getBedrock().getAddress() + ":" + config.getBedrock().getPort());
|
2019-07-08 23:35:32 +00:00
|
|
|
} else {
|
2019-09-15 23:46:05 +00:00
|
|
|
logger.severe("Failed to start Geyser on " + config.getBedrock().getAddress() + ":" + config.getBedrock().getPort());
|
2019-07-08 23:35:32 +00:00
|
|
|
throwable.printStackTrace();
|
|
|
|
}
|
|
|
|
}).join();
|
2019-08-09 21:09:47 +00:00
|
|
|
|
2019-09-21 16:00:13 +00:00
|
|
|
if (config.getMetrics().isEnabled()) {
|
2019-12-21 17:35:48 +00:00
|
|
|
metrics = new Metrics(this, "GeyserMC", config.getMetrics().getUniqueId(), false, java.util.logging.Logger.getLogger(""));
|
2019-09-21 16:00:13 +00:00
|
|
|
metrics.addCustomChart(new Metrics.SingleLineChart("servers", () -> 1));
|
2019-12-21 17:35:48 +00:00
|
|
|
metrics.addCustomChart(new Metrics.SingleLineChart("players", players::size));
|
2020-01-04 05:58:58 +00:00
|
|
|
metrics.addCustomChart(new Metrics.SimplePie("authMode", authType.name()::toLowerCase));
|
2019-12-21 17:53:19 +00:00
|
|
|
metrics.addCustomChart(new Metrics.SimplePie("platform", platformType::getPlatformName));
|
2019-09-21 16:00:13 +00:00
|
|
|
}
|
2019-09-15 23:46:05 +00:00
|
|
|
|
|
|
|
double completeTime = (System.currentTimeMillis() - startupTime) / 1000D;
|
2019-12-21 22:19:32 +00:00
|
|
|
logger.info(String.format("Done (%ss)! Run /geyser help for help!", new DecimalFormat("#.###").format(completeTime)));
|
2019-07-08 17:55:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void shutdown() {
|
2020-03-17 16:43:09 +00:00
|
|
|
bootstrap.getGeyserLogger().info("Shutting down Geyser.");
|
2019-07-08 17:55:14 +00:00
|
|
|
shuttingDown = true;
|
|
|
|
|
2020-04-06 13:33:36 +00:00
|
|
|
if (players.size() >= 1) {
|
2020-04-08 00:03:25 +00:00
|
|
|
bootstrap.getGeyserLogger().info("Kicking " + players.size() + " player(s)");
|
2020-04-06 13:33:36 +00:00
|
|
|
|
|
|
|
for (GeyserSession playerSession : players.values()) {
|
|
|
|
playerSession.disconnect("Geyser Proxy shutting down.");
|
|
|
|
}
|
|
|
|
|
|
|
|
CompletableFuture<Void> future = CompletableFuture.runAsync(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
// Simulate a long-running Job
|
|
|
|
try {
|
|
|
|
while (true) {
|
|
|
|
if (players.size() == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeUnit.MILLISECONDS.sleep(100);
|
|
|
|
}
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
throw new IllegalStateException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Block and wait for the future to complete
|
|
|
|
try {
|
|
|
|
future.get();
|
|
|
|
bootstrap.getGeyserLogger().info("Kicked all players");
|
|
|
|
} catch (Exception e) {
|
|
|
|
// Quietly fail
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-08 17:55:14 +00:00
|
|
|
generalThreadPool.shutdown();
|
2019-12-21 17:38:45 +00:00
|
|
|
bedrockServer.close();
|
2020-03-17 16:43:09 +00:00
|
|
|
players.clear();
|
|
|
|
remoteServer = null;
|
|
|
|
authType = null;
|
2020-04-19 23:08:14 +00:00
|
|
|
this.getCommandManager().getCommands().clear();
|
2020-04-06 13:33:36 +00:00
|
|
|
|
|
|
|
bootstrap.getGeyserLogger().info("Geyser shutdown successfully.");
|
2019-07-08 17:55:14 +00:00
|
|
|
}
|
2019-08-06 01:59:54 +00:00
|
|
|
|
2019-10-16 18:55:05 +00:00
|
|
|
public void addPlayer(GeyserSession player) {
|
2020-04-09 14:26:14 +00:00
|
|
|
players.put(player.getSocketAddress(), player);
|
2019-08-09 01:42:55 +00:00
|
|
|
}
|
|
|
|
|
2019-10-16 18:55:05 +00:00
|
|
|
public void removePlayer(GeyserSession player) {
|
2020-04-09 14:26:14 +00:00
|
|
|
players.remove(player.getSocketAddress());
|
2019-08-09 01:42:55 +00:00
|
|
|
}
|
2019-12-01 21:16:52 +00:00
|
|
|
|
2019-12-21 17:53:19 +00:00
|
|
|
public static GeyserConnector start(PlatformType platformType, IGeyserBootstrap bootstrap) {
|
2020-03-17 16:43:09 +00:00
|
|
|
return new GeyserConnector(platformType, bootstrap);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void reload() {
|
|
|
|
shutdown();
|
|
|
|
bootstrap.onEnable();
|
|
|
|
}
|
|
|
|
|
|
|
|
public IGeyserLogger getLogger() {
|
|
|
|
return bootstrap.getGeyserLogger();
|
2019-12-01 21:16:52 +00:00
|
|
|
}
|
|
|
|
|
2020-03-17 16:43:09 +00:00
|
|
|
public IGeyserConfiguration getConfig() {
|
|
|
|
return bootstrap.getGeyserConfig();
|
2019-12-01 21:16:52 +00:00
|
|
|
}
|
2019-12-21 17:35:48 +00:00
|
|
|
|
2020-04-19 23:08:14 +00:00
|
|
|
public CommandManager getCommandManager() {
|
|
|
|
return (CommandManager) bootstrap.getGeyserCommandManager();
|
|
|
|
}
|
|
|
|
|
2019-12-21 17:35:48 +00:00
|
|
|
public static GeyserConnector getInstance() {
|
|
|
|
return instance;
|
|
|
|
}
|
2019-07-08 17:55:14 +00:00
|
|
|
}
|