TSG-2489 DOH 支持 IP 归属地
* tfe maat IP 归属地回调表的注册从 pangu 移动到 tfe_resource 中 * 修复 Pangu IP 归属地 SRC && DST 同时命中时 maat 计数的 bug
This commit is contained in:
@@ -65,10 +65,6 @@ enum scan_table
|
||||
PXY_CTRL_HTTP_RES_HDR,
|
||||
PXY_CTRL_HTTP_RES_BODY,
|
||||
PXY_CTRL_SUBSCRIBE_ID,
|
||||
PXY_CTRL_IP_SRC_ASN,
|
||||
PXY_CTRL_IP_DST_ASN,
|
||||
PXY_CTRL_IP_SRC_LOCATION,
|
||||
PXY_CTRL_IP_DST_LOCATION,
|
||||
PXY_CTRL_APP_ID,
|
||||
__SCAN_TABLE_MAX
|
||||
};
|
||||
@@ -95,10 +91,6 @@ enum manipulate_profile_table
|
||||
POLICY_PROFLIE_TABLE_REJECT,
|
||||
POLICY_PROFILE_TABLE_INSERT,
|
||||
POLICY_PROFILE_TABLE_HIJACK,
|
||||
POLICY_ASN_USER_DEFINED,
|
||||
POLICY_ASN_BUILT_IN,
|
||||
POLICY_LOCATION_USER_DEFINED,
|
||||
POLICY_LOCATION_BUILT_IN,
|
||||
POLICY_PROFILE_TABLE_MAX
|
||||
};
|
||||
|
||||
@@ -115,22 +107,6 @@ struct manipulate_profile
|
||||
pthread_mutex_t lock;
|
||||
};
|
||||
|
||||
struct ip_data_table
|
||||
{
|
||||
int profile_id;
|
||||
|
||||
int ref_cnt;
|
||||
|
||||
char *asn;
|
||||
char *organization;
|
||||
|
||||
char *country_full;
|
||||
char *province_full;
|
||||
char *city_full;
|
||||
|
||||
pthread_mutex_t lock;
|
||||
};
|
||||
|
||||
struct policy_action_param
|
||||
{
|
||||
int ref_cnt;
|
||||
@@ -767,161 +743,11 @@ int maat_table_init(const char* table_name,
|
||||
return table_id;
|
||||
}
|
||||
|
||||
static char* tfe_unescape(char* s)
|
||||
{
|
||||
int i=0,j=0;
|
||||
int len=strlen(s);
|
||||
for(i=0,j=0;i<len;i++)
|
||||
{
|
||||
if(s[i]=='\\')
|
||||
{
|
||||
switch(s[i+1])
|
||||
{
|
||||
case '&':
|
||||
s[j]='&';
|
||||
break;
|
||||
case 'b':
|
||||
s[j]=' ';//space,0x20;
|
||||
break;
|
||||
case '\\':
|
||||
s[j]='\\';
|
||||
break;
|
||||
default:
|
||||
s[j]=s[i];
|
||||
i--; //undo the followed i++
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
j++;
|
||||
}
|
||||
else
|
||||
{
|
||||
s[j]=s[i];
|
||||
j++;
|
||||
}
|
||||
}
|
||||
s[j]='\0';
|
||||
return s;
|
||||
}
|
||||
|
||||
void ip_asn_table_new_cb(int table_id, const char* key, const char* table_line, MAAT_PLUGIN_EX_DATA* ad, long argl, void* argp)
|
||||
{
|
||||
int addr_type;
|
||||
int ret=0,profile_id=0,is_valid=0;
|
||||
char start_ip[40], end_ip[40],asn[40]={0};
|
||||
char organization[TFE_PATH_MAX];
|
||||
|
||||
ret=sscanf(table_line, "%d\t%d\t%s\t%s\t%s\t%s\t%d", &profile_id, &addr_type, start_ip, end_ip, asn, organization, &is_valid);
|
||||
if(ret!=7)
|
||||
{
|
||||
TFE_LOG_ERROR(g_pangu_rt->local_logger, "Policy table parse ip ASN failed, ret:%d, %s", ret, table_line);
|
||||
return;
|
||||
}
|
||||
tfe_unescape(organization);
|
||||
|
||||
struct ip_data_table* ip_asn=ALLOC(struct ip_data_table, 1);
|
||||
memset(ip_asn, 0, sizeof(struct ip_data_table));
|
||||
ip_asn->profile_id=profile_id;
|
||||
ip_asn->asn=tfe_strdup(asn);
|
||||
ip_asn->organization=tfe_strdup(organization);
|
||||
ip_asn->ref_cnt=1;
|
||||
pthread_mutex_init(&(ip_asn->lock), NULL);
|
||||
|
||||
TFE_LOG_INFO(g_pangu_rt->local_logger, "Policy table add success %d", profile_id);
|
||||
|
||||
*ad = ip_asn;
|
||||
}
|
||||
|
||||
void ip_location_table_new_cb(int table_id, const char* key, const char* table_line, MAAT_PLUGIN_EX_DATA* ad, long argl, void* argp)
|
||||
{
|
||||
int ret=0,profile_id=0,is_valid=0;
|
||||
int geoname_id=0, addr_type=0;
|
||||
double latitude, longitude, coords;
|
||||
char language[40], start_ip[40], end_ip[40];
|
||||
char continent_abbr[TFE_PATH_MAX],continent_full[TFE_PATH_MAX];
|
||||
char country_abbr[TFE_PATH_MAX],province_abbr[TFE_PATH_MAX], time_zone[TFE_PATH_MAX];
|
||||
char country_full[TFE_PATH_MAX],province_full[TFE_PATH_MAX], city_full[TFE_PATH_MAX];
|
||||
|
||||
ret=sscanf(table_line, "%d\t%d\t%d\t%s\t%s\t%lf\t%lf\t%lf\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%d", &profile_id,&geoname_id,
|
||||
&addr_type, start_ip,end_ip,&latitude,&longitude,&coords,language,
|
||||
continent_abbr,continent_full, country_abbr,country_full,province_abbr,province_full,
|
||||
city_full,time_zone,&is_valid);
|
||||
|
||||
if(ret!=18)
|
||||
{
|
||||
TFE_LOG_ERROR(g_pangu_rt->local_logger, "Policy table parse ip location failed, ret:%d, %s", ret, table_line);
|
||||
return;
|
||||
}
|
||||
tfe_unescape(continent_full);
|
||||
tfe_unescape(country_full);
|
||||
tfe_unescape(province_full);
|
||||
tfe_unescape(city_full);
|
||||
|
||||
struct ip_data_table* ip_asn=ALLOC(struct ip_data_table, 1);
|
||||
memset(ip_asn, 0, sizeof(struct ip_data_table));
|
||||
ip_asn->profile_id=profile_id;
|
||||
ip_asn->country_full=tfe_strdup(country_full);
|
||||
ip_asn->province_full=tfe_strdup(province_full);
|
||||
ip_asn->city_full=tfe_strdup(city_full);
|
||||
ip_asn->ref_cnt=1;
|
||||
pthread_mutex_init(&(ip_asn->lock), NULL);
|
||||
|
||||
TFE_LOG_INFO(g_pangu_rt->local_logger, "Policy table add success %d", profile_id);
|
||||
|
||||
*ad = ip_asn;
|
||||
}
|
||||
|
||||
void ip_table_dup_cb(int table_id, MAAT_PLUGIN_EX_DATA *to, MAAT_PLUGIN_EX_DATA *from, long argl, void* argp)
|
||||
{
|
||||
struct ip_data_table* ip_asn=(struct ip_data_table*)(*from);
|
||||
pthread_mutex_lock(&(ip_asn->lock));
|
||||
ip_asn->ref_cnt++;
|
||||
pthread_mutex_unlock(&(ip_asn->lock));
|
||||
*to=ip_asn;
|
||||
}
|
||||
|
||||
void ip_table_free_cb(int table_id, MAAT_PLUGIN_EX_DATA* ad, long argl, void* argp)
|
||||
{
|
||||
if(*ad==NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
struct ip_data_table* ip_asn=(struct ip_data_table*)(*ad);
|
||||
pthread_mutex_lock(&(ip_asn->lock));
|
||||
ip_asn->ref_cnt--;
|
||||
if(ip_asn->ref_cnt>0)
|
||||
{
|
||||
pthread_mutex_unlock(&(ip_asn->lock));
|
||||
return;
|
||||
}
|
||||
pthread_mutex_unlock(&(ip_asn->lock));
|
||||
pthread_mutex_destroy(&(ip_asn->lock));
|
||||
|
||||
if(ip_asn->asn) FREE(&ip_asn->asn);
|
||||
if(ip_asn->organization) FREE(&ip_asn->organization);
|
||||
if(ip_asn->country_full) FREE(&ip_asn->country_full);
|
||||
if(ip_asn->province_full) FREE(&ip_asn->province_full);
|
||||
if(ip_asn->city_full) FREE(&ip_asn->city_full);
|
||||
|
||||
FREE(&ip_asn);
|
||||
*ad=NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
void ip_table_free(struct ip_data_table* ip_asn)
|
||||
{
|
||||
ip_table_free_cb(0, (void **)&ip_asn, 0, NULL);
|
||||
}
|
||||
|
||||
const char* table_name_idx2str(int profile_idx)
|
||||
{
|
||||
const char *table_name_map[] = {"TSG_PROFILE_RESPONSE_PAGES",
|
||||
"PXY_PROFILE_INSERT_SCRIPTS",
|
||||
"PXY_PROFILE_HIJACK_FILES",
|
||||
"TSG_IP_ASN_USER_DEFINED",
|
||||
"TSG_IP_ASN_BUILT_IN",
|
||||
"TSG_IP_LOCATION_USER_DEFINED",
|
||||
"TSG_IP_LOCATION_BUILT_IN"};
|
||||
"PXY_PROFILE_HIJACK_FILES"};
|
||||
|
||||
return table_name_map[profile_idx];
|
||||
}
|
||||
@@ -953,34 +779,6 @@ int maat_table_ex_init(int profile_idx,
|
||||
return -1;
|
||||
}
|
||||
|
||||
int maat_ip_table_init(int profile_idx,
|
||||
Maat_plugin_EX_free_func_t* free_func,
|
||||
Maat_plugin_EX_dup_func_t* dup_func)
|
||||
{
|
||||
int table_id=0;
|
||||
|
||||
Maat_plugin_EX_new_func_t *new_func[] = {
|
||||
[POLICY_PROFLIE_TABLE_REJECT] = NULL,
|
||||
[POLICY_PROFILE_TABLE_INSERT] = NULL,
|
||||
[POLICY_PROFILE_TABLE_HIJACK] = NULL,
|
||||
[POLICY_ASN_USER_DEFINED] = ip_asn_table_new_cb,
|
||||
[POLICY_ASN_BUILT_IN] = ip_asn_table_new_cb,
|
||||
[POLICY_LOCATION_USER_DEFINED] = ip_location_table_new_cb,
|
||||
[POLICY_LOCATION_BUILT_IN] = ip_location_table_new_cb,
|
||||
};
|
||||
|
||||
const char *table_name = table_name_idx2str(profile_idx);
|
||||
table_id=g_pangu_rt->plolicy_table_id[profile_idx]=Maat_table_register(g_pangu_rt->maat, table_name);
|
||||
if(table_id >= 0)
|
||||
{
|
||||
table_id=Maat_ip_plugin_EX_register(g_pangu_rt->maat, table_id, new_func[profile_idx], free_func, dup_func,
|
||||
0, NULL);
|
||||
return 0;
|
||||
}
|
||||
TFE_LOG_INFO(NULL, "Pangu HTTP register table %s failed.", table_name);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int pangu_policy_init(const char* profile_path, const char* static_section, const char* dynamic_section)
|
||||
{
|
||||
int ret = 0;
|
||||
@@ -995,10 +793,6 @@ int pangu_policy_init(const char* profile_path, const char* static_section, cons
|
||||
table_name[PXY_CTRL_HTTP_RES_HDR] = "TSG_FIELD_HTTP_RES_HDR";
|
||||
table_name[PXY_CTRL_HTTP_RES_BODY] = "TSG_FIELD_HTTP_RES_CONTENT";
|
||||
table_name[PXY_CTRL_SUBSCRIBE_ID] = "TSG_OBJ_SUBSCRIBER_ID";
|
||||
table_name[PXY_CTRL_IP_SRC_ASN]="TSG_SECURITY_SOURCE_ASN";
|
||||
table_name[PXY_CTRL_IP_DST_ASN]="TSG_SECURITY_DESTINATION_ASN";
|
||||
table_name[PXY_CTRL_IP_SRC_LOCATION]="TSG_SECURITY_SOURCE_LOCATION";
|
||||
table_name[PXY_CTRL_IP_DST_LOCATION]="TSG_SECURITY_DESTINATION_LOCATION";
|
||||
table_name[PXY_CTRL_APP_ID] = "TSG_OBJ_APP_ID";
|
||||
for (int i = 0; i < __SCAN_TABLE_MAX; i++)
|
||||
{
|
||||
@@ -1046,14 +840,6 @@ int pangu_policy_init(const char* profile_path, const char* static_section, cons
|
||||
goto error_out;
|
||||
}
|
||||
}
|
||||
for(int i = POLICY_ASN_USER_DEFINED; i < POLICY_PROFILE_TABLE_MAX; i++)
|
||||
{
|
||||
ret = maat_ip_table_init(i, ip_table_free_cb, ip_table_dup_cb);
|
||||
if(ret<0)
|
||||
{
|
||||
goto error_out;
|
||||
}
|
||||
}
|
||||
|
||||
g_pangu_rt->dyn_maat = (Maat_feather_t)tfe_bussiness_resouce_get(DYNAMINC_MAAT);
|
||||
g_pangu_rt->subscriber_id_table_id=Maat_table_register(g_pangu_rt->dyn_maat, "TSG_DYN_SUBSCRIBER_IP");
|
||||
@@ -2657,22 +2443,22 @@ int http_ip_location_scan(struct Maat_rule_t *result, struct ip_address *sip, st
|
||||
char buff[TFE_STRING_MAX]={0};
|
||||
struct ip_data_table* ip_location_client=NULL, *ip_location_server=NULL;
|
||||
|
||||
Maat_ip_plugin_get_EX_data(g_pangu_rt->maat, g_pangu_rt->plolicy_table_id[POLICY_LOCATION_USER_DEFINED], sip, (void **)&ip_location_client, 1);
|
||||
Maat_ip_plugin_get_EX_data(g_pangu_rt->maat, g_pangu_rt->plolicy_table_id[POLICY_LOCATION_USER_DEFINED], dip, (void **)&ip_location_server, 1);
|
||||
Maat_ip_plugin_get_EX_data(g_pangu_rt->maat, tfe_bussiness_tableid_get(TABLE_IP_LOCATION_USER_DEFINED), sip, (void **)&ip_location_client, 1);
|
||||
Maat_ip_plugin_get_EX_data(g_pangu_rt->maat, tfe_bussiness_tableid_get(TABLE_IP_LOCATION_USER_DEFINED), dip, (void **)&ip_location_server, 1);
|
||||
|
||||
if (ip_location_client == NULL)
|
||||
{
|
||||
Maat_ip_plugin_get_EX_data(g_pangu_rt->maat, g_pangu_rt->plolicy_table_id[POLICY_LOCATION_BUILT_IN], sip, (void **)&ip_location_client, 1);
|
||||
Maat_ip_plugin_get_EX_data(g_pangu_rt->maat, tfe_bussiness_tableid_get(TABLE_IP_LOCATION_BUILT_IN), sip, (void **)&ip_location_client, 1);
|
||||
}
|
||||
if (ip_location_server == NULL)
|
||||
{
|
||||
Maat_ip_plugin_get_EX_data(g_pangu_rt->maat, g_pangu_rt->plolicy_table_id[POLICY_LOCATION_BUILT_IN], dip, (void **)&ip_location_server, 1);
|
||||
Maat_ip_plugin_get_EX_data(g_pangu_rt->maat, tfe_bussiness_tableid_get(TABLE_IP_LOCATION_BUILT_IN), dip, (void **)&ip_location_server, 1);
|
||||
}
|
||||
|
||||
if(ip_location_server!=NULL)
|
||||
{
|
||||
snprintf(buff, sizeof(buff), "%s.%s.", ip_location_server->country_full, ip_location_server->city_full);
|
||||
scan_ret = Maat_full_scan_string(g_pangu_rt->maat, g_pangu_rt->scan_table_id[PXY_CTRL_IP_DST_LOCATION],
|
||||
scan_ret = Maat_full_scan_string(g_pangu_rt->maat, tfe_bussiness_tableid_get(TABLE_SECURITY_DESTINATION_LOCATION),
|
||||
CHARSET_GBK, buff, strlen(buff),
|
||||
result+hit_cnt, NULL, MAX_SCAN_RESULT-hit_cnt,
|
||||
&(ctx->scan_mid), (int) thread_id);
|
||||
@@ -2687,9 +2473,9 @@ int http_ip_location_scan(struct Maat_rule_t *result, struct ip_address *sip, st
|
||||
if(ip_location_client!=NULL)
|
||||
{
|
||||
snprintf(buff, sizeof(buff), "%s.%s.", ip_location_client->country_full, ip_location_client->city_full);
|
||||
scan_ret = Maat_full_scan_string(g_pangu_rt->maat, g_pangu_rt->scan_table_id[PXY_CTRL_IP_SRC_LOCATION],
|
||||
scan_ret = Maat_full_scan_string(g_pangu_rt->maat, tfe_bussiness_tableid_get(TABLE_SECURITY_SOURCE_LOCATION),
|
||||
CHARSET_GBK, buff, strlen(buff),
|
||||
result+hit_cnt, NULL, MAX_SCAN_RESULT-hit_cnt,
|
||||
result+hit_cnt+hit_cnt_ip, NULL, MAX_SCAN_RESULT-hit_cnt-hit_cnt_ip,
|
||||
&(ctx->scan_mid), (int) thread_id);
|
||||
if(scan_ret>0)
|
||||
{
|
||||
@@ -2713,21 +2499,21 @@ int http_ip_asn_scan(struct Maat_rule_t *result, struct ip_address* sip, struct
|
||||
char buff[TFE_STRING_MAX]={0};
|
||||
struct ip_data_table* ip_asn_client=NULL, *ip_asn_server=NULL;
|
||||
|
||||
Maat_ip_plugin_get_EX_data(g_pangu_rt->maat, g_pangu_rt->plolicy_table_id[POLICY_ASN_USER_DEFINED], sip, (void **)&ip_asn_client, 1);
|
||||
Maat_ip_plugin_get_EX_data(g_pangu_rt->maat, g_pangu_rt->plolicy_table_id[POLICY_ASN_USER_DEFINED], dip, (void **)&ip_asn_server, 1);
|
||||
Maat_ip_plugin_get_EX_data(g_pangu_rt->maat, tfe_bussiness_tableid_get(TABLE_IP_ASN_USER_DEFINED), sip, (void **)&ip_asn_client, 1);
|
||||
Maat_ip_plugin_get_EX_data(g_pangu_rt->maat, tfe_bussiness_tableid_get(TABLE_IP_ASN_USER_DEFINED), dip, (void **)&ip_asn_server, 1);
|
||||
|
||||
if (ip_asn_client == NULL)
|
||||
{
|
||||
Maat_ip_plugin_get_EX_data(g_pangu_rt->maat, g_pangu_rt->plolicy_table_id[POLICY_ASN_BUILT_IN], sip, (void **)&ip_asn_client, 1);
|
||||
Maat_ip_plugin_get_EX_data(g_pangu_rt->maat, tfe_bussiness_tableid_get(TABLE_IP_ASN_BUILT_IN), sip, (void **)&ip_asn_client, 1);
|
||||
}
|
||||
if (ip_asn_server == NULL)
|
||||
{
|
||||
Maat_ip_plugin_get_EX_data(g_pangu_rt->maat, g_pangu_rt->plolicy_table_id[POLICY_ASN_BUILT_IN], dip, (void **)&ip_asn_server, 1);
|
||||
Maat_ip_plugin_get_EX_data(g_pangu_rt->maat, tfe_bussiness_tableid_get(TABLE_IP_ASN_BUILT_IN), dip, (void **)&ip_asn_server, 1);
|
||||
}
|
||||
|
||||
if(ip_asn_server!=NULL)
|
||||
{
|
||||
scan_ret = Maat_full_scan_string(g_pangu_rt->maat, g_pangu_rt->scan_table_id[PXY_CTRL_IP_DST_ASN],
|
||||
scan_ret = Maat_full_scan_string(g_pangu_rt->maat, tfe_bussiness_tableid_get(TABLE_SECURITY_DESTINATION_ASN),
|
||||
CHARSET_UTF8, ip_asn_server->asn, strlen(ip_asn_server->asn),
|
||||
result+hit_cnt, NULL, MAX_SCAN_RESULT-hit_cnt,
|
||||
&(ctx->scan_mid), (int) thread_id);
|
||||
@@ -2741,9 +2527,9 @@ int http_ip_asn_scan(struct Maat_rule_t *result, struct ip_address* sip, struct
|
||||
}
|
||||
if(ip_asn_client!=NULL)
|
||||
{
|
||||
scan_ret = Maat_full_scan_string(g_pangu_rt->maat, g_pangu_rt->scan_table_id[PXY_CTRL_IP_SRC_ASN],
|
||||
scan_ret = Maat_full_scan_string(g_pangu_rt->maat, tfe_bussiness_tableid_get(TABLE_SECURITY_SOURCE_ASN),
|
||||
CHARSET_UTF8, ip_asn_client->asn, strlen(ip_asn_client->asn),
|
||||
result+hit_cnt, NULL, MAX_SCAN_RESULT-hit_cnt,
|
||||
result+hit_cnt+hit_cnt_ip, NULL, MAX_SCAN_RESULT-hit_cnt-hit_cnt_ip,
|
||||
&(ctx->scan_mid), (int) thread_id);
|
||||
if(scan_ret>0)
|
||||
{
|
||||
@@ -2759,30 +2545,6 @@ int http_ip_asn_scan(struct Maat_rule_t *result, struct ip_address* sip, struct
|
||||
return hit_cnt_ip;
|
||||
}
|
||||
|
||||
int tfe_stream_addr_to_address(const struct tfe_stream_addr *addr, struct ip_address *dest_ip, struct ip_address *source_ip)
|
||||
{
|
||||
if(addr==NULL) return -1;
|
||||
if (addr->addrtype == TFE_ADDR_STREAM_TUPLE4_V4)
|
||||
{
|
||||
const struct tfe_stream_addr_tuple4_v4 * tuple4_v4 = addr->tuple4_v4;
|
||||
source_ip->ip_type=4;
|
||||
source_ip->ipv4=tuple4_v4->saddr.s_addr;
|
||||
|
||||
dest_ip->ip_type=4;
|
||||
dest_ip->ipv4=tuple4_v4->daddr.s_addr;
|
||||
}
|
||||
if (addr->addrtype == TFE_ADDR_STREAM_TUPLE4_V6)
|
||||
{
|
||||
const struct tfe_stream_addr_tuple4_v6 * tuple4_v6 = addr->tuple4_v6;
|
||||
source_ip->ip_type=6;
|
||||
memcpy((char *)(source_ip->ipv6), tuple4_v6->saddr.__in6_u.__u6_addr32, IPV6_ADDR_LEN);
|
||||
|
||||
dest_ip->ip_type=6;
|
||||
memcpy((char *)(dest_ip->ipv6),tuple4_v6->daddr.__in6_u.__u6_addr32, IPV6_ADDR_LEN);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void pangu_on_http_begin(const struct tfe_stream * stream,
|
||||
const struct tfe_http_session * session, unsigned int thread_id, void ** pme)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user