David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 3 | #include <haproxy/api.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 4 | #include <haproxy/arg.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 5 | #include <haproxy/cfgparse.h> |
Willy Tarreau | 8d36697 | 2020-05-27 16:10:29 +0200 | [diff] [blame] | 6 | #include <haproxy/errors.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 7 | #include <haproxy/global.h> |
Willy Tarreau | cd72d8c | 2020-06-02 19:11:26 +0200 | [diff] [blame] | 8 | #include <haproxy/http.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 9 | #include <haproxy/http_ana.h> |
Willy Tarreau | 126ba3a | 2020-06-04 18:26:43 +0200 | [diff] [blame] | 10 | #include <haproxy/http_fetch.h> |
Willy Tarreau | 8773533 | 2020-06-04 09:08:41 +0200 | [diff] [blame] | 11 | #include <haproxy/http_htx.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 12 | #include <haproxy/htx.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 13 | #include <haproxy/sample.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 14 | #include <haproxy/tools.h> |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 15 | #include <dac.h> |
| 16 | |
| 17 | static struct { |
| 18 | void *atlasimgptr; |
| 19 | char *jsonpath; |
| 20 | char *cookiename; |
| 21 | size_t cookienamelen; |
| 22 | da_atlas_t atlas; |
| 23 | da_evidence_id_t useragentid; |
| 24 | da_severity_t loglevel; |
| 25 | char separator; |
| 26 | unsigned char daset:1; |
| 27 | } global_deviceatlas = { |
| 28 | .loglevel = 0, |
| 29 | .jsonpath = 0, |
| 30 | .cookiename = 0, |
| 31 | .cookienamelen = 0, |
| 32 | .useragentid = 0, |
| 33 | .daset = 0, |
| 34 | .separator = '|', |
| 35 | }; |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 36 | |
| 37 | static int da_json_file(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 38 | const struct proxy *defpx, const char *file, int line, |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 39 | char **err) |
| 40 | { |
| 41 | if (*(args[1]) == 0) { |
| 42 | memprintf(err, "deviceatlas json file : expects a json path.\n"); |
| 43 | return -1; |
| 44 | } |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 45 | global_deviceatlas.jsonpath = strdup(args[1]); |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | static int da_log_level(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 50 | const struct proxy *defpx, const char *file, int line, |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 51 | char **err) |
| 52 | { |
| 53 | int loglevel; |
| 54 | if (*(args[1]) == 0) { |
| 55 | memprintf(err, "deviceatlas log level : expects an integer argument.\n"); |
| 56 | return -1; |
| 57 | } |
| 58 | |
| 59 | loglevel = atol(args[1]); |
| 60 | if (loglevel < 0 || loglevel > 3) { |
| 61 | memprintf(err, "deviceatlas log level : expects a log level between 0 and 3, %s given.\n", args[1]); |
| 62 | } else { |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 63 | global_deviceatlas.loglevel = (da_severity_t)loglevel; |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | static int da_property_separator(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 70 | const struct proxy *defpx, const char *file, int line, |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 71 | char **err) |
| 72 | { |
| 73 | if (*(args[1]) == 0) { |
| 74 | memprintf(err, "deviceatlas property separator : expects a character argument.\n"); |
| 75 | return -1; |
| 76 | } |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 77 | global_deviceatlas.separator = *args[1]; |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 78 | return 0; |
| 79 | } |
| 80 | |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 81 | static int da_properties_cookie(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 82 | const struct proxy *defpx, const char *file, int line, |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 83 | char **err) |
| 84 | { |
| 85 | if (*(args[1]) == 0) { |
| 86 | memprintf(err, "deviceatlas cookie name : expects a string argument.\n"); |
| 87 | return -1; |
| 88 | } else { |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 89 | global_deviceatlas.cookiename = strdup(args[1]); |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 90 | } |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 91 | global_deviceatlas.cookienamelen = strlen(global_deviceatlas.cookiename); |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 92 | return 0; |
| 93 | } |
| 94 | |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 95 | static size_t da_haproxy_read(void *ctx, size_t len, char *buf) |
| 96 | { |
| 97 | return fread(buf, 1, len, ctx); |
| 98 | } |
| 99 | |
| 100 | static da_status_t da_haproxy_seek(void *ctx, off_t off) |
| 101 | { |
| 102 | return fseek(ctx, off, SEEK_SET) != -1 ? DA_OK : DA_SYS; |
| 103 | } |
| 104 | |
| 105 | static void da_haproxy_log(da_severity_t severity, da_status_t status, |
| 106 | const char *fmt, va_list args) |
| 107 | { |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 108 | if (global_deviceatlas.loglevel && severity <= global_deviceatlas.loglevel) { |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 109 | char logbuf[256]; |
| 110 | vsnprintf(logbuf, sizeof(logbuf), fmt, args); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 111 | ha_warning("deviceatlas : %s.\n", logbuf); |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 115 | #define DA_COOKIENAME_DEFAULT "DAPROPS" |
| 116 | |
Willy Tarreau | 876054d | 2016-12-21 20:39:16 +0100 | [diff] [blame] | 117 | /* |
| 118 | * module init / deinit functions. Returns 0 if OK, or a combination of ERR_*. |
| 119 | */ |
| 120 | static int init_deviceatlas(void) |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 121 | { |
Christopher Faulet | fc633b6 | 2020-11-06 15:24:23 +0100 | [diff] [blame] | 122 | int err_code = ERR_NONE; |
Willy Tarreau | 876054d | 2016-12-21 20:39:16 +0100 | [diff] [blame] | 123 | |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 124 | if (global_deviceatlas.jsonpath != 0) { |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 125 | FILE *jsonp; |
| 126 | da_property_decl_t extraprops[] = {{0, 0}}; |
| 127 | size_t atlasimglen; |
| 128 | da_status_t status; |
| 129 | |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 130 | jsonp = fopen(global_deviceatlas.jsonpath, "r"); |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 131 | if (jsonp == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 132 | ha_alert("deviceatlas : '%s' json file has invalid path or is not readable.\n", |
| 133 | global_deviceatlas.jsonpath); |
Willy Tarreau | 876054d | 2016-12-21 20:39:16 +0100 | [diff] [blame] | 134 | err_code |= ERR_ALERT | ERR_FATAL; |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 135 | goto out; |
| 136 | } |
| 137 | |
| 138 | da_init(); |
| 139 | da_seterrorfunc(da_haproxy_log); |
| 140 | status = da_atlas_compile(jsonp, da_haproxy_read, da_haproxy_seek, |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 141 | &global_deviceatlas.atlasimgptr, &atlasimglen); |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 142 | fclose(jsonp); |
| 143 | if (status != DA_OK) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 144 | ha_alert("deviceatlas : '%s' json file is invalid.\n", |
| 145 | global_deviceatlas.jsonpath); |
Willy Tarreau | 876054d | 2016-12-21 20:39:16 +0100 | [diff] [blame] | 146 | err_code |= ERR_ALERT | ERR_FATAL; |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 147 | goto out; |
| 148 | } |
| 149 | |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 150 | status = da_atlas_open(&global_deviceatlas.atlas, extraprops, |
| 151 | global_deviceatlas.atlasimgptr, atlasimglen); |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 152 | |
| 153 | if (status != DA_OK) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 154 | ha_alert("deviceatlas : data could not be compiled.\n"); |
Willy Tarreau | 876054d | 2016-12-21 20:39:16 +0100 | [diff] [blame] | 155 | err_code |= ERR_ALERT | ERR_FATAL; |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 156 | goto out; |
| 157 | } |
| 158 | |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 159 | if (global_deviceatlas.cookiename == 0) { |
| 160 | global_deviceatlas.cookiename = strdup(DA_COOKIENAME_DEFAULT); |
| 161 | global_deviceatlas.cookienamelen = strlen(global_deviceatlas.cookiename); |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 162 | } |
| 163 | |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 164 | global_deviceatlas.useragentid = da_atlas_header_evidence_id(&global_deviceatlas.atlas, |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 165 | "user-agent"); |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 166 | global_deviceatlas.daset = 1; |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 167 | |
| 168 | fprintf(stdout, "Deviceatlas module loaded.\n"); |
| 169 | } |
| 170 | |
| 171 | out: |
Willy Tarreau | 876054d | 2016-12-21 20:39:16 +0100 | [diff] [blame] | 172 | return err_code; |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 173 | } |
| 174 | |
Willy Tarreau | b149eed | 2016-12-21 21:03:49 +0100 | [diff] [blame] | 175 | static void deinit_deviceatlas(void) |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 176 | { |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 177 | if (global_deviceatlas.jsonpath != 0) { |
| 178 | free(global_deviceatlas.jsonpath); |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 179 | } |
| 180 | |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 181 | if (global_deviceatlas.daset == 1) { |
| 182 | free(global_deviceatlas.cookiename); |
| 183 | da_atlas_close(&global_deviceatlas.atlas); |
| 184 | free(global_deviceatlas.atlasimgptr); |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | da_fini(); |
| 188 | } |
| 189 | |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 190 | static int da_haproxy(const struct arg *args, struct sample *smp, da_deviceinfo_t *devinfo) |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 191 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 192 | struct buffer *tmp; |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 193 | da_propid_t prop, *pprop; |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 194 | da_status_t status; |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 195 | da_type_t proptype; |
| 196 | const char *propname; |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 197 | int i; |
| 198 | |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 199 | tmp = get_trash_chunk(); |
| 200 | chunk_reset(tmp); |
| 201 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 202 | propname = (const char *) args[0].data.str.area; |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 203 | i = 0; |
| 204 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 205 | for (; propname != 0; i ++, |
| 206 | propname = (const char *) args[i].data.str.area) { |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 207 | status = da_atlas_getpropid(&global_deviceatlas.atlas, |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 208 | propname, &prop); |
| 209 | if (status != DA_OK) { |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 210 | chunk_appendf(tmp, "%c", global_deviceatlas.separator); |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 211 | continue; |
| 212 | } |
| 213 | pprop = ∝ |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 214 | da_atlas_getproptype(&global_deviceatlas.atlas, *pprop, &proptype); |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 215 | |
| 216 | switch (proptype) { |
| 217 | case DA_TYPE_BOOLEAN: { |
| 218 | bool val; |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 219 | status = da_getpropboolean(devinfo, *pprop, &val); |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 220 | if (status == DA_OK) { |
| 221 | chunk_appendf(tmp, "%d", val); |
| 222 | } |
| 223 | break; |
| 224 | } |
| 225 | case DA_TYPE_INTEGER: |
| 226 | case DA_TYPE_NUMBER: { |
| 227 | long val; |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 228 | status = da_getpropinteger(devinfo, *pprop, &val); |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 229 | if (status == DA_OK) { |
| 230 | chunk_appendf(tmp, "%ld", val); |
| 231 | } |
| 232 | break; |
| 233 | } |
| 234 | case DA_TYPE_STRING: { |
| 235 | const char *val; |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 236 | status = da_getpropstring(devinfo, *pprop, &val); |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 237 | if (status == DA_OK) { |
| 238 | chunk_appendf(tmp, "%s", val); |
| 239 | } |
| 240 | break; |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 241 | } |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 242 | default: |
| 243 | break; |
| 244 | } |
| 245 | |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 246 | chunk_appendf(tmp, "%c", global_deviceatlas.separator); |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 247 | } |
| 248 | |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 249 | da_close(devinfo); |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 250 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 251 | if (tmp->data) { |
| 252 | --tmp->data; |
| 253 | tmp->area[tmp->data] = 0; |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 254 | } |
| 255 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 256 | smp->data.u.str.area = tmp->area; |
| 257 | smp->data.u.str.data = tmp->data; |
David Carlier | 7df4185 | 2019-07-10 21:19:24 +0100 | [diff] [blame] | 258 | smp->data.type = SMP_T_STR; |
David Carlier | 8167f30 | 2015-06-01 13:50:06 +0200 | [diff] [blame] | 259 | |
| 260 | return 1; |
| 261 | } |
| 262 | |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 263 | static int da_haproxy_conv(const struct arg *args, struct sample *smp, void *private) |
| 264 | { |
| 265 | da_deviceinfo_t devinfo; |
| 266 | da_status_t status; |
| 267 | const char *useragent; |
| 268 | char useragentbuf[1024] = { 0 }; |
| 269 | int i; |
| 270 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 271 | if (global_deviceatlas.daset == 0 || smp->data.u.str.data == 0) { |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 272 | return 1; |
| 273 | } |
| 274 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 275 | i = smp->data.u.str.data > sizeof(useragentbuf) ? sizeof(useragentbuf) : smp->data.u.str.data; |
| 276 | memcpy(useragentbuf, smp->data.u.str.area, i - 1); |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 277 | useragentbuf[i - 1] = 0; |
| 278 | |
| 279 | useragent = (const char *)useragentbuf; |
| 280 | |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 281 | status = da_search(&global_deviceatlas.atlas, &devinfo, |
| 282 | global_deviceatlas.useragentid, useragent, 0); |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 283 | |
| 284 | return status != DA_OK ? 0 : da_haproxy(args, smp, &devinfo); |
| 285 | } |
| 286 | |
| 287 | #define DA_MAX_HEADERS 24 |
| 288 | |
| 289 | static int da_haproxy_fetch(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 290 | { |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 291 | da_evidence_t ev[DA_MAX_HEADERS]; |
| 292 | da_deviceinfo_t devinfo; |
| 293 | da_status_t status; |
David CARLIER | 4de0eba | 2019-04-24 20:41:53 +0100 | [diff] [blame] | 294 | struct channel *chn; |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 295 | struct htx *htx; |
| 296 | struct htx_blk *blk; |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 297 | char vbuf[DA_MAX_HEADERS][1024] = {{ 0 }}; |
| 298 | int i, nbh = 0; |
| 299 | |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 300 | if (global_deviceatlas.daset == 0) { |
David CARLIER | 4de0eba | 2019-04-24 20:41:53 +0100 | [diff] [blame] | 301 | return 0; |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 302 | } |
| 303 | |
David CARLIER | 4de0eba | 2019-04-24 20:41:53 +0100 | [diff] [blame] | 304 | chn = (smp->strm ? &smp->strm->req : NULL); |
Christopher Faulet | d1185cc | 2020-05-05 11:49:57 +0200 | [diff] [blame] | 305 | htx = smp_prefetch_htx(smp, chn, NULL, 1); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 306 | if (!htx) |
| 307 | return 0; |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 308 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 309 | i = 0; |
| 310 | for (blk = htx_get_first_blk(htx); nbh < DA_MAX_HEADERS && blk; blk = htx_get_next_blk(htx, blk)) { |
| 311 | size_t vlen; |
| 312 | char *pval; |
| 313 | da_evidence_id_t evid; |
| 314 | enum htx_blk_type type; |
| 315 | struct ist n, v; |
| 316 | char hbuf[24] = { 0 }; |
| 317 | char tval[1024] = { 0 }; |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 318 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 319 | type = htx_get_blk_type(blk); |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 320 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 321 | if (type == HTX_BLK_HDR) { |
| 322 | n = htx_get_blk_name(htx, blk); |
| 323 | v = htx_get_blk_value(htx, blk); |
| 324 | } else if (type == HTX_BLK_EOH) { |
| 325 | break; |
| 326 | } else { |
| 327 | continue; |
| 328 | } |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 329 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 330 | /* The HTTP headers used by the DeviceAtlas API are not longer */ |
| 331 | if (n.len >= sizeof(hbuf)) { |
| 332 | continue; |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 333 | } |
| 334 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 335 | memcpy(hbuf, n.ptr, n.len); |
| 336 | hbuf[n.len] = 0; |
| 337 | pval = v.ptr; |
| 338 | vlen = v.len; |
| 339 | evid = -1; |
| 340 | i = v.len > sizeof(tval) - 1 ? sizeof(tval) - 1 : v.len; |
| 341 | memcpy(tval, v.ptr, i); |
| 342 | tval[i] = 0; |
| 343 | pval = tval; |
David CARLIER | 4de0eba | 2019-04-24 20:41:53 +0100 | [diff] [blame] | 344 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 345 | if (strcasecmp(hbuf, "Accept-Language") == 0) { |
| 346 | evid = da_atlas_accept_language_evidence_id(&global_deviceatlas.atlas); |
| 347 | } else if (strcasecmp(hbuf, "Cookie") == 0) { |
| 348 | char *p, *eval; |
| 349 | size_t pl; |
David CARLIER | 4de0eba | 2019-04-24 20:41:53 +0100 | [diff] [blame] | 350 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 351 | eval = pval + vlen; |
| 352 | /** |
| 353 | * The cookie value, if it exists, is located between the current header's |
| 354 | * value position and the next one |
| 355 | */ |
| 356 | if (http_extract_cookie_value(pval, eval, global_deviceatlas.cookiename, |
| 357 | global_deviceatlas.cookienamelen, 1, &p, &pl) == NULL) { |
David CARLIER | 4de0eba | 2019-04-24 20:41:53 +0100 | [diff] [blame] | 358 | continue; |
| 359 | } |
| 360 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 361 | vlen -= global_deviceatlas.cookienamelen - 1; |
| 362 | pval = p; |
| 363 | evid = da_atlas_clientprop_evidence_id(&global_deviceatlas.atlas); |
| 364 | } else { |
| 365 | evid = da_atlas_header_evidence_id(&global_deviceatlas.atlas, hbuf); |
| 366 | } |
David CARLIER | 4de0eba | 2019-04-24 20:41:53 +0100 | [diff] [blame] | 367 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 368 | if (evid == -1) { |
| 369 | continue; |
David CARLIER | 4de0eba | 2019-04-24 20:41:53 +0100 | [diff] [blame] | 370 | } |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 371 | |
| 372 | i = vlen > sizeof(vbuf[nbh]) - 1 ? sizeof(vbuf[nbh]) - 1 : vlen; |
| 373 | memcpy(vbuf[nbh], pval, i); |
| 374 | vbuf[nbh][i] = 0; |
| 375 | ev[nbh].key = evid; |
| 376 | ev[nbh].value = vbuf[nbh]; |
| 377 | ++ nbh; |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 378 | } |
| 379 | |
Willy Tarreau | bee9dde | 2016-12-21 21:25:06 +0100 | [diff] [blame] | 380 | status = da_searchv(&global_deviceatlas.atlas, &devinfo, |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 381 | ev, nbh); |
| 382 | |
| 383 | return status != DA_OK ? 0 : da_haproxy(args, smp, &devinfo); |
| 384 | } |
| 385 | |
Willy Tarreau | 0d74f77 | 2015-06-01 15:42:29 +0200 | [diff] [blame] | 386 | static struct cfg_kw_list dacfg_kws = {{ }, { |
| 387 | { CFG_GLOBAL, "deviceatlas-json-file", da_json_file }, |
David CARLIER | 4de0eba | 2019-04-24 20:41:53 +0100 | [diff] [blame] | 388 | { CFG_GLOBAL, "deviceatlas-log-level", da_log_level }, |
| 389 | { CFG_GLOBAL, "deviceatlas-property-separator", da_property_separator }, |
| 390 | { CFG_GLOBAL, "deviceatlas-properties-cookie", da_properties_cookie }, |
| 391 | { 0, NULL, NULL }, |
Willy Tarreau | 0d74f77 | 2015-06-01 15:42:29 +0200 | [diff] [blame] | 392 | }}; |
| 393 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 394 | INITCALL1(STG_REGISTER, cfg_register_keywords, &dacfg_kws); |
| 395 | |
Willy Tarreau | f63386a | 2015-06-01 15:39:50 +0200 | [diff] [blame] | 396 | /* Note: must not be declared <const> as its list will be overwritten */ |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 397 | static struct sample_fetch_kw_list fetch_kws = {ILH, { |
David Carlier | 840b024 | 2016-03-16 10:09:55 +0000 | [diff] [blame] | 398 | { "da-csv-fetch", da_haproxy_fetch, ARG12(1,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
David CARLIER | 4de0eba | 2019-04-24 20:41:53 +0100 | [diff] [blame] | 399 | { NULL, NULL, 0, 0, 0 }, |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 400 | }}; |
| 401 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 402 | INITCALL1(STG_REGISTER, sample_register_fetches, &fetch_kws); |
| 403 | |
David Carlier | 608c65a | 2015-09-25 14:16:30 +0100 | [diff] [blame] | 404 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | f63386a | 2015-06-01 15:39:50 +0200 | [diff] [blame] | 405 | static struct sample_conv_kw_list conv_kws = {ILH, { |
David Carlier | 840b024 | 2016-03-16 10:09:55 +0000 | [diff] [blame] | 406 | { "da-csv-conv", da_haproxy_conv, ARG12(1,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR), NULL, SMP_T_STR, SMP_T_STR }, |
David CARLIER | 4de0eba | 2019-04-24 20:41:53 +0100 | [diff] [blame] | 407 | { NULL, NULL, 0, 0, 0 }, |
Willy Tarreau | f63386a | 2015-06-01 15:39:50 +0200 | [diff] [blame] | 408 | }}; |
| 409 | |
David Carlier | 0470d70 | 2019-04-26 12:02:28 +0000 | [diff] [blame] | 410 | static void da_haproxy_register_build_options() |
| 411 | { |
| 412 | char *ptr = NULL; |
| 413 | |
| 414 | #ifdef MOBI_DA_DUMMY_LIBRARY |
| 415 | memprintf(&ptr, "Built with DeviceAtlas support (dummy library only)."); |
| 416 | #else |
| 417 | memprintf(&ptr, "Built with DeviceAtlas support (library version %u.%u).", MOBI_DA_MAJOR, MOBI_DA_MINOR); |
| 418 | #endif |
| 419 | hap_register_build_opts(ptr, 1); |
| 420 | } |
| 421 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 422 | INITCALL1(STG_REGISTER, sample_register_convs, &conv_kws); |
| 423 | |
Willy Tarreau | 172f5ce | 2018-11-26 11:21:50 +0100 | [diff] [blame] | 424 | REGISTER_POST_CHECK(init_deviceatlas); |
| 425 | REGISTER_POST_DEINIT(deinit_deviceatlas); |
David Carlier | 0470d70 | 2019-04-26 12:02:28 +0000 | [diff] [blame] | 426 | INITCALL0(STG_REGISTER, da_haproxy_register_build_options); |