├── config ├── .gitignore ├── README.md ├── LICENSE └── ngx_http_ipip_module.c /config: -------------------------------------------------------------------------------- 1 | ngx_addon_name=ngx_http_ipip_module 2 | HTTP_MODULES="$HTTP_MODULES ngx_http_ipip_module" 3 | NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_ipip_module.c" 4 | 5 | if test -n "$ngx_module_link"; then 6 | ngx_module_type=HTTP 7 | ngx_module_name=ngx_http_ipip_module 8 | ngx_module_srcs="$ngx_addon_dir/ngx_http_ipip_module.c" 9 | 10 | . auto/module 11 | else 12 | HTTP_MODULES="$HTTP_MODULES ngx_http_ipip_module" 13 | NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_ipip_module.c" 14 | fi 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # nginx-ipip-module 2 | Nginx ipip module support datx format 3 | # IPIP.net 4 | Free offline database download https://www.ipip.net/free_download/ 5 | Advanced paid version 6 | [English](https://en.ipip.net/product/ip.html) [中文](https://www.ipip.net/product/ip.html) 7 | 8 | # Installing 9 | #### download (nginx 1.12.1 +) 10 | wget http://nginx.org/download/nginx-VERSION.tar.gz 11 | tar zxvf nginx-VERSION.tar.gz 12 | #### download nginx-ipip-module 13 | git clone https://github.com/ipipdotnet/nginx-ipip-module 14 | 15 | #### compile 16 | cd nginx-VERSION 17 | ./configure --with-compat --add-dynamic-module=../nginx-ipip-module 18 | make modules . 19 | make install 20 | 21 | # Example 22 | http { 23 | 24 | # Specify the data file path 25 | ipip_db /root/ipip.datx 60m; # 60 minute auto reload db file 26 | 27 | server { 28 | listen 80; 29 | server_name localhost; 30 | 31 | # Specifies the IP address to parse, Default $remote_addr 32 | ipip_parse_ip $http_x_forwarded_for; 33 | 34 | location / { 35 | if ($ipip_country_code = HK) { 36 | return 403; 37 | } 38 | 39 | if ($ipip_country_code ~* "(MO|TW)") { 40 | return 403; 41 | } 42 | 43 | if ($ipip_country_code !~ CN) { 44 | return 403; 45 | } 46 | 47 | add_header ip $http_x_forwarded_for; 48 | add_header country_code $ipip_country_code; 49 | add_header country $ipip_country_name; 50 | add_header province $ipip_region_name; 51 | add_header city $ipip_city_name; 52 | add_header owner $ipip_owner_domain; 53 | add_header isp $ipip_isp_domain; 54 | add_header latitude $ipip_latitude; 55 | add_header longitude $ipip_longitude; 56 | 57 | root html; 58 | index index.html index.htm; 59 | } 60 | } 61 | } 62 | 63 | # Directive 64 | ipip_db /path/db/file 60m; Specify the database file path and automatic update time interval 65 | ipip_parse_ip $remote_addr; Specifies the variable that gets the IP 66 | 67 | # Variables 68 | #### Buying a paid database gives you more data 69 | $ipip_continent_code 70 | $ipip_country_name 71 | $ipip_country_code 72 | $ipip_region_name 73 | $ipip_city_name 74 | $ipip_owner_domain 75 | $ipip_isp_domain 76 | $ipip_latitude 77 | $ipip_longitude 78 | $ipip_timezone 79 | $ipip_utc_offset 80 | $ipip_china_admin_code 81 | $ipip_idd_code 82 | $ipip_idc 83 | $ipip_base_station 84 | $ipip_anycast 85 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /ngx_http_ipip_module.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @brief IpIp module for Nginx. 3 | * 4 | * @section LICENSE 5 | * 6 | * Copyright (C) 2018 by ipip.net 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * 24 | */ 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #include 31 | #include 32 | #include 33 | 34 | typedef unsigned char byte; 35 | typedef unsigned int uint; 36 | #define B2IL(b) (((b)[0] & 0xFF) | (((b)[1] << 8) & 0xFF00) | (((b)[2] << 16) & 0xFF0000) | (((b)[3] << 24) & 0xFF000000)) 37 | #define B2IU(b) (((b)[3] & 0xFF) | (((b)[2] << 8) & 0xFF00) | (((b)[1] << 16) & 0xFF0000) | (((b)[0] << 24) & 0xFF000000)) 38 | 39 | struct DBContext { 40 | byte *data; 41 | byte *index; 42 | uint *flag; 43 | uint offset; 44 | } ; 45 | typedef struct { 46 | ngx_str_t db_name; 47 | struct DBContext *db_ctx; 48 | ngx_http_complex_value_t ip_source; 49 | 50 | time_t last_check; 51 | time_t last_change; 52 | time_t check_interval; 53 | } ngx_http_ipip_conf_t; 54 | 55 | char *strtok_r_2(char *str, char const *delims, char **context) { 56 | char *p = NULL, *ret = NULL; 57 | 58 | if (str != NULL) { 59 | *context = str; 60 | } 61 | 62 | if (*context == NULL) { 63 | return NULL; 64 | } 65 | 66 | if ((p = strpbrk(*context, delims)) != NULL) { 67 | *p = 0; 68 | ret = *context; 69 | *context = ++p; 70 | } 71 | else if (**context) { 72 | ret = *context; 73 | *context = NULL; 74 | } 75 | return ret; 76 | } 77 | 78 | static struct DBContext* init_db(const char* ipdb, int* error_code, ngx_conf_t *cf); 79 | static ngx_int_t ngx_http_ipip_reload_db(ngx_http_ipip_conf_t *icf); 80 | static int destroy(struct DBContext* ctx); 81 | static ngx_int_t find_result_by_ip(const struct DBContext* ctx,const char *ip, char *result); 82 | static ngx_int_t ngx_http_ipip_addr_str(ngx_http_request_t *r, char* ipstr); 83 | 84 | int destroy(struct DBContext* ctx) { 85 | if (ctx->flag != NULL) { 86 | free(ctx->flag); 87 | } 88 | if (ctx->index != NULL) { 89 | free(ctx->index); 90 | } 91 | if (ctx->data != NULL) { 92 | free(ctx->data); 93 | } 94 | ctx->offset = 0; 95 | free(ctx); 96 | return 0; 97 | } 98 | 99 | struct DBContext* init_db(const char* ipdb, int* error_code, ngx_conf_t *cf) { 100 | int read_count = 0; 101 | int copy_bytes = 0; 102 | struct DBContext* ctx = (struct DBContext *)malloc(sizeof(struct DBContext)); 103 | if (ctx == NULL) { 104 | (*error_code) = 1; 105 | return NULL; 106 | } 107 | ctx->data = NULL; 108 | ctx->index = NULL; 109 | ctx->flag = NULL; 110 | ctx->offset = 0; 111 | 112 | FILE *file = fopen(ipdb, "rb"); 113 | if (file == NULL) { 114 | (*error_code) = 2; 115 | free(ctx); 116 | return NULL; 117 | } 118 | fseek(file, 0, SEEK_END); 119 | long size = ftell(file); 120 | fseek(file, 0, SEEK_SET); 121 | 122 | ctx->data = (byte *) malloc(size * sizeof(byte)); 123 | read_count = fread(ctx->data, sizeof(byte), (size_t) size, file); 124 | if (read_count <= 0) { 125 | (*error_code) = 3; 126 | free(ctx->data); 127 | free(ctx); 128 | return NULL; 129 | } 130 | //ngx_conf_log_error(NGX_LOG_NOTICE, cf, 0, "total read %d bytes data", read_count); 131 | 132 | fclose(file); 133 | 134 | uint indexLength = B2IU(ctx->data); 135 | 136 | //ngx_conf_log_error(NGX_LOG_NOTICE, cf, 0, "index len = %d", indexLength); 137 | 138 | ctx->index = (byte *) malloc(indexLength * sizeof(byte)); 139 | if (ctx->index == NULL) { 140 | (*error_code) = 4; 141 | free(ctx->data); 142 | free(ctx); 143 | return NULL; 144 | } 145 | if (indexLength > size - 4) { 146 | copy_bytes = size - 4; 147 | } else { 148 | copy_bytes = indexLength; 149 | } 150 | 151 | ngx_memcpy(ctx->index, ctx->data + 4, copy_bytes); 152 | 153 | ctx->offset = indexLength; 154 | 155 | int flag_bytes = 65536 * sizeof(uint); 156 | ctx->flag = (uint *) malloc(flag_bytes); 157 | if (copy_bytes > flag_bytes) { 158 | copy_bytes = flag_bytes; 159 | } 160 | //ngx_conf_log_error(NGX_LOG_NOTICE, cf, 0, "copy %d bytes from index to flag", copy_bytes); 161 | ngx_memcpy(ctx->flag, ctx->index, copy_bytes); 162 | 163 | return ctx; 164 | } 165 | 166 | static ngx_int_t find_result_by_ip(const struct DBContext* ctx, const char *ip, char *result) { 167 | if (ctx == NULL) { 168 | return NGX_ERROR; 169 | } 170 | uint ips[4]; 171 | int num = sscanf(ip, "%d.%d.%d.%d", &ips[0], &ips[1], &ips[2], &ips[3]); 172 | if (num == 4) { 173 | uint ip_prefix_value = ips[0] * 256 + ips[1]; 174 | uint ip2long_value = B2IU(ips); 175 | uint start = ctx->flag[ip_prefix_value]; 176 | uint max_comp_len = ctx->offset - 262144 - 4; 177 | uint index_offset = 0; 178 | uint index_length = 0; 179 | 180 | //uint begin = start * 9 + 262144; 181 | uint end = max_comp_len; 182 | uint low = start * 9 + 262144; 183 | uint high = end; 184 | while (low <= high) { 185 | uint mid = low + ((high - low) >> 1); 186 | mid = low + (uint)((mid - low) / 9) * 9; 187 | if (B2IU(ctx->index + mid) < ip2long_value) { 188 | low = mid + 9; 189 | } else { 190 | high = mid - 9; 191 | } 192 | } 193 | uint target = low; 194 | index_offset = B2IL(ctx->index + target + 4) & 0x00FFFFFF; 195 | index_length = (ctx->index[target+7] << 8) + ctx->index[target+8]; 196 | //fprintf(stderr, "index length = %u\n", index_length); 197 | memcpy(result, ctx->data + ctx->offset + index_offset - 262144, index_length); 198 | result[index_length] = '\0'; 199 | } 200 | return NGX_OK; 201 | } 202 | 203 | ngx_module_t ngx_http_ipip_module; 204 | 205 | static ngx_int_t get_element(ngx_http_request_t *r, char* result, 206 | ngx_http_ipip_conf_t* icf, int index) { 207 | struct DBContext *db_ctx = icf->db_ctx; 208 | int errorcode; 209 | char ipstr[32]; 210 | ngx_str_t complex_ip_val; 211 | 212 | if (icf->ip_source.value.len > 0) { 213 | if (ngx_http_complex_value(r, &icf->ip_source, &complex_ip_val) != NGX_OK) { 214 | return NGX_ERROR; 215 | } 216 | complex_ip_val.data[complex_ip_val.len] = '\0'; 217 | } else { 218 | ngx_http_ipip_addr_str(r, ipstr); 219 | complex_ip_val.len = ngx_strlen(ipstr); 220 | complex_ip_val.data = ngx_pnalloc(r->pool, complex_ip_val.len+1); 221 | ngx_memcpy(complex_ip_val.data, ipstr, complex_ip_val.len); 222 | complex_ip_val.data[complex_ip_val.len] = '\0'; 223 | } 224 | 225 | char db_result[1024] = {"\0"}; 226 | errorcode = find_result_by_ip(db_ctx, (const char*)complex_ip_val.data, db_result); 227 | if (errorcode != NGX_OK) { 228 | return errorcode; 229 | } 230 | 231 | char *rst = NULL; 232 | char *lasts = NULL; 233 | rst = strtok_r_2(db_result, "\t", &lasts); 234 | int cnt = 0; 235 | while (rst) { 236 | if (index == cnt) { 237 | size_t rlen = ngx_strlen(rst); 238 | ngx_memcpy(result, rst, rlen); 239 | result[rlen] = '\0'; 240 | break; 241 | } 242 | rst = strtok_r_2(NULL, "\t", &lasts); 243 | ++ cnt; 244 | } 245 | if (cnt < index) { 246 | return NGX_ERROR; 247 | } 248 | 249 | return NGX_OK; 250 | } 251 | 252 | static ngx_int_t ngx_ipip_set_variable(ngx_http_request_t *r, 253 | ngx_http_variable_value_t *v, int index) { 254 | ngx_http_ipip_conf_t *icf = ngx_http_get_module_main_conf(r, ngx_http_ipip_module); 255 | 256 | ngx_http_ipip_reload_db(icf); 257 | 258 | char result[256] = {"\0"}; 259 | size_t val_len; 260 | 261 | int ret = get_element(r, result, icf, index); 262 | if (ret != NGX_OK) { 263 | return ret; 264 | } 265 | 266 | val_len = ngx_strlen(result); 267 | v->data = ngx_pnalloc(r->pool, val_len + 1); 268 | if (v->data == NULL) { 269 | return NGX_ERROR; 270 | } 271 | 272 | ngx_memcpy(v->data, result, val_len); 273 | v->data[val_len] = '\0'; 274 | 275 | v->len = val_len; 276 | v->valid = 1; 277 | v->no_cacheable = 0; 278 | v->not_found = 0; 279 | 280 | return NGX_OK; 281 | } 282 | 283 | // Keep this order please 284 | #define NGX_IPIP_COUNTRY_NAME_CODE 0 285 | #define NGX_IPIP_REGION_NAME_COEE 1 286 | #define NGX_IPIP_CITY_NAME_CODE 2 287 | #define NGX_IPIP_OWNER_DOMAIN_CODE 3 288 | #define NGX_IPIP_ISP_DOMAIN_CODE 4 289 | #define NGX_IPIP_LATITUDE_CODE 5 290 | #define NGX_IPIP_LONGITUDE_CODE 6 291 | #define NGX_IPIP_TIMEZONE_CODE 7 292 | #define NGX_IPIP_UTC_OFFSET_CODE 8 293 | #define NGX_IPIP_CHINA_ADMIN_CODE 9 294 | #define NGX_IPIP_IDD_CODE_CODE 10 295 | #define NGX_IPIP_COUNTRY_CODE_CODE 11 296 | #define NGX_IPIP_CONTINENT_CODE_CODE 12 297 | #define NGX_IPIP_IDC_CODE 13 298 | #define NGX_IPIP_BASE_STATION_CODE 14 299 | #define NGX_IPIP_COUNTRY_CODE3 15 300 | #define NGX_IPIP_EUROPEAN_UNION 16 301 | #define NGX_IPIP_CURRENCY_CODE 17 302 | #define NGX_IPIP_CURRENCY_NAME 18 303 | #define NGX_IPIP_ANYCAST_CODE 19 304 | 305 | static char *ngx_http_ipip_db(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); 306 | static char *ngx_ipip_parse_ip(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); 307 | static char *ngx_ipip_add_parse_ip_variable(ngx_conf_t *cf, ngx_command_t *dummy, 308 | void *conf); 309 | 310 | static ngx_int_t ngx_http_ipip_add_variables(ngx_conf_t *cf); 311 | 312 | static ngx_int_t ngx_http_ipip_country_variable(ngx_http_request_t *r, 313 | ngx_http_variable_value_t *v, uintptr_t data); 314 | static ngx_int_t ngx_http_ipip_region_variable(ngx_http_request_t *r, 315 | ngx_http_variable_value_t *v, uintptr_t data); 316 | static ngx_int_t ngx_http_ipip_city_variable(ngx_http_request_t *r, 317 | ngx_http_variable_value_t *v, uintptr_t data); 318 | static ngx_int_t ngx_http_ipip_owner_domain_variable(ngx_http_request_t *r, 319 | ngx_http_variable_value_t *v, uintptr_t data); 320 | static ngx_int_t ngx_http_ipip_isp_domain_variable(ngx_http_request_t *r, 321 | ngx_http_variable_value_t *v, uintptr_t data); 322 | static ngx_int_t ngx_http_ipip_latitude_variable(ngx_http_request_t *r, 323 | ngx_http_variable_value_t *v, uintptr_t data); 324 | static ngx_int_t ngx_http_ipip_longitude_variable(ngx_http_request_t *r, 325 | ngx_http_variable_value_t *v, uintptr_t data); 326 | static ngx_int_t ngx_http_ipip_timezone_variable(ngx_http_request_t *r, 327 | ngx_http_variable_value_t *v, uintptr_t data); 328 | 329 | static ngx_int_t ngx_http_ipip_utc_offset_variable(ngx_http_request_t *r, 330 | ngx_http_variable_value_t *v, uintptr_t data); 331 | static ngx_int_t ngx_http_ipip_china_admin_code_variable(ngx_http_request_t *r, 332 | ngx_http_variable_value_t *v, uintptr_t data); 333 | static ngx_int_t ngx_http_ipip_idd_code_variable(ngx_http_request_t *r, 334 | ngx_http_variable_value_t *v, uintptr_t data); 335 | 336 | static ngx_int_t ngx_http_ipip_country_code_variable(ngx_http_request_t *r, 337 | ngx_http_variable_value_t *v, uintptr_t data); 338 | static ngx_int_t ngx_http_ipip_continent_code_variable(ngx_http_request_t *r, 339 | ngx_http_variable_value_t *v, uintptr_t data); 340 | static ngx_int_t ngx_http_ipip_idc_variable(ngx_http_request_t *r, 341 | ngx_http_variable_value_t *v, uintptr_t data); 342 | static ngx_int_t ngx_http_ipip_base_station_variable(ngx_http_request_t *r, 343 | ngx_http_variable_value_t *v, uintptr_t data); 344 | static ngx_int_t ngx_http_ipip_country_code3_variable(ngx_http_request_t *r, 345 | ngx_http_variable_value_t *v, uintptr_t data); 346 | 347 | static ngx_int_t ngx_http_ipip_european_union_variable(ngx_http_request_t *r, 348 | ngx_http_variable_value_t *v, uintptr_t data); 349 | static ngx_int_t ngx_http_ipip_currency_code_variable(ngx_http_request_t *r, 350 | ngx_http_variable_value_t *v, uintptr_t data); 351 | static ngx_int_t ngx_http_ipip_currency_name_variable(ngx_http_request_t *r, 352 | ngx_http_variable_value_t *v, uintptr_t data); 353 | 354 | 355 | 356 | 357 | static ngx_int_t ngx_http_ipip_anycast_variable(ngx_http_request_t *r, 358 | ngx_http_variable_value_t *v, uintptr_t data); 359 | 360 | static void *ngx_http_ipip_create_conf(ngx_conf_t *cf); 361 | static void ngx_http_ipip_cleanup(void *data); 362 | /** 363 | * This module provided directive: ipip. 364 | * 365 | */ 366 | static ngx_command_t ngx_http_ipip_commands[] = { 367 | 368 | { ngx_string("ipip_db"), /* directive */ 369 | NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE2, /* location context and takes*/ 370 | ngx_http_ipip_db, /* configuration setup function */ 371 | NGX_HTTP_MAIN_CONF_OFFSET, /* No offset. Only one context is supported. */ 372 | 0, /* No offset when storing the module configuration on struct. */ 373 | NULL}, 374 | { ngx_string("ipip_parse_ip"), /* directive */ 375 | NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1, 376 | ngx_ipip_parse_ip, /* configuration setup function */ 377 | NGX_HTTP_MAIN_CONF_OFFSET | NGX_HTTP_LOC_CONF_OFFSET | NGX_HTTP_SRV_CONF_OFFSET, /* No offset. Only one context is supported. */ 378 | 0, /* No offset when storing the module configuration on struct. */ 379 | NULL}, 380 | 381 | ngx_null_command /* command termination */ 382 | }; 383 | 384 | /* The module context. */ 385 | static ngx_http_module_t ngx_http_ipip_module_ctx = { 386 | ngx_http_ipip_add_variables, /* preconfiguration */ 387 | NULL, /* postconfiguration */ 388 | 389 | ngx_http_ipip_create_conf, /* create main configuration */ 390 | NULL, /* init main configuration */ 391 | 392 | NULL, /* create server configuration */ 393 | NULL, /* merge server configuration */ 394 | 395 | NULL, /* create location configuration */ 396 | NULL /* merge location configuration */ 397 | }; 398 | 399 | /* Module definition. */ 400 | ngx_module_t ngx_http_ipip_module = { 401 | NGX_MODULE_V1, 402 | &ngx_http_ipip_module_ctx, /* module context */ 403 | ngx_http_ipip_commands, /* module directives */ 404 | NGX_HTTP_MODULE, /* module type */ 405 | NULL, /* init master */ 406 | NULL, /* init module */ 407 | NULL, /* init process */ 408 | NULL, /* init thread */ 409 | NULL, /* exit thread */ 410 | NULL, /* exit process */ 411 | NULL, /* exit master */ 412 | NGX_MODULE_V1_PADDING 413 | }; 414 | 415 | static ngx_http_variable_t ngx_http_ipip_vars[] = { 416 | 417 | { ngx_string("ipip_country_name"), NULL, 418 | ngx_http_ipip_country_variable, 419 | 0, 0, 0 }, 420 | 421 | { ngx_string("ipip_region_name"), NULL, 422 | ngx_http_ipip_region_variable, 423 | 0, 0, 0 }, 424 | 425 | { ngx_string("ipip_city_name"), NULL, 426 | ngx_http_ipip_city_variable, 427 | 0, 0, 0 }, 428 | 429 | { ngx_string("ipip_owner_domain"), NULL, 430 | ngx_http_ipip_owner_domain_variable, 431 | 0, 0, 0 }, 432 | 433 | { ngx_string("ipip_isp_domain"), NULL, 434 | ngx_http_ipip_isp_domain_variable, 435 | 0, 0, 0 }, 436 | 437 | { ngx_string("ipip_latitude"), NULL, 438 | ngx_http_ipip_latitude_variable, 439 | 0, 0, 0 }, 440 | 441 | { ngx_string("ipip_longitude"), NULL, 442 | ngx_http_ipip_longitude_variable, 443 | 0, 0, 0 }, 444 | 445 | { ngx_string("ipip_timezone"), NULL, 446 | ngx_http_ipip_timezone_variable, 447 | 0, 0, 0 }, 448 | 449 | { ngx_string("ipip_utc_offset"), NULL, 450 | ngx_http_ipip_utc_offset_variable, 451 | 0, 0, 0 }, 452 | 453 | { ngx_string("ipip_china_admin_code"), NULL, 454 | ngx_http_ipip_china_admin_code_variable, 455 | 0, 0, 0 }, 456 | 457 | { ngx_string("ipip_idd_code"), NULL, 458 | ngx_http_ipip_idd_code_variable, 459 | 0, 0, 0 }, 460 | 461 | { ngx_string("ipip_country_code"), NULL, 462 | ngx_http_ipip_country_code_variable, 463 | 0, 0, 0 }, 464 | 465 | { ngx_string("ipip_continent_code"), NULL, 466 | ngx_http_ipip_continent_code_variable, 467 | 0, 0, 0 }, 468 | 469 | { ngx_string("ipip_idc"), NULL, 470 | ngx_http_ipip_idc_variable, 471 | 0, 0, 0 }, 472 | 473 | { ngx_string("ipip_base_station"), NULL, 474 | ngx_http_ipip_base_station_variable, 475 | 0, 0, 0 }, 476 | 477 | { ngx_string("ipip_country_code3"), NULL, 478 | ngx_http_ipip_country_code3_variable, 479 | 0, 0, 0 }, 480 | 481 | { ngx_string("ipip_european_union"), NULL, 482 | ngx_http_ipip_european_union_variable, 483 | 0, 0, 0 }, 484 | 485 | { ngx_string("ipip_currency_code"), NULL, 486 | ngx_http_ipip_currency_code_variable, 487 | 0, 0, 0 }, 488 | 489 | { ngx_string("ipip_currency_name"), NULL, 490 | ngx_http_ipip_currency_name_variable, 491 | 0, 0, 0 }, 492 | 493 | { ngx_string("ipip_anycast"), NULL, 494 | ngx_http_ipip_anycast_variable, 495 | 0, 0, 0 }, 496 | 497 | { ngx_null_string, NULL, NULL, 0, 0, 0 } 498 | }; 499 | 500 | static ngx_int_t 501 | ngx_http_ipip_addr_str(ngx_http_request_t *r, char* ipstr) 502 | { 503 | ngx_addr_t addr; 504 | struct sockaddr_in *sin; 505 | 506 | addr.sockaddr = r->connection->sockaddr; 507 | addr.socklen = r->connection->socklen; 508 | 509 | if (addr.sockaddr->sa_family != AF_INET) { 510 | return INADDR_NONE; 511 | } 512 | 513 | sin = (struct sockaddr_in *) addr.sockaddr; 514 | inet_ntop(AF_INET, &(sin->sin_addr), ipstr, INET_ADDRSTRLEN); 515 | 516 | return NGX_OK; 517 | } 518 | 519 | static void 520 | ngx_http_ipip_cleanup(void *data) 521 | { 522 | ngx_http_ipip_conf_t *icf = data; 523 | 524 | if (icf->db_ctx) { 525 | destroy(icf->db_ctx); 526 | icf->db_ctx = NULL; 527 | } 528 | } 529 | 530 | static void * 531 | ngx_http_ipip_create_conf(ngx_conf_t *cf) 532 | { 533 | ngx_pool_cleanup_t *cln; 534 | ngx_http_ipip_conf_t *conf; 535 | 536 | conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_ipip_conf_t)); 537 | if (conf == NULL) { 538 | return NULL; 539 | } 540 | 541 | cln = ngx_pool_cleanup_add(cf->pool, 0); 542 | if (cln == NULL) { 543 | return NULL; 544 | } 545 | 546 | cln->handler = ngx_http_ipip_cleanup; 547 | cln->data = conf; 548 | 549 | return conf; 550 | } 551 | 552 | static ngx_int_t 553 | ngx_http_ipip_region_variable(ngx_http_request_t *r, 554 | ngx_http_variable_value_t *v, uintptr_t data) { 555 | 556 | return ngx_ipip_set_variable(r, v, NGX_IPIP_REGION_NAME_COEE); 557 | } 558 | static ngx_int_t 559 | ngx_http_ipip_city_variable(ngx_http_request_t *r, 560 | ngx_http_variable_value_t *v, uintptr_t data) { 561 | 562 | return ngx_ipip_set_variable(r, v, NGX_IPIP_CITY_NAME_CODE); 563 | } 564 | static ngx_int_t 565 | ngx_http_ipip_owner_domain_variable(ngx_http_request_t *r, 566 | ngx_http_variable_value_t *v, uintptr_t data) { 567 | return ngx_ipip_set_variable(r, v, NGX_IPIP_OWNER_DOMAIN_CODE); 568 | } 569 | static ngx_int_t ngx_http_ipip_isp_domain_variable(ngx_http_request_t *r, 570 | ngx_http_variable_value_t *v, uintptr_t data) { 571 | return ngx_ipip_set_variable(r, v, NGX_IPIP_ISP_DOMAIN_CODE); 572 | } 573 | static ngx_int_t ngx_http_ipip_latitude_variable(ngx_http_request_t *r, 574 | ngx_http_variable_value_t *v, uintptr_t data) { 575 | return ngx_ipip_set_variable(r, v, NGX_IPIP_LATITUDE_CODE); 576 | } 577 | static ngx_int_t ngx_http_ipip_longitude_variable(ngx_http_request_t *r, 578 | ngx_http_variable_value_t *v, uintptr_t data) { 579 | return ngx_ipip_set_variable(r, v, NGX_IPIP_LONGITUDE_CODE); 580 | } 581 | static ngx_int_t 582 | ngx_http_ipip_country_variable(ngx_http_request_t *r, 583 | ngx_http_variable_value_t *v, uintptr_t data) { 584 | return ngx_ipip_set_variable(r, v, NGX_IPIP_COUNTRY_NAME_CODE); 585 | } 586 | static ngx_int_t 587 | ngx_http_ipip_timezone_variable(ngx_http_request_t *r, 588 | ngx_http_variable_value_t *v, uintptr_t data) { 589 | return ngx_ipip_set_variable(r, v, NGX_IPIP_TIMEZONE_CODE); 590 | } 591 | static ngx_int_t ngx_http_ipip_utc_offset_variable(ngx_http_request_t *r, 592 | ngx_http_variable_value_t *v, uintptr_t data) { 593 | return ngx_ipip_set_variable(r, v, NGX_IPIP_UTC_OFFSET_CODE); 594 | } 595 | static ngx_int_t ngx_http_ipip_china_admin_code_variable(ngx_http_request_t *r, 596 | ngx_http_variable_value_t *v, uintptr_t data) { 597 | return ngx_ipip_set_variable(r, v, NGX_IPIP_CHINA_ADMIN_CODE); 598 | } 599 | static ngx_int_t ngx_http_ipip_idd_code_variable(ngx_http_request_t *r, 600 | ngx_http_variable_value_t *v, uintptr_t data) { 601 | return ngx_ipip_set_variable(r, v, NGX_IPIP_IDD_CODE_CODE); 602 | } 603 | static ngx_int_t ngx_http_ipip_country_code_variable(ngx_http_request_t *r, 604 | ngx_http_variable_value_t *v, uintptr_t data) { 605 | return ngx_ipip_set_variable(r, v, NGX_IPIP_COUNTRY_CODE_CODE); 606 | } 607 | static ngx_int_t ngx_http_ipip_continent_code_variable(ngx_http_request_t *r, 608 | ngx_http_variable_value_t *v, uintptr_t data) { 609 | return ngx_ipip_set_variable(r, v, NGX_IPIP_CONTINENT_CODE_CODE); 610 | } 611 | static ngx_int_t ngx_http_ipip_idc_variable(ngx_http_request_t *r, 612 | ngx_http_variable_value_t *v, uintptr_t data) { 613 | return ngx_ipip_set_variable(r, v, NGX_IPIP_IDC_CODE); 614 | } 615 | static ngx_int_t ngx_http_ipip_base_station_variable(ngx_http_request_t *r, 616 | ngx_http_variable_value_t *v, uintptr_t data) { 617 | return ngx_ipip_set_variable(r, v, NGX_IPIP_BASE_STATION_CODE); 618 | } 619 | static ngx_int_t ngx_http_ipip_country_code3_variable(ngx_http_request_t *r, 620 | ngx_http_variable_value_t *v, uintptr_t data) { 621 | return ngx_ipip_set_variable(r, v, NGX_IPIP_COUNTRY_CODE3); 622 | } 623 | static ngx_int_t ngx_http_ipip_european_union_variable(ngx_http_request_t *r, 624 | ngx_http_variable_value_t *v, uintptr_t data) { 625 | return ngx_ipip_set_variable(r, v, NGX_IPIP_EUROPEAN_UNION); 626 | } 627 | static ngx_int_t ngx_http_ipip_currency_code_variable(ngx_http_request_t *r, 628 | ngx_http_variable_value_t *v, uintptr_t data) { 629 | return ngx_ipip_set_variable(r, v, NGX_IPIP_CURRENCY_CODE); 630 | } 631 | static ngx_int_t ngx_http_ipip_currency_name_variable(ngx_http_request_t *r, 632 | ngx_http_variable_value_t *v, uintptr_t data) { 633 | return ngx_ipip_set_variable(r, v, NGX_IPIP_CURRENCY_NAME); 634 | } 635 | static ngx_int_t ngx_http_ipip_anycast_variable(ngx_http_request_t *r, 636 | ngx_http_variable_value_t *v, uintptr_t data) { 637 | return ngx_ipip_set_variable(r, v, NGX_IPIP_ANYCAST_CODE); 638 | } 639 | 640 | static ngx_int_t 641 | ngx_http_ipip_add_variables(ngx_conf_t *cf) 642 | { 643 | ngx_http_variable_t *var, *v; 644 | 645 | for (v = ngx_http_ipip_vars; v->name.len; v++) { 646 | var = ngx_http_add_variable(cf, &v->name, v->flags); 647 | if (var == NULL) { 648 | return NGX_ERROR; 649 | } 650 | 651 | var->get_handler = v->get_handler; 652 | var->data = v->data; 653 | } 654 | ngx_conf_log_error(NGX_LOG_NOTICE, cf, 0, 655 | "enter ipip add variables"); 656 | 657 | return NGX_OK; 658 | } 659 | 660 | static char *ngx_http_ipip_db(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) 661 | { 662 | ngx_str_t *value; 663 | ngx_http_ipip_conf_t *icf = conf; 664 | time_t interval; 665 | 666 | value = cf->args->elts; 667 | int error_code = 0; 668 | icf->db_ctx = init_db((char *) value[1].data, &error_code, cf); 669 | 670 | icf->db_name.data = ngx_pnalloc(cf->pool, value[1].len+1); 671 | icf->db_name.len = value[1].len; 672 | ngx_memcpy(icf->db_name.data, value[1].data, icf->db_name.len); 673 | icf->db_name.data[icf->db_name.len] = '\0'; 674 | 675 | struct stat attr; 676 | int ret = stat((char *)(icf->db_name.data), &attr); 677 | if (ret != 0) { 678 | ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, 679 | "stat db file failed for : %s", (icf->db_name.data)); 680 | return NGX_CONF_ERROR; 681 | } 682 | 683 | 684 | // ngx_time_init(); 685 | 686 | icf->last_check = icf->last_change = ngx_time(); 687 | 688 | interval = ngx_parse_time(&value[2], 1); 689 | 690 | ngx_conf_log_error(NGX_LOG_NOTICE, cf, 0, 691 | "check interval = %d second", (ngx_int_t)interval); 692 | 693 | if (interval == (time_t) NGX_ERROR) { 694 | ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, 695 | "invalid interval for auto_reload \"%V\"", 696 | value[1]); 697 | return NGX_CONF_ERROR; 698 | } 699 | icf->check_interval = interval; 700 | 701 | if (icf->db_ctx != NULL) { 702 | ngx_conf_log_error(NGX_LOG_NOTICE, cf, 0, 703 | "ipip open db = %s success", (char *) value[1].data); 704 | } else { 705 | ngx_conf_log_error(NGX_LOG_NOTICE, cf, 0, 706 | "ipip open db = %s failed, error code = %d", 707 | (char *) value[1].data, error_code); 708 | return NGX_CONF_ERROR; 709 | } 710 | 711 | return NGX_CONF_OK; 712 | } /* ngx_http_ipip_db */ 713 | 714 | static ngx_int_t ngx_http_ipip_reload_db(ngx_http_ipip_conf_t *icf) { 715 | 716 | struct DBContext *tmp_ctx; 717 | struct stat attr; 718 | int error_code = 0; 719 | 720 | ngx_time_update(); 721 | if (icf->check_interval > 0 722 | && icf->last_check + icf->check_interval <= ngx_time()) { 723 | icf->last_check = ngx_time(); 724 | 725 | /*int fd = ngx_open_file((char *)(icf->db_name.data), NGX_FILE_RDONLY, NGX_FILE_OPEN, 0); 726 | if (fd < 0) { 727 | fprintf(stderr, "open file failed for : %s\n", strerror(errno)); 728 | }*/ 729 | if (stat((char *)(icf->db_name.data), &attr) != 0) { 730 | return NGX_ERROR; 731 | } 732 | 733 | //fprintf(stderr, "last change = %u, [%s] file mtime = %lu\n", (unsigned)(icf->last_change), 734 | // (char *) (icf->db_name.data), (unsigned long)attr.st_mtime); 735 | 736 | if (attr.st_mtime > icf->last_change) { 737 | //destroy(icf->db_ctx); 738 | //icf->db_ctx = NULL; 739 | tmp_ctx = init_db((char *) (icf->db_name.data), &error_code, NULL); 740 | 741 | if (tmp_ctx == NULL) { 742 | //fprintf(stderr, "init db failed\n"); 743 | //ngx_conf_log_error(NGX_LOG_NOTICE, ncf, 0, 744 | // "ipip open db = %s failed, error code = %d", 745 | // (char *) value[1].data, error_code); 746 | return NGX_ERROR; 747 | } 748 | 749 | icf->last_change = attr.st_mtime; 750 | 751 | destroy(icf->db_ctx); 752 | icf->db_ctx = tmp_ctx; 753 | 754 | //ngx_conf_log_error(NGX_LOG_NOTICE, ncf, 0, 755 | // "ipip reload db %s success ", (char *) value[1].data); 756 | } 757 | } 758 | return NGX_OK; 759 | } 760 | 761 | static char *ngx_ipip_parse_ip(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) 762 | { 763 | ngx_ipip_add_parse_ip_variable(cf, cmd, NULL); 764 | return NGX_CONF_OK; 765 | } /* ngx_ipip_parse_ip */ 766 | 767 | static char *ngx_ipip_add_parse_ip_variable(ngx_conf_t *cf, ngx_command_t *dummy, 768 | void *handler_conf) { 769 | ngx_str_t* value; 770 | ngx_str_t name, source; 771 | ngx_http_compile_complex_value_t ccv; 772 | 773 | ngx_http_ipip_conf_t* icf = ngx_http_conf_get_module_main_conf(cf, ngx_http_ipip_module); 774 | 775 | value = cf->args->elts; 776 | name = value[0]; 777 | source = value[1]; 778 | if (source.data[0] != '$') { 779 | ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid variable source %s", source.data); 780 | return NGX_CONF_ERROR; 781 | } 782 | 783 | ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t)); 784 | ccv.cf = cf; 785 | ccv.value = &source; 786 | ccv.complex_value = &icf->ip_source; 787 | 788 | if (ngx_http_compile_complex_value(&ccv) != NGX_OK) { 789 | ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, 790 | "unable to compile \"%V\" for \"$%V\"", &source, &name); 791 | return NGX_CONF_ERROR; 792 | } 793 | return NGX_CONF_OK; 794 | } --------------------------------------------------------------------------------