Skip to content

Commit eea77b1

Browse files
authoredAug 15, 2024
Enhance ipinfoLocal.mmdb file lookup (nxtrace#66)
* Enhance ipinfoLocal.mmdb file lookup * Refactor get NEXTTRACE_IPINFOLOCALPATH env variable into util
1 parent 3e0a086 commit eea77b1

File tree

4 files changed

+63
-7
lines changed

4 files changed

+63
-7
lines changed
 

‎README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,13 @@ export NO_COLOR=1
269269
nexttrace --data-provider ip-api.com
270270
## Note There are frequency limits for free queries of the ipinfo and IPInsight APIs. You can purchase services from these providers to remove the limits
271271
## If necessary, you can clone this project, add the token provided by ipinfo or IPInsight and compile it yourself
272+
## Fill the token to: ipgeo/tokens.go
273+
272274
## Note For the offline database IPInfoLocal, please download it manually and rename it to ipinfoLocal.mmdb. (You can download it from here: https://ipinfo.io/signup?ref=free-database-downloads)
275+
## Current directory, nexttrace binary directory and FHS directories (Unix-like) will be searched.
276+
## To customize it, please use environment variables,
277+
export NEXTTRACE_IPINFOLOCALPATH=/xxx/yyy.mmdb
273278
## For the offline database Ip2region, you can download it manually and rename it to ip2region.db, or let NextTrace download it automatically
274-
## Fill the token to: ipgeo/tokens.go
275279
## Please be aware: Due to the serious abuse of IP.SB, you will often be not able to query IP data from this source
276280
## IP-API.com has a stricter restiction on API calls, if you can't query IP data from this source, please try again in a few minutes
277281

‎README_zh_CN.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,15 @@ export NO_COLOR=1
282282
nexttrace --data-provider ip-api.com
283283
## 特别的: 其中 ipinfo 和 IPInsight API 对于免费版查询有频率限制,可从这些服务商自行购买服务以解除限制,如有需要可以 clone 本项目添加其提供的 token 自行编译
284284
## TOKEN填写路径:ipgeo/tokens.go
285-
## 特别的: 对于离线库 IPInfoLocal,请自行下载并命名为 ipinfoLocal.mmdb (可以从这里下载:https://ipinfo.io/signup?ref=free-database-downloads)
285+
286+
## 特别的: 对于离线库 IPInfoLocal,请自行下载并命名为 ipinfoLocal.mmdb
287+
## (可以从这里下载:https://ipinfo.io/signup?ref=free-database-downloads),
288+
## 默认搜索用户当前路径、程序所在路径、和 FHS 路径(Unix-like)
289+
## 如果需要自定义路径,请设置环境变量
290+
export NEXTTRACE_IPINFOLOCALPATH=/xxx/yyy.mmdb
286291
## 对于离线库 Ip2region 可NextTrace自动下载,也可自行下载并命名为 ip2region.db
287292
## 另外:由于IP.SB被滥用比较严重,会经常出现无法查询的问题,请知悉。
288293
## IP-API.com限制调用较为严格,如有查询不到的情况,请几分钟后再试。
289-
290294
# 纯真IP数据库默认使用 http://127.0.0.1:2060 作为查询接口,如需自定义请使用环境变量
291295
export NEXTTRACE_CHUNZHENURL=http://127.0.0.1:2060
292296
## 可使用 https://github.com/freshcn/qqwry 自行搭建纯真IP数据库服务

‎ipgeo/ipinfoLocal.go

+51-4
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,71 @@ package ipgeo
22

33
import (
44
"errors"
5-
"github.com/oschwald/maxminddb-golang"
65
"net"
76
"os"
7+
"path/filepath"
8+
"runtime"
89
"strings"
910
"time"
11+
12+
"github.com/nxtrace/NTrace-core/util"
13+
"github.com/oschwald/maxminddb-golang"
1014
)
1115

1216
const (
13-
ipinfoDataBasePath = "./ipinfoLocal.mmdb"
17+
ipinfoDataBaseFilename = "ipinfoLocal.mmdb"
1418
)
1519

20+
// Cache the path of the ipinfoLocal.mmdb file
21+
var ipinfoDataBasePath = ""
22+
23+
// We will try to get the path of the ipinfoLocal.mmdb file in the following order:
24+
// 1. Use the value of the environment variable NEXTTRACE_IPINFOLOCALPATH
25+
// 2. Search in the current folder and the executable folder
26+
// 3. Search in /usr/local/share/nexttrace/ and /usr/share/nexttrace/ (for Unix/Linux)
27+
// If the file is found, the path will be stored in the ipinfoDataBasePath variable
28+
func getIPInfoLocalPath() (error) {
29+
if ipinfoDataBasePath != "" {
30+
return nil
31+
}
32+
// NEXTTRACE_IPINFOLOCALPATH
33+
if util.EnvIPInfoLocalPath != "" {
34+
if _, err := os.Stat(util.EnvIPInfoLocalPath); err == nil {
35+
ipinfoDataBasePath = util.EnvIPInfoLocalPath
36+
return nil
37+
} else {
38+
return errors.New("NEXTTRACE_IPINFOLOCALPATH is set but the file does not exist")
39+
}
40+
}
41+
folders := []string{}
42+
// current folder
43+
if cur, err := os.Getwd(); err == nil {
44+
folders = append(folders, cur + string(filepath.Separator))
45+
}
46+
// exeutable folder
47+
if exe, err := os.Executable(); err == nil {
48+
folders = append(folders, filepath.Dir(exe) + string(filepath.Separator))
49+
}
50+
if runtime.GOOS != "windows" {
51+
folders = append(folders, "/usr/local/share/nexttrace/")
52+
folders = append(folders, "/usr/share/nexttrace/")
53+
}
54+
for _, folder := range folders {
55+
if _, err := os.Stat(folder + ipinfoDataBaseFilename); err == nil {
56+
ipinfoDataBasePath = folder + ipinfoDataBaseFilename
57+
return nil
58+
}
59+
}
60+
return errors.New("no ipinfoLocal.mmdb found")
61+
}
62+
1663
func IPInfoLocal(ip string, _ time.Duration, _ string, _ bool) (*IPGeoData, error) {
17-
if _, err := os.Stat(ipinfoDataBasePath); os.IsNotExist(err) {
64+
if err := getIPInfoLocalPath(); err != nil {
1865
panic("Cannot find ipinfoLocal.mmdb")
1966
}
2067
region, err := maxminddb.Open(ipinfoDataBasePath)
2168
if err != nil {
22-
panic("Cannot find ipinfoLocal.mmdb")
69+
panic("Cannot open ipinfoLocal.mmdb at " + ipinfoDataBasePath)
2370
}
2471
defer func(region *maxminddb.Reader) {
2572
err := region.Close()

‎util/util.go

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818

1919
var Uninterrupted = GetenvDefault("NEXTTRACE_UNINTERRUPTED", "")
2020
var EnvToken = GetenvDefault("NEXTTRACE_TOKEN", "")
21+
var EnvIPInfoLocalPath = GetenvDefault("NEXTTRACE_IPINFOLOCALPATH", "")
2122
var UserAgent = fmt.Sprintf("NextTrace %s/%s/%s", config.Version, runtime.GOOS, runtime.GOARCH)
2223
var RdnsCache sync.Map
2324
var PowProviderParam = ""

0 commit comments

Comments
 (0)
Please sign in to comment.