2020-08-23 17:14:54 +00:00
|
|
|
#pragma once
|
2020-08-18 20:42:30 +00:00
|
|
|
|
|
|
|
#define MAX_PACKETSIZE 8192
|
2020-08-23 21:09:31 +00:00
|
|
|
#define DEBUGLOG(x) if (settings::VERBOSITY) {x};
|
2020-08-18 20:42:30 +00:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#ifdef _WIN32
|
|
|
|
// windows (UNTESTED)
|
|
|
|
#include <winsock2.h>
|
|
|
|
#include <windows.h>
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#pragma comment(lib, "Ws2_32.lib")
|
|
|
|
|
|
|
|
typedef char buffer_t;
|
|
|
|
//#define errno WSAGetLastError()
|
|
|
|
#define SOCKETINVALID(x) (x == INVALID_SOCKET)
|
|
|
|
#define SOCKETERROR(x) (x == SOCKET_ERROR)
|
|
|
|
#else
|
|
|
|
// posix platform
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
typedef int SOCKET;
|
|
|
|
typedef void buffer_t;
|
|
|
|
#define SOCKETINVALID(x) (x < 0)
|
|
|
|
#define SOCKETERROR(x) (x == -1)
|
|
|
|
#endif
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <cstring>
|
|
|
|
#include <csignal>
|
|
|
|
#include <list>
|
|
|
|
#include <queue>
|
|
|
|
|
2020-08-23 21:09:31 +00:00
|
|
|
#include "Defines.hpp"
|
|
|
|
#include "settings.hpp"
|
|
|
|
|
2020-08-19 20:42:44 +00:00
|
|
|
#if defined(__MINGW32__) && !defined(_GLIBCXX_HAS_GTHREADS)
|
2020-08-19 00:52:02 +00:00
|
|
|
#include "mingw/mingw.mutex.h"
|
|
|
|
#else
|
|
|
|
#include <mutex>
|
|
|
|
#endif
|
|
|
|
|
2020-08-18 20:42:30 +00:00
|
|
|
/*
|
|
|
|
Packets format (sent from the client):
|
2020-08-19 00:11:31 +00:00
|
|
|
[4 bytes] - size of packet including the 4 byte packet type
|
2020-08-18 20:42:30 +00:00
|
|
|
[size bytes] - Encrypted packet (byte swapped && xor'd with 8 byte key; see CNSocketEncryption)
|
|
|
|
[4 bytes] - packet type (which is a combination of the first 4 bytes of the packet and a checksum in some versions)
|
|
|
|
[structure]
|
|
|
|
*/
|
|
|
|
|
|
|
|
// error checking calloc wrapper
|
|
|
|
inline void* xmalloc(size_t sz) {
|
|
|
|
void* res = calloc(1, sz);
|
|
|
|
|
|
|
|
if (res == NULL) {
|
|
|
|
std::cerr << "[FATAL] OpenFusion: calloc failed to allocate memory!" << std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace CNSocketEncryption {
|
|
|
|
// you won't believe how complicated they made it in the client :facepalm:
|
|
|
|
static constexpr const char* defaultKey = "m@rQn~W#";
|
|
|
|
static const unsigned int keyLength = 8;
|
|
|
|
|
|
|
|
int Encrypt_byte_change_A(int ERSize, uint8_t* data, int size);
|
|
|
|
int xorData(uint8_t* buffer, uint8_t* key, int size);
|
|
|
|
uint64_t createNewKey(uint64_t uTime, int32_t iv1, int32_t iv2);
|
|
|
|
int encryptData(uint8_t* buffer, uint8_t* key, int size);
|
|
|
|
int decryptData(uint8_t* buffer, uint8_t* key, int size);
|
|
|
|
}
|
|
|
|
|
2020-08-22 23:31:09 +00:00
|
|
|
struct CNPacketData {
|
2020-08-18 20:42:30 +00:00
|
|
|
void* buf;
|
|
|
|
int size;
|
|
|
|
uint32_t type;
|
|
|
|
|
2020-08-22 23:31:09 +00:00
|
|
|
CNPacketData(void* b, uint32_t t, int l);
|
|
|
|
};
|
|
|
|
|
|
|
|
enum ACTIVEKEY {
|
|
|
|
SOCKETKEY_E,
|
|
|
|
SOCKETKEY_FE
|
2020-08-18 20:42:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class CNSocket;
|
|
|
|
typedef void (*PacketHandler)(CNSocket* sock, CNPacketData* data);
|
|
|
|
|
|
|
|
class CNSocket {
|
|
|
|
private:
|
|
|
|
uint64_t EKey;
|
|
|
|
uint64_t FEKey;
|
|
|
|
int32_t readSize = 0;
|
|
|
|
uint8_t* readBuffer = new uint8_t[MAX_PACKETSIZE];
|
|
|
|
int readBufferIndex = 0;
|
|
|
|
bool activelyReading = false;
|
|
|
|
bool alive = true;
|
|
|
|
|
2020-08-22 23:31:09 +00:00
|
|
|
ACTIVEKEY activeKey;
|
|
|
|
|
2020-08-18 20:42:30 +00:00
|
|
|
bool sendData(uint8_t* data, int size);
|
|
|
|
|
|
|
|
public:
|
|
|
|
SOCKET sock;
|
|
|
|
PacketHandler pHandler;
|
|
|
|
|
|
|
|
CNSocket(SOCKET s, PacketHandler ph);
|
|
|
|
|
|
|
|
void setEKey(uint64_t k);
|
|
|
|
void setFEKey(uint64_t k);
|
|
|
|
uint64_t getEKey();
|
|
|
|
uint64_t getFEKey();
|
2020-08-22 23:31:09 +00:00
|
|
|
void setActiveKey(ACTIVEKEY t);
|
2020-08-18 20:42:30 +00:00
|
|
|
|
|
|
|
void kill();
|
2020-08-22 23:31:09 +00:00
|
|
|
void sendPacket(void* buf, uint32_t packetType, size_t size);
|
2020-08-18 20:42:30 +00:00
|
|
|
void step();
|
|
|
|
bool isAlive();
|
|
|
|
};
|
|
|
|
|
|
|
|
// in charge of accepting new connections and making sure each connection is kept alive
|
|
|
|
class CNServer {
|
|
|
|
protected:
|
|
|
|
std::list<CNSocket*> connections;
|
2020-08-19 01:34:39 +00:00
|
|
|
std::mutex activeCrit;
|
2020-08-18 20:42:30 +00:00
|
|
|
|
|
|
|
SOCKET sock;
|
|
|
|
uint16_t port;
|
|
|
|
socklen_t addressSize;
|
|
|
|
struct sockaddr_in address;
|
|
|
|
void init();
|
|
|
|
|
2020-08-19 00:52:02 +00:00
|
|
|
bool active = true;
|
2020-08-22 18:38:27 +00:00
|
|
|
uint64_t lastTimer;
|
2020-08-19 00:52:02 +00:00
|
|
|
|
2020-08-18 20:42:30 +00:00
|
|
|
public:
|
|
|
|
PacketHandler pHandler;
|
|
|
|
|
|
|
|
CNServer();
|
|
|
|
CNServer(uint16_t p);
|
|
|
|
|
|
|
|
void start();
|
2020-08-19 00:52:02 +00:00
|
|
|
void kill();
|
2020-08-23 21:09:31 +00:00
|
|
|
static void printPacket(CNPacketData *data, int type);
|
2020-08-22 23:31:09 +00:00
|
|
|
virtual void newConnection(CNSocket* cns);
|
2020-08-18 20:42:30 +00:00
|
|
|
virtual void killConnection(CNSocket* cns);
|
2020-08-19 01:34:39 +00:00
|
|
|
virtual void onTimer(); // called every 2 seconds
|
2020-08-18 20:42:30 +00:00
|
|
|
};
|