Skip to content

Commit aaff771

Browse files
author
games647
authored
Merge pull request TuxCoding#612 from juanmuscaria/main
Velocity support
2 parents ec7c421 + 37ac04c commit aaff771

File tree

16 files changed

+1284
-13
lines changed

16 files changed

+1284
-13
lines changed

.github/workflows/maven.yml

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,14 @@ jobs:
2626
# Pull changes
2727
- uses: actions/checkout@v2.3.4
2828

29-
# Cache artifacts - however this has the downside that we don't get notified of
30-
# artifact resolution failures like invalid repository
31-
# Nevertheless the repositories should be more stable and it makes no sense to pull
32-
# a same version every time
33-
# A dry run would make more sense
34-
- uses: actions/cache@v2.1.4
35-
with:
36-
path: ~/.m2/repository
37-
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
38-
restore-keys: |
39-
${{ runner.os }}-maven-
40-
4129
# Setup Java
4230
- name: Set up JDK
43-
uses: actions/setup-java@v2.1.0
31+
uses: actions/setup-java@v2.3.0
4432
with:
4533
distribution: 'adopt'
4634
# Use Java 11, because it's minimum required version
4735
java-version: 11
36+
cache: 'maven'
4837

4938
# Build and test (included in package)
5039
- name: Build with Maven and test

core/src/main/resources/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ database: '{pluginDir}/FastLogin.db'
252252

253253
# MySQL/MariaDB
254254
# If you want to enable it uncomment only the lines below this not this line.
255+
# If on velocity use 'fastlogin.mysql.cj.jdbc.Driver' as driver
255256
#driver: 'com.mysql.jdbc.Driver'
256257
#host: '127.0.0.1'
257258
#port: 3306

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
<module>core</module>
5858
<module>bukkit</module>
5959
<module>bungee</module>
60+
<module>velocity</module>
6061
</modules>
6162

6263
<!--Deployment configuration for the Maven repository-->

velocity/pom.xml

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<!--
2+
3+
SPDX-License-Identifier: MIT
4+
5+
The MIT License (MIT)
6+
7+
Copyright (c) 2015-2021 <Your name and contributors>
8+
9+
Permission is hereby granted, free of charge, to any person obtaining a copy
10+
of this software and associated documentation files (the "Software"), to deal
11+
in the Software without restriction, including without limitation the rights
12+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
copies of the Software, and to permit persons to whom the Software is
14+
furnished to do so, subject to the following conditions:
15+
16+
The above copyright notice and this permission notice shall be included in all
17+
copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25+
SOFTWARE.
26+
27+
-->
28+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
29+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
30+
<modelVersion>4.0.0</modelVersion>
31+
32+
<parent>
33+
<groupId>com.github.games647</groupId>
34+
<artifactId>fastlogin</artifactId>
35+
<version>1.11-SNAPSHOT</version>
36+
<relativePath>../pom.xml</relativePath>
37+
</parent>
38+
39+
<!--This have to be in lowercase because it's used by plugin.yml-->
40+
<artifactId>fastlogin.velocity</artifactId>
41+
<packaging>jar</packaging>
42+
43+
<!--Represents the main plugin-->
44+
<name>FastLoginVelocity</name>
45+
46+
<build>
47+
<plugins>
48+
<plugin>
49+
<groupId>org.codehaus.mojo</groupId>
50+
<artifactId>templating-maven-plugin</artifactId>
51+
<version>1.0.0</version>
52+
<executions>
53+
<execution>
54+
<id>filter-src</id>
55+
<goals>
56+
<goal>filter-sources</goal>
57+
</goals>
58+
</execution>
59+
</executions>
60+
</plugin>
61+
<plugin>
62+
<groupId>org.apache.maven.plugins</groupId>
63+
<artifactId>maven-shade-plugin</artifactId>
64+
<version>3.2.4</version>
65+
<configuration>
66+
<createDependencyReducedPom>false</createDependencyReducedPom>
67+
<shadedArtifactAttached>false</shadedArtifactAttached>
68+
<relocations>
69+
<relocation>
70+
<pattern>com.zaxxer.hikari</pattern>
71+
<shadedPattern>fastlogin.hikari</shadedPattern>
72+
</relocation>
73+
<relocation>
74+
<pattern>net.md_5.bungee.config</pattern>
75+
<shadedPattern>fastlogin.config</shadedPattern>
76+
</relocation>
77+
<relocation>
78+
<pattern>com.mysql</pattern>
79+
<shadedPattern>fastlogin.mysql</shadedPattern>
80+
</relocation>
81+
</relocations>
82+
<artifactSet>
83+
<!--org.slf4j is part of velocity api and runtime,
84+
shading and relocating it causes logger injection to fail-->
85+
<excludes>
86+
<exclude>org.slf4j:*</exclude>
87+
<exclude>com.google.code.gson:gson</exclude>
88+
</excludes>
89+
</artifactSet>
90+
</configuration>
91+
<executions>
92+
<execution>
93+
<phase>package</phase>
94+
<goals>
95+
<goal>shade</goal>
96+
</goals>
97+
</execution>
98+
</executions>
99+
</plugin>
100+
</plugins>
101+
</build>
102+
103+
<repositories>
104+
<repository>
105+
<id>velocity</id>
106+
<url>https://nexus.velocitypowered.com/repository/maven-public/</url>
107+
</repository>
108+
</repositories>
109+
110+
<dependencies>
111+
<!--Common plugin component-->
112+
<dependency>
113+
<groupId>${project.groupId}</groupId>
114+
<artifactId>fastlogin.core</artifactId>
115+
<version>${project.version}</version>
116+
</dependency>
117+
118+
<!--Velocity api-->
119+
<dependency>
120+
<groupId>com.velocitypowered</groupId>
121+
<artifactId>velocity-api</artifactId>
122+
<version>3.0.1</version>
123+
<scope>provided</scope>
124+
</dependency>
125+
126+
<!--Velocity does not ship any database driver-->
127+
<dependency>
128+
<groupId>org.mariadb.jdbc</groupId>
129+
<artifactId>mariadb-java-client</artifactId>
130+
<version>2.7.4</version>
131+
</dependency>
132+
</dependencies>
133+
</project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.github.games647.fastlogin.velocity;
2+
3+
public class PomData {
4+
public static final String DISPLAY_NAME = "${project.name}";
5+
public static final String NAME = "${project.parent.artifactId}";
6+
public static final String VERSION = "${project.version}-${git.commit.id.abbrev}";
7+
public static final String DESCRIPTION = "${project.parent.description}";
8+
public static final String URL = "${project.parent.url}";
9+
}
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
/*
2+
* SPDX-License-Identifier: MIT
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2015-2021 <Your name and contributors>
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in all
16+
* copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
* SOFTWARE.
25+
*/
26+
package com.github.games647.fastlogin.velocity;
27+
28+
import com.github.games647.fastlogin.core.AsyncScheduler;
29+
import com.github.games647.fastlogin.core.message.ChangePremiumMessage;
30+
import com.github.games647.fastlogin.core.message.ChannelMessage;
31+
import com.github.games647.fastlogin.core.message.SuccessMessage;
32+
import com.github.games647.fastlogin.core.shared.FastLoginCore;
33+
import com.github.games647.fastlogin.core.shared.PlatformPlugin;
34+
import com.github.games647.fastlogin.velocity.listener.ConnectListener;
35+
import com.github.games647.fastlogin.velocity.listener.PluginMessageListener;
36+
import com.google.common.collect.MapMaker;
37+
import com.google.common.io.ByteArrayDataOutput;
38+
import com.google.common.io.ByteStreams;
39+
import com.google.inject.Inject;
40+
import com.velocitypowered.api.command.CommandSource;
41+
import com.velocitypowered.api.event.Subscribe;
42+
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
43+
import com.velocitypowered.api.event.proxy.ProxyShutdownEvent;
44+
import com.velocitypowered.api.plugin.Plugin;
45+
import com.velocitypowered.api.plugin.annotation.DataDirectory;
46+
import com.velocitypowered.api.proxy.Player;
47+
import com.velocitypowered.api.proxy.ProxyServer;
48+
import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier;
49+
import com.velocitypowered.api.proxy.server.RegisteredServer;
50+
51+
import java.io.IOException;
52+
import java.net.InetSocketAddress;
53+
import java.nio.charset.StandardCharsets;
54+
import java.nio.file.Files;
55+
import java.nio.file.Path;
56+
import java.nio.file.StandardOpenOption;
57+
import java.util.Collections;
58+
import java.util.List;
59+
import java.util.UUID;
60+
import java.util.concurrent.ConcurrentMap;
61+
62+
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
63+
64+
import org.slf4j.Logger;
65+
66+
//TODO: Support for floodgate
67+
@Plugin(id = PomData.NAME, name = PomData.DISPLAY_NAME, description = PomData.DESCRIPTION, url = PomData.URL,
68+
version = PomData.VERSION, authors = {"games647", "https://github.com/games647/FastLogin/graphs/contributors"})
69+
public class FastLoginVelocity implements PlatformPlugin<CommandSource> {
70+
71+
private final ProxyServer server;
72+
private final Path dataDirectory;
73+
private final Logger logger;
74+
private final ConcurrentMap<InetSocketAddress, VelocityLoginSession> session = new MapMaker().weakKeys().makeMap();
75+
private static final String PROXY_ID_fILE = "proxyId.txt";
76+
77+
private FastLoginCore<Player, CommandSource, FastLoginVelocity> core;
78+
private AsyncScheduler scheduler;
79+
private UUID proxyId;
80+
81+
@Inject
82+
public FastLoginVelocity(ProxyServer server, Logger logger, @DataDirectory Path dataDirectory) {
83+
this.server = server;
84+
this.logger = logger;
85+
this.dataDirectory = dataDirectory;
86+
}
87+
88+
@Subscribe
89+
public void onProxyInitialization(ProxyInitializeEvent event) {
90+
scheduler = new AsyncScheduler(logger, getThreadFactory());
91+
core = new FastLoginCore<>(this);
92+
core.load();
93+
loadOrGenerateProxyId();
94+
if (!core.setupDatabase() || proxyId == null) {
95+
return;
96+
}
97+
98+
server.getEventManager().register(this, new ConnectListener(this, core.getRateLimiter()));
99+
server.getEventManager().register(this, new PluginMessageListener(this));
100+
server.getChannelRegistrar().register(MinecraftChannelIdentifier.create(getName(), ChangePremiumMessage.CHANGE_CHANNEL));
101+
server.getChannelRegistrar().register(MinecraftChannelIdentifier.create(getName(), SuccessMessage.SUCCESS_CHANNEL));
102+
}
103+
104+
@Subscribe
105+
public void onProxyShutdown(ProxyShutdownEvent event) {
106+
if (core != null) {
107+
core.close();
108+
}
109+
}
110+
111+
@Override
112+
public String getName() {
113+
return PomData.NAME;
114+
}
115+
116+
@Override
117+
public Path getPluginFolder() {
118+
return dataDirectory;
119+
}
120+
121+
@Override
122+
public Logger getLog() {
123+
return logger;
124+
}
125+
126+
@Override
127+
public void sendMessage(CommandSource receiver, String message) {
128+
receiver.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(message));
129+
}
130+
131+
@Override
132+
public AsyncScheduler getScheduler() {
133+
return scheduler;
134+
}
135+
136+
@Override
137+
public boolean isPluginInstalled(String name) {
138+
return server.getPluginManager().isLoaded(name);
139+
}
140+
141+
public FastLoginCore<Player, CommandSource, FastLoginVelocity> getCore() {
142+
return core;
143+
}
144+
145+
public ConcurrentMap<InetSocketAddress, VelocityLoginSession> getSession() {
146+
return session;
147+
}
148+
149+
public ProxyServer getProxy() {
150+
return server;
151+
}
152+
153+
public void sendPluginMessage(RegisteredServer server, ChannelMessage message) {
154+
if (server != null) {
155+
ByteArrayDataOutput dataOutput = ByteStreams.newDataOutput();
156+
message.writeTo(dataOutput);
157+
158+
MinecraftChannelIdentifier channel = MinecraftChannelIdentifier.create(getName(), message.getChannelName());
159+
server.sendPluginMessage(channel, dataOutput.toByteArray());
160+
}
161+
}
162+
163+
private void loadOrGenerateProxyId() {
164+
Path idFile = dataDirectory.resolve(PROXY_ID_fILE);
165+
boolean shouldGenerate = false;
166+
167+
if (Files.exists(idFile)) {
168+
try {
169+
List<String> lines = Files.readAllLines(idFile, StandardCharsets.UTF_8);
170+
if (lines.isEmpty()) {
171+
shouldGenerate = true;
172+
} else {
173+
proxyId = UUID.fromString(lines.get(0));
174+
}
175+
} catch (IOException e) {
176+
logger.error("Unable to load proxy id from '{}'", idFile.toAbsolutePath());
177+
logger.error("Detailed exception:", e);
178+
} catch (IllegalArgumentException e) {
179+
logger.error("'{}' contains an invalid uuid! FastLogin will not work without a valid id.", idFile.toAbsolutePath());
180+
}
181+
} else {
182+
shouldGenerate = true;
183+
}
184+
185+
if (shouldGenerate) {
186+
proxyId = UUID.randomUUID();
187+
try {
188+
Files.write(idFile, Collections.singletonList(proxyId.toString()), StandardOpenOption.CREATE);
189+
} catch (IOException e) {
190+
logger.error("Unable to save proxy id to '{}'", idFile.toAbsolutePath());
191+
logger.error("Detailed exception:", e);
192+
}
193+
}
194+
}
195+
196+
public UUID getProxyId() {
197+
return proxyId;
198+
}
199+
}

0 commit comments

Comments
 (0)