Initial commit
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user