Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add antigravity (subscribed allowlists with wildcard support) #1579

Merged
merged 11 commits into from Sep 29, 2023
Merged
13 changes: 13 additions & 0 deletions src/api/docs/content/specs/lists.yaml
Expand Up @@ -178,18 +178,21 @@ components:
items:
allOf:
- $ref: 'lists.yaml#/components/schemas/list'
- $ref: 'lists.yaml#/components/schemas/type'
- $ref: 'lists.yaml#/components/schemas/comment'
- $ref: 'lists.yaml#/components/schemas/groups'
- $ref: 'lists.yaml#/components/schemas/enabled'
- $ref: 'lists.yaml#/components/schemas/readonly'
put:
allOf:
- $ref: 'lists.yaml#/components/schemas/comment'
- $ref: 'lists.yaml#/components/schemas/type'
- $ref: 'lists.yaml#/components/schemas/groups'
- $ref: 'lists.yaml#/components/schemas/enabled'
post:
allOf:
- $ref: 'lists.yaml#/components/schemas/list'
- $ref: 'lists.yaml#/components/schemas/type'
- $ref: 'lists.yaml#/components/schemas/comment'
- $ref: 'lists.yaml#/components/schemas/groups'
- $ref: 'lists.yaml#/components/schemas/enabled'
Expand All @@ -200,6 +203,16 @@ components:
description: Address of the list
type: string
example: https://hosts-file.net/ad_servers.txt
type:
type: object
properties:
type:
description: Type of list
type: string
enum:
- "allow"
- "block"
example: "block"
comment:
type: object
properties:
Expand Down
36 changes: 28 additions & 8 deletions src/api/list.c
Expand Up @@ -114,6 +114,8 @@ static int api_list_read(struct ftl_conn *api,
// Properties added in https://github.com/pi-hole/pi-hole/pull/3951
if(listtype == GRAVITY_ADLISTS)
{
const char *adlist_type = table.type_int == ADLIST_BLOCK ? "block" : "allow";
JSON_REF_STR_IN_OBJECT(row, "type", adlist_type);
JSON_ADD_NUMBER_TO_OBJECT(row, "date_updated", table.date_updated);
JSON_ADD_NUMBER_TO_OBJECT(row, "number", table.number);
JSON_ADD_NUMBER_TO_OBJECT(row, "invalid_domains", table.invalid_domains);
Expand Down Expand Up @@ -192,7 +194,7 @@ static int api_list_write(struct ftl_conn *api,
{
return send_json_error(api, 400,
"bad_request",
"Invalid request: No item \"domain\" in payload",
"Invalid request: No valid item \"domain\" in payload",
NULL);
}
break;
Expand All @@ -205,7 +207,7 @@ static int api_list_write(struct ftl_conn *api,
{
return send_json_error(api, 400,
"bad_request",
"Invalid request: No item \"name\" in payload",
"Invalid request: No valid item \"name\" in payload",
NULL);
}
break;
Expand All @@ -218,7 +220,7 @@ static int api_list_write(struct ftl_conn *api,
{
return send_json_error(api, 400,
"bad_request",
"Invalid request: No item \"client\" in payload",
"Invalid request: No valid item \"client\" in payload",
NULL);
}
break;
Expand All @@ -231,7 +233,7 @@ static int api_list_write(struct ftl_conn *api,
{
return send_json_error(api, 400,
"bad_request",
"Invalid request: No item \"address\" in payload",
"Invalid request: No valid item \"address\" in payload",
NULL);
}
break;
Expand All @@ -258,11 +260,29 @@ static int api_list_write(struct ftl_conn *api,
else
row.comment = NULL; // Default value

cJSON *json_type = cJSON_GetObjectItemCaseSensitive(api->payload.json, "type");
if(cJSON_IsString(json_type) && strlen(json_type->valuestring) > 0)
row.type = json_type->valuestring;

// Check if there is a type field in the payload (only for lists)
if(listtype == GRAVITY_ADLISTS)
{
cJSON *json_type = cJSON_GetObjectItemCaseSensitive(api->payload.json, "type");
if(cJSON_IsString(json_type) && strlen(json_type->valuestring) > 0)
row.type_int = strcasecmp(json_type->valuestring, "allow") == 0 ? ADLIST_ALLOW : ADLIST_BLOCK;
else
{
return send_json_error(api, 400,
"bad_request",
"Invalid request: No valid item \"type\" in payload",
NULL);
}
}
else
row.type = NULL; // Default value
{
cJSON *json_type = cJSON_GetObjectItemCaseSensitive(api->payload.json, "type");
if(cJSON_IsString(json_type) && strlen(json_type->valuestring) > 0)
row.type = json_type->valuestring;
else
row.type = NULL; // Default value
}

cJSON *json_kind = cJSON_GetObjectItemCaseSensitive(api->payload.json, "kind");
if(cJSON_IsString(json_kind) && strlen(json_kind->valuestring) > 0)
Expand Down
2 changes: 2 additions & 0 deletions src/api/search.c
Expand Up @@ -65,6 +65,8 @@ static int search_table(struct ftl_conn *api,
if(listtype == GRAVITY_GRAVITY)
{
// Add gravity specific parameters
const char *adlist_type = table.type_int == ADLIST_BLOCK ? "block" : "allow";
JSON_REF_STR_IN_OBJECT(row, "type", adlist_type);
JSON_ADD_NUMBER_TO_OBJECT(row, "date_updated", table.date_updated);
JSON_ADD_NUMBER_TO_OBJECT(row, "number", table.number);
JSON_ADD_NUMBER_TO_OBJECT(row, "invalid_domains", table.invalid_domains);
Expand Down
8 changes: 5 additions & 3 deletions src/args.c
Expand Up @@ -325,20 +325,22 @@ void parse_args(int argc, char* argv[])

// If the first argument is "gravity" (e.g., /usr/bin/pihole-FTL gravity),
// we offer some specialized gravity tools
if(argc > 1 && strcmp(argv[1], "gravity") == 0)
if(argc > 1 && (strcmp(argv[1], "gravity") == 0 || strcmp(argv[1], "antigravity") == 0))
{
const bool antigravity = strcmp(argv[1], "antigravity") == 0;

// pihole-FTL gravity parseList <infile> <outfile> <adlistID>
if(argc == 6 && strcmp(argv[2], "parseList") == 0)
{
// Parse the given list and write the result to the given file
exit(gravity_parseList(argv[3], argv[4], argv[5], false));
exit(gravity_parseList(argv[3], argv[4], argv[5], false, antigravity));
}

// pihole-FTL gravity checkList <infile>
if(argc == 4 && strcmp(argv[2], "checkList") == 0)
{
// Parse the given list and write the result to the given file
exit(gravity_parseList(argv[3], "", "-1", true));
exit(gravity_parseList(argv[3], "", "-1", true, antigravity));
}

printf("Incorrect usage of pihole-FTL gravity subcommand\n");
Expand Down