添加与修复

This commit is contained in:
Saturneric 2019-02-26 13:43:52 +08:00
parent ba45facdad
commit 3a8df3e069
6 changed files with 114 additions and 57 deletions

View File

@ -67,9 +67,15 @@ struct encrypt_post{
Byte *buff = nullptr; Byte *buff = nullptr;
// 内容长度 // 内容长度
uint32_t buff_size = 0; uint32_t buff_size = 0;
Document edoc;
bool Parse(string json);
void GetJSON(string &json);
void SetBuff(Byte *buff, uint32_t size); void SetBuff(Byte *buff, uint32_t size);
void FreeBuff(void); void FreeBuff(void);
~encrypt_post(void); ~encrypt_post(void);
void InitNew(uint64_t client_id, Addr t_addr, const char *type);
}; };
//回复数据包 //回复数据包
@ -323,10 +329,12 @@ public:
static void BuildBeatsRawData(raw_data &rwd); static void BuildBeatsRawData(raw_data &rwd);
static void BuildSmallRawData(raw_data &rwd, const char *info); static void BuildSmallRawData(raw_data &rwd, const char *info);
static void Post2SignedRawData(void *buff, uint32_t buff_size, const char *info, aes_key256 &key, raw_data &rw); static void Post2SignedRawData(void *buff, uint32_t buff_size, const char *info, aes_key256 &key, raw_data &rw);
static void Post2SignedRawData(encrypt_post &ecyp, aes_key256 &key, raw_data &rw);
static void SignedRawData2Post(raw_data &rwd, encrypt_post &pst, aes_key256 &key); static void SignedRawData2Post(raw_data &rwd, encrypt_post &pst, aes_key256 &key);
static void Post2Packet(packet &pkt, encrypt_post &pst, aes_key256 &key); static void Post2Packet(packet &pkt, encrypt_post &pst, aes_key256 &key);
static void Packet2Post(packet &pkt, encrypt_post &pst, aes_key256 &key); static void Packet2Post(packet &pkt, encrypt_post &pst, aes_key256 &key);
static void GetPostInfo(packet &pkt, encrypt_post &pst); static void GetPostInfo(packet &pkt, encrypt_post &pst);
static void SendConnectionInfo(SocketTCPClient *pcnt_sock, bool ifshort);
}; };
//通用客户端类 //通用客户端类
@ -335,6 +343,8 @@ class Client{
list<request_listener *> req_lst; list<request_listener *> req_lst;
list<raw_data *> rwd_lst; list<raw_data *> rwd_lst;
list<encrypt_post *> ecryp_lst; list<encrypt_post *> ecryp_lst;
//TCP模式下有效二进制段列表
list<raw_data *> rwd_tcp;
// 回复处理列表 // 回复处理列表
list<respond *> res_lst; list<respond *> res_lst;
// 请求监听端口 // 请求监听端口

View File

@ -34,6 +34,7 @@ void *clientRequestDeamon(void *pvclt){
pthread_exit(NULL); pthread_exit(NULL);
} }
//客户端回复处理守护线程
void *clientRespondDeamon(void *pvclt){ void *clientRespondDeamon(void *pvclt){
clock_thread_info *pclt = (clock_thread_info *) pvclt; clock_thread_info *pclt = (clock_thread_info *) pvclt;
Client *pclient = (Client *) pclt->args; Client *pclient = (Client *) pclt->args;
@ -78,6 +79,7 @@ void *clientRespondDeamon(void *pvclt){
pthread_exit(NULL); pthread_exit(NULL);
} }
//客户端请求监听管理线程
void Client::ProcessRequestListener(void){ void Client::ProcessRequestListener(void){
// 加锁 // 加锁
if (pthread_mutex_lock(&mutex_clt) != 0) throw "lock error"; if (pthread_mutex_lock(&mutex_clt) != 0) throw "lock error";
@ -119,6 +121,7 @@ void Client::ProcessRequestListener(void){
req_lst.remove_if([](auto &preq){return preq->active == false;}); req_lst.remove_if([](auto &preq){return preq->active == false;});
} }
//设置客户端守护时钟
void setClientClock(Client *pclient,int clicks){ void setClientClock(Client *pclient,int clicks){
pthread_mutex_init(&mutex_clt, nullptr); pthread_mutex_init(&mutex_clt, nullptr);
// 注册回复数据接收时钟 // 注册回复数据接收时钟
@ -143,6 +146,7 @@ void setClientClock(Client *pclient,int clicks){
} }
//创建监听结构
void Client::NewRequest(request **ppreq,string send_ip,int send_port,string type, string data, bool if_encrypt){ void Client::NewRequest(request **ppreq,string send_ip,int send_port,string type, string data, bool if_encrypt){
request *pnreq = new request(); request *pnreq = new request();
pnreq->type = type; pnreq->type = type;
@ -154,7 +158,7 @@ void Client::NewRequest(request **ppreq,string send_ip,int send_port,string type
*ppreq = pnreq; *ppreq = pnreq;
} }
//创建新的对服务器的请求 //创建新的对服务器的请求的监听
void Client::NewRequestListener(request *preq, int timeout, void *args, void (*callback)(respond *,void *)){ void Client::NewRequestListener(request *preq, int timeout, void *args, void (*callback)(respond *,void *)){
request_listener *pnrl = new request_listener(); request_listener *pnrl = new request_listener();
packet npkt; packet npkt;
@ -167,9 +171,8 @@ void Client::NewRequestListener(request *preq, int timeout, void *args, void (*c
SQEServer::Request2Packet(npkt, *preq); SQEServer::Request2Packet(npkt, *preq);
Server::Packet2Rawdata(npkt, pnrl->trwd); Server::Packet2Rawdata(npkt, pnrl->trwd);
// 检查请求是否要求加密 //检查请求是否要求加密
if(preq->if_encrypt == true){ if(preq->if_encrypt == true){
Server::EncryptRSARawMsg(pnrl->trwd, sqe_pbc); Server::EncryptRSARawMsg(pnrl->trwd, sqe_pbc);
Server::SignedRawdata(&pnrl->trwd,"RPKT"); Server::SignedRawdata(&pnrl->trwd,"RPKT");
} }

View File

@ -529,11 +529,11 @@ int client(string instruct, vector<string> &configs, vector<string> &lconfigs, v
pid_t fpid = fork(); pid_t fpid = fork();
if(fpid == 0){ if(fpid == 0){
//守护进程 //守护进程
printf("Client Register Deamon Has Been Created."); printf("Client Register Deamon Has Been Created.\n");
nclt.server_cnt = new SocketTCPCServer(9052); nclt.server_cnt = new SocketTCPCServer(9052);
nclt.server_cnt->Listen(); nclt.server_cnt->Listen();
// 获得共享内存地址 //获得共享内存地址
Byte *buff = (Byte *)shmat(shmid, NULL, 0); Byte *buff = (Byte *)shmat(shmid, NULL, 0);
if(shmid == -1){ if(shmid == -1){
printf("SHMAT Failed.\n"); printf("SHMAT Failed.\n");

View File

@ -82,11 +82,14 @@ void *connectionDeamon(void *args){
SocketTCPCServer ntcps; SocketTCPCServer ntcps;
ntcps.SetDataSFD(pcntl->data_sfd); ntcps.SetDataSFD(pcntl->data_sfd);
ntcps.SetClientAddr(pcntl->client_addr); ntcps.SetClientAddr(pcntl->client_addr);
// 获得连接的类型是长链还是断链 // 获得连接的类型是长链还是断链
size = ntcps.RecvRAW_SM(&buff, t_addr); size = ntcps.RecvRAW_SM(&buff, t_addr);
raw_data *pnrwd = new raw_data(); raw_data *pnrwd = new raw_data();
// 长连接还是短连接
// 检测连接是长连接还是短连接
bool if_sm = true; bool if_sm = true;
string dget = "DGET";
if(Server::CheckRawMsg(buff, size)){ if(Server::CheckRawMsg(buff, size)){
Server::ProcessSignedRawMsg(buff, size, *pnrwd); Server::ProcessSignedRawMsg(buff, size, *pnrwd);
if(!memcmp(&pnrwd->info, "LCNT", sizeof(uint32_t))){ if(!memcmp(&pnrwd->info, "LCNT", sizeof(uint32_t))){
@ -96,6 +99,7 @@ void *connectionDeamon(void *args){
else if(!memcmp(&pnrwd->info, "SCNT", sizeof(uint32_t))){ else if(!memcmp(&pnrwd->info, "SCNT", sizeof(uint32_t))){
if_sm = true; if_sm = true;
printf("Short Connection From Server.\n"); printf("Short Connection From Server.\n");
ntcps.SendRespond(dget);
} }
else if(!memcmp(&pnrwd->info, "CNTL", sizeof(uint32_t))){ else if(!memcmp(&pnrwd->info, "CNTL", sizeof(uint32_t))){
if_sm = true; if_sm = true;
@ -105,31 +109,37 @@ void *connectionDeamon(void *args){
pthread_exit(NULL); pthread_exit(NULL);
} }
else{ else{
printf("Connection illegal.\n"); //断开无效连接
printf("Connection Illegal.\n");
delete pnrwd; delete pnrwd;
pthread_exit(NULL); pthread_exit(NULL);
} }
} }
else{ else{
printf("Connection illegal.\n"); printf("Connection Illegal.\n");
delete pnrwd; delete pnrwd;
pthread_exit(NULL); pthread_exit(NULL);
} }
delete pnrwd; delete pnrwd;
while (1) { while (1) {
//区分长连接与短连接
if(if_sm) size = ntcps.RecvRAW(&buff, t_addr); if(if_sm) size = ntcps.RecvRAW(&buff, t_addr);
else size = ntcps.RecvRAW_SM(&buff, t_addr); else size = ntcps.RecvRAW_SM(&buff, t_addr);
raw_data *pnrwd = new raw_data(); raw_data *pnrwd = new raw_data();
packet *nppkt = new packet(); packet *nppkt = new packet();
encrypt_post *pncryp = new encrypt_post(); encrypt_post *pncryp = new encrypt_post();
if(size > 0){ if(size > 0){
printf("MSG: %s\n", buff);
if(Server::CheckRawMsg(buff, size)){ if(Server::CheckRawMsg(buff, size)){
Server::ProcessSignedRawMsg(buff, size, *pnrwd); Server::ProcessSignedRawMsg(buff, size, *pnrwd);
//获得端对端加密报文
if(!memcmp(&pnrwd->info, "ECYP", sizeof(uint32_t))){ if(!memcmp(&pnrwd->info, "ECYP", sizeof(uint32_t))){
printf("Get Encrypt Post.\n");
Server::Rawdata2Packet(*nppkt, *pnrwd); Server::Rawdata2Packet(*nppkt, *pnrwd);
SQEServer::Packet2Post(*nppkt, *pncryp, pcntl->key); SQEServer::Packet2Post(*nppkt, *pncryp, pcntl->key);
//获得注册信息反馈报文
if(!memcmp(&pncryp->type, "JRES", sizeof(uint32_t))){ if(!memcmp(&pncryp->type, "JRES", sizeof(uint32_t))){
string jres_str = string(pncryp->buff,pncryp->buff_size); string jres_str = string(pncryp->buff,pncryp->buff_size);
Document ndoc; Document ndoc;
@ -140,9 +150,11 @@ void *connectionDeamon(void *args){
} }
} }
} }
//心跳连接
else if(!memcmp(&pnrwd->info, "BEAT", sizeof(uint32_t))){ else if(!memcmp(&pnrwd->info, "BEAT", sizeof(uint32_t))){
//printf("Connection Beated.\n"); //printf("Connection Beated.\n");
} }
//管理指令连接
else if(!memcmp(&pnrwd->info, "SCMD", sizeof(uint32_t))){ else if(!memcmp(&pnrwd->info, "SCMD", sizeof(uint32_t))){
// 来自管理员的命令 // 来自管理员的命令

View File

@ -1068,11 +1068,11 @@ void *clientChecker(void *args){
pthread_exit(NULL); pthread_exit(NULL);
} }
//注册客户端通信维持线程
void *clientListener(void *args){ void *clientListener(void *args){
client_listen *pcltl = (client_listen *)args; client_listen *pcltl = (client_listen *)args;
char *buff; char *buff;
Addr taddr; Addr taddr;
printf("Start listening to client.\n");
while(1){ while(1){
// 如果连接断开 // 如果连接断开
if(pcltl->if_connected == false) break; if(pcltl->if_connected == false) break;
@ -1115,6 +1115,44 @@ void *clientListener(void *args){
} }
pthread_exit(NULL); pthread_exit(NULL);
} }
bool resFromClient(SocketTCPClient *pcnt_sock){
char *buff;
Addr taddr;
pcnt_sock->RecvRAW(&buff, taddr);
if (!memcmp(buff, "DGET", sizeof(uint32_t))) {
free(buff);
return true;
}
else {
free(buff);
return false;
}
}
void encrypt_post::InitNew(uint64_t client_id, Addr t_addr, const char * type){
this->client_id = client_id;
this->t_addr = t_addr;
memcpy(&this->type, type, sizeof(uint32_t));
this->p_id = rand64();
}
void SQEServer::Post2SignedRawData(encrypt_post &ecyp, aes_key256 &key, raw_data &rw) {
packet *pnpkt = new packet();
Post2Packet(*pnpkt, ecyp, key);
Packet2Rawdata(*pnpkt, rw);
Server::freePcaketServer(*pnpkt);
delete pnpkt;
Server::SignedRawdata(&rw,"ECYP");
}
void SQEServer::SendConnectionInfo(SocketTCPClient *pcnt_sock, bool ifshort) {
raw_data nsrwd;
//说明连接类型
if(ifshort) SQEServer::BuildSmallRawData(nsrwd, "SCNT");
else SQEServer::BuildSmallRawData(nsrwd, "LCNT");
pcnt_sock->SendRAW(nsrwd.msg, nsrwd.msg_size);
Server::freeRawdataServer(nsrwd);
}
void *clientWaitDeamon(void *pvclt){ void *clientWaitDeamon(void *pvclt){
clock_thread_info *pcti = (clock_thread_info *) pvclt; clock_thread_info *pcti = (clock_thread_info *) pvclt;
@ -1140,39 +1178,43 @@ void *clientWaitDeamon(void *pvclt){
printf("Get Register: %s[%s]\n",pclr->name.data(),pclr->tag.data()); printf("Get Register: %s[%s]\n",pclr->name.data(),pclr->tag.data());
// 注册信息报文 // 注册信息报文
string res_type = "{\"status\":\"ok\",\"passwd\":null}"; string res_type = "{\"status\":\"ok\",\"passwd\":null}";
Document ndoc; encrypt_post *ncryp = new encrypt_post();
ndoc.Parse(res_type.data()); ncryp->Parse(res_type);
uint8_t *ppidx = (uint8_t *)&pclr->passwd; uint8_t *ppidx = (uint8_t *)&pclr->passwd;
ndoc["passwd"].SetArray(); ncryp->edoc["passwd"].SetArray();
Document::AllocatorType &allocator = ndoc.GetAllocator(); Document::AllocatorType &allocator = ncryp->edoc.GetAllocator();
for(int i = 0; i < 8; i++){ for(int i = 0; i < 8; i++){
ndoc["passwd"].PushBack(ppidx[i], allocator); ncryp->edoc["passwd"].PushBack(ppidx[i], allocator);
} }
StringBuffer sb; //初始化端对端加密报文
Writer<StringBuffer> writer(sb); string send_data;
ndoc.Accept(writer); ncryp->GetJSON(send_data);
encrypt_post *ncryp = new encrypt_post();
string send_data = sb.GetString();
// 初始化端对端加密报文 ncryp->InitNew(pclr->client_id, pclr->t_addr, "JRES");
ncryp->SetBuff((Byte *)send_data.data(),(uint32_t)send_data.size()); ncryp->SetBuff((Byte *)send_data.data(),(uint32_t)send_data.size());
ncryp->client_id = pclr->client_id; raw_data *pnrwd = new raw_data();
ncryp->p_id = rand64(); SQEServer::Post2SignedRawData(*ncryp, pclr->key, *pnrwd);
ncryp->t_addr = pclr->t_addr;
memcpy(&ncryp->type, "JRES", sizeof(uint32_t)); //发送连接属性信息
SQEServer::SendConnectionInfo(pcnt_sock);
packet *nppkt = new packet();
SQEServer::Post2Packet(*nppkt, *ncryp, pclr->key); if (resFromClient(pcnt_sock)) {
raw_data *pnrwd = new raw_data(), nsrwd; pcnt_sock->SendRAW(pnrwd->msg, pnrwd->msg_size);
Server::Packet2Rawdata(*nppkt, *pnrwd); pcnt_sock->Close();
Server::SignedRawdata(pnrwd, "ECYP"); }
// 说明连接类型 else {
SQEServer::BuildSmallRawData(nsrwd, "SCNT"); //注册连接未被识别
pcnt_sock->SendRAW(nsrwd.msg, nsrwd.msg_size); error::printError("Client connection error.");
Server::freeRawdataServer(nsrwd); delete pclr;
delete pnrwd;
pcnt_sock->SendRAW(pnrwd->msg, pnrwd->msg_size); delete ncryp;
pcnt_sock->Close(); clockThreadFinish(pcti->tid);
pthread_exit(NULL);
}
client_listen *pcltl = new client_listen(); client_listen *pcltl = new client_listen();
pcltl->pcltr = pclr; pcltl->pcltr = pclr;
@ -1203,7 +1245,6 @@ void *clientWaitDeamon(void *pvclt){
} }
delete pclr; delete pclr;
delete nppkt;
delete pnrwd; delete pnrwd;
delete ncryp; delete ncryp;
// 等待接收线程返回 // 等待接收线程返回

View File

@ -71,19 +71,19 @@ ssize_t SocketUDPServer::RecvRAW(char **p_rdt, Addr &taddr){
if(set_fcntl){ if(set_fcntl){
tlen = recvfrom(server_sfd, buff, BUFSIZ, 0, (struct sockaddr *)(&tsai), &tsai_size); tlen = recvfrom(server_sfd, buff, BUFSIZ, 0, (struct sockaddr *)(&tsai), &tsai_size);
// 读取错误 //读取错误
if(tlen == -1 && errno != EAGAIN){ if(tlen == -1 && errno != EAGAIN){
*p_rdt = nullptr; *p_rdt = nullptr;
printf("%d",errno); printf("%d",errno);
perror("recv"); perror("recv");
return -1; return -1;
} }
// 缓冲区没有信息 //缓冲区没有信息
else if(tlen == 0 || (tlen == -1 && errno == EAGAIN)){ else if(tlen == 0 || (tlen == -1 && errno == EAGAIN)){
*p_rdt = nullptr; *p_rdt = nullptr;
return 0; return 0;
} }
// 成功读取信息 //成功读取信息
else{ else{
*p_rdt = (char *)malloc(tlen); *p_rdt = (char *)malloc(tlen);
taddr.SetSockAddr(tsai); taddr.SetSockAddr(tsai);
@ -191,21 +191,12 @@ void SocketTCPCServer::SendRespond(string &str){
} }
ssize_t SocketTCPClient::RecvRAW(char **p_rdt, Addr &taddr){ ssize_t SocketTCPClient::RecvRAW(char **p_rdt, Addr &taddr){
ssize_t bdtas = 0 ,tmp_bdtas; ssize_t tmp_bdtas = recv(client_sfd, buff, BUFSIZ, 0);
*p_rdt = nullptr; if (tmp_bdtas > 0) {
while ((tmp_bdtas = recv(client_sfd, buff, BUFSIZ, 0)) > 0) { *p_rdt = (char *)malloc(tmp_bdtas);
if(*p_rdt == nullptr){ memcpy(*p_rdt, buff, tmp_bdtas);
*p_rdt = (char *)malloc(tmp_bdtas); }
memcpy(*p_rdt, buff, tmp_bdtas); return tmp_bdtas;
}
else{
*p_rdt = (char *)realloc(*p_rdt, bdtas + tmp_bdtas);
memcpy(*p_rdt + bdtas, buff, tmp_bdtas);
}
bdtas += tmp_bdtas;
printf("Get Data Size %lu",tmp_bdtas);
}
return bdtas;
} }