Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion conf/artalk.example.zh-CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ captcha:
ip_region:
# 启用 IP 属地展示
enabled: false
# 数据文件路径 (.xdb 格式)
# 数据文件路径 (.xdb 或标准版 .ipdb 格式)
db_path: ./data/ip2region.xdb
# 显示精度 ["province", "city", "country"]
precision: province
Expand Down
2 changes: 1 addition & 1 deletion conf/artalk.example.zh-TW.yml
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ captcha:
ip_region:
# 啟用 IP 屬地展示
enabled: false
# 數據文件路徑 (.xdb 格式)
# 數據文件路徑 (.xdb 或標準版 .ipdb 格式)
db_path: ./data/ip2region.xdb
# 顯示精度 ["province", "city", "country"]
precision: province
Expand Down
24 changes: 21 additions & 3 deletions docs/docs/en/guide/frontend/ip-region.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,28 @@ This feature is disabled by default. You can enable the IP region display featur

## IP Region Database

Before enabling the IP region display feature, you need to download a database file:
Artalk supports the following two database formats. Choose one; existing configurations and database files remain supported.

- [GitHub Download](https://github.com/lionsoul2014/ip2region/raw/master/data/ip2region.xdb) / [Mirror Download](https://gh-proxy.com/?q=https%3A%2F%2Fgithub.com%2Flionsoul2014%2Fip2region%2Fblob%2Fmaster%2Fdata%2Fip2region.xdb) (recommended within China)
### ip2region (`.xdb`)

- [GitHub Download (IPv4)](https://github.com/lionsoul2014/ip2region/raw/master/data/ip2region_v4.xdb) / [Mirror Download](https://gh-proxy.com/?q=https%3A%2F%2Fgithub.com%2Flionsoul2014%2Fip2region%2Fblob%2Fmaster%2Fdata%2Fip2region_v4.xdb) (recommended within China)

After downloading, manually place it in the `./data/` directory, and name the file: `ip2region.xdb`.

Artalk supports both the legacy (`country|area|province|city|ISP`) and current (`country|province|city|ISP|country code`) upstream ip2region data layouts. Existing databases do not need to be renamed or converted.

ip2region provides separate IPv4 and [IPv6 databases](https://github.com/lionsoul2014/ip2region/raw/master/data/ip2region_v6.xdb). Currently, `db_path` loads one file. To cover both address families with one file, use the standard IPDB below.

### qqwry standard edition (`.ipdb`)

The standard edition of [metowolf/qqwry.ipdb](https://github.com/metowolf/qqwry.ipdb) provides Chinese country, province, and city fields, with IPv4 and IPv6 support in one file.

- [Download standard edition](https://cdn.jsdelivr.net/npm/qqwry.ipdb/qqwry.ipdb) / [Alternative download](https://unpkg.com/qqwry.ipdb/qqwry.ipdb)

Save the file as `./data/qqwry.ipdb` and set `ip_region.db_path` to that path. Keep the `.ipdb` extension: Artalk uses it to select the IPDB reader without an additional database type setting. Do not use the raw edition, `qqwry.raw.ipdb`, which lacks the separate, complete province and city fields required for precision settings.

The IPDB reader loads the entire file into memory. Keep `cache.enabled: true` to reuse the loaded data; disabling caching reloads the file on every query. Restart Artalk after replacing a database file to refresh the cached data.

## Precision Settings

You can find this configuration item in the settings.
Expand All @@ -22,14 +38,16 @@ You can find this configuration item in the settings.
| `city` | City | `Sichuan Chengdu` |
| `country` | Country/Region | `China` |

When province data is missing, `province` precision falls back to the country or region without exposing the more precise city.

Configuration file:

```yaml
# IP Region
ip_region:
# Enable IP region display
enabled: false
# Data file path (.xdb format)
# Data file path (.xdb or standard .ipdb)
db_path: ./data/ip2region.xdb
# Display precision ["province", "city", "country"]
precision: province
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/zh/guide/env.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ ATK_TRUSTED_DOMAINS_0="https://a.com"

| 环境变量 | 默认值 | 描述 | 路径 |
| --- | --- | --- | --- |
| **ATK_IP_REGION_DB_PATH** | `"./data/ip2region.xdb"` | 数据文件路径 (.xdb 格式) | ip_region.db_path (IP 属地 > 数据文件路径) |
| **ATK_IP_REGION_DB_PATH** | `"./data/ip2region.xdb"` | 数据文件路径 (.xdb 或标准版 .ipdb 格式) | ip_region.db_path (IP 属地 > 数据文件路径) |
| **ATK_IP_REGION_ENABLED** | `false` | 启用 IP 属地展示 | ip_region.enabled (IP 属地 > 启用 IP 属地展示) |
| **ATK_IP_REGION_PRECISION** | `"province"` | 显示精度 (可选:`["province", "city", "country"]`) | ip_region.precision (IP 属地 > 显示精度) |

Expand Down
24 changes: 21 additions & 3 deletions docs/docs/zh/guide/frontend/ip-region.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,28 @@ Artalk 内置用户 IP 属地展示功能,并且你可以设置显示的精度

## IP 属地数据库

在开启 IP 属地展示功能之前,你需要下载一个数据库文件:
Artalk 支持以下两种数据库格式,选择其中一种即可。已有配置和数据库文件可以继续使用。

- [GitHub 下载](https://github.com/lionsoul2014/ip2region/raw/master/data/ip2region.xdb) / [镜像站下载](https://gh-proxy.com/?q=https%3A%2F%2Fgithub.com%2Flionsoul2014%2Fip2region%2Fblob%2Fmaster%2Fdata%2Fip2region.xdb) (境内推荐)
### ip2region(`.xdb`)

- [GitHub 下载(IPv4)](https://github.com/lionsoul2014/ip2region/raw/master/data/ip2region_v4.xdb) / [镜像站下载](https://gh-proxy.com/?q=https%3A%2F%2Fgithub.com%2Flionsoul2014%2Fip2region%2Fblob%2Fmaster%2Fdata%2Fip2region_v4.xdb) (境内推荐)

下载后请手动放置到 `./data/` 目录下,文件命名为:`ip2region.xdb`

Artalk 兼容 ip2region 上游的旧版(`国家|区域|省份|城市|运营商`)和新版(`国家|省份|城市|运营商|国家代码`)数据。已有数据库无需改名或转换。

ip2region 的 IPv4 和 [IPv6 数据库](https://github.com/lionsoul2014/ip2region/raw/master/data/ip2region_v6.xdb) 分开提供。当前 `db_path` 只加载一个文件;需要同时覆盖 IPv4 和 IPv6 时,可选择下面的标准版 IPDB。

### qqwry 标准版(`.ipdb`)

[metowolf/qqwry.ipdb](https://github.com/metowolf/qqwry.ipdb) 的标准版提供中文国家、省、市字段,一个文件同时支持 IPv4 和 IPv6。

- [下载标准版](https://cdn.jsdelivr.net/npm/qqwry.ipdb/qqwry.ipdb) / [备用下载](https://unpkg.com/qqwry.ipdb/qqwry.ipdb)

将文件保存为 `./data/qqwry.ipdb`,并将 `ip_region.db_path` 设置为该路径。请保留 `.ipdb` 扩展名,Artalk 会据此选择 IPDB 读取器,无需额外设置数据库类型。不要使用 `qqwry.raw.ipdb` 原版,它没有独立、完整的省市字段,不适用于这里的精度设置。

IPDB 读取器会将整个文件载入内存。建议保持 `cache.enabled: true`,以便复用已加载的数据;关闭缓存后,每次查询都会重新加载文件。替换数据库文件后,请重启 Artalk 以刷新内存中的数据。

## 精度设置

你可在设置中找到该配置项。
Expand All @@ -22,14 +38,16 @@ Artalk 内置用户 IP 属地展示功能,并且你可以设置显示的精度
| `city` | 精确到城市 | `四川成都` |
| `country` | 精确到国家、地区 | `中国` |

省份数据缺失时,`province` 精度回退到国家或地区,不显示更精确的城市。

配置文件:

```yaml
# IP 属地
ip_region:
# 启用 IP 属地展示
enabled: false
# 数据文件路径 (.xdb 格式)
# 数据文件路径(.xdb 或标准版 .ipdb)
db_path: ./data/ip2region.xdb
# 显示精度 ["province", "city", "country"]
precision: province
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
github.com/gofiber/swagger v1.1.1
github.com/golang-jwt/jwt/v5 v5.3.1
github.com/iancoleman/strcase v0.3.0
github.com/ipipdotnet/ipdb-go v1.3.4-0.20241009061153-a6a2f5d964c9
github.com/jedib0t/go-pretty/v6 v6.8.3
github.com/jeremywohl/flatten v1.0.1
github.com/knadh/koanf v1.5.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,8 @@ github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf h1:WfD7V
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf/go.mod h1:hyb9oH7vZsitZCiBt0ZvifOrB+qc8PS5IiilCIb87rg=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/ipipdotnet/ipdb-go v1.3.4-0.20241009061153-a6a2f5d964c9 h1:AZHxngdTumGVXFSDiyFfmv1uHO04SYhDmOAkRcjpZE4=
github.com/ipipdotnet/ipdb-go v1.3.4-0.20241009061153-a6a2f5d964c9/go.mod h1:yZ+8puwe3R37a/3qRftXo40nZVQbxYDLqls9o5foexs=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
Expand Down
33 changes: 26 additions & 7 deletions internal/core/service_ip_region.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,49 @@
package core

import "github.com/artalkjs/artalk/v2/internal/ip_region"
import (
"sync"

"github.com/artalkjs/artalk/v2/internal/ip_region"
)

var _ Service = (*IPRegionService)(nil)

type IPRegionService struct {
app *App
app *App
mu sync.Mutex
conf ip_region.IPRegionConf
client *ip_region.IPRegion
}

func NewIPRegionService(app *App) *IPRegionService {
return &IPRegionService{app: app}
}

func (s *IPRegionService) Init() error {
return nil
return s.Dispose()
}

func (s *IPRegionService) Dispose() error {
s.mu.Lock()
defer s.mu.Unlock()
s.client = nil // Cached databases are memory-only; release them for GC.
return nil
}

func (s *IPRegionService) Query(ip string) string {
client := ip_region.NewIPRegion(ip_region.IPRegionConf{
return s.getClient().IP2Region(ip)
}

func (s *IPRegionService) getClient() *ip_region.IPRegion {
s.mu.Lock()
defer s.mu.Unlock()
conf := ip_region.IPRegionConf{
IPRegionConf: s.app.Conf().IPRegion,
CacheEnabled: s.app.Conf().Cache.Enabled,
})

return client.IP2Region(ip)
}
if s.client == nil || s.conf != conf {
s.conf = conf
s.client = ip_region.NewIPRegion(conf)
}
return s.client
}
96 changes: 96 additions & 0 deletions internal/core/service_ip_region_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package core

import (
"encoding/binary"
"encoding/json"
"os"
"path/filepath"
"sync"
"testing"

"github.com/artalkjs/artalk/v2/internal/config"
)

func TestIPRegionServiceConfigurationChanges(t *testing.T) {
// Each tiny IPDB has a single leaf for every address. Using real readers here
// exercises cache invalidation through the service, not only config equality.
writeIPDB := func(path, province, city string) {
t.Helper()
record := "中国\t" + province + "\t" + city
data := make([]byte, 11+len(record))
binary.BigEndian.PutUint32(data, 2)
binary.BigEndian.PutUint32(data[4:], 2)
binary.BigEndian.PutUint16(data[9:], uint16(len(record)))
copy(data[11:], record)
meta, err := json.Marshal(map[string]any{"ip_version": 3, "node_count": 1, "total_size": len(data),
"fields": []string{"country_name", "region_name", "city_name"}, "languages": map[string]int{"CN": 0}})
if err != nil {
t.Fatal(err)
}
body := make([]byte, 4)
binary.BigEndian.PutUint32(body, uint32(len(meta)))
if err := os.WriteFile(path, append(append(body, meta...), data...), 0600); err != nil {
t.Fatal(err)
}
}
first := filepath.Join(t.TempDir(), "first.ipdb")
writeIPDB(first, "福建省", "福州市")
second := filepath.Join(t.TempDir(), "second.ipdb")
writeIPDB(second, "浙江省", "杭州市")
conf := &config.Config{}
conf.IPRegion = config.IPRegionConf{Enabled: true, DBPath: first, Precision: "province"}
conf.Cache.Enabled = true
app := NewApp(conf)
service := NewIPRegionService(app)
check := func(want string) {
t.Helper()
if got := service.Query("240e::1"); got != want {
t.Fatalf("got %q want %q", got, want)
}
}
check("福建")
updated := *app.Conf()
updated.IPRegion.DBPath = second
app.SetConf(&updated)
check("浙江")
updated = *app.Conf()
updated.IPRegion.Precision = "city"
app.SetConf(&updated)
check("浙江杭州")
updated = *app.Conf()
updated.Cache.Enabled = false
app.SetConf(&updated)
writeIPDB(second, "广东省", "广州市")
check("广东广州")
writeIPDB(second, "四川省", "成都市")
check("四川成都")
updated = *app.Conf()
updated.Cache.Enabled = true
app.SetConf(&updated)
check("四川成都")
writeIPDB(second, "福建省", "福州市")
check("四川成都") // Cached until the service is reinitialized.
if err := service.Init(); err != nil {
t.Fatal(err)
}
check("福建福州")

// Disposing/reinitializing only drops memory-owned readers, so in-flight
// requests remain safe while later requests acquire a new reader.
var wg sync.WaitGroup
for range 8 {
wg.Go(func() {
for range 10 {
if got := service.Query("1.0.1.1"); got != "福建福州" {
t.Errorf("concurrent lookup: %q", got)
}
}
})
}
for range 10 {
if err := service.Dispose(); err != nil {
t.Fatal(err)
}
}
wg.Wait()
}
58 changes: 51 additions & 7 deletions internal/ip_region/ip_region.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package ip_region

import (
"errors"
"fmt"
"net"
"strings"
"sync"

"github.com/artalkjs/artalk/v2/internal/config"
"github.com/artalkjs/artalk/v2/internal/log"
ipdb "github.com/ipipdotnet/ipdb-go"
)

type IPRegion struct {
conf IPRegionConf
conf IPRegionConf
mu sync.Mutex
cached regionSearcher
}

type IPRegionConf struct {
Expand All @@ -23,18 +30,55 @@ func NewIPRegion(conf IPRegionConf) *IPRegion {
}

func (ipRegion *IPRegion) IP2Region(ip string) string {
if strings.TrimSpace(ip) == "" {
ip = ipScraper(ip)
if net.ParseIP(ip) == nil {
return ""
}

ip = ipScraper(ip)
region, err := search(ip, ipRegion.conf.DBPath, ipRegion.conf.CacheEnabled)
region, err := ipRegion.search(ip)
if err != nil {
if !strings.HasPrefix(err.Error(), "invalid ip address") {
log.Warn("[IP2Region] ", err)
if !strings.HasPrefix(err.Error(), "invalid ip address") &&
!errors.Is(err, ipdb.ErrNoSupportIPv4) && !errors.Is(err, ipdb.ErrNoSupportIPv6) &&
!errors.Is(err, ipdb.ErrDataNotExists) {
log.Warnf("[IP2Region] %q: %v", ipRegion.conf.DBPath, err)
}
return ""
}

return regionScraper(region, config.IPRegionPrecision(ipRegion.conf.Precision))
return region.format(config.IPRegionPrecision(ipRegion.conf.Precision))
}

func (ipRegion *IPRegion) search(ip string) (result region, err error) {
// Both upstream readers may panic on corrupt database offsets. Treat a
// broken local database as a failed lookup, not a failed comment request.
defer func() {
if recovered := recover(); recovered != nil {
result = region{}
err = fmt.Errorf("invalid IP region database: %v", recovered)
}
}()
searcher, err := ipRegion.getSearcher()
if err != nil {
return region{}, err
}
if !ipRegion.conf.CacheEnabled {
defer searcher.Close()
}
return searcher.Search(ip)
}

func (ipRegion *IPRegion) getSearcher() (regionSearcher, error) {
if !ipRegion.conf.CacheEnabled {
return newSearcher(ipRegion.conf.DBPath, false)
}
ipRegion.mu.Lock()
defer ipRegion.mu.Unlock()
if ipRegion.cached == nil {
searcher, err := newSearcher(ipRegion.conf.DBPath, true)
if err != nil {
return nil, err // Retry on the next lookup if the file is repaired.
}
ipRegion.cached = searcher
}
return ipRegion.cached, nil
}
Loading
Loading