Compare commits

...

7 Commits

Author SHA1 Message Date
429e0cfeaa
implemented message delays in read_socket_raw
All checks were successful
Codacy Scan / Codacy Security Scan (push) Successful in 24s
Build WHY2-chat / test-why2 (./out/why2-chat-client, ./configure.sh, ubuntu-latest, ./out/why2-chat-server) (push) Successful in 2m21s
Test Project / test-project (./configure.sh, gdb -ex "run" -ex "quit" --batch, ubuntu-latest, ./test) (push) Successful in 2m26s
Test WHY2-core / test-why2 (why2, ./configure.sh, gdb -ex "run" -ex "quit" --batch, ubuntu-latest, ./out/why2-core-test, valgrind --leak-check=full --show-leak-kinds=reachable --track-origins=yes -s) (push) Successful in 3m5s
Test WHY2-logger / test-why2 (why2-logger, ./configure.sh, gdb -ex "run" -ex "quit" --batch, ubuntu-latest, ./out/why2-logger-test, valgrind --leak-check=full --show-leak-kinds=reachable --track-origins=yes -s) (push) Successful in 3m17s
2025-02-04 18:34:22 +01:00
01455eab12
created max_message_delay_violations in server cfg 2025-02-04 18:04:49 +01:00
05ed50ddfd
declaring find_connection at the file top 2025-02-03 21:33:17 +01:00
fa6826ca05
using set_server fn in server-side programs 2025-02-03 21:12:10 +01:00
ffcdfa0657
created is_server & set_server functions 2025-02-03 21:11:18 +01:00
7fe2172ce9
created min_message_delay in server cfg 2025-02-03 21:08:36 +01:00
6daf74d0be
removing non-ascii characters at read_socket_raw 2025-02-03 20:33:23 +01:00
6 changed files with 79 additions and 2 deletions

View File

@ -89,6 +89,9 @@ void why2_chat_set_client_server_key(char *key); //SET KEY USED FOR ENCRYPTION B
char *why2_chat_get_client_server_key(void);
void why2_chat_deallocate_client_server_key(void);
void __why2_chat_set_server(why2_bool value); //IF PROGRAM IS SERVER-SIDE
why2_bool __why2_chat_is_server(void); //IF PROGRAM IS SERVER-SIDE
void __why2_set_asking_password(why2_bool value); //IF HASH SHOULD BE SENT INSTEAD OF NORMAL MESSAGE
why2_bool __why2_get_asking_password(void);

View File

@ -26,4 +26,6 @@ min_username_length = 4 # Minimal username length
max_username_tries = 3 # Times asking client for username (if client tries to use invalid username)
max_password_tries = 3 # Same as above but for invalid password
max_message_length = 100 # Maximal message length
max_message_length = 100 # Maximal message length
min_message_delay = 250 # Minimal ms delay between user messages (to prevent spamming)
max_message_delay_violations = 4 # Maximal 'spam' messages that can be sent in row without getting disconnected

View File

@ -25,6 +25,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
why2_bool asking_password = 0;
why2_bool asking_username = 0;
why2_bool is_server = 0;
char *client_server_key = NULL;
void why2_chat_set_client_server_key(char *key)
@ -43,6 +44,16 @@ void why2_chat_deallocate_client_server_key(void)
client_server_key = NULL;
}
void __why2_chat_set_server(why2_bool value)
{
is_server = value;
}
why2_bool __why2_chat_is_server(void)
{
return is_server;
}
void __why2_set_asking_password(why2_bool value)
{
asking_password = value;

View File

@ -35,6 +35,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
int main(void)
{
__why2_chat_set_server(1);
why2_check_version(); //CHECK FOR UPDATES
why2_chat_init_authority(); //CREATE AUTHORITY DIRECTORY
why2_chat_init_keys(); //CREATE ECC KEY

View File

@ -35,6 +35,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
int main(void)
{
__why2_chat_set_server(1);
why2_check_version(); //CHECK FOR UPDATES
why2_chat_init_server_config(); //CREATE server.toml CONFIGURATION
why2_chat_init_keys(); //CREATE ECC KEY

View File

@ -21,6 +21,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
@ -50,6 +51,8 @@ typedef struct _connection_node
char *username;
unsigned long id;
char *key; //EACH USER WILL USE DIFFERENT KEY FOR COMMUNICATION
struct timespec last_message_time; //USED TO PREVENT SPAMMING
unsigned char spam_violations; //SPAM MESSAGES IN ROW
} connection_node_t; //SINGLE LINKED LIST
typedef struct _read_socket_raw_thread_node
@ -67,6 +70,8 @@ enum ENCRYPTION_DECRYPTION
why2_list_t connection_list = WHY2_LIST_EMPTY;
why2_list_t waiting_list = WHY2_LIST_EMPTY;
why2_node_t *find_connection(int connection);
char *get_string_from_json(struct json_object *json, char *string)
{
struct json_object *object;
@ -246,6 +251,55 @@ char *read_socket_raw(int socket, char *key)
recv(socket, wait_buffer, 1, MSG_PEEK);
why2_deallocate(wait_buffer);
//PREVENT FROM SPAMMING
if (__why2_chat_is_server())
{
//VARIABLES
int minimal_delay_ns = server_config_int("min_message_delay") * 1000000;
struct timespec current_time;
why2_node_t *node = find_connection(socket);
connection_node_t *client_node;
if (node != NULL)
{
client_node = (connection_node_t*) node -> value;
//GET TIME
clock_gettime(CLOCK_MONOTONIC, &current_time);
//NOT THE FIRST MESSAGE
if (client_node -> last_message_time.tv_nsec != -1)
{
int64_t difference = (current_time.tv_sec - client_node -> last_message_time.tv_sec) * 1000000000LL + (current_time.tv_nsec - client_node -> last_message_time.tv_nsec);
if (difference < minimal_delay_ns) //USER IS SPAMMING
{
//CHECK IF USER REACHED MAXIMUM VIOLATIONS
if (client_node -> spam_violations > server_config_int("max_message_delay_violations")) return NULL;
//WAIT UNTIL MINIMAL DELAY IS ACHIEVED
struct timespec sleep_time =
{
.tv_sec = (minimal_delay_ns - difference) / 1000000000,
.tv_nsec = (minimal_delay_ns - difference) % 1000000000
};
nanosleep(&sleep_time, NULL); //SLEEP
//ADD VIOLATION
client_node -> spam_violations++;
} else //USER IS NOT SPAMMING
{
//RESET VIOLATIONS
client_node -> spam_violations = 0;
}
}
//WRITE NEW TIME
clock_gettime(CLOCK_MONOTONIC, &client_node -> last_message_time); //GET TIME
}
}
//FIND THE RECEIVED SIZE
ioctl(socket, FIONREAD, &content_size);
@ -275,6 +329,9 @@ char *read_socket_raw(int socket, char *key)
encrypt_decrypt_message(&output, key, DECRYPTION); //DECRYPT
//REMOVE NON-ASCII
remove_non_ascii(&output);
//VALIDATE JSON FORMAT
struct json_object *json = json_tokener_parse(output);
if (json == NULL)
@ -897,7 +954,9 @@ void *why2_communicate_thread(void *arg)
pthread_self(),
why2_strdup(username),
get_latest_id(),
client_server_key
client_server_key,
{ -1, -1 },
0
};
why2_list_push(&connection_list, &node, sizeof(node)); //ADD TO LIST