1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
/*
* Copyright (C) 2021 Elytrium
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.elytrium.limboauth;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.field.FieldType;
import com.j256.ormlite.jdbc.JdbcPooledConnectionSource;
import com.j256.ormlite.table.TableUtils;
import com.velocitypowered.api.command.CommandManager;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.plugin.Dependency;
import com.velocitypowered.api.plugin.Plugin;
import com.velocitypowered.api.plugin.PluginContainer;
import com.velocitypowered.api.plugin.annotation.DataDirectory;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import net.elytrium.limboapi.api.Limbo;
import net.elytrium.limboapi.api.LimboFactory;
import net.elytrium.limboapi.api.chunk.Dimension;
import net.elytrium.limboapi.api.chunk.VirtualWorld;
import net.elytrium.limboapi.api.file.SchematicFile;
import net.elytrium.limboapi.api.file.WorldFile;
import net.elytrium.limboauth.command.ChangePasswordCommand;
import net.elytrium.limboauth.command.DestroySessionCommand;
import net.elytrium.limboauth.command.ForceUnregisterCommand;
import net.elytrium.limboauth.command.LimboAuthCommand;
import net.elytrium.limboauth.command.TotpCommand;
import net.elytrium.limboauth.command.UnregisterCommand;
import net.elytrium.limboauth.handler.AuthSessionHandler;
import net.elytrium.limboauth.listener.AuthListener;
import net.elytrium.limboauth.model.RegisteredPlayer;
import net.elytrium.limboauth.utils.UpdatesChecker;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.slf4j.Logger;
@Plugin(
id = "limboauth",
name = "LimboAuth",
version = BuildConstants.AUTH_VERSION,
url = "https://elytrium.net/",
authors = {"hevav", "mdxd44"},
dependencies = {@Dependency(id = "limboapi")}
)
@SuppressFBWarnings({"EI_EXPOSE_REP", "MS_EXPOSE_REP"})
public class LimboAuth {
private static LimboAuth instance;
private final HttpClient client = HttpClient.newHttpClient();
private final Path dataDirectory;
private final Logger logger;
private final ProxyServer server;
private final LimboFactory factory;
private final Set<String> unsafePasswords = new HashSet<>();
private Map<String, CachedUser> cachedAuthChecks;
private Dao<RegisteredPlayer, String> playerDao;
private Pattern nicknameValidationPattern;
private Limbo authServer;
private Component nicknameInvalid;
@Inject
@SuppressWarnings("OptionalGetWithoutIsPresent")
public LimboAuth(ProxyServer server, Logger logger, @Named("limboapi") PluginContainer factory, @DataDirectory Path dataDirectory) {
setInstance(this);
this.server = server;
this.logger = logger;
this.dataDirectory = dataDirectory;
this.factory = (LimboFactory) factory.getInstance().get();
}
@Subscribe
public void onProxyInitialization(ProxyInitializeEvent event) throws Exception {
System.setProperty("com.j256.simplelogging.level", "ERROR");
this.reload();
UpdatesChecker.checkForUpdates(this.getLogger());
}
@SuppressWarnings("SwitchStatementWithTooFewBranches")
public void reload() throws Exception {
Settings.IMP.reload(new File(this.dataDirectory.toFile().getAbsoluteFile(), "config.yml"));
if (Settings.IMP.MAIN.CHECK_PASSWORD_STRENGTH) {
this.unsafePasswords.clear();
Path unsafePasswordsFile = Paths.get(this.dataDirectory.toFile().getAbsolutePath(), Settings.IMP.MAIN.UNSAFE_PASSWORDS_FILE);
if (!unsafePasswordsFile.toFile().exists()) {
Files.copy(Objects.requireNonNull(this.getClass().getResourceAsStream("/unsafe_passwords.txt")), unsafePasswordsFile);
}
this.unsafePasswords.addAll(Files.lines(unsafePasswordsFile).collect(Collectors.toSet()));
}
this.cachedAuthChecks = new ConcurrentHashMap<>();
Settings.DATABASE dbConfig = Settings.IMP.DATABASE;
JdbcPooledConnectionSource connectionSource;
// requireNonNull prevents the shade plugin from excluding the drivers in minimized jar.
switch (dbConfig.STORAGE_TYPE.toLowerCase(Locale.ROOT)) {
case "h2": {
Objects.requireNonNull(org.h2.Driver.class);
Objects.requireNonNull(org.h2.engine.Engine.class);
connectionSource = new JdbcPooledConnectionSource("jdbc:h2:" + this.dataDirectory.toFile().getAbsoluteFile() + "/limboauth");
break;
}
case "mysql": {
Objects.requireNonNull(com.mysql.cj.jdbc.Driver.class);
Objects.requireNonNull(com.mysql.cj.conf.url.SingleConnectionUrl.class);
connectionSource = new JdbcPooledConnectionSource(
"jdbc:mysql://" + dbConfig.HOSTNAME + "/" + dbConfig.DATABASE + dbConfig.CONNECTION_PARAMETERS, dbConfig.USER, dbConfig.PASSWORD
);
break;
}
case "postgresql": {
Objects.requireNonNull(org.postgresql.Driver.class);
connectionSource = new JdbcPooledConnectionSource(
"jdbc:postgresql://" + dbConfig.HOSTNAME + "/" + dbConfig.DATABASE + dbConfig.CONNECTION_PARAMETERS, dbConfig.USER, dbConfig.PASSWORD
);
break;
}
default: {
this.getLogger().error("WRONG DATABASE TYPE.");
this.server.shutdown();
return;
}
}
TableUtils.createTableIfNotExists(connectionSource, RegisteredPlayer.class);
this.playerDao = DaoManager.createDao(connectionSource, RegisteredPlayer.class);
this.nicknameValidationPattern = Pattern.compile(Settings.IMP.MAIN.ALLOWED_NICKNAME_REGEX);
this.migrateDb(this.playerDao);
CommandManager manager = this.server.getCommandManager();
manager.unregister("unregister");
manager.unregister("forceunregister");
manager.unregister("changepassword");
manager.unregister("destroysession");
manager.unregister("2fa");
manager.unregister("limboauth");
manager.register("unregister", new UnregisterCommand(this, this.playerDao), "unreg");
manager.register("forceunregister", new ForceUnregisterCommand(this, this.server, this.playerDao), "forceunreg");
manager.register("changepassword", new ChangePasswordCommand(this.playerDao), "changepass");
manager.register("destroysession", new DestroySessionCommand(this));
if (Settings.IMP.MAIN.ENABLE_TOTP) {
manager.register("2fa", new TotpCommand(this.playerDao), "totp");
}
manager.register("limboauth", new LimboAuthCommand(), "la", "auth", "lauth");
Settings.MAIN.AUTH_COORDS authCoords = Settings.IMP.MAIN.AUTH_COORDS;
VirtualWorld authWorld = this.factory.createVirtualWorld(
Dimension.valueOf(Settings.IMP.MAIN.DIMENSION),
authCoords.X, authCoords.Y, authCoords.Z,
(float) authCoords.YAW, (float) authCoords.PITCH
);
if (Settings.IMP.MAIN.LOAD_WORLD) {
try {
Path path = this.dataDirectory.resolve(Settings.IMP.MAIN.WORLD_FILE_PATH);
WorldFile file;
switch (Settings.IMP.MAIN.WORLD_FILE_TYPE) {
case "schematic": {
file = new SchematicFile(path);
break;
}
default: {
this.getLogger().error("Incorrect world file type.");
this.server.shutdown();
return;
}
}
Settings.MAIN.WORLD_COORDS coords = Settings.IMP.MAIN.WORLD_COORDS;
file.toWorld(this.factory, authWorld, coords.X, coords.Y, coords.Z);
} catch (IOException e) {
e.printStackTrace();
}
}
this.authServer = this.factory.createLimbo(authWorld);
this.nicknameInvalid = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.NICKNAME_INVALID_KICK);
this.server.getEventManager().unregisterListeners(this);
this.server.getEventManager().register(this, new AuthListener(this.playerDao));
Executors.newScheduledThreadPool(1, task -> new Thread(task, "purge-cache")).scheduleAtFixedRate(() ->
this.checkCache(this.cachedAuthChecks, Settings.IMP.MAIN.PURGE_CACHE_MILLIS),
Settings.IMP.MAIN.PURGE_CACHE_MILLIS,
Settings.IMP.MAIN.PURGE_CACHE_MILLIS,
TimeUnit.MILLISECONDS
);
}
public void migrateDb(Dao<RegisteredPlayer, String> playerDao) {
Set<FieldType> tables = new HashSet<>();
Collections.addAll(tables, playerDao.getTableInfo().getFieldTypes());
String findSql;
switch (Settings.IMP.DATABASE.STORAGE_TYPE) {
case "h2": {
findSql = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '"
+ playerDao.getTableInfo().getTableName() + "';";
break;
}
case "postgresql":
case "mysql": {
findSql = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '" + Settings.IMP.DATABASE.DATABASE
+ "' AND TABLE_NAME = '" + playerDao.getTableInfo().getTableName() + "';";
break;
}
default: {
this.getLogger().error("WRONG DATABASE TYPE.");
this.server.shutdown();
return;
}
}
try {
playerDao.queryRaw(findSql).forEach(e -> tables.removeIf(q -> q.getColumnName().equalsIgnoreCase(e[0])));
tables.forEach(t -> {
try {
String columnDefinition = t.getColumnDefinition();
StringBuilder builder = new StringBuilder("ALTER TABLE `auth` ADD ");
List<String> dummy = new ArrayList<>();
if (columnDefinition == null) {
playerDao.getConnectionSource().getDatabaseType().appendColumnArg(t.getTableName(), builder, t, dummy, dummy, dummy, dummy);
} else {
playerDao.getConnectionSource().getDatabaseType().appendEscapedEntityName(builder, t.getColumnName());
builder.append(" ").append(columnDefinition).append(" ");
}
playerDao.executeRawNoArgs(builder.toString());
} catch (SQLException e) {
e.printStackTrace();
}
});
} catch (SQLException e) {
e.printStackTrace();
}
}
public void cacheAuthUser(Player player) {
String username = player.getUsername();
this.cachedAuthChecks.remove(username);
this.cachedAuthChecks.put(username, new CachedUser(player.getRemoteAddress().getAddress(), System.currentTimeMillis()));
}
public void removePlayerFromCache(String username) {
this.cachedAuthChecks.remove(username);
}
public boolean needAuth(Player player) {
String username = player.getUsername();
if (!this.cachedAuthChecks.containsKey(username)) {
return true;
}
return !this.cachedAuthChecks.get(username).getInetAddress().equals(player.getRemoteAddress().getAddress());
}
public void authPlayer(Player player) {
String nickname = player.getUsername();
if (!this.nicknameValidationPattern.matcher(nickname).matches()) {
player.disconnect(this.nicknameInvalid);
return;
}
if (!Settings.IMP.MAIN.ONLINE_MODE_NEED_AUTH && player.isOnlineMode()) {
RegisteredPlayer registeredPlayer = AuthSessionHandler.fetchInfo(this.playerDao, player.getUsername());
if (registeredPlayer == null || registeredPlayer.getHash().isEmpty()) {
this.factory.passLoginLimbo(player);
return;
}
}
// Send player to auth virtual server.
try {
this.authServer.spawnPlayer(player, new AuthSessionHandler(this.playerDao, player, this, nickname));
} catch (Throwable t) {
this.getLogger().error("Error", t);
}
}
public boolean isPremium(String nickname) {
try {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(String.format(Settings.IMP.MAIN.ISPREMIUM_AUTH_URL, nickname)))
.build();
HttpResponse<String> response = this.client.send(request, HttpResponse.BodyHandlers.ofString());
return response.statusCode() == 200;
} catch (IOException | InterruptedException e) {
this.getLogger().error("Unable to authenticate with Mojang", e);
return true;
}
}
private void checkCache(Map<String, CachedUser> userMap, long time) {
userMap.entrySet().stream()
.filter(u -> u.getValue().getCheckTime() + time <= System.currentTimeMillis())
.map(Map.Entry::getKey)
.forEach(userMap::remove);
}
private static void setInstance(LimboAuth instance) {
LimboAuth.instance = instance;
}
public static LimboAuth getInstance() {
return instance;
}
public Set<String> getUnsafePasswords() {
return this.unsafePasswords;
}
public Logger getLogger() {
return this.logger;
}
public ProxyServer getServer() {
return this.server;
}
private static class CachedUser {
private final InetAddress inetAddress;
private final long checkTime;
public CachedUser(InetAddress inetAddress, long checkTime) {
this.inetAddress = inetAddress;
this.checkTime = checkTime;
}
public InetAddress getInetAddress() {
return this.inetAddress;
}
public long getCheckTime() {
return this.checkTime;
}
}
}
|