Skip to content

Commit 140bedf

Browse files
authored
feat: Multi-Geolocations DNS (#21)
1 parent e6b2afe commit 140bedf

File tree

14 files changed

+792
-617
lines changed

14 files changed

+792
-617
lines changed

cmd/ips/cmd_mdns.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright (c) 2023 [email protected]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package ips
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/spf13/cobra"
23+
)
24+
25+
func init() {
26+
rootCmd.AddCommand(mdnsCmd)
27+
28+
// mdns
29+
mdnsCmd.Flags().StringVarP(&dnsClientNet, "net", "", "udp", "Specifies the network protocol to be used by the DNS client. tcp, udp, tcp-tls.")
30+
mdnsCmd.Flags().IntVarP(&dnsClientTimeoutMs, "client-timeout", "", 1000, "Defines the timeout in milliseconds for DNS client requests.")
31+
mdnsCmd.Flags().BoolVarP(&dnsClientSingleInflight, "single-inflight", "", false, "Indicates whether the DNS client should avoid making duplicate queries concurrently.")
32+
mdnsCmd.Flags().IntVarP(&mdnsTimeoutS, "timeout", "", 20, "Specifies the timeout in seconds for MDNS operations.")
33+
mdnsCmd.Flags().StringVarP(&mdnsExchangeAddress, "exchange-address", "", "119.29.29.29", "Defines the address of the DNS server to be used for MDNS queries.")
34+
mdnsCmd.Flags().IntVarP(&mdnsRetryTimes, "retry-times", "", 3, "Sets the number of times an MDNS query should be retried on failure.")
35+
36+
// operate
37+
mdnsCmd.Flags().StringVarP(&fields, "fields", "f", "", UsageFields)
38+
mdnsCmd.Flags().BoolVarP(&useDBFields, "use-db-fields", "", false, UsageUseDBFields)
39+
mdnsCmd.Flags().StringVarP(&rewriteFiles, "rewrite-files", "r", "", UsageRewriteFiles)
40+
mdnsCmd.Flags().StringVarP(&lang, "lang", "", "", UsageLang)
41+
42+
// database
43+
mdnsCmd.Flags().StringSliceVarP(&rootFile, "file", "i", nil, UsageQueryFile)
44+
mdnsCmd.Flags().StringSliceVarP(&rootFormat, "format", "", nil, UsageQueryFormat)
45+
mdnsCmd.Flags().StringSliceVarP(&rootIPv4File, "ipv4-file", "", nil, UsageQueryIPv4File)
46+
mdnsCmd.Flags().StringSliceVarP(&rootIPv4Format, "ipv4-format", "", nil, UsageQueryIPv4Format)
47+
mdnsCmd.Flags().StringSliceVarP(&rootIPv6File, "ipv6-file", "", nil, UsageQueryIPv6File)
48+
mdnsCmd.Flags().StringSliceVarP(&rootIPv6Format, "ipv6-format", "", nil, UsageQueryIPv6Format)
49+
mdnsCmd.Flags().StringVarP(&readerOption, "database-option", "", "", UsageReaderOption)
50+
mdnsCmd.Flags().StringVarP(&hybridMode, "hybrid-mode", "", "aggregation", UsageHybridMode)
51+
52+
// output
53+
mdnsCmd.Flags().StringVarP(&rootTextValuesSep, "text-values-sep", "", "", UsageTextValuesSep)
54+
}
55+
56+
var mdnsCmd = &cobra.Command{
57+
Use: "mdns <domain>",
58+
Short: "Execute Multi-Geolocation DNS queries.",
59+
Long: `The 'ips mdns' command is designed to query domain name resolutions across multiple regions.
60+
61+
Utilizing EDNS capabilities, this command sends the client's IP address to the DNS server, which then returns the domain name resolution results for the corresponding region.
62+
63+
The command provides a global perspective on domain name resolutions, enabling users to quickly identify any anomalies in DNS resolutions.
64+
65+
For more detailed information and advanced configuration options, please refer to https://github.com/sjzar/ips/blob/main/docs/mdns.md`,
66+
PreRun: PreRunInit,
67+
Run: MDNS,
68+
}
69+
70+
func MDNS(cmd *cobra.Command, args []string) {
71+
72+
if len(args) == 0 {
73+
_ = cmd.Help()
74+
return
75+
}
76+
77+
ret, err := manager.MDNSResolve(args[0])
78+
if err != nil {
79+
return
80+
}
81+
fmt.Println(ret)
82+
}

cmd/ips/config.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,27 @@ var (
137137

138138
// addr specifies the server address.
139139
addr string
140+
141+
// mdns
142+
143+
// dnsClientNet specifies the network protocol to be used by the DNS client. tcp, udp, tcp-tls.
144+
dnsClientNet string
145+
146+
// dnsClientTimeoutMs defines the timeout in milliseconds for DNS client requests.
147+
dnsClientTimeoutMs int
148+
149+
// dnsClientSingleInflight indicates whether the DNS client should avoid making duplicate
150+
// queries concurrently.
151+
dnsClientSingleInflight bool
152+
153+
// mdnsTimeoutS specifies the timeout in seconds for MDNS operations.
154+
mdnsTimeoutS int
155+
156+
// mdnsExchangeAddress defines the address of the DNS server to be used for MDNS queries.
157+
mdnsExchangeAddress string
158+
159+
// mdnsRetryTimes sets the number of times an MDNS query should be retried on failure.
160+
mdnsRetryTimes int
140161
)
141162

142163
// GetFlagConfig initializes and returns the configuration for the IP service.
@@ -260,6 +281,30 @@ func GetFlagConfig() *ips.Config {
260281
conf.MyIPTimeoutS = myIPTimeoutS
261282
}
262283

284+
if len(dnsClientNet) > 0 {
285+
conf.DNSClientNet = dnsClientNet
286+
}
287+
288+
if dnsClientTimeoutMs > 0 {
289+
conf.DNSClientTimeoutMs = dnsClientTimeoutMs
290+
}
291+
292+
if dnsClientSingleInflight {
293+
conf.DNSClientSingleInflight = dnsClientSingleInflight
294+
}
295+
296+
if mdnsTimeoutS > 0 {
297+
conf.MDNSTimeoutS = mdnsTimeoutS
298+
}
299+
300+
if len(mdnsExchangeAddress) > 0 {
301+
conf.MDNSExchangeAddress = mdnsExchangeAddress
302+
}
303+
304+
if mdnsRetryTimes > 0 {
305+
conf.MDNSRetryTimes = mdnsRetryTimes
306+
}
307+
263308
return conf
264309
}
265310

docs/mdns.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# IPS 多地域域名解析命令说明
2+
3+
## 简介
4+
5+
`ips mdns` 命令旨在查询多地域的域名解析结果。
6+
7+
该命令利用了 EDNS 功能,向 DNS 服务器发送客户端 IP 地址,以获取相应地域的域名解析结果。
8+
9+
命令提供了域名解析的全球视角,能够帮助用户快速识别 DNS 解析中的异常情况。
10+
11+
## 使用方法
12+
13+
通过 `ips mdns` 命令,用户可以指定域名和 DNS 服务器地址进行查询。结果将以表格形式显示,清晰地展示不同地理位置的解析结果。
14+
15+
## 命令语法
16+
17+
```shell
18+
ips mdns <domain> [flags]
19+
```
20+
21+
- `--net string`: 指定 DNS 请求的网络类型,可选值为 `tcp``udp``tcp-tls`,默认为 `udp`
22+
- `--client-timeout int`: 指定每个 DNS 请求客户端超时时间,单位为毫秒,默认为 1000 毫秒。
23+
- `--single-inflight`: 指定是否合并并发的 DNS 请求,默认为 `false`
24+
- `--timeout int`: 指定 MDNS 命令的总超时时间,单位为秒,默认为 20 秒。
25+
- `--exchange-address string`: 指定 DNS 服务器地址,默认为 `119.29.29.29`
26+
- `--retry-times`: 指定 DNS 请求的重试次数,默认为 3 次。
27+
28+
## 示例
29+
30+
### 查询域名的多地域解析结果
31+
32+
```shell
33+
ips mdns i0.hdslb.com
34+
+--------------------+-------------------------------------------------+-------------------------------------------------+
35+
| GEOISP | CNAME | IP |
36+
+--------------------+-------------------------------------------------+-------------------------------------------------+
37+
| 27.224.0.0 | i0.hdslb.com.04f6a54d.c.cdnhwc1.com [华为] | 60.165.116.47 [中国 甘肃 兰州 电信] |
38+
| [中国 甘肃 电信] | hcdnw.biliv6.c.cdnhwc2.com [华为] | 60.165.116.48 [中国 甘肃 兰州 电信] |
39+
+--------------------+-------------------------------------------------+-------------------------------------------------+
40+
| 36.133.72.0 | i0.hdslb.com.w.kunlunno.com [阿里] | 221.181.64.184 [中国 上海 上海 移动] |
41+
| [中国 上海 移动] | | 221.181.64.148 [中国 上海 上海 移动] |
42+
+--------------------+-------------------------------------------------+-------------------------------------------------+
43+
| 36.133.108.0 | i0.hdslb.com.04f6a54d.c.cdnhwc1.com [华为] | 39.136.138.59 [中国 重庆 重庆 移动] |
44+
| [中国 重庆 移动] | hcdnw.biliv6.d.cdn.chinamobile.com [移动] | 39.136.138.58 [中国 重庆 重庆 移动] |
45+
+--------------------+-------------------------------------------------+-------------------------------------------------+
46+
| 1.56.0.0 | i0.hdslb.com.04f6a54d.c.cdnhwc1.com [华为] | 218.10.185.43 [中国 黑龙江 鹤岗 联通] |
47+
| [中国 黑龙江 联通] | hcdnw.biliv6.c.cdnhwc2.com [华为] | 218.60.101.84 [中国 辽宁 大连 联通] |
48+
+--------------------+-------------------------------------------------+-------------------------------------------------+
49+
| 42.202.0.0 | i0.hdslb.com.download.ks-cdn.com [金山] | 123.184.57.130 [中国 辽宁 沈阳 电信] |
50+
| [中国 辽宁 电信] | k1-ipv6.gslb.ksyuncdn.com [金山] | 123.184.57.129 [中国 辽宁 沈阳 电信] |
51+
+--------------------+-------------------------------------------------+-------------------------------------------------+
52+
| TOTAL | 11 | 730 |
53+
+--------------------+-------------------------------------------------+-------------------------------------------------+
54+
<省略部分输出>
55+
```
56+
57+
### 使用自定义 DNS 服务器
58+
59+
```shell
60+
ips mdns i0.hdslb.com --exchange-address 8.8.8.8
61+
```
62+
63+
## 注意事项
64+
65+
- 如果不同地理位置的解析结果相同,建议检查本地 DNS 劫持问题,例如防火墙在端口 53 上的流量重定向。

docs/mdns_en.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# IPS MDNS Command Documentation
2+
3+
## Introduction
4+
5+
The `ips mdns` command is designed to query domain name resolutions across multiple regions.
6+
7+
Utilizing EDNS capabilities, this command sends the client's IP address to the DNS server, which then returns the domain name resolution results for the corresponding region.
8+
9+
The command provides a global perspective on domain name resolutions, enabling users to quickly identify any anomalies in DNS resolutions.
10+
11+
## Usage
12+
13+
Using the `ips mdns` command, users can specify a domain and a DNS server address for querying. The results are displayed in a table format, clearly showing the resolution results from different geographical locations.
14+
15+
## Command Syntax
16+
17+
```shell
18+
ips mdns <domain> [flags]
19+
```
20+
21+
- `--net string`: Specifies the network type for DNS requests, options include `tcp`, `udp`, and `tcp-tls`, with the default being `udp`.
22+
- `--client-timeout int`: Sets the client-side timeout for each DNS request in milliseconds, defaulting to 1000 milliseconds.
23+
- `--single-inflight`: Specifies whether to merge concurrent DNS requests, defaulting to `false`.
24+
- `--timeout int`: Sets the overall timeout for the MDNS command in seconds, with a default of 20 seconds.
25+
- `--exchange-address string`: Specifies the DNS server address, defaulting to `119.29.29.29`.
26+
- `--retry-times`: Determines the number of retry attempts for DNS requests, defaulting to 3 times.
27+
28+
## Examples
29+
30+
### Querying Multi-Region Domain Name Resolutions
31+
32+
```shell
33+
ips mdns i0.hdslb.com
34+
+--------------------+-------------------------------------------------+-------------------------------------------------+
35+
| GEOISP | CNAME | IP |
36+
+--------------------+-------------------------------------------------+-------------------------------------------------+
37+
| 27.224.0.0 | i0.hdslb.com.04f6a54d.c.cdnhwc1.com [华为] | 60.165.116.47 [中国 甘肃 兰州 电信] |
38+
| [中国 甘肃 电信] | hcdnw.biliv6.c.cdnhwc2.com [华为] | 60.165.116.48 [中国 甘肃 兰州 电信] |
39+
+--------------------+-------------------------------------------------+-------------------------------------------------+
40+
| 36.133.72.0 | i0.hdslb.com.w.kunlunno.com [阿里] | 221.181.64.184 [中国 上海 上海 移动] |
41+
| [中国 上海 移动] | | 221.181.64.148 [中国 上海 上海 移动] |
42+
+--------------------+-------------------------------------------------+-------------------------------------------------+
43+
| 36.133.108.0 | i0.hdslb.com.04f6a54d.c.cdnhwc1.com [华为] | 39.136.138.59 [中国 重庆 重庆 移动] |
44+
| [中国 重庆 移动] | hcdnw.biliv6.d.cdn.chinamobile.com [移动] | 39.136.138.58 [中国 重庆 重庆 移动] |
45+
+--------------------+-------------------------------------------------+-------------------------------------------------+
46+
| 1.56.0.0 | i0.hdslb.com.04f6a54d.c.cdnhwc1.com [华为] | 218.10.185.43 [中国 黑龙江 鹤岗 联通] |
47+
| [中国 黑龙江 联通] | hcdnw.biliv6.c.cdnhwc2.com [华为] | 218.60.101.84 [中国 辽宁 大连 联通] |
48+
+--------------------+-------------------------------------------------+-------------------------------------------------+
49+
| 42.202.0.0 | i0.hdslb.com.download.ks-cdn.com [金山] | 123.184.57.130 [中国 辽宁 沈阳 电信] |
50+
| [中国 辽宁 电信] | k1-ipv6.gslb.ksyuncdn.com [金山] | 123.184.57.129 [中国 辽宁 沈阳 电信] |
51+
+--------------------+-------------------------------------------------+-------------------------------------------------+
52+
| TOTAL | 11 | 730 |
53+
+--------------------+-------------------------------------------------+-------------------------------------------------+
54+
<omitted part of the output>
55+
```
56+
57+
### Using a Custom DNS Server
58+
59+
```shell
60+
ips mdns i0.hdslb.com --exchange-address 8.8.8.8
61+
```
62+
63+
## Important Notes
64+
65+
- If the resolution results are identical across different geographical locations, it is advised to check for local DNS hijacking issues, such as firewalls redirecting traffic on port 53.

docs/usage.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- [IPS 转存命令说明](./dump.md) - 转存 IP 地理位置数据库。
1010
- [IPS 打包命令说明](./pack.md) - 打包 IP 地理位置数据库。
1111
- [IPS 查询命令说明](./query.md) - 查询 IP 地理位置。
12+
- [IPS 多地域域名解析命令说明](./mdns.md) - 查询多地域域名解析结果。
1213
- [IPS 服务命令说明](./server.md) - 启动 IPS 服务。
1314

1415
## 支持的数据库格式

docs/usage_en.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Welcome to IPS! This is a command-line tool and library designed to help you eas
99
- [IPS Dump Command Documentation](./dump_en.md) - Dump IP geolocation databases.
1010
- [IPS Pack Command Documentation](./pack_en.md) - Package IP geolocation databases.
1111
- [IPS Command Documentation](./query_en.md) - Query IP geolocation information.
12+
- [IPS MDNS Command Documentation](./mdns_en.md) - Query Multi-Geolocations DNS resolution results.
1213
- [IPS Server Command Documentation](./server_en.md) - Start the IPS service.
1314

1415
## Supported Database Formats

go.mod

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
module github.com/sjzar/ips
22

3-
go 1.16
3+
go 1.18
44

55
require (
66
github.com/dilfish/awdb-golang/awdb-golang v1.0.20210701
77
github.com/gin-gonic/gin v1.9.1
88
github.com/maxmind/mmdbwriter v1.0.0
99
github.com/miekg/dns v1.1.41
10+
github.com/olekukonko/tablewriter v0.0.5
1011
github.com/oschwald/maxminddb-golang v1.12.0
1112
github.com/pion/stun/v2 v2.0.0
1213
github.com/schollz/progressbar/v3 v3.13.1
@@ -17,3 +18,52 @@ require (
1718
golang.org/x/net v0.14.0
1819
golang.org/x/text v0.12.0
1920
)
21+
22+
require (
23+
github.com/bytedance/sonic v1.9.1 // indirect
24+
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
25+
github.com/davecgh/go-spew v1.1.1 // indirect
26+
github.com/fsnotify/fsnotify v1.6.0 // indirect
27+
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
28+
github.com/gin-contrib/sse v0.1.0 // indirect
29+
github.com/go-playground/locales v0.14.1 // indirect
30+
github.com/go-playground/universal-translator v0.18.1 // indirect
31+
github.com/go-playground/validator/v10 v10.14.0 // indirect
32+
github.com/goccy/go-json v0.10.2 // indirect
33+
github.com/hashicorp/hcl v1.0.0 // indirect
34+
github.com/inconshreveable/mousetrap v1.0.1 // indirect
35+
github.com/json-iterator/go v1.1.12 // indirect
36+
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
37+
github.com/leodido/go-urn v1.2.4 // indirect
38+
github.com/magiconair/properties v1.8.6 // indirect
39+
github.com/mattn/go-isatty v0.0.19 // indirect
40+
github.com/mattn/go-runewidth v0.0.14 // indirect
41+
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
42+
github.com/mitchellh/mapstructure v1.5.0 // indirect
43+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
44+
github.com/modern-go/reflect2 v1.0.2 // indirect
45+
github.com/pelletier/go-toml v1.9.5 // indirect
46+
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
47+
github.com/pion/dtls/v2 v2.2.7 // indirect
48+
github.com/pion/logging v0.2.2 // indirect
49+
github.com/pion/transport/v2 v2.2.1 // indirect
50+
github.com/pion/transport/v3 v3.0.1 // indirect
51+
github.com/pmezard/go-difflib v1.0.0 // indirect
52+
github.com/rivo/uniseg v0.2.0 // indirect
53+
github.com/spf13/afero v1.9.2 // indirect
54+
github.com/spf13/cast v1.5.0 // indirect
55+
github.com/spf13/jwalterweatherman v1.1.0 // indirect
56+
github.com/spf13/pflag v1.0.5 // indirect
57+
github.com/subosito/gotenv v1.4.1 // indirect
58+
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
59+
github.com/ugorji/go/codec v1.2.11 // indirect
60+
go4.org/netipx v0.0.0-20220812043211-3cc044ffd68d // indirect
61+
golang.org/x/arch v0.3.0 // indirect
62+
golang.org/x/crypto v0.12.0 // indirect
63+
golang.org/x/sys v0.11.0 // indirect
64+
golang.org/x/term v0.11.0 // indirect
65+
google.golang.org/protobuf v1.30.0 // indirect
66+
gopkg.in/ini.v1 v1.67.0 // indirect
67+
gopkg.in/yaml.v2 v2.4.0 // indirect
68+
gopkg.in/yaml.v3 v3.0.1 // indirect
69+
)

0 commit comments

Comments
 (0)