The server doesn't need a restart when regex are changed
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
from signal import SIGUSR1
|
||||||
import subprocess, re, os
|
import subprocess, re, os
|
||||||
|
|
||||||
#c++ -o proxy proxy.cpp
|
#c++ -o proxy proxy.cpp
|
||||||
@@ -37,8 +38,13 @@ class Proxy:
|
|||||||
filter_map = self.compile_filters()
|
filter_map = self.compile_filters()
|
||||||
filters_codes = list(filter_map.keys())
|
filters_codes = list(filter_map.keys())
|
||||||
proxy_binary_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),"./proxy")
|
proxy_binary_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),"./proxy")
|
||||||
|
config_file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),"./config_file")
|
||||||
|
with open(config_file_path,'w') as config_file:
|
||||||
|
for line in filters_codes:
|
||||||
|
config_file.write(line + '\n')
|
||||||
|
|
||||||
self.process = subprocess.Popen(
|
self.process = subprocess.Popen(
|
||||||
[proxy_binary_path, str(self.public_host), str(self.public_port), str(self.internal_host), str(self.internal_port), *filters_codes],
|
[proxy_binary_path, str(self.public_host), str(self.public_port), str(self.internal_host), str(self.internal_port), config_file_path],
|
||||||
stdout=subprocess.PIPE, universal_newlines=True
|
stdout=subprocess.PIPE, universal_newlines=True
|
||||||
)
|
)
|
||||||
for stdout_line in iter(self.process.stdout.readline, ""):
|
for stdout_line in iter(self.process.stdout.readline, ""):
|
||||||
@@ -68,7 +74,14 @@ class Proxy:
|
|||||||
return status
|
return status
|
||||||
|
|
||||||
def reload(self):
|
def reload(self):
|
||||||
if self.process: self.restart()
|
if self.process:
|
||||||
|
filter_map = self.compile_filters()
|
||||||
|
filters_codes = list(filter_map.keys())
|
||||||
|
config_file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),"./config_file")
|
||||||
|
with open(config_file_path,'w') as config_file:
|
||||||
|
for line in filters_codes:
|
||||||
|
config_file.write(line + '\n')
|
||||||
|
self.process.send_signal(SIGUSR1)
|
||||||
|
|
||||||
def isactive(self):
|
def isactive(self):
|
||||||
return True if self.process else False
|
return True if self.process else False
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <csignal>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
#include <boost/regex.hpp>
|
#include <boost/regex.hpp>
|
||||||
#include <boost/shared_ptr.hpp>
|
#include <boost/shared_ptr.hpp>
|
||||||
@@ -36,6 +38,7 @@ unhexlify(InputIterator first, InputIterator last, OutputIterator ascii) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
vector<pair<string,boost::regex>> regex_s_c_w, regex_c_s_w, regex_s_c_b, regex_c_s_b;
|
vector<pair<string,boost::regex>> regex_s_c_w, regex_c_s_w, regex_s_c_b, regex_c_s_b;
|
||||||
|
const char* config_file;
|
||||||
|
|
||||||
bool filter_data(unsigned char* data, const size_t& bytes_transferred, vector<pair<string,boost::regex>> const &blacklist, vector<pair<string,boost::regex>> const &whitelist){
|
bool filter_data(unsigned char* data, const size_t& bytes_transferred, vector<pair<string,boost::regex>> const &blacklist, vector<pair<string,boost::regex>> const &whitelist){
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
@@ -315,19 +318,74 @@ void push_regex(char* arg, bool case_sensitive, vector<pair<string,boost::regex>
|
|||||||
if (case_sensitive){
|
if (case_sensitive){
|
||||||
boost::regex regex(reinterpret_cast<char*>(expr),
|
boost::regex regex(reinterpret_cast<char*>(expr),
|
||||||
reinterpret_cast<char*>(expr) + expr_len);
|
reinterpret_cast<char*>(expr) + expr_len);
|
||||||
|
cout << "Added case sensitive regex " << expr << endl;
|
||||||
v.push_back(make_pair(string(arg), regex));
|
v.push_back(make_pair(string(arg), regex));
|
||||||
} else {
|
} else {
|
||||||
boost::regex regex(reinterpret_cast<char*>(expr),
|
boost::regex regex(reinterpret_cast<char*>(expr),
|
||||||
reinterpret_cast<char*>(expr) + expr_len, boost::regex::icase);
|
reinterpret_cast<char*>(expr) + expr_len, boost::regex::icase);
|
||||||
|
cout << "Added case insensitive regex " << expr << endl;
|
||||||
v.push_back(make_pair(string(arg), regex));
|
v.push_back(make_pair(string(arg), regex));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void update_regex(){
|
||||||
|
fstream fd;
|
||||||
|
fd.open(config_file,ios::in);
|
||||||
|
if (!fd.is_open()){
|
||||||
|
std::cerr << "Error: config file couln't be opened" << std::endl;
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
regex_s_c_w.clear();
|
||||||
|
regex_c_s_w.clear();
|
||||||
|
regex_s_c_b.clear();
|
||||||
|
regex_c_s_b.clear();
|
||||||
|
|
||||||
|
string line;
|
||||||
|
while(getline(fd, line)){
|
||||||
|
char tp[line.length() +1];
|
||||||
|
strcpy(tp, line.c_str());
|
||||||
|
if (strlen(tp) >= 2){
|
||||||
|
bool case_sensitive = true;
|
||||||
|
if(tp[0] == '0'){
|
||||||
|
case_sensitive = false;
|
||||||
|
}
|
||||||
|
switch(tp[1]){
|
||||||
|
case 'C': { // Client to server Blacklist
|
||||||
|
push_regex(tp, case_sensitive, regex_c_s_b);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'c': { // Client to server Whitelist
|
||||||
|
push_regex(tp, case_sensitive, regex_c_s_w);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'S': { // Server to client Blacklist
|
||||||
|
push_regex(tp, case_sensitive, regex_s_c_b);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 's': { // Server to client Whitelist
|
||||||
|
push_regex(tp, case_sensitive, regex_s_c_w);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void signal_handler(int signal_num)
|
||||||
|
{
|
||||||
|
if (signal_num == SIGUSR1){
|
||||||
|
cout << "Updating configurtation" << endl;
|
||||||
|
update_regex();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
if (argc < 5)
|
if (argc < 6)
|
||||||
{
|
{
|
||||||
std::cerr << "usage: tcpproxy_server <local host ip> <local port> <forward host ip> <forward port> 0C..... 1S....." << std::endl;
|
std::cerr << "usage: tcpproxy_server <local host ip> <local port> <forward host ip> <forward port> <config_file>" << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -335,32 +393,12 @@ int main(int argc, char* argv[])
|
|||||||
const unsigned short forward_port = static_cast<unsigned short>(::atoi(argv[4]));
|
const unsigned short forward_port = static_cast<unsigned short>(::atoi(argv[4]));
|
||||||
const std::string local_host = argv[1];
|
const std::string local_host = argv[1];
|
||||||
const std::string forward_host = argv[3];
|
const std::string forward_host = argv[3];
|
||||||
for (int i=5;i<argc;i++){
|
|
||||||
if (strlen(argv[i]) >= 2){
|
|
||||||
bool case_sensitive = true;
|
signal(SIGUSR1, signal_handler);\
|
||||||
if(argv[i][0] == '0'){
|
|
||||||
case_sensitive = false;
|
config_file = argv[5];
|
||||||
}
|
update_regex();
|
||||||
switch(argv[i][1]){
|
|
||||||
case 'C': { // Client to server Blacklist
|
|
||||||
push_regex(argv[i], case_sensitive, regex_c_s_b);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 'c': { // Client to server Whitelist
|
|
||||||
push_regex(argv[i], case_sensitive, regex_c_s_w);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 'S': { // Server to client Blacklist
|
|
||||||
push_regex(argv[i], case_sensitive, regex_s_c_b);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 's': { // Server to client Whitelist
|
|
||||||
push_regex(argv[i], case_sensitive, regex_s_c_w);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
boost::asio::io_service ios;
|
boost::asio::io_service ios;
|
||||||
|
|
||||||
|
|||||||
@@ -196,12 +196,12 @@ class ProxyManager:
|
|||||||
#remove old filters
|
#remove old filters
|
||||||
for f in old_filters:
|
for f in old_filters:
|
||||||
if not f in new_filters:
|
if not f in new_filters:
|
||||||
restart_required = True
|
restart_required = False
|
||||||
del filters[f]
|
del filters[f]
|
||||||
|
|
||||||
for f in new_filters:
|
for f in new_filters:
|
||||||
if not f in old_filters:
|
if not f in old_filters:
|
||||||
restart_required = True
|
restart_required = False
|
||||||
filter_info = [ele for ele in data['filters'] if ele["id"] == f][0]
|
filter_info = [ele for ele in data['filters'] if ele["id"] == f][0]
|
||||||
filters[f] = Filter(
|
filters[f] = Filter(
|
||||||
is_case_sensitive=filter_info["is_case_sensitive"],
|
is_case_sensitive=filter_info["is_case_sensitive"],
|
||||||
|
|||||||
Reference in New Issue
Block a user