Compare commits
7 Commits
97248c3772
...
429e0cfeaa
Author | SHA1 | Date | |
---|---|---|---|
429e0cfeaa | |||
01455eab12 | |||
05ed50ddfd | |||
fa6826ca05 | |||
ffcdfa0657 | |||
7fe2172ce9 | |||
6daf74d0be |
@ -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);
|
||||
|
||||
|
@ -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
|
@ -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;
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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, ¤t_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
|
||||
|
Loading…
x
Reference in New Issue
Block a user