Skip to content

Commit 365a349

Browse files
committed
Initial commit
0 parents  commit 365a349

File tree

80 files changed

+5191
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+5191
-0
lines changed

pom.xml

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>me.ANONIMUS.mcprotocol</groupId>
8+
<artifactId>McProtocolLib</artifactId>
9+
<version>1.0</version>
10+
11+
<properties>
12+
<maven.compiler.source>8</maven.compiler.source>
13+
<maven.compiler.target>8</maven.compiler.target>
14+
</properties>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.projectlombok</groupId>
19+
<artifactId>lombok</artifactId>
20+
<version>1.18.20</version>
21+
<scope>provided</scope>
22+
</dependency>
23+
<dependency>
24+
<groupId>io.netty</groupId>
25+
<artifactId>netty-all</artifactId>
26+
<version>4.1.67.Final</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.reflections</groupId>
30+
<artifactId>reflections</artifactId>
31+
<version>0.9.12</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>com.google.code.gson</groupId>
35+
<artifactId>gson</artifactId>
36+
<version>2.8.7</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>com.google.guava</groupId>
40+
<artifactId>guava</artifactId>
41+
<version>23.0</version>
42+
</dependency>
43+
<dependency>
44+
<groupId>commons-codec</groupId>
45+
<artifactId>commons-codec</artifactId>
46+
<version>1.13</version>
47+
</dependency>
48+
</dependencies>
49+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package me.ANONIMUS.mcprotocol;
2+
3+
import me.ANONIMUS.mcprotocol.network.protocol.ProtocolType;
4+
import me.ANONIMUS.mcprotocol.network.protocol.data.ConnectionState;
5+
import me.ANONIMUS.mcprotocol.network.protocol.packet.PacketDirection;
6+
import me.ANONIMUS.mcprotocol.network.protocol.packet.PacketRegistry;
7+
import me.ANONIMUS.mcprotocol.utils.ColorUtil;
8+
9+
import java.util.Arrays;
10+
import java.util.stream.Collectors;
11+
12+
public class MinecraftProtocol {
13+
public static final String prefix = "BetterMcProtocol";
14+
public static final float version = 1.0F;
15+
16+
public static void init() {
17+
System.out.println(ColorUtil.ANSI_BRIGHT_WHITE + "Setting up " + prefix + " " + ColorUtil.ANSI_BRIGHT_BLACK + "(" + ColorUtil.ANSI_BRIGHT_YELLOW + version + ColorUtil.ANSI_BRIGHT_BLACK + ")" + ColorUtil.ANSI_BRIGHT_WHITE + " created by " + ColorUtil.ANSI_BRIGHT_RED + "Ikos3k");
18+
System.out.println();
19+
PacketRegistry.init();
20+
21+
System.out.println(ColorUtil.ANSI_BRIGHT_YELLOW + "Supported versions: " + ColorUtil.ANSI_BRIGHT_WHITE + Arrays.stream(ProtocolType.values()).filter(protocolType ->
22+
protocolType != ProtocolType.PROTOCOL_UNKNOWN).map(ProtocolType::getPrefix).collect(Collectors.joining(ColorUtil.ANSI_BRIGHT_RED + ", " + ColorUtil.ANSI_BRIGHT_WHITE)));
23+
System.out.println(ColorUtil.ANSI_BRIGHT_YELLOW + "Successfully loaded: " + ColorUtil.ANSI_BRIGHT_WHITE + getPacketsSize() + ColorUtil.ANSI_BRIGHT_YELLOW + " packets" + ColorUtil.ANSI_RESET);
24+
}
25+
26+
private static int getPacketsSize() {
27+
int i = 0;
28+
for (PacketDirection direction : PacketDirection.values()) {
29+
for (ConnectionState connectionState : ConnectionState.values()) {
30+
i += connectionState.getPacketsByDirection(direction).size();
31+
}
32+
}
33+
return i;
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package me.ANONIMUS.mcprotocol.exception;
2+
3+
public class BetterException extends RuntimeException {
4+
public BetterException(final String message) {
5+
super(message);
6+
}
7+
8+
@Override
9+
public void printStackTrace() {
10+
super.printStackTrace();
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package me.ANONIMUS.mcprotocol.network.client;
2+
3+
import me.ANONIMUS.mcprotocol.network.objects.Session;
4+
import me.ANONIMUS.mcprotocol.network.protocol.packet.Packet;
5+
6+
public interface ClientListener {
7+
void connected(Session session);
8+
9+
void disconnected(Session session, String reason, Throwable cause);
10+
11+
void packetReceived(Session session, Packet packet);
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package me.ANONIMUS.mcprotocol.network.client;
2+
3+
import io.netty.bootstrap.Bootstrap;
4+
import io.netty.channel.*;
5+
import io.netty.channel.nio.NioEventLoopGroup;
6+
import io.netty.channel.socket.SocketChannel;
7+
import io.netty.channel.socket.nio.NioSocketChannel;
8+
import io.netty.handler.timeout.ReadTimeoutHandler;
9+
import lombok.Data;
10+
import me.ANONIMUS.mcprotocol.exception.BetterException;
11+
import me.ANONIMUS.mcprotocol.network.objects.Session;
12+
import me.ANONIMUS.mcprotocol.network.protocol.ProtocolType;
13+
import me.ANONIMUS.mcprotocol.network.protocol.data.ConnectionState;
14+
import me.ANONIMUS.mcprotocol.network.protocol.handlers.PacketCodec;
15+
import me.ANONIMUS.mcprotocol.network.protocol.handlers.VarInt21FrameCodec;
16+
import me.ANONIMUS.mcprotocol.network.protocol.packet.Packet;
17+
import me.ANONIMUS.mcprotocol.network.protocol.packet.PacketDirection;
18+
import me.ANONIMUS.mcprotocol.network.protocol.packet.impl.client.play.ClientKeepAlivePacket;
19+
import me.ANONIMUS.mcprotocol.network.protocol.packet.impl.server.login.ServerLoginDisconnectPacket;
20+
import me.ANONIMUS.mcprotocol.network.protocol.packet.impl.server.login.ServerLoginSetCompressionPacket;
21+
import me.ANONIMUS.mcprotocol.network.protocol.packet.impl.server.login.ServerLoginSuccessPacket;
22+
import me.ANONIMUS.mcprotocol.network.protocol.packet.impl.server.play.ServerDisconnectPacket;
23+
import me.ANONIMUS.mcprotocol.network.protocol.packet.impl.server.play.ServerKeepAlivePacket;
24+
import me.ANONIMUS.mcprotocol.objects.GameProfile;
25+
import me.ANONIMUS.mcprotocol.objects.ServerData;
26+
import net.md_5.bungee.api.chat.BaseComponent;
27+
28+
import java.net.Proxy;
29+
import java.util.concurrent.TimeUnit;
30+
31+
@Data
32+
public class MinecraftClient {
33+
private final ConnectionState connectionState;
34+
private final int protocol;
35+
private Session remoteSession;
36+
private EventLoopGroup group;
37+
38+
private ClientListener listener;
39+
40+
public MinecraftClient(final ConnectionState connectionState, final ProtocolType protocolType) {
41+
this.connectionState = connectionState;
42+
this.protocol = protocolType.getProtocol();
43+
}
44+
45+
public MinecraftClient(final ConnectionState connectionState, final int protocol) {
46+
this(connectionState, ProtocolType.getByProtocolID(protocol));
47+
}
48+
49+
public MinecraftClient(final ConnectionState connectionState, final Session session) {
50+
this(connectionState, session.getProtocolID());
51+
}
52+
53+
public void connect(final String ip, final int port) {
54+
connect(ip, port, Proxy.NO_PROXY);
55+
}
56+
57+
public void connect(final String ip, final int port, final Proxy proxy) {
58+
if (this.group != null || this.remoteSession != null)
59+
return;
60+
61+
this.group = new NioEventLoopGroup();
62+
63+
Bootstrap bootstrap = new Bootstrap()
64+
.group(group)
65+
.channel(NioSocketChannel.class)
66+
.option(ChannelOption.TCP_NODELAY, true)
67+
.option(ChannelOption.IP_TOS, 0x18)
68+
.handler(new ChannelInitializer<SocketChannel>() {
69+
@Override
70+
protected void initChannel(SocketChannel socketChannel) {
71+
final ChannelPipeline pipeline = socketChannel.pipeline();
72+
pipeline.addLast("timer", new ReadTimeoutHandler(30));
73+
pipeline.addLast("frameCodec", new VarInt21FrameCodec());
74+
pipeline.addLast("packetCodec", new PacketCodec(connectionState, PacketDirection.CLIENTBOUND));
75+
pipeline.addLast("handler", new SimpleChannelInboundHandler<Packet>() {
76+
77+
@Override
78+
public void channelActive(ChannelHandlerContext ctx) throws InterruptedException {
79+
TimeUnit.MILLISECONDS.sleep(150);
80+
listener.connected(remoteSession);
81+
}
82+
83+
@Override
84+
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
85+
cause.printStackTrace();
86+
}
87+
88+
@Override
89+
public void channelInactive(ChannelHandlerContext ctx) {
90+
remoteSession.disconnect();
91+
}
92+
93+
@Override
94+
protected void channelRead0(ChannelHandlerContext channelHandlerContext, Packet packet) {
95+
if (packet instanceof ServerLoginSetCompressionPacket) {
96+
remoteSession.setCompressionThreshold(((ServerLoginSetCompressionPacket) packet).getThreshold());
97+
} else if (packet instanceof ServerLoginSuccessPacket) {
98+
remoteSession.setGameProfile(new GameProfile(((ServerLoginSuccessPacket) packet).getUuid(), ((ServerLoginSuccessPacket) packet).getUsername()));
99+
remoteSession.setConnectionState(ConnectionState.PLAY);
100+
} else if (packet instanceof ServerLoginDisconnectPacket) {
101+
disconnect(BaseComponent.toLegacyText(((ServerLoginDisconnectPacket) packet).getReason()), null);
102+
} else if (packet instanceof ServerDisconnectPacket) {
103+
disconnect(BaseComponent.toLegacyText(((ServerDisconnectPacket) packet).getReason()), null);
104+
} else if (packet instanceof ServerKeepAlivePacket) {
105+
remoteSession.sendPacket(new ClientKeepAlivePacket(((ServerKeepAlivePacket) packet).getKeepaliveId()));
106+
}
107+
108+
listener.packetReceived(remoteSession, packet);
109+
}
110+
});
111+
}
112+
});
113+
try {
114+
remoteSession = (new Session(bootstrap.connect(ip, port).syncUninterruptibly().channel()));
115+
remoteSession.setProtocolID(protocol);
116+
remoteSession.setConnectionState(connectionState);
117+
remoteSession.setServerData(new ServerData(ip, port));
118+
} catch (Exception e) {
119+
disconnect("Connection problem!", null);
120+
}
121+
}
122+
123+
public void disconnect(String cause, Throwable throwable) {
124+
if (remoteSession != null) {
125+
remoteSession.disconnect();
126+
}
127+
group.shutdownGracefully();
128+
129+
listener.disconnected(remoteSession, cause, throwable);
130+
131+
throw new BetterException(cause);
132+
}
133+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package me.ANONIMUS.mcprotocol.network.objects;
2+
3+
import io.netty.channel.Channel;
4+
import lombok.Data;
5+
import me.ANONIMUS.mcprotocol.network.protocol.data.ConnectionState;
6+
import me.ANONIMUS.mcprotocol.network.protocol.handlers.CompressionCodec;
7+
import me.ANONIMUS.mcprotocol.network.protocol.handlers.PacketCodec;
8+
import me.ANONIMUS.mcprotocol.network.protocol.packet.Packet;
9+
import me.ANONIMUS.mcprotocol.network.server.ServerHandler;
10+
import me.ANONIMUS.mcprotocol.objects.GameProfile;
11+
import me.ANONIMUS.mcprotocol.objects.ServerData;
12+
13+
@Data
14+
public class Session {
15+
private final Channel channel;
16+
private ServerHandler handler;
17+
private GameProfile gameProfile;
18+
private ServerData serverData;
19+
20+
public void sendPacket(Packet p) {
21+
channel.writeAndFlush(p);
22+
}
23+
24+
public void setConnectionState(ConnectionState state) {
25+
getPacketCodec().setConnectionState(state);
26+
}
27+
28+
public void setProtocolID(int protocol) {
29+
getPacketCodec().setProtocol(protocol);
30+
}
31+
32+
public int getProtocolID() {
33+
return getPacketCodec().getProtocol();
34+
}
35+
36+
public ConnectionState getConnectionState() {
37+
return getPacketCodec().getConnectionState();
38+
}
39+
40+
public PacketCodec getPacketCodec() {
41+
return ((PacketCodec) channel.pipeline().get("packetCodec"));
42+
}
43+
44+
public void setCompressionThreshold(final int threshold) {
45+
if (getConnectionState() == ConnectionState.LOGIN) {
46+
if (channel.pipeline().get("compression") == null) {
47+
channel.pipeline().addBefore("packetCodec", "compression", new CompressionCodec(threshold));
48+
}
49+
} else {
50+
throw new UnsupportedOperationException();
51+
}
52+
}
53+
54+
public void disconnect() {
55+
if (handler != null) {
56+
handler.disconnect();
57+
}
58+
if (channel != null) {
59+
channel.close();
60+
}
61+
}
62+
63+
@Override
64+
public String toString() {
65+
return "channel: " + channel.remoteAddress()
66+
+ " profile: " + (gameProfile != null ? gameProfile : "null");
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package me.ANONIMUS.mcprotocol.network.protocol;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class Protocol {
7+
private final int id;
8+
private final int[] protocols;
9+
10+
public Protocol(int id, int... protocols) {
11+
this.id = id;
12+
this.protocols = protocols;
13+
}
14+
15+
public Protocol(int id, ProtocolType... protocolTypes) {
16+
this.id = id;
17+
18+
this.protocols = new int[protocolTypes.length];
19+
for (int i = 0; i < this.protocols.length; i++) {
20+
this.protocols[i] = protocolTypes[i].getProtocol();
21+
}
22+
}
23+
24+
public static Protocol getAllAvailable(int id) {
25+
return new Protocol(id, ProtocolType.values());
26+
}
27+
}

0 commit comments

Comments
 (0)