fix:客户端第一个命令判断进入

This commit is contained in:
tomyee 2024-12-18 13:05:31 +08:00
parent 23987b5975
commit 1cee5d570f
22 changed files with 170 additions and 125 deletions

3
.gitignore vendored
View File

@ -49,3 +49,6 @@
/out/*
*.plist
./files/*
./newfile/*

View File

@ -15,7 +15,21 @@ gcc -o server server.c -lpthread -L/usr/local/lib -I/usr/local/include -lgmssl -
gcc -o client client.c
~~~
启动测试:
1、启动服务端./server
2、启动客户端./client
3、客户端根据提示输入命令及内容
3、客户端根据提示输入命令及内容
启动报错处理
tong ~/Tomyee/money/linux/code (master●●●)$ ./server
dyld[90941]: Library not loaded: @rpath/libgmssl.3.dylib
Referenced from: <E7AE7786-E167-3C49-96A1-C5BE4CE5147D> /Users/tong/Tomyee/money/linux/code/server
Reason: no LC_RPATH's found
[1] 90941 abort ./server
临时办法export DYLD_LIBRARY_PATH=/usr/local/lib:$DYLD_LIBRARY_PATH

BIN
client

Binary file not shown.

View File

@ -13,6 +13,10 @@
void upload_file(int client_socket)
{
//发送通知服务器是上传文件
send(client_socket, "upload", strlen("upload"), 0);
char file_name[BUFFER_SIZE];
char file_content[BUFFER_SIZE];
mkdir("./newfile", 0777);
@ -53,8 +57,8 @@ void upload_file(int client_socket)
}
fprintf(plain_file, "%s", file_content);
fclose(plain_file);
printf("文件创建成功,开始上传。\n");
printf("文件创建成功,准备上传。\n");
// 发送文件名(不含后缀)到服务器
send(client_socket, base_name, strlen(base_name), 0);
// 等待服务器确认收到文件名
@ -92,19 +96,28 @@ void upload_file(int client_socket)
// 发送文件传输结束标志
send(client_socket, "EOF", strlen("EOF"), 0);
printf("文件已上传。\n");
// 等待服务器发送加密压缩包的路径
char compressed_path[BUFFER_SIZE] = {0};
ssize_t bytes_received = read(client_socket, compressed_path, BUFFER_SIZE - 1);
if (bytes_received > 0) {
compressed_path[bytes_received] = '\0'; // 确保字符串以空字符结尾
printf("服务器发送的加密压缩包路径是: %s\n", compressed_path);
} else {
perror("读取服务器发送的路径失败");
}
}
void download_file(int client_socket)
{
char output_file_name[BUFFER_SIZE];
char file_name[BUFFER_SIZE];
printf("输入要下载的文件名(不带路径): ");
scanf("%s", file_name);
char file_path[BUFFER_SIZE];
printf("输入要下载的文件(加密后的文件压缩包)完整路径: ");
scanf("%s", file_path);
// 发送下载请求到服务器
send(client_socket, "DOWNLOAD", strlen("DOWNLOAD"), 0);
send(client_socket, file_name, strlen(file_name), 0);
send(client_socket, "download", strlen("download"), 0);
send(client_socket, file_path, strlen(file_path), 0);
// 等待服务端准备完成的信号
char ack[BUFFER_SIZE] = {0};
int ack_read = read(client_socket, ack, BUFFER_SIZE);
@ -119,8 +132,8 @@ void download_file(int client_socket)
return;
}
// 创建输出文件
sprintf(output_file_name, "%s_output.txt", file_name);
FILE *output = fopen(output_file_name, "wb");
FILE *output = fopen("./output.txt", "wb");
if (!output)
{
perror("文件打开失败");
@ -134,7 +147,7 @@ void download_file(int client_socket)
fwrite(buffer, 1, bytes_read, output);
}
fclose(output);
printf("文件已下载,保存在 %s 中。\n", output_file_name);
printf("文件已下载,保存在当前目录下的output.txt中。\n");
}
int main()
@ -171,18 +184,18 @@ int main()
char command[10];
while (1)
{
printf("请输入命令 (UPLOAD/DOWNLOAD/EXIT): ");
printf("请输入命令 (upload/download/exit): ");
scanf("%s", command);
if (strcmp(command, "UPLOAD") == 0)
if (strcmp(command, "upload") == 0)
{
upload_file(client_socket);
}
else if (strcmp(command, "DOWNLOAD") == 0)
else if (strcmp(command, "download") == 0)
{
download_file(client_socket);
}
else if (strcmp(command, "EXIT") == 0)
else if (strcmp(command, "exit") == 0)
{
break;
}

View File

@ -1 +0,0 @@
sÓÍQÒ÷W%ƒ¡ßKó)$ýí´o&TI…ùÍwɽ5·m=Öýø3oØßßð¯šg

Binary file not shown.

View File

@ -1,2 +0,0 @@
123
EOFDOWNLOADtest

View File

@ -1 +0,0 @@
№в;;ўXµg:•ЇL@зA

Binary file not shown.

View File

@ -1 +0,0 @@
321

View File

@ -1,2 +0,0 @@
123
EOF

View File

@ -1,2 +0,0 @@
123
EOFDOWNLOAD123_decrypted.txt

2
gcc.sh Executable file
View File

@ -0,0 +1,2 @@
gcc -o server server.c -lpthread -L/usr/local/lib -I/usr/local/include -lgmssl -lz
gcc -o client client.c

1
newfile/1234.txt Normal file
View File

@ -0,0 +1 @@
1233

1
newfile/123455.txt Normal file
View File

@ -0,0 +1 @@
1234

BIN
server

Binary file not shown.

220
server.c
View File

@ -20,7 +20,9 @@
#define PORT 8001
#define MAX_CLIENTS 5
#define BUFFER_SIZE 1024
// 密钥和偏移量
uint8_t key[SM4_KEY_SIZE];
uint8_t iv[SM4_BLOCK_SIZE];
// 将信号量定义为全局变量
dispatch_semaphore_t semaphore;
int log_pipe[2]; // 日志记录的管道
@ -41,6 +43,8 @@ void create_timestamp_dir(char *dir_name)
t->tm_hour, t->tm_min, t->tm_sec);
mkdir("./files", 0777);
mkdir(dir_name, 0777);
// 创建解密结果文件目录
mkdir("./files/decrypt", 0777);
}
// 记录日志
@ -197,131 +201,118 @@ void *client_handler(void *arg)
// 初始化缓冲区为 0
memset(buffer, 0, BUFFER_SIZE);
// 接收文件名
int bytes = read(client_socket, buffer, BUFFER_SIZE);
// 接收第一个参数判断参数客户端需要上传还是下载
int bytes = read(client_socket, buffer, BUFFER_SIZE - 1);// 预留一个字节用于NULL终止符
if (bytes <= 0)
{
perror("接收文件名失败");
perror("接收客户端命令失败");
return NULL;
}
buffer[bytes] = '\0'; // 确保以 NULL 结尾
char file_name[256];
snprintf(file_name, sizeof(file_name), "%s", buffer);
printf("接收文件名:%s\n", file_name);
// 发送 ACK 确认
send(client_socket, "ACK", strlen("ACK"), 0);
char upload_or_download[256] ;
snprintf(upload_or_download, sizeof(upload_or_download), "%s", buffer);
printf("客户端发起请求:%s,等待后续指令....\n", upload_or_download);
char origin_file_path[256], dir_name[128], file_path[256], compressed_file[256], decrypted_file[256];
// 先生成./files目录再生成时间戳目录调用 create_timestamp_dir 函数,生成一个基于时间戳的目录名称,并将其存储在 dir_name 中
create_timestamp_dir(dir_name);
// 根据传入的文件名以及时间戳拼接生成源文件路径、加密后的文件、压缩的文件、解密的文件路径
sprintf(origin_file_path, "%s/%s.txt", dir_name, buffer);
sprintf(file_path, "%s/%s.enc", dir_name, buffer);
sprintf(compressed_file, "%s/%s.gz", dir_name, buffer);
// sprintf(decrypted_file, sizeof(decrypted_file), "%s/%s_decrypted.txt", dir_name, buffer);
snprintf(decrypted_file, sizeof(decrypted_file), "%s/%s_decrypted.txt", dir_name, buffer);
printf("源文件路径%s\n加密后的文件%s\n压缩的文件%s\n解密的文件路径%s\n", origin_file_path, file_path, compressed_file, decrypted_file);
FILE *file = fopen(origin_file_path, "wb");
if (!file)
{
perror("文件打开失败");
return NULL;
}
// 按块读取源文件内容并保存到origin_file_path
while (1)
if (strcmp(upload_or_download, "upload") == 0)
{
// 接收文件名(不含后缀)
int bytes = read(client_socket, buffer, BUFFER_SIZE);
if (bytes < 0)
if (bytes <= 0)
{
perror("读取数据失败");
fclose(file);
perror("接收文件名失败");
return NULL;
}
// 检测是否接收到结束标志
if (strncmp(buffer, "EOF", 3) == 0)
break;
// 将数据写入文件
if (fwrite(buffer, 1, bytes, file) != bytes)
buffer[bytes] = '\0'; // 确保以 NULL 结尾
char file_name[256];
snprintf(file_name, sizeof(file_name), "%s", buffer);
printf("接收文件名:%s\n", file_name);
// 发送 ACK 确认
send(client_socket, "ACK", strlen("ACK"), 0);
// 先生成./files目录再生成时间戳目录调用 create_timestamp_dir 函数,生成一个基于时间戳的目录名称,并将其存储在 dir_name 中
create_timestamp_dir(dir_name);
// 根据传入的文件名以及时间戳拼接生成源文件路径
sprintf(origin_file_path, "%s/%s.txt", dir_name, buffer);
// 加密后的文件路径
sprintf(file_path, "%s/%s.enc", dir_name, buffer);
// 加密后压缩包的文件路径
sprintf(compressed_file, "%s/%s.gz", dir_name, buffer);
// 解密的文件路径
snprintf(decrypted_file, sizeof(decrypted_file), "./files/decrypt/%s_decrypted.txt", buffer);
printf("源文件路径:%s\n加密后的文件:%s\n压缩的文件:%s\n解密的文件路径:%s\n", origin_file_path, file_path, compressed_file, decrypted_file);
FILE *file = fopen(origin_file_path, "wb");
if (!file)
{
perror("写入文件失败");
fclose(file);
perror("文件打开失败");
return NULL;
}
// 按块读取源文件内容并保存到origin_file_path
while (1)
{
int bytes = read(client_socket, buffer, BUFFER_SIZE -1);
if (bytes < 0)
{
perror("读取数据失败");
fclose(file);
return NULL;
}
// 检测是否接收到结束标志
if (strncmp(buffer, "EOF", 3) == 0)
break;
// 将数据写入文件
if (fwrite(buffer, 1, bytes, file) != bytes)
{
perror("写入文件失败");
fclose(file);
return NULL;
}
}
fclose(file);
log_operation("文件已接收。\n");
// 加密文件
printf("正在加密文件...\n");
sm4_cbc_padding_encrypt_file(origin_file_path, file_path, key, iv);
printf("文件加密完成。\n");
char log_message_file_path[BUFFER_SIZE];
sprintf(log_message_file_path, "文件已加密。加密文件保存路径为:%s\n", file_path);
log_operation(log_message_file_path);
// 压缩文件
printf("正在压缩被加密的文件...\n");
compress_file(file_path, compressed_file);
printf("文件压缩完成。\n");
// 删除加密文件
// remove(file_path);
char log_message_compressed_file[BUFFER_SIZE];
sprintf(log_message_compressed_file, "文件已加密。加密文件保存路径为:%s\n", compressed_file);
// 发送压缩的加密文件路径给客户端
send(client_socket, compressed_file, strlen(compressed_file), 0);
log_operation(log_message_compressed_file);
}
fclose(file);
log_operation("文件已接收。\n");
// 随机生成加密密钥和 IV
uint8_t key[SM4_KEY_SIZE];
if (rand_bytes(key, SM4_KEY_SIZE) != 1)
{
fprintf(stderr, "随机密钥生成失败。\n");
return NULL;
}
uint8_t iv[SM4_BLOCK_SIZE];
if (rand_bytes(iv, SM4_BLOCK_SIZE) != 1)
{
fprintf(stderr, "随机IV生成失败。\n");
return NULL;
}
// 打印密钥和 IV
printf("随机生成的密钥: ");
for (size_t i = 0; i < SM4_KEY_SIZE; i++)
{
printf("%02X ", key[i]);
}
printf("\n");
printf("随机生成的IV: ");
for (size_t i = 0; i < SM4_BLOCK_SIZE; i++)
{
printf("%02X ", iv[i]);
}
printf("\n");
// 加密文件
printf("正在加密文件...\n");
sm4_cbc_padding_encrypt_file(origin_file_path, file_path, key, iv);
printf("文件加密完成。\n");
char log_message_file_path[BUFFER_SIZE];
sprintf(log_message_file_path, "文件已加密。加密文件保存路径为:%s\n", file_path);
log_operation(log_message_file_path);
// 压缩文件
printf("正在压缩被加密的文件...\n");
compress_file(file_path, compressed_file);
printf("文件压缩完成。\n");
// 删除加密文件
// remove(file_path);
char log_message_compressed_file[BUFFER_SIZE];
sprintf(log_message_compressed_file, "文件已加密。加密文件保存路径为:%s\n", compressed_file);
log_operation(log_message_compressed_file);
// 等待客户端请求
memset(buffer, 0, BUFFER_SIZE);
read(client_socket, buffer, BUFFER_SIZE);
if (strcmp(buffer, "DOWNLOAD") == 0)
else if (strcmp(upload_or_download, "download") == 0)
{
printf("接收到下载请求...\n");
// 读取文件名
memset(buffer, 0, BUFFER_SIZE);
// 读取文件路径
read(client_socket, buffer, BUFFER_SIZE);
char requested_file[256];
snprintf(requested_file, sizeof(requested_file), "%s", buffer);
printf("客户端请求的文件名: %s\n", requested_file);
printf("接收到下载请求,正在解压缩文件...\n");
decompress_file(compressed_file, decrypted_file);
sprintf(compressed_file, "%s", buffer);
// 解压缩、解密并发送给客户端
decompress_file(compressed_file, compressed_file);
printf("文件解压缩完成。\n");
printf("正在解密文件...\n");
sm4_cbc_padding_decrypt_file(decrypted_file, decrypted_file, key, iv);
printf("文件解密完成。\n");
log_operation("文件已解压并解密,准备发送。\n");
// 向客户端发送 "READY" 信号
send(client_socket, "READY", strlen("READY"), 0);
@ -339,7 +330,10 @@ void *client_handler(void *arg)
// 删除解密后的文件
// remove(decrypted_file);
}
else
{
perror("读取客户端命令失败");
}
// 关闭连接
close(client_socket);
dispatch_semaphore_signal(semaphore);
@ -390,6 +384,32 @@ int main()
return -1;
}
// 随机生成加密密钥和 IV
if (rand_bytes(key, SM4_KEY_SIZE) != 1)
{
fprintf(stderr, "随机密钥生成失败。\n");
return -1;
}
if (rand_bytes(iv, SM4_BLOCK_SIZE) != 1)
{
fprintf(stderr, "随机IV生成失败。\n");
return -1;
}
// 打印密钥和 IV
printf("随机生成的密钥: ");
for (size_t i = 0; i < SM4_KEY_SIZE; i++)
{
printf("%02X ", key[i]);
}
printf("\n");
printf("随机生成的IV: ");
for (size_t i = 0; i < SM4_BLOCK_SIZE; i++)
{
printf("%02X ", iv[i]);
}
printf("\n");
// 示例:客户端处理逻辑
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); // 等待信号量
// printf("客户端进入,当前资源被占用\n");