Initial commit
This commit is contained in:
3
Java/Messenger/.idea/.gitignore
generated
vendored
Normal file
3
Java/Messenger/.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
13
Java/Messenger/.idea/compiler.xml
generated
Normal file
13
Java/Messenger/.idea/compiler.xml
generated
Normal file
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CompilerConfiguration">
|
||||
<annotationProcessing>
|
||||
<profile name="Maven default annotation processors profile" enabled="true">
|
||||
<sourceOutputDir name="target/generated-sources/annotations" />
|
||||
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
||||
<outputRelativeToContentRoot value="true" />
|
||||
<module name="Messenger" />
|
||||
</profile>
|
||||
</annotationProcessing>
|
||||
</component>
|
||||
</project>
|
||||
6
Java/Messenger/.idea/discord.xml
generated
Normal file
6
Java/Messenger/.idea/discord.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="DiscordProjectSettings">
|
||||
<option name="show" value="PROJECT_FILES" />
|
||||
</component>
|
||||
</project>
|
||||
20
Java/Messenger/.idea/jarRepositories.xml
generated
Normal file
20
Java/Messenger/.idea/jarRepositories.xml
generated
Normal file
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="RemoteRepositoriesConfiguration">
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Central Repository" />
|
||||
<option name="url" value="https://repo.maven.apache.org/maven2" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Maven Central repository" />
|
||||
<option name="url" value="https://repo1.maven.org/maven2" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="jboss.community" />
|
||||
<option name="name" value="JBoss Community repository" />
|
||||
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
|
||||
</remote-repository>
|
||||
</component>
|
||||
</project>
|
||||
14
Java/Messenger/.idea/misc.xml
generated
Normal file
14
Java/Messenger/.idea/misc.xml
generated
Normal file
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="MavenProjectsManager">
|
||||
<option name="originalFiles">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/pom.xml" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
124
Java/Messenger/.idea/uiDesigner.xml
generated
Normal file
124
Java/Messenger/.idea/uiDesigner.xml
generated
Normal file
@@ -0,0 +1,124 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Palette2">
|
||||
<group name="Swing">
|
||||
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
|
||||
</item>
|
||||
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
|
||||
</item>
|
||||
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
|
||||
</item>
|
||||
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
|
||||
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
|
||||
</item>
|
||||
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
|
||||
<initial-values>
|
||||
<property name="text" value="Button" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||
<initial-values>
|
||||
<property name="text" value="RadioButton" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||
<initial-values>
|
||||
<property name="text" value="CheckBox" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
|
||||
<initial-values>
|
||||
<property name="text" value="Label" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||
<preferred-size width="150" height="-1" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||
<preferred-size width="150" height="-1" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||
<preferred-size width="150" height="-1" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||
<preferred-size width="200" height="200" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||
<preferred-size width="200" height="200" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
|
||||
</item>
|
||||
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
|
||||
<preferred-size width="-1" height="20" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
|
||||
</item>
|
||||
</group>
|
||||
</component>
|
||||
</project>
|
||||
33
Java/Messenger/pom.xml
Normal file
33
Java/Messenger/pom.xml
Normal file
@@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>de.craftix</groupId>
|
||||
<artifactId>Messenger</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>8.0.26</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>de.craftix</groupId>
|
||||
<artifactId>GameEngine</artifactId>
|
||||
<version>3.0</version>
|
||||
<scope>system</scope>
|
||||
<systemPath>D:/Java/GameEngine/target/GameEngine-3.0.jar</systemPath>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,37 @@
|
||||
package de.craftix.messenger;
|
||||
|
||||
import de.craftix.engine.GameEngine;
|
||||
import de.craftix.engine.render.Screen;
|
||||
import de.craftix.messenger.onscreen.ComponentManager;
|
||||
import de.craftix.server.User;
|
||||
|
||||
import java.util.Base64;
|
||||
|
||||
public class Messenger extends GameEngine {
|
||||
private static User user;
|
||||
|
||||
public static void main(String[] args) {
|
||||
Screen.antialiasing(true);
|
||||
Screen.showFrames(true);
|
||||
Screen.setResizeable(true);
|
||||
Screen.setFramesPerSecond(120);
|
||||
Screen.setAntialiasingEffectTextures(false);
|
||||
setup(800, 600, "Messenger v0.1", new Messenger(), 20);
|
||||
|
||||
//ServerConnection.connect();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialise() {
|
||||
ComponentManager.setup();
|
||||
}
|
||||
|
||||
public static User getUser() { return user; }
|
||||
|
||||
public static String decode(String msg) {
|
||||
return new String(Base64.getDecoder().decode(msg));
|
||||
}
|
||||
public static String encode(String msg) {
|
||||
return Base64.getEncoder().encodeToString(msg.getBytes());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package de.craftix.messenger;
|
||||
|
||||
import de.craftix.server.Packet;
|
||||
import de.craftix.server.Server;
|
||||
import sun.misc.Unsafe;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
|
||||
public class ServerConnection {
|
||||
|
||||
private static ObjectInputStream in;
|
||||
private static ObjectOutputStream out;
|
||||
private static Thread clientThread;
|
||||
|
||||
public static void connect() {
|
||||
try {
|
||||
Socket socket = new Socket();
|
||||
socket.connect(new InetSocketAddress(Server.IP, Server.PORT), 5000);
|
||||
out = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream()));
|
||||
out.flush();
|
||||
in = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
|
||||
|
||||
disableWarning();
|
||||
|
||||
System.out.println("Connected to Server");
|
||||
|
||||
startThread();
|
||||
}catch (Exception e) { e.printStackTrace(); }
|
||||
}
|
||||
|
||||
private static void startThread() {
|
||||
clientThread = new Thread(() -> {
|
||||
while (!clientThread.isInterrupted()) {
|
||||
try {
|
||||
Object o = in.readObject();
|
||||
if (o instanceof Packet) {
|
||||
Packet packet = (Packet) o;
|
||||
Packet output = handlePacket(packet);
|
||||
out.writeObject(output);
|
||||
out.flush();
|
||||
}
|
||||
}catch (Exception e) { e.printStackTrace(); }
|
||||
}
|
||||
});
|
||||
clientThread.start();
|
||||
}
|
||||
|
||||
private static Packet handlePacket(Packet packet) {
|
||||
if (packet.getType() == Packet.PacketType.PING)
|
||||
return new Packet(Packet.PacketType.PING, null);
|
||||
|
||||
return new Packet(Packet.PacketType.BOOLEAN, false);
|
||||
}
|
||||
|
||||
public static Packet sendRequest(Packet packet) {
|
||||
Packet answer = new Packet(Packet.PacketType.BOOLEAN, false);
|
||||
try {
|
||||
//Kill Thread
|
||||
Method m = Thread.class.getDeclaredMethod("stop0", Object.class);
|
||||
m.setAccessible(true);
|
||||
m.invoke(clientThread, new ThreadDeath());
|
||||
|
||||
out.writeObject(packet);
|
||||
out.flush();
|
||||
Object o = in.readObject();
|
||||
if (o instanceof Packet)
|
||||
answer = (Packet) o;
|
||||
}catch (Exception e) { e.printStackTrace(); }
|
||||
finally { startThread(); }
|
||||
return answer;
|
||||
}
|
||||
|
||||
private static void disableWarning() {
|
||||
try {
|
||||
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
|
||||
theUnsafe.setAccessible(true);
|
||||
Unsafe u = (Unsafe) theUnsafe.get(null);
|
||||
|
||||
Class<?> cls = Class.forName("jdk.internal.module.IllegalAccessLogger");
|
||||
Field logger = cls.getDeclaredField("logger");
|
||||
u.putObjectVolatile(cls, u.staticFieldOffset(logger), null);
|
||||
} catch (Exception ignored) {}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package de.craftix.messenger.onscreen;
|
||||
|
||||
import de.craftix.engine.render.Sprite;
|
||||
import de.craftix.engine.var.*;
|
||||
import de.craftix.messenger.Messenger;
|
||||
|
||||
import java.awt.Image;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class ComponentManager {
|
||||
private final static Scene mainScene = new Scene();
|
||||
|
||||
public static void setup() {
|
||||
mainScene.setBackground(new Sprite(new BufferedImage(800, 600, Image.SCALE_DEFAULT), false), false);
|
||||
|
||||
// TODO: Add Components
|
||||
|
||||
Messenger.setActiveScene(mainScene);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package de.craftix.server;
|
||||
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.net.Socket;
|
||||
|
||||
public class ClientHandler {
|
||||
protected String address;
|
||||
protected Socket con;
|
||||
protected ObjectInputStream in;
|
||||
protected ObjectOutputStream out;
|
||||
protected Thread clientThread;
|
||||
protected User user;
|
||||
|
||||
protected ClientHandler(Socket con, ObjectInputStream in, ObjectOutputStream out) {
|
||||
this.con = con;
|
||||
this.in = in;
|
||||
this.out = out;
|
||||
this.address = con.getInetAddress().getHostAddress() + ":" + con.getPort();
|
||||
|
||||
System.out.println("Client connected [" + address + "]");
|
||||
|
||||
startClientThread();
|
||||
}
|
||||
|
||||
private void startClientThread() {
|
||||
clientThread = new Thread(() -> {
|
||||
while (!clientThread.isInterrupted()) {
|
||||
try {
|
||||
Object o = in.readObject();
|
||||
if (o instanceof Packet) {
|
||||
Packet output = handlePacket((Packet) o);
|
||||
sendPacket(output);
|
||||
}
|
||||
}catch (Exception e) {
|
||||
disconnect();
|
||||
}
|
||||
}
|
||||
});
|
||||
clientThread.start();
|
||||
}
|
||||
|
||||
private void disconnect() {
|
||||
clientThread.interrupt();
|
||||
Server.clients.remove(this);
|
||||
|
||||
System.out.println("Client disconnected [" + address + "]");
|
||||
}
|
||||
|
||||
public void sendPacket(Packet packet) {
|
||||
try {
|
||||
out.writeObject(packet);
|
||||
out.flush();
|
||||
}catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private Packet handlePacket(Packet packet) {
|
||||
if (packet.getType() == Packet.PacketType.PING)
|
||||
return new Packet(Packet.PacketType.PING, null);
|
||||
|
||||
if (packet.getType() == Packet.PacketType.LOGIN) {
|
||||
String[] arr = (String[]) packet.getValue();
|
||||
String username = arr[0];
|
||||
String password = arr[1];
|
||||
User user = User.getUser(username);
|
||||
assert user != null;
|
||||
if (Server.checkPassword(user.getUniqueID(), password)) {
|
||||
this.user = user;
|
||||
return new Packet(Packet.PacketType.GET_USER, user);
|
||||
}
|
||||
else
|
||||
return new Packet(Packet.PacketType.BOOLEAN, false);
|
||||
}
|
||||
|
||||
if (packet.getType() == Packet.PacketType.REGISTER) {
|
||||
String[] arr = (String[]) packet.getValue();
|
||||
String username = arr[0];
|
||||
String password = arr[1];
|
||||
User[] users = User.getUsers();
|
||||
for (User u : users) {
|
||||
if (u.username.equals(username))
|
||||
return new Packet(Packet.PacketType.BOOLEAN, false);
|
||||
}
|
||||
User.createUser(User.Rank.USER, username, password);
|
||||
User user = User.getUser(username);
|
||||
this.user = user;
|
||||
return new Packet(Packet.PacketType.GET_USER, user);
|
||||
}
|
||||
|
||||
return new Packet(Packet.PacketType.BOOLEAN, false);
|
||||
}
|
||||
|
||||
}
|
||||
61
Java/Messenger/src/main/java/de/craftix/server/MySQL.java
Normal file
61
Java/Messenger/src/main/java/de/craftix/server/MySQL.java
Normal file
@@ -0,0 +1,61 @@
|
||||
package de.craftix.server;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
|
||||
public class MySQL {
|
||||
|
||||
protected static String server;
|
||||
protected static int port;
|
||||
protected static String database;
|
||||
protected static String username;
|
||||
protected static String password;
|
||||
protected static Connection con;
|
||||
|
||||
public static void connect() {
|
||||
if (isConnected()) return;
|
||||
try {
|
||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||
con = DriverManager.getConnection("jdbc:mysql://" + server + ":" + port + "/" + database, username, password);
|
||||
}catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("MySQL connected");
|
||||
}
|
||||
|
||||
public static void disconnect() {
|
||||
if (!isConnected()) return;
|
||||
try {
|
||||
con.close();
|
||||
con = null;
|
||||
}catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("MySQL disconnected");
|
||||
}
|
||||
|
||||
public static boolean isConnected() {
|
||||
return con != null;
|
||||
}
|
||||
|
||||
public static void insert(String qry) {
|
||||
if (!isConnected()) throw new NullPointerException("MySQL not connected");
|
||||
try {
|
||||
con.prepareStatement(qry).executeUpdate();
|
||||
}catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static ResultSet getData(String qry) {
|
||||
if (!isConnected()) throw new NullPointerException("MySQL not connected");
|
||||
try {
|
||||
return con.prepareStatement(qry).executeQuery();
|
||||
}catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
43
Java/Messenger/src/main/java/de/craftix/server/Packet.java
Normal file
43
Java/Messenger/src/main/java/de/craftix/server/Packet.java
Normal file
@@ -0,0 +1,43 @@
|
||||
package de.craftix.server;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Packet implements Serializable {
|
||||
|
||||
public enum PacketType {
|
||||
PING(0),
|
||||
UPDATE(1),
|
||||
GET_USER(2),
|
||||
LOGIN(3),
|
||||
REGISTER(4),
|
||||
BOOLEAN(5);
|
||||
|
||||
private final byte id;
|
||||
PacketType(int id)
|
||||
{ this.id = (byte) id; }
|
||||
|
||||
public byte getID()
|
||||
{ return id; }
|
||||
|
||||
public static PacketType getByID(byte id) {
|
||||
for (PacketType type : values()) {
|
||||
if (type.getID() == id)
|
||||
return type;
|
||||
}
|
||||
return PING;
|
||||
}
|
||||
}
|
||||
|
||||
private final Object value;
|
||||
private final byte type;
|
||||
|
||||
public Packet(PacketType type, Object value) {
|
||||
this.value = value;
|
||||
this.type = type.getID();
|
||||
}
|
||||
|
||||
public PacketType getType()
|
||||
{ return PacketType.getByID(type); }
|
||||
public Object getValue()
|
||||
{ return value; }
|
||||
}
|
||||
64
Java/Messenger/src/main/java/de/craftix/server/Server.java
Normal file
64
Java/Messenger/src/main/java/de/craftix/server/Server.java
Normal file
@@ -0,0 +1,64 @@
|
||||
package de.craftix.server;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.sql.ResultSet;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class Server {
|
||||
public static final String IP = "localhost";
|
||||
public static final int PORT = 187;
|
||||
|
||||
protected static ServerSocket socket;
|
||||
protected static List<ClientHandler> clients;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
MySQL.server = "213.136.89.237";
|
||||
MySQL.port = 3306;
|
||||
MySQL.database = "Messenger";
|
||||
MySQL.username = "Messenger";
|
||||
MySQL.password = "v0dn2Ml60AcaX@VN";
|
||||
|
||||
MySQL.connect();
|
||||
createMySQLTables();
|
||||
|
||||
startServerLoop();
|
||||
}
|
||||
|
||||
private static void createMySQLTables() {
|
||||
MySQL.insert("CREATE TABLE IF NOT EXISTS Users (Rank INT(10), ID INT(10), UUID VARCHAR(100), Username VARCHAR(100), Password VARCHAR(100))");
|
||||
MySQL.insert("CREATE TABLE IF NOT EXISTS Chats (ID INT(10), Owner VARCHAR(100))");
|
||||
MySQL.insert("CREATE TABLE IF NOT EXISTS ChatUser (ChatID INT(10), Member VARCHAR(100))");
|
||||
MySQL.insert("CREATE TABLE IF NOT EXISTS Messages (Chat INT(10), Sender VARCHAR(100), Timestamp INT(255), Message VARCHAR(2000))");
|
||||
}
|
||||
|
||||
private static void startServerLoop() throws Exception {
|
||||
socket = new ServerSocket(PORT);
|
||||
clients = new ArrayList<>();
|
||||
|
||||
System.out.println("Server started successfully");
|
||||
|
||||
while (!Thread.currentThread().isInterrupted()) {
|
||||
Socket client = socket.accept();
|
||||
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(client.getOutputStream()));
|
||||
out.flush();
|
||||
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(client.getInputStream()));
|
||||
clients.add(new ClientHandler(client, in, out));
|
||||
}
|
||||
}
|
||||
|
||||
protected static boolean checkPassword(UUID user, String password) {
|
||||
try {
|
||||
ResultSet rs = MySQL.getData("SELECT Password FROM Users WHERE UUID = \"" + user + "\"");
|
||||
assert rs != null;
|
||||
if (rs.next()) {
|
||||
String truePassword = rs.getString("Password");
|
||||
return password.equals(truePassword);
|
||||
}
|
||||
}catch (Exception e) { e.printStackTrace(); }
|
||||
return false;
|
||||
}
|
||||
}
|
||||
130
Java/Messenger/src/main/java/de/craftix/server/User.java
Normal file
130
Java/Messenger/src/main/java/de/craftix/server/User.java
Normal file
@@ -0,0 +1,130 @@
|
||||
package de.craftix.server;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.sql.ResultSet;
|
||||
import java.util.ArrayList;
|
||||
import java.util.UUID;
|
||||
|
||||
public class User implements Serializable {
|
||||
public static User getUser(UUID uuid) {
|
||||
if (!MySQL.isConnected()) throw new NullPointerException("MySQL not connected!");
|
||||
try {
|
||||
ResultSet rs = MySQL.getData("SELECT * FROM Users WHERE UUID = \"" + uuid + "\"");
|
||||
assert rs != null;
|
||||
if (rs.next()) {
|
||||
int id = rs.getInt("ID");
|
||||
Rank rank = Rank.getRankByID(rs.getInt("Rank"));
|
||||
String username = rs.getString("Username");
|
||||
return new User(id, uuid, rank, username);
|
||||
}
|
||||
}catch (Exception e) { e.printStackTrace(); }
|
||||
return null;
|
||||
}
|
||||
public static User getUser(String username) {
|
||||
if (!MySQL.isConnected()) throw new NullPointerException("MySQL not connected!");
|
||||
try {
|
||||
ResultSet rs = MySQL.getData("SELECT * FROM Users WHERE Username = \"" + username + "\"");
|
||||
assert rs != null;
|
||||
if (rs.next()) {
|
||||
int id = rs.getInt("ID");
|
||||
UUID uuid = UUID.fromString(rs.getString("UUID"));
|
||||
Rank rank = Rank.getRankByID(rs.getInt("Rank"));
|
||||
return new User(id, uuid, rank, username);
|
||||
}
|
||||
}catch (Exception e) { e.printStackTrace(); }
|
||||
return null;
|
||||
}
|
||||
public static User[] getUsers() {
|
||||
if (!MySQL.isConnected()) throw new NullPointerException("MySQL not connected!");
|
||||
ArrayList<User> users = new ArrayList<>();
|
||||
|
||||
try {
|
||||
ResultSet rs = MySQL.getData("SELECT * FROM Users");
|
||||
assert rs != null;
|
||||
if (rs.next()) {
|
||||
int id = rs.getInt("ID");
|
||||
UUID uuid = UUID.fromString(rs.getString("UUID"));
|
||||
Rank rank = Rank.getRankByID(rs.getInt("Rank"));
|
||||
String username = rs.getString("Username");
|
||||
users.add(new User(id, uuid, rank, username));
|
||||
}
|
||||
}catch (Exception e) { e.printStackTrace(); }
|
||||
|
||||
return users.toArray(new User[0]);
|
||||
}
|
||||
|
||||
protected static User createUser(Rank rank, String username, String password) {
|
||||
User[] users = getUsers();
|
||||
|
||||
int id = 0;
|
||||
boolean foundID = false;
|
||||
while (!foundID) {
|
||||
for (User u : users) {
|
||||
if (u.id == id) {
|
||||
id++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
foundID = true;
|
||||
}
|
||||
|
||||
UUID uuid = UUID.randomUUID();
|
||||
boolean foundUUID = false;
|
||||
while (!foundUUID) {
|
||||
for (User u : users) {
|
||||
if (u.uuid.equals(uuid)) {
|
||||
uuid = UUID.randomUUID();
|
||||
break;
|
||||
}
|
||||
}
|
||||
foundUUID = true;
|
||||
}
|
||||
|
||||
User user = new User(id, uuid, rank, username);
|
||||
MySQL.insert("INSERT INTO Users (Rank, ID, UUID, Username, Password) VALUES (" + rank.getRankID() + ", " + id + ", \"" + uuid + "\", \"" +
|
||||
username + "\", \"" + password + "\")");
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
private final int id;
|
||||
private final UUID uuid;
|
||||
protected Rank rank;
|
||||
protected String username;
|
||||
|
||||
private User(int id, UUID uuid, Rank rank, String username) {
|
||||
this.id = id;
|
||||
this.uuid = uuid;
|
||||
this.rank = rank;
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
protected void saveUser() {
|
||||
if (!MySQL.isConnected()) throw new NullPointerException("MySQL not connected!");
|
||||
MySQL.insert("UPDATE Users SET Username = \"" + username + "\", Rank = " + rank + " WHERE UUID = \"" + uuid + "\"");
|
||||
}
|
||||
|
||||
public int getID() { return id; }
|
||||
public UUID getUniqueID() { return uuid; }
|
||||
public Rank getRank() { return rank; }
|
||||
public String getUsername() { return username; }
|
||||
|
||||
public enum Rank implements Serializable {
|
||||
USER(0),
|
||||
PREMIUM(1),
|
||||
MODERATOR(2),
|
||||
ADMINISTRATOR(3);
|
||||
|
||||
private final int rankID;
|
||||
Rank(int rank) { this.rankID = rank; }
|
||||
public int getRankID() { return rankID; }
|
||||
|
||||
public static Rank getRankByID(int id) {
|
||||
for (Rank rank : values()) {
|
||||
if (rank.getRankID() == id)
|
||||
return rank;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
Java/Messenger/target/classes/de/craftix/server/MySQL.class
Normal file
BIN
Java/Messenger/target/classes/de/craftix/server/MySQL.class
Normal file
Binary file not shown.
Binary file not shown.
BIN
Java/Messenger/target/classes/de/craftix/server/Packet.class
Normal file
BIN
Java/Messenger/target/classes/de/craftix/server/Packet.class
Normal file
Binary file not shown.
BIN
Java/Messenger/target/classes/de/craftix/server/Server.class
Normal file
BIN
Java/Messenger/target/classes/de/craftix/server/Server.class
Normal file
Binary file not shown.
BIN
Java/Messenger/target/classes/de/craftix/server/User$Rank.class
Normal file
BIN
Java/Messenger/target/classes/de/craftix/server/User$Rank.class
Normal file
Binary file not shown.
BIN
Java/Messenger/target/classes/de/craftix/server/User.class
Normal file
BIN
Java/Messenger/target/classes/de/craftix/server/User.class
Normal file
Binary file not shown.
Reference in New Issue
Block a user