Files
RSA-Chat/src/c/RSA-Chat.c
T

272 lines
9.4 KiB
C

#include "RSA-Chat.h"
#include "text.c"
#include "file.c"
int listen_mode=1;int send_mode=1;
int stop_flag=0;
struct receive_input{
int sockfd;
EVP_PKEY *key;
struct addrinfo* result;
};
void* receive_f(void* _i){
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
struct receive_input* input = (struct receive_input*)_i;
if(input->sockfd<0||!listen_mode){
return NULL;
}
char* buffer = malloc(BUFFER);
char* filename = malloc(BUFFER);
long file_size=0;
while(1){
memset(buffer,0,BUFFER);
ssize_t received = recvfrom(input->sockfd, buffer, BUFFER, 0,NULL, NULL);
if(received<=0){
perror("receive_f(): recvfrom failed.");
}
char* output = malloc(received);
int size = text_decrypt(input->key,buffer,output);
if(size < 0){
return NULL;
}
char* header = malloc(11);
memcpy(header,output,10);
FILE* write;
if(strcmp(header,"!!!FILE!!!")==0){
strcpy(filename,output+10+sizeof(long));
write = fopen(filename,"a");
memcpy(&file_size,output+10,sizeof(long));
}else if(file_size>0){
fwrite(output,1,size,write);
file_size-=size;
if(file_size==0){
printf("\033[32m已接收文件: %s\033[0m\n",filename);
fclose(write);
}
if(file_size<0){
perror("receive_f(): file receive failed.");
file_size=0;
fclose(write);
}
}else{
output = realloc(output,size);
printf("\033[32m%s\033[0m\n",output);
free(output);
}
free(header);
}
return NULL;
}
enum command_type{help=0,receive,send_c,quit,change,unknown};
enum command_type command(char* _i){
if(strcmp(_i,"h")==0||strcmp(_i,"help")==0||strcmp(_i,"?")==0){
return help;
}else if(strcmp(_i,"r")==0||strcmp(_i,"recv")==0){
return receive;
}else if(strcmp(_i,"s")==0||strcmp(_i,"send")==0){
return send_c;
}else if(strcmp(_i,"q")==0||strcmp(_i,"quit")==0||strcmp(_i,"exit")==0){
return quit;
}else if(strcmp(_i,"c")==0||strcmp(_i,"change")==0){
return change;
}else{
return unknown;
}
}
int main(int argn,char** argc){
if(argn==1){
printf("用法: %s <本地端口> <远程公钥路径>\n",argc[0]);
printf(" %s --offline <远程公钥路径>\n",argc[0]);
return 0;
}
FILE* peer_key_file = fopen(argc[2],"r");
EVP_PKEY* local_key = EVP_PKEY_new();char* local_key_c = malloc(BUFFER);
EVP_PKEY* peer_key = EVP_PKEY_new();char* peer_key_c = malloc(BUFFER);
int peer_bits=0;int local_bits=0;
if(peer_key_file==NULL){
printf("远程公钥加载失败,发送模式将被禁用\n");
send_mode=0;
}else{
size_t peer_key_file_size = fread(peer_key_c,1,BUFFER,peer_key_file);
peer_key_c = realloc(peer_key_c,peer_key_file_size);
BIO* peer_bio = BIO_new_mem_buf(peer_key_c,peer_key_file_size);
PEM_read_bio_PUBKEY(peer_bio,&peer_key,NULL,NULL);
peer_bits = EVP_PKEY_bits(peer_key);
fclose(peer_key_file);
}
FILE* local_key_file = fopen("local.key","r");
if(local_key_file==NULL){
printf("本地私钥加载失败,监听模式将被禁用\n");
listen_mode=0;
}else{
size_t local_key_file_size = fread(local_key_c,1,BUFFER,local_key_file);
local_key_c = realloc(local_key_c,local_key_file_size);
BIO* local_bio = BIO_new_mem_buf(local_key_c,local_key_file_size);
PEM_read_bio_PrivateKey(local_bio,&local_key,NULL,NULL);
local_bits = EVP_PKEY_bits(local_key);
fclose(local_key_file);
}
free(local_key_c);free(peer_key_c);
int sockfd = -1;struct addrinfo* result;
if(strcmp(argc[1],"--offline")!=0){
sockfd = socket(AF_INET6,SOCK_DGRAM,0);
if(sockfd < 0){
perror("套接字创建失败");
return -1;
}
int no = 0;
if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, &no, sizeof(no))) {
perror("Setsockopt IPV6_V6ONLY失败");
close(sockfd);
return -1;
}
unsigned short int port = atoi(argc[1]);
if(port>65535){
perror("错误端口数字");
return -1;
}
struct sockaddr_in6 serverAddr;
serverAddr.sin6_family = AF_INET6;
serverAddr.sin6_port = htons(port);
serverAddr.sin6_addr = in6addr_any;
if (bind(sockfd, &serverAddr, sizeof(serverAddr))) {
perror("bind失败");
close(sockfd);
return 1;
}
struct addrinfo hints={0};
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
printf("UDP套接字已建立在端口%u\n",port);
if(send_mode){
char* buffer = malloc(BUFFER);
printf("请输入 <远程IP> <端口>:\n");
retry:
memset(buffer,0,strlen(buffer));
char peer_port[10]={0};
scanf("%s%s",buffer,peer_port);
if (getaddrinfo(buffer,peer_port, &hints, &result)) {
perror("错误的地址或端口,请重试:\n$ ");
goto retry;
}
free(buffer);
}
}
pthread_t receive_t;struct receive_input receive_i = {sockfd, local_key, result};
pthread_create(&receive_t,NULL,receive_f,&receive_i);
char* buffer = malloc(BUFFER);
char* ioput = malloc(BUFFER);
char input[BUFFER]={0};
printf("初始化成功\n");
while(1){
memset(input,0,BUFFER);
scanf("%s",input);
switch(command(input)){
case receive:
if(!listen_mode){
printf("接收模式已被禁用\n");
break;
}
printf("请粘贴Base64字符串:\n");
scanf("%s",ioput);
base64_decode(ioput,buffer,NULL);
memset(ioput,0,BUFFER);
int size = text_decrypt(local_key,buffer,ioput);
memset(buffer,0,BUFFER);
if(size < 0){
perror("解密失败");
}else{
printf("%s\n",ioput);
}
memset(ioput,0,BUFFER);
break;
case send_c:
if(!send_mode){
printf("发送模式已被禁用\n");
break;
}
printf("请输入要发送的文本或文件路径:\n");
scanf("%s",ioput);
FILE* send_file = fopen(ioput,"r");
if(strlen(ioput)>(peer_bits/8-80)){
perror("Text too long for key of peer.\n");
break;
}
if(send_file!=NULL){
int send_stat = send_file_f(ioput,sockfd,peer_key,result);
fclose(send_file);
if(send_stat!=0){
perror("文件发送失败");
}else{
printf("\033[32m文件发送成功: %s\033[0m\n",ioput);
}
break;
}
size = text_encrypt(peer_key,ioput,buffer,strlen(ioput));
memset(ioput,0,BUFFER);
if(size < 0){
perror("加密失败");
}else{
if(sockfd>0){
ssize_t sent = sendto(sockfd, buffer, size, 0,result->ai_addr, result->ai_addrlen);
if (sent < 0){
perror("sendto失败");
}
}
base64_encode(buffer,size,ioput);
memset(buffer,0,BUFFER);
printf("%s\n",ioput);
memset(ioput,0,BUFFER);
}
break;
case quit:
stop_flag = 1;
close(sockfd);
EVP_PKEY_free(local_key);
EVP_PKEY_free(peer_key);
pthread_cancel(receive_t);
pthread_join(receive_t,NULL);
return 0;
case change:
struct addrinfo hints={0};
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
printf("请输入 <远程IP> <端口>:\n");
memset(buffer,0,strlen(buffer));
char peer_port[10]={0};
while(1){
scanf("%s%s",buffer,peer_port);
if (getaddrinfo(buffer,peer_port, &hints, &result)) {
perror("错误的地址或端口,请重试:\n ");
}else{
break;
}
}
memset(buffer,0,BUFFER);
break;
case help:
default:
printf("命令:\n");
printf(" send/s [文件路径/密文] | 发送密文或文件至远程地址或将密文编码为Base64输出至stdout\n");
printf(" recv/r [Base64密文] | 解密Base64编码的密文并输出至stdout\n");
printf(" change/c [IP] [端口] | 修改远程IP\n");
printf(" help/h/? | 展示当前页面\n");
printf(" q/quit/exit | 退出程序\n");
}
}
stop_flag = 1;
close(sockfd);
EVP_PKEY_free(local_key);
EVP_PKEY_free(peer_key);
pthread_cancel(receive_t);
pthread_join(receive_t,NULL);
return 0;
}