mirror of
				https://github.com/wangyu-/udp2raw.git
				synced 2025-10-31 02:05:34 +08:00 
			
		
		
		
	log macro to print file name and line number
This commit is contained in:
		
							
								
								
									
										24
									
								
								encrypt.cpp
									
									
									
									
									
								
							
							
						
						
									
										24
									
								
								encrypt.cpp
									
									
									
									
									
								
							| @@ -6,6 +6,8 @@ | |||||||
| #include <stdio.h> | #include <stdio.h> | ||||||
| #include <encrypt.h> | #include <encrypt.h> | ||||||
|  |  | ||||||
|  | #include "log.h" | ||||||
|  |  | ||||||
| //static uint64_t seq=1; | //static uint64_t seq=1; | ||||||
|  |  | ||||||
| static int8_t zero_iv[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,   0,0,0,0};//this prog use zero iv,you should make sure first block of data contains a random/nonce data | static int8_t zero_iv[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,   0,0,0,0};//this prog use zero iv,you should make sure first block of data contains a random/nonce data | ||||||
| @@ -18,6 +20,10 @@ static const int disable_aes=0; | |||||||
| int auth_mode=auth_md5; | int auth_mode=auth_md5; | ||||||
| int cipher_mode=cipher_aes128cbc; | int cipher_mode=cipher_aes128cbc; | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
| //int auth(uint8_t *data,) | //int auth(uint8_t *data,) | ||||||
| /* | /* | ||||||
| int my_encrypt(uint8_t *data,uint8_t *output,int &len,uint8_t * key) | int my_encrypt(uint8_t *data,uint8_t *output,int &len,uint8_t * key) | ||||||
| @@ -48,6 +54,7 @@ int auth_md5_verify(const char *data,int &len) | |||||||
| { | { | ||||||
| 	if(len<16) | 	if(len<16) | ||||||
| 	{ | 	{ | ||||||
|  | 		log(log_trace,"auth_md5_verify len<16\n"); | ||||||
| 		return -1; | 		return -1; | ||||||
| 	} | 	} | ||||||
| 	char md5_res[16]; | 	char md5_res[16]; | ||||||
| @@ -56,6 +63,7 @@ int auth_md5_verify(const char *data,int &len) | |||||||
|  |  | ||||||
| 	if(memcmp(md5_res,data+len-16,16)!=0) | 	if(memcmp(md5_res,data+len-16,16)!=0) | ||||||
| 	{ | 	{ | ||||||
|  | 		log(log_trace,"auth_md5_verify md5 check failed\n"); | ||||||
| 		return -2; | 		return -2; | ||||||
| 	} | 	} | ||||||
| 	len-=16; | 	len-=16; | ||||||
| @@ -91,8 +99,8 @@ int cipher_none_encrypt(const char *data,char *output,int &len,char * key) | |||||||
| } | } | ||||||
| int cipher_aes128cbc_decrypt(const char *data,char *output,int &len,char * key) | int cipher_aes128cbc_decrypt(const char *data,char *output,int &len,char * key) | ||||||
| { | { | ||||||
| 	if(len%16 !=0) return -1; | 	if(len%16 !=0) {log(log_trace,"len%16!=0");return -1;} | ||||||
| 	if(len<2) return -1; | 	if(len<2) {log(log_trace,"len <2 ");return -1;}return -1; | ||||||
| 	AES_CBC_decrypt_buffer((unsigned char *)output,(unsigned char *)data,len,(unsigned char *)key,(unsigned char *)zero_iv); | 	AES_CBC_decrypt_buffer((unsigned char *)output,(unsigned char *)data,len,(unsigned char *)key,(unsigned char *)zero_iv); | ||||||
| 	len=((unsigned char)output[len-2])*256u+((unsigned char)output[len-1]); | 	len=((unsigned char)output[len-2])*256u+((unsigned char)output[len-1]); | ||||||
| 	return 0; | 	return 0; | ||||||
| @@ -135,14 +143,14 @@ int cipher_decrypt(const char *data,char *output,int &len,char * key) | |||||||
|  |  | ||||||
| int my_encrypt(const char *data,char *output,int &len,char * key) | int my_encrypt(const char *data,char *output,int &len,char * key) | ||||||
| { | { | ||||||
| 	if(len<0) return -1; | 	if(len<0) {log(log_trace,"len<0");return -1;} | ||||||
| 	if(len>65535) return -1; | 	if(len>65535) {log(log_trace,"len>65535");return -1;} | ||||||
|  |  | ||||||
| 	char buf[65535+100]; | 	char buf[65535+100]; | ||||||
| 	char buf2[65535+100]; | 	char buf2[65535+100]; | ||||||
| 	memcpy(buf,data,len); | 	memcpy(buf,data,len); | ||||||
| 	if(auth_cal(buf,buf2,len)!=0) {return -1;} | 	if(auth_cal(buf,buf2,len)!=0) {log(log_trace,"auth_cal failed ");return -1;} | ||||||
| 	if(cipher_encrypt(buf2,output,len,key) !=0) {return -1;} | 	if(cipher_encrypt(buf2,output,len,key) !=0) {log(log_trace,"auth_cal failed ");return -1;} | ||||||
| 	return 0; | 	return 0; | ||||||
|  |  | ||||||
| } | } | ||||||
| @@ -151,8 +159,8 @@ int my_decrypt(const char *data,char *output,int &len,char * key) | |||||||
| 	if(len<0) return -1; | 	if(len<0) return -1; | ||||||
| 	if(len>65535) return -1; | 	if(len>65535) return -1; | ||||||
|  |  | ||||||
| 	if(cipher_decrypt(data,output,len,key) !=0) {return -1;} | 	if(cipher_decrypt(data,output,len,key) !=0) {log(log_trace,"cipher_decrypt failed "); return -1;} | ||||||
| 	if(auth_verify(output,len)!=0) {return -1;} | 	if(auth_verify(output,len)!=0) {log(log_trace,"auth_verify failed ");return -1;} | ||||||
|  |  | ||||||
| 	return 0; | 	return 0; | ||||||
| } | } | ||||||
|   | |||||||
| @@ -12,6 +12,10 @@ int my_decrypt(const char *data,char *output,int &len,char * key); | |||||||
| int my_encrypt_pesudo_header(uint8_t *data,uint8_t *output,int &len,uint8_t * key,uint8_t *header,int hlen); | int my_encrypt_pesudo_header(uint8_t *data,uint8_t *output,int &len,uint8_t * key,uint8_t *header,int hlen); | ||||||
| int my_decrypt_pesudo_header(uint8_t *data,uint8_t *output,int &len,uint8_t * key,uint8_t *header,int hlen); | int my_decrypt_pesudo_header(uint8_t *data,uint8_t *output,int &len,uint8_t * key,uint8_t *header,int hlen); | ||||||
|  |  | ||||||
|  |  | ||||||
|  | unsigned short csum(const unsigned short *ptr,int nbytes) ; | ||||||
|  |  | ||||||
|  |  | ||||||
| const int auth_none=0; | const int auth_none=0; | ||||||
| const int auth_md5=1; | const int auth_md5=1; | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										12
									
								
								log.cpp
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								log.cpp
									
									
									
									
									
								
							| @@ -1,10 +1,11 @@ | |||||||
| #include <log.h> | #include <log.h> | ||||||
|  |  | ||||||
| const int log_level=log_debug; | const int log_level=log_debug; | ||||||
|  | int enable_log_position=1; | ||||||
|  | int enable_log_color=1; | ||||||
| char log_text[][10]={"FATAL","ERROR","WARN","INFO","DEBUG","TRACE"}; | char log_text[][10]={"FATAL","ERROR","WARN","INFO","DEBUG","TRACE"}; | ||||||
| char log_color[][10]={RED,RED,YEL,GRN,BLU,""}; | char log_color[][10]={RED,RED,YEL,GRN,BLU,""}; | ||||||
| void log(int level,const char* str, ...) { | void log0(const char * file,const char * function,int line,int level,const char* str, ...) { | ||||||
|  |  | ||||||
| 	if(level>log_level) return ; | 	if(level>log_level) return ; | ||||||
| 	if(level>log_trace||level<0) return ; | 	if(level>log_trace||level<0) return ; | ||||||
| @@ -17,17 +18,20 @@ void log(int level,const char* str, ...) { | |||||||
| 	time(&timer); | 	time(&timer); | ||||||
| 	tm_info = localtime(&timer); | 	tm_info = localtime(&timer); | ||||||
|  |  | ||||||
|  | 	if(enable_log_color) | ||||||
| 	printf(log_color[level]); | 	printf(log_color[level]); | ||||||
|  |  | ||||||
| 	strftime(buffer, 100, "%Y-%m-%d %H:%M:%S", tm_info); | 	strftime(buffer, 100, "%Y-%m-%d %H:%M:%S", tm_info); | ||||||
| 	printf("[%s][%s]",buffer,log_text[level]); | 	printf("[%s][%s]",buffer,log_text[level],file,line); | ||||||
|  |  | ||||||
|  | 	if(enable_log_position)printf("[%s,func:%s,line:%d]",file,function,line); | ||||||
|  |  | ||||||
| 	va_list vlist; | 	va_list vlist; | ||||||
| 	va_start(vlist, str); | 	va_start(vlist, str); | ||||||
| 	vfprintf(stdout, str, vlist); | 	vfprintf(stdout, str, vlist); | ||||||
| 	va_end(vlist); | 	va_end(vlist); | ||||||
| 	printf(RESET); | 	if(enable_log_color) | ||||||
|  | 		printf(RESET); | ||||||
| 	//printf("\n"); | 	//printf("\n"); | ||||||
| 	fflush(stdout); | 	fflush(stdout); | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										4
									
								
								log.h
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								log.h
									
									
									
									
									
								
							| @@ -73,9 +73,9 @@ const int log_debug=4; | |||||||
| const int log_trace=5; | const int log_trace=5; | ||||||
|  |  | ||||||
|  |  | ||||||
|  | #define log(...) log0(__FILE__,__FUNCTION__,__LINE__,__VA_ARGS__) | ||||||
|  |  | ||||||
|  | void log0(const char * file,const char * function,int line,int level,const char* str, ...); | ||||||
| void log(int level,const char* str, ...); |  | ||||||
|  |  | ||||||
|  |  | ||||||
| #endif | #endif | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user