-
Notifications
You must be signed in to change notification settings - Fork 64
RealIP Fabric support #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
29e8db2
add Fabric support
Draylar 427ce1a
change logger name to TCPShield, remove realip reference
Draylar f72269e
remove fabric reference in initialize method
Draylar f7778da
change exception types, update exception message to be more descriptive
Draylar 1242e9d
remove disconnect packet
Draylar ef3d8a2
runtime -> tcpshieldxception when creating default config
Draylar 7ad2eca
rename mixins.json file to tcpshield.mixins.json
Draylar 825b948
remove geyser parsing from fabric config
Draylar 885e859
remove fabric API dependency
Draylar fbb0141
Update README.md
paulzhng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| pluginManagement { | ||
| repositories { | ||
| jcenter() | ||
| maven { | ||
| name = 'Fabric' | ||
| url = 'https://maven.fabricmc.net/' | ||
| } | ||
| gradlePluginPortal() | ||
| } | ||
| } |
18 changes: 18 additions & 0 deletions
18
src/main/java/net/tcpshield/tcpshield/fabric/TCPShieldFabric.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package net.tcpshield.tcpshield.fabric; | ||
|
|
||
| import net.fabricmc.api.ModInitializer; | ||
| import net.tcpshield.tcpshield.HandshakePacketHandler; | ||
| import net.tcpshield.tcpshield.fabric.impl.FabricConfigImpl; | ||
|
|
||
| import java.util.logging.Logger; | ||
|
|
||
| public class TCPShieldFabric implements ModInitializer { | ||
|
|
||
| public static final Logger LOGGER = Logger.getLogger("TCPShield"); | ||
| public static final HandshakePacketHandler PACKET_HANDLER = new HandshakePacketHandler(LOGGER, new FabricConfigImpl()); | ||
|
|
||
| @Override | ||
| public void onInitialize() { | ||
| LOGGER.info("TCPShield has been loaded."); | ||
| } | ||
| } |
105 changes: 105 additions & 0 deletions
105
src/main/java/net/tcpshield/tcpshield/fabric/impl/FabricConfigImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| package net.tcpshield.tcpshield.fabric.impl; | ||
|
|
||
| import net.fabricmc.loader.api.FabricLoader; | ||
| import net.tcpshield.tcpshield.abstraction.TCPShieldConfig; | ||
| import net.tcpshield.tcpshield.exception.TCPShieldInitializationException; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.List; | ||
|
|
||
| public class FabricConfigImpl extends TCPShieldConfig { | ||
|
|
||
| private final Path configLocation = new File(FabricLoader.getInstance().getConfigDir().toString(), "tcpshield.yml").toPath(); | ||
|
|
||
| public FabricConfigImpl() { | ||
| if(!configExists()) { | ||
| createDefaultConfig(); | ||
| } | ||
|
|
||
| ConfigData config = loadConfig(); | ||
|
|
||
| this.onlyProxy = config.onlyAllowProxyConnections; | ||
| this.ipWhitelistFolder = new File(FabricLoader.getInstance().getGameDirectory(), "ip-whitelist"); | ||
| this.geyser = false; | ||
| this.debug = config.debugMode; | ||
| } | ||
|
|
||
| /** | ||
| * @return whether the file specified in {@link FabricConfigImpl#configLocation} exists. | ||
| */ | ||
| private boolean configExists() { | ||
| return Files.exists(configLocation); | ||
| } | ||
|
|
||
| /** | ||
| * Creates the default configuration file at the location specified in {@link FabricConfigImpl#configLocation}. | ||
| * | ||
| * <p> | ||
| * This operation will always override an existing configuration file if it exists. | ||
| * Callers should check {@link FabricConfigImpl#configExists()} if they wish to avoid this behavior. | ||
| */ | ||
| private void createDefaultConfig() { | ||
| // Ensure the parent directory of our config file exists. | ||
| // If it does not exist, attempt to create it now. | ||
| Path parentDirectory = configLocation.getParent(); | ||
| if (!Files.exists(parentDirectory) || !Files.isDirectory(parentDirectory)) { | ||
| parentDirectory.toFile().mkdirs(); | ||
| } | ||
|
|
||
| // Copy the config.yml data from our mod jar to the loader config folder. | ||
| try (InputStream in = getClass().getClassLoader().getResourceAsStream("config.yml")) { | ||
| Files.copy(in, configLocation); | ||
| } catch (Exception e) { | ||
| throw new TCPShieldInitializationException(e); | ||
| } | ||
| } | ||
|
|
||
| private ConfigData loadConfig() { | ||
| try { | ||
| List<String> strings = Files.readAllLines(configLocation); | ||
|
|
||
| boolean onlyAllowProxyConnections = true; | ||
| boolean debugMode = false; | ||
|
|
||
| // Rudimentary config parsing | ||
| for (String line : strings) { | ||
| String[] keyValue = line.split(": "); | ||
|
|
||
| // A config option will only be valid if it is in the format 'a: b'. | ||
| // Ensure that is the case now. | ||
| if (keyValue.length == 2) { | ||
| String key = keyValue[0]; | ||
| String value = keyValue[1]; | ||
|
|
||
| switch (key) { | ||
| case "only-allow-proxy-connections": | ||
| onlyAllowProxyConnections = Boolean.parseBoolean(value); | ||
| break; | ||
| case "debug-mode": | ||
| debugMode = Boolean.parseBoolean(value); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return new ConfigData(onlyAllowProxyConnections, debugMode); | ||
| } catch (Exception e) { | ||
| throw new TCPShieldInitializationException("Couldn't load config in config/tclshield.yml!"); | ||
| } | ||
| } | ||
|
|
||
| private static class ConfigData { | ||
|
|
||
| protected boolean onlyAllowProxyConnections; | ||
| protected boolean debugMode; | ||
|
|
||
| public ConfigData(boolean onlyAllowProxyConnections, boolean debugMode) { | ||
| this.onlyAllowProxyConnections = onlyAllowProxyConnections; | ||
| this.debugMode = debugMode; | ||
| } | ||
| } | ||
| } |
24 changes: 24 additions & 0 deletions
24
src/main/java/net/tcpshield/tcpshield/fabric/impl/FabricPacketImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package net.tcpshield.tcpshield.fabric.impl; | ||
|
|
||
| import net.minecraft.network.packet.c2s.handshake.HandshakeC2SPacket; | ||
| import net.tcpshield.tcpshield.abstraction.IPacket; | ||
| import net.tcpshield.tcpshield.fabric.mixin.HandshakeC2SPacketAccessor; | ||
|
|
||
| public class FabricPacketImpl implements IPacket { | ||
|
|
||
| private final HandshakeC2SPacket handshake; | ||
|
|
||
| public FabricPacketImpl(HandshakeC2SPacket handshake) { | ||
| this.handshake = handshake; | ||
| } | ||
|
|
||
| @Override | ||
| public String getRawPayload() { | ||
| return ((HandshakeC2SPacketAccessor) handshake).getAddress(); | ||
| } | ||
|
|
||
| @Override | ||
| public void modifyOriginalPacket(String hostname) throws Exception { | ||
| // NO OPERATION | ||
| } | ||
| } | ||
40 changes: 40 additions & 0 deletions
40
src/main/java/net/tcpshield/tcpshield/fabric/impl/FabricPlayerImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package net.tcpshield.tcpshield.fabric.impl; | ||
|
|
||
| import net.minecraft.network.ClientConnection; | ||
| import net.minecraft.network.packet.c2s.handshake.HandshakeC2SPacket; | ||
| import net.minecraft.network.packet.s2c.login.LoginDisconnectS2CPacket; | ||
| import net.minecraft.text.LiteralText; | ||
| import net.tcpshield.tcpshield.abstraction.IPlayer; | ||
| import net.tcpshield.tcpshield.exception.IPModificationFailureException; | ||
| import net.tcpshield.tcpshield.fabric.mixin.ClientConnectionAccessor; | ||
|
|
||
| import java.net.InetSocketAddress; | ||
|
|
||
| public class FabricPlayerImpl implements IPlayer { | ||
|
|
||
| private final ClientConnection connection; | ||
| private String ip; | ||
|
|
||
| public FabricPlayerImpl(HandshakeC2SPacket packet, ClientConnection connection) { | ||
| this.connection = connection; | ||
| this.ip = ((InetSocketAddress) ((ClientConnectionAccessor) connection).getChannel().remoteAddress()).getAddress().getHostAddress(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getIP() { | ||
| return ip; | ||
| } | ||
|
|
||
| @Override | ||
| public void setIP(InetSocketAddress ip) throws IPModificationFailureException { | ||
| // At this point, the IP/connection believe the player has the IP of TCPShield. | ||
| // The ip passed into this method contains their CORRECT data, which we have to assign to the player network connection. | ||
| ((ClientConnectionAccessor) connection).setAddress(ip); | ||
| this.ip = ((InetSocketAddress) ((ClientConnectionAccessor) connection).getChannel().remoteAddress()).getAddress().getHostAddress(); | ||
| } | ||
|
|
||
| @Override | ||
| public void disconnect() { | ||
| connection.disconnect(new LiteralText("Connection failed. Please try again or contact an administrator.")); | ||
Draylar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
20 changes: 20 additions & 0 deletions
20
src/main/java/net/tcpshield/tcpshield/fabric/mixin/ClientConnectionAccessor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package net.tcpshield.tcpshield.fabric.mixin; | ||
|
|
||
| import io.netty.channel.Channel; | ||
| import net.minecraft.network.ClientConnection; | ||
| import org.spongepowered.asm.mixin.Mixin; | ||
| import org.spongepowered.asm.mixin.gen.Accessor; | ||
|
|
||
| import java.net.SocketAddress; | ||
|
|
||
| @Mixin(ClientConnection.class) | ||
| public interface ClientConnectionAccessor { | ||
| @Accessor | ||
| void setAddress(SocketAddress address); | ||
|
|
||
| @Accessor | ||
| Channel getChannel(); | ||
|
|
||
| @Accessor | ||
| SocketAddress getAddress(); | ||
| } |
11 changes: 11 additions & 0 deletions
11
src/main/java/net/tcpshield/tcpshield/fabric/mixin/HandshakeC2SPacketAccessor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package net.tcpshield.tcpshield.fabric.mixin; | ||
|
|
||
| import net.minecraft.network.packet.c2s.handshake.HandshakeC2SPacket; | ||
| import org.spongepowered.asm.mixin.Mixin; | ||
| import org.spongepowered.asm.mixin.gen.Accessor; | ||
|
|
||
| @Mixin(HandshakeC2SPacket.class) | ||
| public interface HandshakeC2SPacketAccessor { | ||
| @Accessor | ||
| String getAddress(); | ||
| } |
32 changes: 32 additions & 0 deletions
32
src/main/java/net/tcpshield/tcpshield/fabric/mixin/ServerHandshakeMixin.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package net.tcpshield.tcpshield.fabric.mixin; | ||
|
|
||
| import net.minecraft.network.ClientConnection; | ||
| import net.minecraft.network.packet.c2s.handshake.HandshakeC2SPacket; | ||
| import net.minecraft.server.network.ServerHandshakeNetworkHandler; | ||
| import net.tcpshield.tcpshield.abstraction.IPacket; | ||
| import net.tcpshield.tcpshield.abstraction.IPlayer; | ||
| import net.tcpshield.tcpshield.fabric.TCPShieldFabric; | ||
| import net.tcpshield.tcpshield.fabric.impl.FabricPacketImpl; | ||
| import net.tcpshield.tcpshield.fabric.impl.FabricPlayerImpl; | ||
| import org.spongepowered.asm.mixin.Final; | ||
| import org.spongepowered.asm.mixin.Mixin; | ||
| import org.spongepowered.asm.mixin.Shadow; | ||
| import org.spongepowered.asm.mixin.injection.At; | ||
| import org.spongepowered.asm.mixin.injection.Inject; | ||
| import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
|
||
| @Mixin(ServerHandshakeNetworkHandler.class) | ||
| public class ServerHandshakeMixin { | ||
|
|
||
| @Shadow @Final private ClientConnection connection; | ||
|
|
||
| @Inject( | ||
| method = "onHandshake", | ||
| at = @At("HEAD")) | ||
| private void onHandshake(HandshakeC2SPacket handshake, CallbackInfo ci) { | ||
| IPacket packet = new FabricPacketImpl(handshake); | ||
| IPlayer player = new FabricPlayerImpl(handshake, connection); | ||
|
|
||
| TCPShieldFabric.PACKET_HANDLER.onHandshake(packet, player); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| { | ||
| "schemaVersion": 1, | ||
| "id": "tcp-shield", | ||
| "version": "2.5", | ||
| "name": "TCPShield", | ||
| "description": "TCPShield IP parsing capabilities for Fabric", | ||
| "authors": [ | ||
| "TCPShield" | ||
| ], | ||
| "contact": { | ||
| "homepage": "https://tcpshield.com" | ||
| }, | ||
| "license": "MIT", | ||
| "environment": "*", | ||
| "entrypoints": { | ||
| "main": [ | ||
| "net.tcpshield.tcpshield.fabric.TCPShieldFabric" | ||
| ] | ||
| }, | ||
| "mixins": [ | ||
| "tcpshield.mixins.json" | ||
| ], | ||
| "depends": { | ||
| "fabricloader": "*", | ||
| "minecraft": "*" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| { | ||
| "required": true, | ||
| "minVersion": "0.8", | ||
| "package": "net.tcpshield.tcpshield.fabric.mixin", | ||
| "compatibilityLevel": "JAVA_8", | ||
| "mixins": [ | ||
| "ClientConnectionAccessor", | ||
| "HandshakeC2SPacketAccessor", | ||
| "ServerHandshakeMixin" | ||
| ], | ||
| "injectors": { | ||
| "defaultRequire": 1 | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.