From b0466348da869cde2735e6ea0804466c086b7fd9 Mon Sep 17 00:00:00 2001 From: fa1seut0pia <178009183+fa1seut0pia@users.noreply.github.com> Date: Mon, 9 Feb 2026 01:59:05 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E9=81=BF=E5=85=8D=20shell=20=E9=80=9A?= =?UTF-8?q?=E9=85=8D=E7=AC=A6=E5=B1=95=E5=BC=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- binding/c/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/binding/c/Makefile b/binding/c/Makefile index f9a93f8..b42fb4f 100644 --- a/binding/c/Makefile +++ b/binding/c/Makefile @@ -15,7 +15,7 @@ xdb_util.o: xdb_util.c xdb_searcher_lib: xdb_util.o xdb_searcher.o mkdir -p build/lib mkdir -p build/include - ar -rc build/lib/libxdb.a `find . -name *.o` + ar -rc build/lib/libxdb.a `find . -name "*.o"` cp xdb_api.h build/include clean: From e183607da59d30c9685c463543f3e8738d2c7727 Mon Sep 17 00:00:00 2001 From: fa1seut0pia <178009183+fa1seut0pia@users.noreply.github.com> Date: Mon, 9 Feb 2026 01:59:05 +0800 Subject: [PATCH 2/5] =?UTF-8?q?ngx=5Fmodule=5Flibs=20=E4=B8=8E=20bingding/?= =?UTF-8?q?c/Makefile=20=E7=94=9F=E6=88=90=E7=9A=84=20libxdb.a=20=E4=BF=9D?= =?UTF-8?q?=E6=8C=81=E4=B8=80=E8=87=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- binding/nginx/config | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/binding/nginx/config b/binding/nginx/config index 1da0253..2708e94 100644 --- a/binding/nginx/config +++ b/binding/nginx/config @@ -12,7 +12,7 @@ if test -n "$ngx_module_link"; then ngx_module_name=$ngx_addon_name ngx_module_deps="$NGX_HTTP_IP2REGION_DEPS" ngx_module_srcs="$NGX_HTTP_IP2REGION_SRCS" - ngx_module_libs="-lxdb_searcher" + ngx_module_libs="-lxdb" . auto/module else From 7d6ea0030387d3cd22e273fbb88289bf2e28738f Mon Sep 17 00:00:00 2001 From: fa1seut0pia <178009183+fa1seut0pia@users.noreply.github.com> Date: Mon, 9 Feb 2026 03:29:43 +0800 Subject: [PATCH 3/5] =?UTF-8?q?nginx=20=E6=A8=A1=E5=9D=97=E6=94=AF?= =?UTF-8?q?=E6=8C=81IPv6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- binding/nginx/src/ngx_http_ip2region_module.c | 421 ++++++++++++++---- binding/nginx/src/ngx_http_ip2region_module.h | 13 +- 2 files changed, 334 insertions(+), 100 deletions(-) diff --git a/binding/nginx/src/ngx_http_ip2region_module.c b/binding/nginx/src/ngx_http_ip2region_module.c index aa3e3e7..93543f9 100644 --- a/binding/nginx/src/ngx_http_ip2region_module.c +++ b/binding/nginx/src/ngx_http_ip2region_module.c @@ -4,7 +4,9 @@ #include "ngx_http_ip2region_module.h" -static ngx_int_t ngx_http_ua_parser_test_full_name(char *name); +static ngx_int_t ngx_http_ip2region_is_absolute_path(char *name); +static char *ngx_http_ip2region_init_searcher(ngx_conf_t *cf, char *db_name, + char *cache_policy, xdb_version_t *expected_version, const char *directive_name); static ngx_int_t ngx_http_ip2region_add_variables(ngx_conf_t *cf); @@ -28,6 +30,9 @@ static ngx_http_module_t ngx_http_ip2region_ctx = { }; +static char *ngx_http_ip2region_init(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); +static char *ngx_http_ip2region_init_v6(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); + static ngx_command_t ngx_http_ip2region_commands[] = { { ngx_string("ip2region_db"), NGX_HTTP_MAIN_CONF | NGX_CONF_TAKE12, @@ -36,6 +41,13 @@ static ngx_command_t ngx_http_ip2region_commands[] = { 0, NULL }, + { ngx_string("ip2region_db6"), + NGX_HTTP_MAIN_CONF | NGX_CONF_TAKE12, + ngx_http_ip2region_init_v6, + NGX_HTTP_MAIN_CONF_OFFSET, + 0, + NULL }, + ngx_null_command }; @@ -66,25 +78,168 @@ static ngx_http_variable_t ngx_http_ip2region_vars[] = { }; +// 用于初始化带版本校验的搜索器的辅助函数 +static char * +ngx_http_ip2region_init_searcher(ngx_conf_t *cf, char *db_name, + char *cache_policy, xdb_version_t *expected_version, const char *directive_name) +{ + ip2region_searcher_t *ip2region_searcher; + int err; + char *db_path; + size_t len; + + if(ngx_http_ip2region_is_absolute_path(db_name) == NGX_OK) { + db_path = db_name; + } else { // relative path to conf directory + len = ngx_cycle->conf_prefix.len + strlen(db_name) + 1; + db_path = malloc(len); + if (db_path == NULL) { + ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, + "failed to allocate memory for db_path"); + return NGX_CONF_ERROR; + } + memset(db_path, '\0', len); + memcpy(db_path, ngx_cycle->conf_prefix.data, ngx_cycle->conf_prefix.len); + strcat(db_path, db_name); + } + + ip2region_searcher = ngx_palloc(cf->pool, sizeof(ip2region_searcher_t)); + + if(ip2region_searcher == NULL) { + if(ngx_http_ip2region_is_absolute_path(db_name) != NGX_OK) { + free(db_path); + } + return NGX_CONF_ERROR; + } + + ip2region_searcher->v_index = NULL; + ip2region_searcher->c_buffer = NULL; + + // 检查XDB文件的版本信息以确定IP类型 + xdb_header_t *header = xdb_load_header_from_file(db_path); + if (header == NULL) { + ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, + "failed to load xdb header from: %s", db_path); + if(ngx_http_ip2region_is_absolute_path(db_name) != NGX_OK) { + free(db_path); + } + return NGX_CONF_ERROR; + } + + xdb_version_t *xdb_version = xdb_version_from_header(header); + if (xdb_version == NULL) { + ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, + "failed to determine xdb version from header: %s", db_path); + xdb_free_header((void *)header); + if(ngx_http_ip2region_is_absolute_path(db_name) != NGX_OK) { + free(db_path); + } + return NGX_CONF_ERROR; + } + + // 验证XDB文件版本是否匹配 + if (xdb_version->id != expected_version->id) { + ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, + "%s expects %s xdb file, but got %s: %s", + directive_name, expected_version->name, xdb_version->name, db_path); + xdb_free_header((void *)header); + if(ngx_http_ip2region_is_absolute_path(db_name) != NGX_OK) { + free(db_path); + } + return NGX_CONF_ERROR; + } + + if (strcmp(cache_policy, "file") == 0) { + err = xdb_new_with_file_only(xdb_version, &ip2region_searcher->searcher, db_path); + xdb_free_header((void *)header); + if (err != 0) { + ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, + "failed to create searcher: %s", db_path); + if(ngx_http_ip2region_is_absolute_path(db_name) != NGX_OK) { + free(db_path); + } + return NGX_CONF_ERROR; + } + } else if (strcmp(cache_policy, "vectorIndex") == 0) { + ip2region_searcher->v_index = xdb_load_vector_index_from_file(db_path); + if (ip2region_searcher->v_index == NULL) { + ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, + "failed to load vector index from: %s", db_path); + xdb_free_header((void *)header); + if(ngx_http_ip2region_is_absolute_path(db_name) != NGX_OK) { + free(db_path); + } + return NGX_CONF_ERROR; + } + + err = xdb_new_with_vector_index(xdb_version, &ip2region_searcher->searcher, db_path, ip2region_searcher->v_index); + xdb_free_header((void *)header); + if (err != 0) { + ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, + "failed to create vector index cached searcher: %s", db_path); + xdb_free_vector_index((void *)ip2region_searcher->v_index); + if(ngx_http_ip2region_is_absolute_path(db_name) != NGX_OK) { + free(db_path); + } + return NGX_CONF_ERROR; + } + } else if (strcmp(cache_policy, "content") == 0) { + ip2region_searcher->c_buffer = xdb_load_content_from_file(db_path); + if (ip2region_searcher->c_buffer == NULL) { + ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, + "failed to load xdb content: %s", db_path); + xdb_free_header((void *)header); + if(ngx_http_ip2region_is_absolute_path(db_name) != NGX_OK) { + free(db_path); + } + return NGX_CONF_ERROR; + } + + err = xdb_new_with_buffer(xdb_version, &ip2region_searcher->searcher, ip2region_searcher->c_buffer); + xdb_free_header((void *)header); + if (err != 0) { + ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, + "failed to create content cached searcher: %s", db_path); + xdb_free_content((void *)ip2region_searcher->c_buffer); + if(ngx_http_ip2region_is_absolute_path(db_name) != NGX_OK) { + free(db_path); + } + return NGX_CONF_ERROR; + } + } else { + ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, + "invalid cache policy, options: file/vectorIndex/content"); + xdb_free_header((void *)header); + if(ngx_http_ip2region_is_absolute_path(db_name) != NGX_OK) { + free(db_path); + } + return NGX_CONF_ERROR; + } + + if(ngx_http_ip2region_is_absolute_path(db_name) != NGX_OK) { + free(db_path); + } + + return (char *)ip2region_searcher; +} + + static char * ngx_http_ip2region_init(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { ngx_http_ip2region_conf_t *ip2region_cf; - ip2region_searcher_t *ip2region_searcher; char *db_name, *cache_policy; ngx_str_t *value; - int err; - char *db_path; - size_t len; + char *result; + ip2region_cf = conf; - if (ip2region_cf->ip2region_searcher) { - return "is duplicate"; + if (ip2region_cf->v4_searcher) { + return "ip2region_db is duplicate"; } value = cf->args->elts; - db_name = (char *)value[1].data; // default cache_policy: content @@ -94,71 +249,49 @@ ngx_http_ip2region_init(ngx_conf_t *cf, ngx_command_t *cmd, cache_policy = (char *)value[2].data; } - if(ngx_http_ua_parser_test_full_name(db_name) == NGX_OK) { - db_path = db_name; - } else { // relative path to conf directory - len = ngx_cycle->conf_prefix.len + strlen(db_name) + 1; - db_path = malloc(len); - memset(db_path, '\0', len); - memcpy(db_path, ngx_cycle->conf_prefix.data, ngx_cycle->conf_prefix.len); - strcat(db_path, db_name); - } - - ip2region_searcher = ngx_palloc(cf->pool, sizeof(ip2region_searcher_t)); - - if(ip2region_searcher == NULL) { + result = ngx_http_ip2region_init_searcher(cf, db_name, cache_policy, + xdb_version_v4(), "ip2region_db"); + if (result == NGX_CONF_ERROR) { return NGX_CONF_ERROR; } - ip2region_searcher->v_index = NULL; - ip2region_searcher->c_buffer = NULL; + ip2region_cf->v4_searcher = (ip2region_searcher_t *)result; - if (strcmp(cache_policy, "file") == 0) { - err = xdb_new_with_file_only(&ip2region_searcher->searcher, db_path); - if (err != 0) { - ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, - "failed to create searcher: %s", db_path); - return NGX_CONF_ERROR; - } - } else if (strcmp(cache_policy, "vectorIndex") == 0) { - ip2region_searcher->v_index = xdb_load_vector_index_from_file(db_path); - if (ip2region_searcher->v_index == NULL) { - ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, - "failed to load vector index from: %s", db_path); - return NGX_CONF_ERROR; - } + return NGX_CONF_OK; +} - err = xdb_new_with_vector_index(&ip2region_searcher->searcher, db_path, ip2region_searcher->v_index); - if (err != 0) { - ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, - "failed to create vector index cached searcher: %s", db_path); - return NGX_CONF_ERROR; - } - } else if (strcmp(cache_policy, "content") == 0) { - ip2region_searcher->c_buffer = xdb_load_content_from_file(db_path); - if (ip2region_searcher->c_buffer == NULL) { - ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, - "failed to load xdb content: %s", db_path); - return NGX_CONF_ERROR; - } +static char * +ngx_http_ip2region_init_v6(ngx_conf_t *cf, ngx_command_t *cmd, + void *conf) +{ + ngx_http_ip2region_conf_t *ip2region_cf; + char *db_name, *cache_policy; + ngx_str_t *value; + char *result; - err = xdb_new_with_buffer(&ip2region_searcher->searcher, ip2region_searcher->c_buffer); - if (err != 0) { - ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, - "failed to create content cached searcher: %s", db_path); - return NGX_CONF_ERROR; - } + ip2region_cf = conf; + + if (ip2region_cf->v6_searcher) { + return "ip2region_db6 is duplicate"; + } + + value = cf->args->elts; + db_name = (char *)value[1].data; + + // default cache_policy: content + if(cf->args->nelts == 2) { + cache_policy = "content"; } else { - ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, - "invalid cache policy `%V`, options: file/vectorIndex/content", &value[2]); + cache_policy = (char *)value[2].data; + } + + result = ngx_http_ip2region_init_searcher(cf, db_name, cache_policy, + xdb_version_v6(), "ip2region_db6"); + if (result == NGX_CONF_ERROR) { return NGX_CONF_ERROR; } - ip2region_cf->ip2region_searcher = ip2region_searcher; - - if(ngx_http_ua_parser_test_full_name(db_name) != NGX_OK) { - free(db_path); - } + ip2region_cf->v6_searcher = (ip2region_searcher_t *)result; return NGX_CONF_OK; } @@ -212,9 +345,11 @@ ngx_http_ip2region_variable(ngx_http_request_t *r, { ngx_http_ip2region_conf_t *ip2region_conf; struct sockaddr_in *sin; - char region[512] = {'\0'}; - int err; + char region_buffer[512] = {'\0'}; + xdb_region_buffer_t region; + int err = 1; unsigned int ip; + xdb_searcher_t *searcher_ptr = NULL; #if (NGX_HAVE_INET6) u_char *p; @@ -224,47 +359,119 @@ ngx_http_ip2region_variable(ngx_http_request_t *r, ip2region_conf = ngx_http_get_module_main_conf(r, ngx_http_ip2region_module); - if (ip2region_conf->ip2region_searcher != NULL) { + if (ip2region_conf->v4_searcher == NULL && ip2region_conf->v6_searcher == NULL) { + v->not_found = 1; + return NGX_OK; + } - switch (r->connection->sockaddr->sa_family) { - case AF_INET: - sin = (struct sockaddr_in *) r->connection->sockaddr; - ip = htonl(sin->sin_addr.s_addr); - err = xdb_search(&ip2region_conf->ip2region_searcher->searcher, ip, region, sizeof(region)); - if (err == 0) { - v->data = (unsigned char *)region; - v->len = strlen(region); - return NGX_OK; - } + // 初始化 region buffer + err = xdb_region_buffer_init(®ion, region_buffer, sizeof(region_buffer)); + if (err != 0) { + v->not_found = 1; + return NGX_OK; + } + + switch (r->connection->sockaddr->sa_family) { + case AF_INET: + sin = (struct sockaddr_in *) r->connection->sockaddr; + + // 检查是否有IPv4 searcher + if (ip2region_conf->v4_searcher == NULL) { + ngx_log_error(NGX_LOG_WARN, r->connection->log, 0, + "no IPv4 searcher available for IPv4 address"); break; + } + + searcher_ptr = &ip2region_conf->v4_searcher->searcher; + + // 正确转换IP地址字节序,按ip2region期望的格式 + ip = ntohl(sin->sin_addr.s_addr); // 将网络字节序转换为主机字节序 + // 按照xdb_parse_v4_ip中的格式重新组织字节 + { + bytes_ip_t ip_bytes[4]; + ip_bytes[0] = (ip >> 24) & 0xFF; + ip_bytes[1] = (ip >> 16) & 0xFF; + ip_bytes[2] = (ip >> 8) & 0xFF; + ip_bytes[3] = ip & 0xFF; + err = xdb_search(searcher_ptr, ip_bytes, 4, ®ion); + } + if (err == 0) { + v->data = (unsigned char *)region.value; + v->len = strlen(region.value); + xdb_region_buffer_free(®ion); + return NGX_OK; + } else { + ngx_log_error(NGX_LOG_WARN, r->connection->log, 0, + "ip2region search failed for IPv4 address"); + } + break; #if (NGX_HAVE_INET6) - case AF_INET6: - sin6 = (struct sockaddr_in6 *) r->connection->sockaddr; - p = sin6->sin6_addr.s6_addr; + case AF_INET6: + sin6 = (struct sockaddr_in6 *) r->connection->sockaddr; + p = sin6->sin6_addr.s6_addr; - if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { + if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { + // 处理IPv4映射的IPv6地址 + if (ip2region_conf->v4_searcher != NULL) { + searcher_ptr = &ip2region_conf->v4_searcher->searcher; addr = p[12] << 24; addr += p[13] << 16; addr += p[14] << 8; addr += p[15]; - - ip = htonl(addr); - err = xdb_search(&ip2region_conf->ip2region_searcher->searcher, ip, region, sizeof(region)); + // 按照xdb_parse_v4_ip中的格式重新组织字节 + { + bytes_ip_t ip_bytes[4]; + ip_bytes[0] = (addr >> 24) & 0xFF; + ip_bytes[1] = (addr >> 16) & 0xFF; + ip_bytes[2] = (addr >> 8) & 0xFF; + ip_bytes[3] = addr & 0xFF; + err = xdb_search(searcher_ptr, ip_bytes, 4, ®ion); + } if (err == 0) { - v->data = (unsigned char *)region; - v->len = strlen(region); + v->data = (unsigned char *)region.value; + v->len = strlen(region.value); + xdb_region_buffer_free(®ion); return NGX_OK; + } else { + ngx_log_error(NGX_LOG_WARN, r->connection->log, 0, + "ip2region search failed for IPv4-mapped IPv6 address"); } } - break; + } else { + // 处理纯IPv6地址 + if (ip2region_conf->v6_searcher != NULL) { + searcher_ptr = &ip2region_conf->v6_searcher->searcher; + bytes_ip_t ip6_bytes[16]; + if (p != NULL) { + memcpy(ip6_bytes, p, 16); + err = xdb_search(searcher_ptr, ip6_bytes, 16, ®ion); + if (err == 0) { + v->data = (unsigned char *)region.value; + v->len = strlen(region.value); + xdb_region_buffer_free(®ion); + return NGX_OK; + } else { + ngx_log_error(NGX_LOG_WARN, r->connection->log, 0, + "ip2region search failed for IPv6 address"); + } + } + } else { + ngx_log_error(NGX_LOG_WARN, r->connection->log, 0, + "no IPv6 searcher available for IPv6 address"); + } + } + break; #endif - } } + // 如果搜索失败,释放 region buffer + xdb_region_buffer_free(®ion); + ngx_log_error(NGX_LOG_INFO, r->connection->log, 0, + "ip2region: no region found for IP address"); v->not_found = 1; return NGX_OK; } @@ -275,28 +482,48 @@ ngx_http_ip2region_cleanup(void *data) { ngx_http_ip2region_conf_t *ip2region_conf = data; - if(ip2region_conf->ip2region_searcher != NULL) { - xdb_close(&ip2region_conf->ip2region_searcher->searcher); + // 清理 IPv4 searcher + if(ip2region_conf->v4_searcher != NULL) { + xdb_close(&ip2region_conf->v4_searcher->searcher); // check and free the vector index - if (ip2region_conf->ip2region_searcher->v_index != NULL) { - xdb_close_vector_index(ip2region_conf->ip2region_searcher->v_index); - ip2region_conf->ip2region_searcher->v_index = NULL; + if (ip2region_conf->v4_searcher->v_index != NULL) { + xdb_free_vector_index((void *)ip2region_conf->v4_searcher->v_index); + ip2region_conf->v4_searcher->v_index = NULL; } // check and free the content buffer - if (ip2region_conf->ip2region_searcher->c_buffer != NULL) { - xdb_close_content(ip2region_conf->ip2region_searcher->c_buffer); - ip2region_conf->ip2region_searcher->c_buffer = NULL; + if (ip2region_conf->v4_searcher->c_buffer != NULL) { + xdb_free_content((void *)ip2region_conf->v4_searcher->c_buffer); + ip2region_conf->v4_searcher->c_buffer = NULL; } - ip2region_conf->ip2region_searcher = NULL; + ip2region_conf->v4_searcher = NULL; + } + + // 清理 IPv6 searcher + if(ip2region_conf->v6_searcher != NULL) { + xdb_close(&ip2region_conf->v6_searcher->searcher); + + // check and free the vector index + if (ip2region_conf->v6_searcher->v_index != NULL) { + xdb_free_vector_index((void *)ip2region_conf->v6_searcher->v_index); + ip2region_conf->v6_searcher->v_index = NULL; + } + + // check and free the content buffer + if (ip2region_conf->v6_searcher->c_buffer != NULL) { + xdb_free_content((void *)ip2region_conf->v6_searcher->c_buffer); + ip2region_conf->v6_searcher->c_buffer = NULL; + } + + ip2region_conf->v6_searcher = NULL; } } static ngx_int_t -ngx_http_ua_parser_test_full_name(char *name) +ngx_http_ip2region_is_absolute_path(char *name) { #if (NGX_WIN32) u_char c0, c1; diff --git a/binding/nginx/src/ngx_http_ip2region_module.h b/binding/nginx/src/ngx_http_ip2region_module.h index 8861cee..76489dc 100644 --- a/binding/nginx/src/ngx_http_ip2region_module.h +++ b/binding/nginx/src/ngx_http_ip2region_module.h @@ -8,7 +8,13 @@ #include #include #include -#include +#include + +#if (NGX_HAVE_INET6) +#include +#include +#include +#endif typedef struct { xdb_searcher_t searcher; @@ -16,8 +22,9 @@ typedef struct { xdb_content_t *c_buffer; } ip2region_searcher_t; -typedef struct { - ip2region_searcher_t *ip2region_searcher; +typedef struct { + ip2region_searcher_t *v4_searcher; // IPv4 searcher for ip2region_db + ip2region_searcher_t *v6_searcher; // IPv6 searcher for ip2region_db6 } ngx_http_ip2region_conf_t; #endif From 007cb0808a0448211f5900b817d8547ab29c8f51 Mon Sep 17 00:00:00 2001 From: fa1seut0pia <178009183+fa1seut0pia@users.noreply.github.com> Date: Sun, 22 Mar 2026 09:35:54 +0800 Subject: [PATCH 4/5] =?UTF-8?q?nginx=20=E6=A8=A1=E5=9D=97=E6=94=AF?= =?UTF-8?q?=E6=8C=81IPv6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- binding/nginx/Dockerfile | 37 ++++++++++++++ binding/nginx/README.md | 40 ++++++++++++--- binding/nginx/README_zh.md | 99 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 170 insertions(+), 6 deletions(-) create mode 100644 binding/nginx/Dockerfile create mode 100644 binding/nginx/README_zh.md diff --git a/binding/nginx/Dockerfile b/binding/nginx/Dockerfile new file mode 100644 index 0000000..b2f4342 --- /dev/null +++ b/binding/nginx/Dockerfile @@ -0,0 +1,37 @@ +ARG NGINX_VERSION=1.29.6 +FROM nginx:${NGINX_VERSION} AS build +ARG NGINX_VERSION + +# prepare the build environment +RUN apt-get update && \ + apt-get install -y build-essential libpcre2-dev zlib1g-dev libssl-dev git + +WORKDIR /usr/src +RUN curl -LO https://github.com/nginx/nginx/releases/download/release-${NGINX_VERSION}/nginx-${NGINX_VERSION}.tar.gz && \ + tar -zxf nginx-$NGINX_VERSION.tar.gz + +COPY . /usr/src/ip2region + +WORKDIR /usr/src/ip2region/binding/c +RUN make xdb_searcher_lib + +# parameters for building dynamic modules +WORKDIR /usr/src +RUN nginx -V 2>&1 | grep 'configure arguments' | sed 's/ --/ \\\n --/g' | sed "s/pie'/pie' \\\/g" | grep -v 'configure arguments' >> /tmp/conf_arg + +RUN echo \ + ' --add-dynamic-module=$(pwd)/../ip2region/binding/nginx \\\n' \ + ' --with-cc-opt="-I $(pwd)/../ip2region/binding/c/build/include" \\\n' \ + ' --with-ld-opt="-L $(pwd)/../ip2region/binding/c/build/lib"' >> /tmp/conf_arg +RUN cat /tmp/conf_arg + +WORKDIR /usr/src/nginx-$NGINX_VERSION +RUN eval "./configure $(cat /tmp/conf_arg)" + +RUN make modules && \ + cp objs/ngx_http_ip2region_module.so /etc/nginx/modules + +# for buildx export +FROM scratch AS export_so +ARG NGINX_VERSION +COPY --from=build /etc/nginx/modules/ngx_http_ip2region_module.so /ngx_http_ip2region_module.so diff --git a/binding/nginx/README.md b/binding/nginx/README.md index 150d9ee..6a22890 100644 --- a/binding/nginx/README.md +++ b/binding/nginx/README.md @@ -1,3 +1,5 @@ +:globe_with_meridians: [中文简体](README_zh.md) | [English](README.md) + # nginx-ip2region ## build @@ -5,18 +7,16 @@ ```shell $ mkdir -p workspace $ cd workspace -$ wget https://nginx.org/download/nginx-1.23.4.tar.gz -$ tar -zxf nginx-1.23.4.tar.gz && rm -rf nginx-1.23.4.tar.gz +$ wget https://nginx.org/download/nginx-1.23.6.tar.gz +$ tar -zxf nginx-1.23.6.tar.gz && rm -rf nginx-1.23.6.tar.gz $ git clone https://github.com/lionsoul2014/ip2region.git -$ # please check to version v2.13.0 by running -$ git checkout v2.13.0 $ cd ip2region/binding/c $ make xdb_searcher_lib -$ cd ../../../nginx-1.23.4 +$ cd ../../../nginx-1.23.6 $ ./configure \ --add-module=$(PWD)/../ip2region/binding/nginx \ --with-cc-opt="-I $(PWD)/../ip2region/binding/c/build/include" \ - --with-ld-opt="-L$(PWD)/../ip2region/binding/c/build/lib" + --with-ld-opt="-L $(PWD)/../ip2region/binding/c/build/lib" $ make $ make install ``` @@ -69,3 +69,31 @@ nginx access log sample {"remote_addr": "127.0.0.1", "region": "Reserved|Reserved|Reserved|0|0", "http_x_forwarded_for": ""} {"remote_addr": "127.0.0.1", "region": "Reserved|Reserved|Reserved|0|0", "http_x_forwarded_for": ""} ``` + +Additionally, you can build the nginx dynamic module using the Dockerfile in the current directory. + +> * The [buildx](https://github.com/docker/buildx) plugin is required to enable export functionality. + +```shell + +docker build -t export_so -o type=tar,dest=./so.tar . +# The final result is a dynamic module named ngx_http_ip2region_module.so. +tar xf so.tar && rm so.tar + +``` + +usage of dynamic modules + +``` + +# nginx.conf +load_module /etc/nginx/my-modules/ngx_http_ip2region_module.so; + +http { + # ... + ip2region_db /etc/nginx/conf.d/ip2region_v4.xdb content; + ip2region_db6 /etc/nginx/conf.d/ip2region_v6.xdb content; + # ... +} +``` + diff --git a/binding/nginx/README_zh.md b/binding/nginx/README_zh.md new file mode 100644 index 0000000..74063be --- /dev/null +++ b/binding/nginx/README_zh.md @@ -0,0 +1,99 @@ +:globe_with_meridians: [中文简体](README_zh.md) | [English](README.md) + +# nginx-ip2region + +## build + +```shell +$ mkdir -p workspace +$ cd workspace +$ wget https://nginx.org/download/nginx-1.23.6.tar.gz +$ tar -zxf nginx-1.23.6.tar.gz && rm -rf nginx-1.23.6.tar.gz +$ git clone https://github.com/lionsoul2014/ip2region.git +$ cd ip2region/binding/c +$ make xdb_searcher_lib +$ cd ../../../nginx-1.23.6 +$ ./configure \ + --add-module=$(PWD)/../ip2region/binding/nginx \ + --with-cc-opt="-I $(PWD)/../ip2region/binding/c/build/include" \ + --with-ld-opt="-L$(PWD)/../ip2region/binding/c/build/lib" +$ make +$ make install +``` + +## nginx conf + +> Syntax: `ip2region_db xdb_file_path [cache_policy Optional]`; +> Context: http + +cache_policy: `file` or `vectorIndex` or `content`, default: `content` + +Edit `nginx.conf` add `ip2region_db` directive + +```nginx +... +http { + + log_format main escape=json '{' + '"remote_addr": "$remote_addr", ' + '"region": "$ip2region", ' + '"http_x_forwarded_for": "$http_x_forwarded_for"' + '}'; + + access_log logs/access.log main; + + # set xdb file path + ip2region_db ip2region.xdb; + # ip2region_db ip2region.xdb vectorIndex; + # ip2region_db ip2region.xdb file; + # ip2region_db ip2region.xdb content; + + server { + listen 80; + server_name localhost; + + location / { + root html; + index index.html index.htm; + } + } +} + +``` + +Copy `ip2region_v4.xdb` to `nginx/config` folder (rename name it to ip2region.xdb), then restart nginx, the `region` data stored in `ip2region` variable + +nginx access log sample + +```log +{"remote_addr": "127.0.0.1", "region": "Reserved|Reserved|Reserved|0|0", "http_x_forwarded_for": ""} +{"remote_addr": "127.0.0.1", "region": "Reserved|Reserved|Reserved|0|0", "http_x_forwarded_for": ""} +``` + +另外,也可以使用当前路径的 Dockerfile 构建 nginx 动态模块 + +> *需要安装 [buildx](https://github.com/docker/buildx) 插件以支持导出* + +```shell + +docker build -t export_so -o type=tar,dest=./so.tar . +# 最终得到一个叫 ngx_http_ip2region_module.so 的动态模块 +tar xf so.tar && rm so.tar + +``` + +动态模块使用方式 + +``` + +# nginx.conf +load_module /etc/nginx/my-modules/ngx_http_ip2region_module.so; + +http { + # ... + ip2region_db /etc/nginx/conf.d/ip2region_v4.xdb content; + ip2region_db6 /etc/nginx/conf.d/ip2region_v6.xdb content; + # ... +} +``` + From 1eba8ece3bfd504c1f492c2e3a19053b2a58eba6 Mon Sep 17 00:00:00 2001 From: fa1seut0pia <178009183+fa1seut0pia@users.noreply.github.com> Date: Sun, 22 Mar 2026 09:50:24 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E5=AE=B9=E5=99=A8=E5=8C=96=20golang=20xdb?= =?UTF-8?q?=5Fmaker?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- maker/golang/Dockerfile | 35 ++++++++++++++++++++++++++++ maker/golang/README.md | 31 +++++++++++++++++++++++++ maker/golang/README_zh.md | 49 ++++++++++++++++++++++++++++++++++++--- 3 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 maker/golang/Dockerfile diff --git a/maker/golang/Dockerfile b/maker/golang/Dockerfile new file mode 100644 index 0000000..ded7bc0 --- /dev/null +++ b/maker/golang/Dockerfile @@ -0,0 +1,35 @@ +# ============================================ +# ip2region xdb maker - Golang version +# Multi-stage build, resulting in an image of approximately 10 MB. +# ============================================ + +FROM golang:1.22-alpine AS builder + +WORKDIR /build + +COPY . ./ + +# Compile a static binary (disable CGO for fully static linking) +RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o xdb_maker + +FROM alpine:3.19 + +RUN apk --no-cache add ca-certificates tzdata && \ + cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ + echo "Asia/Shanghai" > /etc/timezone + +# Create a non-root user. +RUN adduser -D -g '' appuser + +WORKDIR /app + +# Copy binaries from the build stage. +COPY --from=builder /build/xdb_maker /app/ + +RUN mkdir -p /app/data && chown -R appuser:appuser /app + +USER appuser + +ENTRYPOINT ["/app/xdb_maker"] + +CMD ["--help"] diff --git a/maker/golang/README.md b/maker/golang/README.md index f4aecec..36a0ab2 100644 --- a/maker/golang/README.md +++ b/maker/golang/README.md @@ -156,3 +156,34 @@ For example: use the source files under data to bench test the xdb files in data *Please note that the `src` file used for the bench test must be the same as the source file used to generate the xdb*. If an error occurs during execution, it will stop immediately. You can also execute with the `--ignore-error=true` parameter to ignore errors and view the failed statistics at the end. + + +# Docker + +```bash +# Build the image (run in the maker/golang directory). +cd ip2region/maker/golang +docker build -t ip2region-maker . + +# Generate IPv4 xdb +docker run --rm -v $(pwd)/../../data:/app/data ip2region-maker \ + gen --src=/app/data/ipv4_source.txt \ + --dst=/app/data/ip2region_v4.xdb \ + --version=ipv4 + +# Generate IPv6 xdb +docker run --rm -v $(pwd)/../../data:/app/data ip2region-maker \ + gen --src=/app/data/ipv6_source.txt \ + --dst=/app/data/ip2region_v6.xdb \ + --version=ipv6 + +# Interactive IPv4 Query +docker run -it --rm -v $(pwd)/../../data:/app/data ip2region-maker \ + search --db=/app/data/ip2region_v4.xdb + +# Bench test IPv4 +docker run --rm -v $(pwd)/../../data:/app/data ip2region-maker \ + bench --db=/app/data/ip2region_v4.xdb \ + --src=/app/data/ipv4_source.txt \ + --version=ipv4 +``` diff --git a/maker/golang/README_zh.md b/maker/golang/README_zh.md index 63015e3..8709059 100644 --- a/maker/golang/README_zh.md +++ b/maker/golang/README_zh.md @@ -5,16 +5,18 @@ # 程序编译 通过如下方式编译得到 xdb_maker 可执行程序: + ``` -# 切换到golang maker 根目录 +# 切换到 golang maker 根目录 make ``` -编译成功后会在当前目录生成一个 xdb_maker 的可执行文件 +编译成功后会在当前目录生成一个 xdb_maker 的可执行文件 # `xdb` 数据生成 通过 `xdb_maker gen` 命令生成 ip2region.xdb 二进制文件: + ``` ./xdb_maker gen [command options] options: @@ -26,6 +28,7 @@ options: ``` 例如,使用默认的仓库 data/ 下默认的原始数据生成生成 xdb 文件到当前目录: + ```bash # ipv4 ./xdb_maker gen --src=../../data/ipv4_source.txt --dst=./ip2region_v4.xdb --version=ipv4 @@ -35,10 +38,10 @@ options: 生成过程中数据字段自定义请参考 [xdb-文件生成#自定义数据字段](https://ip2region.net/doc/data/xdb_make#field-list) - # `xdb` 数据查询 通过 `xdb_maker search` 命令来测试查询输入的 ip: + ``` ➜ golang git:(v2.0_xdb) ✗ ./xdb_maker search ./xdb_maker search [command options] @@ -47,6 +50,7 @@ options: ``` 例如,使用自带的 xdb 文件来运行查询测试: + ```bash # ipv4 ./xdb_maker search --db=../../data/ip2region_v4.xdb @@ -78,6 +82,7 @@ ip2region>> 240e:3b7:3273:51d0:13f9:bf0:3db1:aa3f # `xdb` 数据编辑 通过 `xdb_maker edit` 命令来编辑原始的 IP 数据: + ``` ./xdb_maker edit [command options] options: @@ -86,6 +91,7 @@ options: ``` 例如,使用编辑器打开 `./data/ipv4_source.txt` 会看到如下的操作面板: + ```bash ./xdb_maker edit --src=../../data/ipv4_source.txt --version=ipv4 init the editor from source @ `../../data/ipv4_source.txt` ... @@ -101,6 +107,7 @@ editor>> ``` 通过 `put` 命令修改指定 IP 段的定位信息,例如: + ```bash editor>> put 36.132.128.0|36.132.147.255|中国|黑龙江省|哈尔滨市|移动|CN Put(36.132.128.0|36.132.147.255|中国|黑龙江省|哈尔滨市|移动): Ok, with 1 deletes and 2 additions @@ -108,6 +115,7 @@ Put(36.132.128.0|36.132.147.255|中国|黑龙江省|哈尔滨市|移动): Ok, wi ``` 通过 `put_file` 命令从文件中批量载入修改,文件中的 IP 段不需要像 ./data/ipvx_source.txt 中的数据那么严格,不需要前后连续,不同 IP 段有重叠也没关系,编辑器会自动分析处理,例如: + ```bash *editor>> put_file ../../data/sample/ip.test.txt PutFile(../../data/sample/ip.test.txt): Ok, with 25 deletes and 25 additions @@ -115,6 +123,7 @@ PutFile(../../data/sample/ip.test.txt): Ok, with 25 deletes and 25 additions ``` 通过 `save` 命令保存修改,保存成功后,再通过上面的命令从修改后的原始 IP 文件重新生成 xdb 即可: + ```bash *editor>> save all segments saved to ../../data/ipv4_source.txt @@ -124,6 +133,7 @@ editor>> # bench 测试 如果你自主生成了 `xdb` 文件,请确保运行如下的 `xdb_maker bench` 命令来确保生成的的 `xdb` 文件的正确性: + ``` ./xdb_maker bench [command options] options: @@ -135,6 +145,7 @@ options: ``` 例如:使用 data 下的源文件来 bench 测试 data 的 xdb 文件: + ```bash # ipv4 ./xdb_maker bench --db=../../data/ip2region_v4.xdb --src=../../data/ipv4_source.txt --version=ipv4 @@ -142,5 +153,37 @@ options: #ipv6 ./xdb_maker bench --db=../../data/ip2region_v6.xdb --src=../../data/ipv6_source.txt --version=ipv6 ``` + *请注意 bench 测试使用的 `src` 文件需要是对应的生成 xdb 的源文件相同*。 如果运行过程中有错误会立马停止运行,也可以执行 --ignore-error=true 参数来忽略错误,在最后看 failed 的统计结果。 + + +# Docker + +```bash +# 构建镜像(在 maker/golang 目录下执行) +cd ip2region/maker/golang +docker build -t ip2region-maker . + +# 生成 IPv4 xdb +docker run --rm -v $(pwd)/../../data:/app/data ip2region-maker \ + gen --src=/app/data/ipv4_source.txt \ + --dst=/app/data/ip2region_v4.xdb \ + --version=ipv4 + +# 生成 IPv6 xdb +docker run --rm -v $(pwd)/../../data:/app/data ip2region-maker \ + gen --src=/app/data/ipv6_source.txt \ + --dst=/app/data/ip2region_v6.xdb \ + --version=ipv6 + +# 交互式查询 IPv4 +docker run -it --rm -v $(pwd)/../../data:/app/data ip2region-maker \ + search --db=/app/data/ip2region_v4.xdb + +# Bench 测试 IPv4 +docker run --rm -v $(pwd)/../../data:/app/data ip2region-maker \ + bench --db=/app/data/ip2region_v4.xdb \ + --src=/app/data/ipv4_source.txt \ + --version=ipv4 +```