• R/O
  • SSH

fcfserver: Commit

Server application performing load balancing and monitoring of nodejs applications


Commit MetaInfo

Revision2f1dcadd616d53dcd9ff4bba749293c78d12d2e7 (tree)
Zeit2021-05-07 13:09:38
AutorVladimir Markin <v.o.markin221@gmai...>
CommiterVladimir Markin

Log Message

zero socet data protection

Ändern Zusammenfassung

Diff

diff -r a86f2ea9efee -r 2f1dcadd616d endpoint.cpp
--- a/endpoint.cpp Fri Apr 23 01:09:59 2021 +0300
+++ b/endpoint.cpp Fri May 07 07:09:38 2021 +0300
@@ -43,13 +43,13 @@
4343 public:
4444 file_buffer_t()
4545 : _path(get_temporary_path()), _stream(_path, std::fstream::out | std::fstream::in | std::fstream::app | std::fstream::binary), _first_read(true) {
46-
46+
4747 }
4848
4949 ~file_buffer_t(){
5050 try { _stream.close(); } catch (std::exception e) {}
51- try {
52- std::filesystem::remove(_path);
51+ try {
52+ std::filesystem::remove(_path);
5353 }
5454 catch (std::exception e) {
5555 fcf::err << "ERROR REMOVE FILE TEMP FILE " << _path.string();
@@ -77,9 +77,9 @@
7777 }
7878
7979 endpoint_t::endpoint_t(const server_configuration_t& a_server_config, size_t a_handler_index)
80- : _slave(a_server_config.slave),
81- _config(a_server_config.endpoints[a_handler_index]),
82- _start_group(0),
80+ : _slave(a_server_config.slave),
81+ _config(a_server_config.endpoints[a_handler_index]),
82+ _start_group(0),
8383 _stop(false) {
8484
8585 for(size_t i = 0; i < a_server_config.endpoints.size() && i < a_handler_index; ++i){
@@ -129,7 +129,7 @@
129129 }
130130 if (leak_proteced)
131131 argv1.push_back("--leak-protected");
132-
132+
133133
134134 std::vector<std::string> argv2;
135135 argv2.push_back(hc.script);
@@ -163,19 +163,19 @@
163163
164164 std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
165165 std::string node_path = conv.to_bytes(boost::process::search_path("node").wstring());
166- _subprocesses[hi]->append(node_path.c_str(),
166+ _subprocesses[hi]->append(node_path.c_str(),
167167 argv1,
168168 argv2,
169169 envv,
170170 hc.nodeOptions[procindex],
171- hc.user.c_str(),
172- hc.password.c_str(),
171+ hc.user.c_str(),
172+ hc.password.c_str(),
173173 hc.dataPorts[procindex*2],
174174 hc.dataPorts[procindex*2+1],
175- hc.controlPorts[procindex*2],
176- hc.controlPorts[procindex*2+1],
177- hc.startTimeout,
178- hc.stopTimeout,
175+ hc.controlPorts[procindex*2],
176+ hc.controlPorts[procindex*2+1],
177+ hc.startTimeout,
178+ hc.stopTimeout,
179179 !!leak_proteced);
180180 ++procindex;
181181
@@ -199,7 +199,7 @@
199199 _socket6->listen(_config.listenQueue);
200200 _endpoint_thread6 = std::thread(&endpoint_t::_endpoint_handler, std::ref(*this), _socket6.get());
201201 }
202-
202+
203203 }
204204
205205 void endpoint_t::stop() {
@@ -325,7 +325,7 @@
325325
326326 if (std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - start_request).count() > max_request_timeout)
327327 throw fcf::except::max_request_timeout(a_socket->get_address(), a_socket->get_port());
328-
328+
329329 first_buffer_offset += recc;
330330 total_size = first_buffer_offset;
331331 rec_total_size = first_buffer_offset;
@@ -371,32 +371,32 @@
371371 path_cmp_weigth = 0;
372372 break;
373373 } else if (width >= 1 &&
374- !cur_host_empty && cur_host_iseq &&
375- !cur_path_empty && cur_path_match &&
374+ !cur_host_empty && cur_host_iseq &&
375+ !cur_path_empty && cur_path_match &&
376376 (width != 1 || path_cmp_weigth > cur_path_cmp_width)) {
377377 new_handler_index = hi;
378378 width = 1;
379379 path_cmp_weigth = cur_path_cmp_width;
380380 } else if (width > 2 &&
381- !cur_host_empty && cur_host_iseq &&
381+ !cur_host_empty && cur_host_iseq &&
382382 cur_path_empty) {
383383 new_handler_index = hi;
384384 width = 2;
385385 path_cmp_weigth = 0;
386386 } else if (width > 3 &&
387- cur_host_empty &&
387+ cur_host_empty &&
388388 !cur_path_empty && cur_path_iseq){
389389 new_handler_index = hi;
390390 width = 3;
391391 path_cmp_weigth = 0;
392392 } else if (width >= 4 &&
393- cur_host_empty &&
393+ cur_host_empty &&
394394 (width != 4 || path_cmp_weigth > cur_path_cmp_width)){
395395 new_handler_index = hi;
396396 width = 4;
397397 path_cmp_weigth = cur_path_cmp_width;
398398 } else if (width > 5 &&
399- cur_host_empty &&
399+ cur_host_empty &&
400400 cur_path_empty){
401401 new_handler_index = hi;
402402 width = 5;
@@ -426,15 +426,24 @@
426426 if (!is_big_data) {
427427 if (first_buffer_offset < full_pack)
428428 residual_buffer.resize(full_pack - first_buffer_offset);
429+ bool firstEmpty = true;
429430 while (rec_total_size < full_pack) {
430- rec_total_size += a_socket->recv(&residual_buffer[rec_total_size - first_buffer_offset], full_pack - rec_total_size < MAX_PACK ? full_pack - rec_total_size : MAX_PACK);
431+ size_t size = a_socket->recv(&residual_buffer[rec_total_size - first_buffer_offset], full_pack - rec_total_size < MAX_PACK ? full_pack - rec_total_size : MAX_PACK);
432+ if (!size && !firstEmpty)
433+ throw std::runtime_error("Empty receive data (recv())");
434+ firstEmpty = !!size;
435+ rec_total_size += size;
431436 if (std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - start_request).count() > max_request_timeout)
432437 throw fcf::except::max_request_timeout_fi(a_socket->get_address(), a_socket->get_port(), request_header_info.host.c_str(), request_header_info.path.c_str());
433438 }
434439 } else if (rec_total_size < full_pack) {
435440 file_buffer.reset(new file_buffer_t());
441+ bool firstEmpty = true;
436442 while (rec_total_size < full_pack) {
437443 size_t size = a_socket->recv(&big_data_buffer[0], full_pack - rec_total_size < MAX_PACK ? full_pack - rec_total_size : MAX_PACK);
444+ if (!size && !firstEmpty)
445+ throw std::runtime_error("Empty receive data (recv())");
446+ firstEmpty = !!size;
438447 rec_total_size += size;
439448 file_buffer->write(&big_data_buffer[0], size);
440449 if (std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - start_request).count() > max_request_timeout)
@@ -464,18 +473,30 @@
464473
465474 // send header with data
466475 offset = 0;
467- while(offset < first_buffer_offset){
468- offset += process_socket->send(&first_buffer[offset], first_buffer_offset - offset);
469- if (std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - start_request).count() > max_request_timeout)
470- throw fcf::except::max_request_timeout_fi(a_socket->get_address(), a_socket->get_port(), request_header_info.host.c_str(), request_header_info.path.c_str());
476+ {
477+ bool firstEmpty = true;
478+ while(offset < first_buffer_offset){
479+ size_t size = process_socket->send(&first_buffer[offset], first_buffer_offset - offset);
480+ offset += size;
481+ if (!size && !firstEmpty)
482+ throw std::runtime_error("Empty send data to process (send())");
483+ firstEmpty = !!size;
484+ if (std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - start_request).count() > max_request_timeout)
485+ throw fcf::except::max_request_timeout_fi(a_socket->get_address(), a_socket->get_port(), request_header_info.host.c_str(), request_header_info.path.c_str());
486+ }
471487 }
472488
473489 // send residual message to process
474490 offset = 0;
475491 if (!is_big_data) {
476492 //residual_buffer
493+ bool firstEmpty = true;
477494 while((offset + first_buffer_offset) < full_pack) {
478- offset += process_socket->send(&residual_buffer[offset], (full_pack - first_buffer_offset) - offset);
495+ size_t size = process_socket->send(&residual_buffer[offset], (full_pack - first_buffer_offset) - offset);
496+ offset += size;
497+ if (!size && !firstEmpty)
498+ throw std::runtime_error("Empty send data to process (send())");
499+ firstEmpty = !!size;
479500 if (std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - start_request).count() > max_request_timeout)
480501 throw fcf::except::max_request_timeout_fi(a_socket->get_address(), a_socket->get_port(), request_header_info.host.c_str(), request_header_info.path.c_str());
481502 }
@@ -485,8 +506,13 @@
485506 file_buffer->read(&big_data_buffer[0], pack_size);
486507
487508 size_t part_size = 0;
509+ bool firstEmpty = true;
488510 while(part_size < pack_size) {
489- part_size += process_socket->send(&big_data_buffer[part_size], pack_size - part_size);
511+ size_t size = process_socket->send(&big_data_buffer[part_size], pack_size - part_size);
512+ part_size += size;
513+ if (!size && !firstEmpty)
514+ throw std::runtime_error("Empty send data to process (send())");
515+ firstEmpty = !!size;
490516 if (std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - start_request).count() > max_request_timeout)
491517 throw fcf::except::max_request_timeout_fi(a_socket->get_address(), a_socket->get_port(), request_header_info.host.c_str(), request_header_info.path.c_str());
492518 }
@@ -523,7 +549,8 @@
523549
524550 // send header with data to client
525551 full_pack = response_header_info.content_length + response_header_info.header_end + 4;
526- offset = 0;
552+ offset = 0;
553+ bool firstEmpty = true;
527554 while (offset < total_size) {
528555 if (!sps->active) {
529556 _buffered_forwarding(&first_buffer[offset],
@@ -539,13 +566,18 @@
539566 completed = true;
540567 break;
541568 }
542- offset += a_socket->send(&first_buffer[offset], total_size - offset > MAX_PACK ? MAX_PACK : (unsigned int)(total_size - offset));
569+ size_t size = a_socket->send(&first_buffer[offset], total_size - offset > MAX_PACK ? MAX_PACK : (unsigned int)(total_size - offset));
570+ offset += size;
571+ if (!size && !firstEmpty)
572+ throw std::runtime_error("Empty send data to client (send())");
573+ firstEmpty = !!size;
543574 if (std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - start_response).count() > max_response_timeout)
544575 throw fcf::except::max_response_timeout_fi(a_socket->get_address(), a_socket->get_port(), request_header_info.host.c_str(), request_header_info.path.c_str());
545576 }
546577
547578 // resend data to client
548579 if (!completed) {
580+ bool firstEmpty = true;
549581 while(total_size < full_pack) {
550582 if (!sps->active) {
551583 _buffered_forwarding(&first_buffer[0],
@@ -563,6 +595,9 @@
563595 }
564596
565597 offset = process_socket->recv(&first_buffer[0], MAX_PACK);
598+ if (!offset && !firstEmpty)
599+ throw std::runtime_error("Empty receive data from process (send())");
600+ firstEmpty = !!offset;
566601 if (std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - start_response).count() > max_response_timeout)
567602 throw fcf::except::max_response_timeout_fi(a_socket->get_address(), a_socket->get_port(), request_header_info.host.c_str(), request_header_info.path.c_str());
568603
@@ -582,10 +617,17 @@
582617 }
583618
584619 int sendc = 0;
585- while(sendc < offset){
586- sendc += a_socket->send(&first_buffer[sendc], offset - sendc);
587- if (std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - start_response).count() > max_response_timeout)
588- throw fcf::except::max_response_timeout_fi(a_socket->get_address(), a_socket->get_port(), request_header_info.host.c_str(), request_header_info.path.c_str());
620+ {
621+ bool firstEmpty = true;
622+ while(sendc < offset){
623+ bool size = a_socket->send(&first_buffer[sendc], offset - sendc);
624+ sendc += size;
625+ if (!size && !firstEmpty)
626+ throw std::runtime_error("Empty send data to client (send())");
627+ firstEmpty = !!size;
628+ if (std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - start_response).count() > max_response_timeout)
629+ throw fcf::except::max_response_timeout_fi(a_socket->get_address(), a_socket->get_port(), request_header_info.host.c_str(), request_header_info.path.c_str());
630+ }
589631 }
590632
591633 total_size += offset;
@@ -614,27 +656,48 @@
614656
615657 // send header with data
616658 offset = 0;
617- while(offset < total_size){
618- offset += process_socket->send(&first_buffer[offset], total_size - offset > MAX_PACK ? MAX_PACK : (unsigned int)(total_size - offset));
619- if (std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - start_request).count() > max_request_timeout)
620- throw fcf::except::max_request_timeout_fi(a_socket->get_address(), a_socket->get_port(), request_header_info.host.c_str(), request_header_info.path.c_str());
659+ {
660+ bool firstEmpty = true;
661+ while(offset < total_size){
662+ size_t size = process_socket->send(&first_buffer[offset], total_size - offset > MAX_PACK ? MAX_PACK : (unsigned int)(total_size - offset));
663+ offset += size;
664+ if (!size && !firstEmpty)
665+ throw std::runtime_error("Empty send data to process (send())");
666+ firstEmpty = !!size;
667+
668+ if (std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - start_request).count() > max_request_timeout)
669+ throw fcf::except::max_request_timeout_fi(a_socket->get_address(), a_socket->get_port(), request_header_info.host.c_str(), request_header_info.path.c_str());
670+ }
621671 }
622672
623673 // resend data to process
624674 full_pack = request_header_info.content_length + request_header_info.header_end + 4;
625- while(total_size < full_pack){
626- offset = a_socket->recv(&first_buffer[0], MAX_PACK);
627- if (std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - start_request).count() > max_request_timeout)
628- throw fcf::except::max_request_timeout_fi(a_socket->get_address(), a_socket->get_port(), request_header_info.host.c_str(), request_header_info.path.c_str());
629-
630- int sendc = 0;
631- while(sendc < offset){
632- sendc += process_socket->send(&first_buffer[sendc], offset - sendc);
675+ {
676+ bool firstEmpty = true;
677+ while(total_size < full_pack){
678+ offset = a_socket->recv(&first_buffer[0], MAX_PACK);
679+ if (!offset && !firstEmpty)
680+ throw std::runtime_error("Empty receive data from client (recv())");
681+ firstEmpty = !!offset;
633682 if (std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - start_request).count() > max_request_timeout)
634683 throw fcf::except::max_request_timeout_fi(a_socket->get_address(), a_socket->get_port(), request_header_info.host.c_str(), request_header_info.path.c_str());
684+
685+ int sendc = 0;
686+ {
687+ bool firstEmpty = true;
688+ while(sendc < offset){
689+ size_t size = process_socket->send(&first_buffer[sendc], offset - sendc);
690+ sendc += size;
691+ if (!size && !firstEmpty)
692+ throw std::runtime_error("Empty send data to process (send())");
693+ firstEmpty = !!size;
694+ if (std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - start_request).count() > max_request_timeout)
695+ throw fcf::except::max_request_timeout_fi(a_socket->get_address(), a_socket->get_port(), request_header_info.host.c_str(), request_header_info.path.c_str());
696+ }
697+ }
698+
699+ total_size += offset;
635700 }
636-
637- total_size += offset;
638701 }
639702
640703 // get response
@@ -665,30 +728,50 @@
665728 }
666729
667730 // send header with data to client
668- offset = 0;
669- while(offset < total_size){
670- offset += a_socket->send(&first_buffer[offset], total_size - offset > MAX_PACK ? MAX_PACK : (unsigned int)(total_size - offset));
671- if (std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - start_response).count() > max_response_timeout)
672- throw fcf::except::max_response_timeout_fi(a_socket->get_address(), a_socket->get_port(), request_header_info.host.c_str(), request_header_info.path.c_str());
731+ offset = 0;
732+ {
733+ bool firstEmpty = true;
734+ while(offset < total_size){
735+ size_t size = a_socket->send(&first_buffer[offset], total_size - offset > MAX_PACK ? MAX_PACK : (unsigned int)(total_size - offset));
736+ offset += size;
737+ if (!size && !firstEmpty)
738+ throw std::runtime_error("Empty sending data to client(send())");
739+ firstEmpty = !!size;
740+
741+ if (std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - start_response).count() > max_response_timeout)
742+ throw fcf::except::max_response_timeout_fi(a_socket->get_address(), a_socket->get_port(), request_header_info.host.c_str(), request_header_info.path.c_str());
743+ }
673744 }
674745
675746 // resend data to client
676747 full_pack = response_header_info.content_length + response_header_info.header_end + 4;
677- while(total_size < full_pack){
678- offset = process_socket->recv(&first_buffer[0], MAX_PACK);
679- if (std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - start_request).count() > max_response_timeout)
680- throw fcf::except::max_response_timeout_fi(a_socket->get_address(), a_socket->get_port(), request_header_info.host.c_str(), request_header_info.path.c_str());
681-
682- int sendc = 0;
683- while(sendc < offset){
684- sendc += a_socket->send(&first_buffer[sendc], offset - sendc);
748+ {
749+ bool firstEmpty = true;
750+ while(total_size < full_pack){
751+ offset = process_socket->recv(&first_buffer[0], MAX_PACK);
752+ if (!offset && !firstEmpty)
753+ throw std::runtime_error("Empty receive data from process(recv())");
754+ firstEmpty = !!offset;
685755 if (std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - start_request).count() > max_response_timeout)
686756 throw fcf::except::max_response_timeout_fi(a_socket->get_address(), a_socket->get_port(), request_header_info.host.c_str(), request_header_info.path.c_str());
687- }
688757
689- total_size += offset;
758+ int sendc = 0;
759+ {
760+ bool firstEmpty = true;
761+ while(sendc < offset){
762+ size_t size = a_socket->send(&first_buffer[sendc], offset - sendc);
763+ sendc += size;
764+ if (!size && !firstEmpty)
765+ throw std::runtime_error("Empty receive data (recv())");
766+ firstEmpty = !!size;
767+ if (std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - start_request).count() > max_response_timeout)
768+ throw fcf::except::max_response_timeout_fi(a_socket->get_address(), a_socket->get_port(), request_header_info.host.c_str(), request_header_info.path.c_str());
769+ }
770+ }
771+
772+ total_size += offset;
773+ }
690774 }
691-
692775 }
693776
694777 if (response_header_info.connection != "keep-alive")
@@ -707,7 +790,7 @@
707790 }
708791
709792 } catch(const std::exception& e) {
710- fcf::trc << "IP:" << a_socket->get_address() << " PORT:" << a_socket->get_port() << " " << e.what();
793+ fcf::trc << "IP:" << a_socket->get_address() << " PORT:" << a_socket->get_port() << " " << e.what();
711794 } catch (...) {
712795 fcf::trc << "Unknown Exception in handler";
713796 }
diff -r a86f2ea9efee -r 2f1dcadd616d server.cpp
--- a/server.cpp Fri Apr 23 01:09:59 2021 +0300
+++ b/server.cpp Fri May 07 07:09:38 2021 +0300
@@ -62,9 +62,11 @@
6262 try {
6363 client.reset(_socket->accept());
6464 rc = client->recv(&buffer[0], sizeof(buffer));
65- } catch(const std::exception& e){
65+ if (!rc)
66+ throw new std::runtime_error("Empty inner command");
67+ } catch(const std::exception& e) {
6668 lock.lock();
67- throw e;
69+ throw std::runtime_error(e.what());
6870 }
6971 lock.lock();
7072 if (_stop)
Show on old repository browser