summaryrefslogtreecommitdiffstats
path: root/src/detection/publicip/publicip.c
blob: abca57c3e1535ffc48be33b250b0b099cb72f8e2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "publicip.h"
#include "common/networking.h"

#define FF_UNINITIALIZED ((const char*) (uintptr_t) -1)
static FFNetworkingState states[2];
static const char* statuses[2] = { FF_UNINITIALIZED, FF_UNINITIALIZED };

void ffPreparePublicIp(FFPublicIPOptions* options) {
    FFNetworkingState* state = &states[options->ipv6];
    const char** status = &statuses[options->ipv6];
    if (*status != FF_UNINITIALIZED) {
        fputs("Error: PublicIp module can only be used once due to internal limitations\n", stderr);
        exit(1);
    }

    state->timeout = options->timeout;
    state->ipv6 = options->ipv6;

    if (options->url.length == 0) {
        state->compression = true;
        state->tfo = true;
        *status = ffNetworkingSendHttpRequest(state, options->ipv6 ? "v6.ipinfo.io" : "ipinfo.io", "/json", NULL);
    } else {
        FF_STRBUF_AUTO_DESTROY host = ffStrbufCreateCopy(&options->url);
        uint32_t hostStartIndex = ffStrbufFirstIndexS(&host, "://");
        if (hostStartIndex < host.length) {
            if (hostStartIndex != 4 || !ffStrbufStartsWithIgnCaseS(&host, "http")) {
                fputs("Error: only http: protocol is supported. Use `Command` module with `curl` if needed\n", stderr);
                exit(1);
            }
            ffStrbufSubstrAfter(&host, hostStartIndex + (uint32_t) (strlen("://") - 1));
        }
        uint32_t pathStartIndex = ffStrbufFirstIndexC(&host, '/');

        FF_STRBUF_AUTO_DESTROY path = ffStrbufCreate();
        if (pathStartIndex != host.length) {
            ffStrbufAppendNS(&path, pathStartIndex, host.chars + (host.length - pathStartIndex));
            host.length = pathStartIndex;
            host.chars[pathStartIndex] = '\0';
        }

        *status = ffNetworkingSendHttpRequest(state, host.chars, path.length == 0 ? "/" : path.chars, NULL);
    }
}

static inline void wrapYyjsonFree(yyjson_doc** doc) {
    assert(doc);
    if (*doc) {
        yyjson_doc_free(*doc);
    }
}

const char* ffDetectPublicIp(FFPublicIPOptions* options, FFPublicIpResult* result) {
    FFNetworkingState* state = &states[options->ipv6];
    const char** status = &statuses[options->ipv6];
    if (*status == FF_UNINITIALIZED) {
        ffPreparePublicIp(options);
    }

    if (*status != NULL) {
        return *status;
    }

    FF_STRBUF_AUTO_DESTROY response = ffStrbufCreateA(4096);
    const char* error = ffNetworkingRecvHttpResponse(state, &response);

    *state = (FFNetworkingState) {};
    *status = FF_UNINITIALIZED;

    if (error == NULL) {
        ffStrbufSubstrAfterFirstS(&response, "\r\n\r\n");
    } else {
        return error;
    }

    if (response.length == 0) {
        return "Empty server response received";
    }

    if (options->url.length == 0) {
        yyjson_doc* FF_A_CLEANUP(wrapYyjsonFree) doc = yyjson_read_opts(response.chars, response.length, 0, NULL, NULL);
        if (doc) {
            yyjson_val* root = yyjson_doc_get_root(doc);
            ffStrbufAppendJsonVal(&result->ip, yyjson_obj_get(root, "ip"));
            ffStrbufDestroy(&result->location);
            ffStrbufInitF(&result->location, "%s, %s", yyjson_get_str(yyjson_obj_get(root, "city")), yyjson_get_str(yyjson_obj_get(root, "country")));
            return NULL;
        }
    }

    ffStrbufDestroy(&result->ip);
    ffStrbufInitMove(&result->ip, &response);
    ffStrbufTrimRightSpace(&result->ip);
    return NULL;
}