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