fix: fixed .env file and parce

This commit is contained in:
AfonsoCMSousa
2026-01-07 19:45:33 +00:00
parent 43827f8f12
commit 4901c4b523
10 changed files with 399 additions and 65 deletions
+58
View File
@@ -0,0 +1,58 @@
#include "file.hpp"
vector<string> read_file(const char *filePath) {
ifstream file(filePath);
if (!file.is_open()) {
return vector<string>();
}
vector<string> lines;
string line;
while (getline(file, line)) {
lines.push_back(line);
}
file.close();
return lines;
}
app_info parce_args(int argc, char *argv[]) {
// example
// ./player_tracker 130 12000 13000
// -id -serverin -serverout
app_info __processed_info;
if (argc <= 1) {
throw invalid_argument("No argc provided");
} else if (argc != 4) {
throw invalid_argument("Invalid number of args provided");
}
__processed_info.app_id = (u_int16_t)atoi(argv[1]);
__processed_info.app_port_in = (u_int16_t)atoi(argv[2]);
__processed_info.app_port_out = (u_int16_t)atoi(argv[3]);
// Parce .env
vector<string> __read_lines = read_file("./.env");
map<string, string> __env_args;
for (size_t i = 0; i < __read_lines.size() - 1; i++) {
string token = __read_lines[i].substr(0, __read_lines[i].find(" = "));
__read_lines[i].erase(0, __read_lines[i].find(" = ") + 3);
__env_args[token] = __read_lines[i];
}
// DEBUG
// for (const auto& [key, value] : __env_args) {
// std::cout << '[' << key << "] = " << value << "; ";
// }
__processed_info.app_api_socket_path = __env_args["API_SOCKET_PATH"].c_str();
__processed_info.max_players = (u_int16_t)atoi(__env_args["MAX_PLAYERS"].c_str());
__processed_info.app_server_out_ip = __env_args["SERVER_OUT_IP"].c_str();
return __processed_info;
}
+2
View File
@@ -24,9 +24,11 @@ int main(int argc, char *argv[]) {
sock.send();
} catch (const runtime_error &e) {
cerr << "Error: " << e.what() << endl;
cout << "Usage: ./PlayerTracker <app_id> <port_in> <port_out>" << endl;
return 1;
}
return 0;
}
+55
View File
@@ -0,0 +1,55 @@
#include "net.hpp"
#include <sys/types.h>
Socket::Socket() {
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
throw runtime_error("Failed to create socket");
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
}
Socket::~Socket() {
close(sock);
}
void Socket::connect(const char *ip, uint16_t port) {
server_addr.sin_port = htons(port);
if (inet_pton(AF_INET, ip, &server_addr.sin_addr) <= 0) {
throw runtime_error("Invalid IP address");
}
}
void Socket::send() {
ssize_t sent_bytes = sendto(sock, &packet_data, sizeof(packet_data), 0,
(struct sockaddr *)&server_addr, sizeof(server_addr));
if (sent_bytes < 0) {
throw runtime_error("Failed to send data");
}
}
void Socket::send(const api_packet &data) {
ssize_t sent_bytes = sendto(sock, &data, sizeof(data), 0,
(struct sockaddr *)&server_addr, sizeof(server_addr));
if (sent_bytes < 0) {
throw runtime_error("Failed to send data");
}
}
void Socket::set_packet(const api_packet &data) {
packet_data = data;
}
api_packet Socket::create_packet(uint8_t tracker_id) {
api_packet pkt;
memset(&pkt, 0, sizeof(pkt));
pkt.message_type = 65; // Handshake message type
pkt.tracker_id = tracker_id;
return pkt;
}
api_packet Socket::get_packet() {
return packet_data;
}
+143
View File
@@ -0,0 +1,143 @@
#include "parcer.h"
#include "server_structs.h"
#include <netinet/in.h>
#include <sys/types.h>
#include <arpa/inet.h>
int ensure(size_t recv_len, size_t offset, size_t need) {
return (offset + need <= recv_len);
}
u_int8_t read_uint8(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok) {
if (!ensure(recv_len, *offset, sizeof(uint8_t))) {
*ok = 0;
return 0;
}
u_int8_t v;
memcpy(&v, buf + *offset, sizeof(v));
*offset += sizeof(v);
return v;
}
u_int16_t read_uint16(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok) {
if (!ensure(recv_len, *offset, sizeof(uint16_t))) {
*ok = 0;
return 0;
}
u_int16_t v;
memcpy(&v, buf + *offset, sizeof(v));
*offset += sizeof(v);
return (u_int16_t)ntohs(v);
}
u_int32_t read_uint32(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok) {
if (!ensure(recv_len, *offset, sizeof(uint32_t))) {
*ok = 0;
return 0;
}
u_int32_t v;
memcpy(&v, buf + *offset, sizeof(v));
*offset += sizeof(v);
return (u_int32_t)ntohl(v);
}
int32_t read_int32(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok) {
if (!ensure(recv_len, *offset, sizeof(int32_t))) {
*ok = 0;
return 0;
}
int32_t v;
memcpy(&v, buf + *offset, sizeof(v));
*offset += sizeof(v);
return (int32_t)ntohl((u_int32_t)v);
}
float read_float(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok) {
if (!ensure(recv_len, *offset, sizeof(float))) {
*ok = 0;
return 0.0f;
}
u_int32_t v_int;
memcpy(&v_int, buf + *offset, sizeof(v_int));
*offset += sizeof(v_int);
v_int = ntohl(v_int);
float v_float;
memcpy(&v_float, &v_int, sizeof(v_float));
return v_float;
}
void read_bytes(const u_int8_t *buf, size_t recv_len, size_t *offset, u_int8_t *out, size_t len, int *ok) {
if (!ensure(recv_len, *offset, len)) {
*ok = 0;
return;
}
memcpy(out, buf + *offset, len);
*offset += len;
}
void read_utf32le_string(const uint8_t *buffer, size_t buf_size, size_t *offset, char *dest, size_t max_len, int *ok) {
size_t i = *offset;
size_t j = 0;
while (i + 3 < buf_size && j < max_len) {
uint32_t codeunit = buffer[i] | (buffer[i + 1] << 8) | (buffer[i + 2] << 16) | (buffer[i + 3] << 24);
if (codeunit == 0) {
i += 4; // termina a string
break;
}
if (codeunit < 0x80) {
dest[j++] = (char)codeunit;
} else {
dest[j++] = '?'; // substitui caracteres fora de ASCII
}
i += 4;
}
dest[j] = '\0';
*offset = i;
*ok = 1;
}
void read_utf16le_string(const uint8_t *buffer, size_t buf_size, size_t *offset, char *dest, size_t max_len, int *ok) {
size_t i = *offset;
size_t j = 0;
while (i + 1 < buf_size && j < max_len - 1) {
uint16_t codeunit = buffer[i] | (buffer[i + 1] << 8);
if (codeunit == 0) {
i += 2; // termina a string
break;
}
if (codeunit < 0x80) {
dest[j++] = (char)codeunit;
} else {
dest[j++] = '?'; // substitui caracteres fora de ASCII
}
i += 2;
}
dest[j] = '\0';
*offset = i;
*ok = 1;
}
void read_string(const u_int8_t *buf, size_t recv_len, size_t *offset, char *out, size_t len, int *ok) {
if (!ensure(recv_len, *offset, len)) {
*ok = 0;
return;
}
for (size_t i = 0; i <= len; i++) {
out[i] = (char)buf[*offset + i];
}
out[len] = '\0'; // Ensure null termination
*offset += len;
*ok = 1;
}
+90
View File
@@ -0,0 +1,90 @@
#include "socket.h"
// =========================
// UDP SOCKET FUCNTIONS
// =========================
// Create a UDP socket
// @return: Socket file descriptor, or -1 on error
int create_udp_socket(void) {
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
perror("[!] socket() failed");
return -1;
}
return sockfd;
}
// Create a UDP socket and connect it to a specific IP and port
// Used for sending data (no bind needed)
// @param ip: Destination IP address as a string
// @return: Socket file descriptor, or -1 on error
int connect_udp_socket(const char *ip, uint16_t port) {
int sockfd = create_udp_socket();
if (sockfd < 0)
return -1;
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(port);
servaddr.sin_addr.s_addr = inet_addr(ip);
if (connect(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
perror("[!] connect() failed");
close(sockfd);
return -1;
}
printf("[+] Connected UDP socket to %s:%d\n", ip, port);
return sockfd;
}
// Create and bind a UDP socket to a local IP and port
// Used for listening for incoming data
// @param ip: Local IP address to bind to
// @param port: Local port to bind to
// @return: Socket file descriptor, or -1 on error
int create_bound_udp_socket(const char *ip, uint16_t port) {
int sockfd = create_udp_socket();
if (sockfd < 0)
return -1;
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(ip);
if (bind(sockfd, (const struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("[!] bind() failed");
close(sockfd);
return -1;
}
printf("[+] Bound UDP socket on %s:%d\n", ip, port);
return sockfd;
}
// Send a UDP message to the specified IP and port
// @param sockfd: Socket file descriptor
// @param message: Message to send
// @param dest_ip: Destination IP address as a string
// @param dest_port: Destination port
// @return: Number of bytes sent, or -1 on error
ssize_t send_udp_message(int sockfd, const char *message, const char *dest_ip, uint16_t dest_port) {
struct sockaddr_in destaddr;
memset(&destaddr, 0, sizeof(destaddr));
destaddr.sin_family = AF_INET;
destaddr.sin_port = htons(dest_port);
destaddr.sin_addr.s_addr = inet_addr(dest_ip);
ssize_t n = sendto(sockfd, message, strlen(message), 0, (const struct sockaddr *)&destaddr, sizeof(destaddr));
if (n < 0) {
perror("[!] sendto() failed");
return -1;
}
printf("[+] Sent %zd bytes to %s:%d\n", n, dest_ip, dest_port);
return n;
}