Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 81237f2

Browse files
committed
1.1.0
1 parent a453b82 commit 81237f2

21 files changed

Lines changed: 9045 additions & 8 deletions

File tree

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,20 @@
1-
# TCP.mk Node
1+
## About TCP.mk
2+
3+
`TCP.mk` is an efficient network toolkit designed to help users quickly diagnose and resolve network issues, improving work efficiency. Our toolset covers a wide range of functionalities, from IP queries to network status detection, making it suitable for various network operation and development scenarios.
4+
5+
6+
### Main Features
7+
- **IP Aggregation Query**: Retrieves geographic location information from multiple IP data sources, enabling easy comparison and avoiding inaccuracies from a single data source.
8+
- **Batch IP Query**: Supports querying up to 50 IPs at once.
9+
- **HTTP Status Detection**: Monitors the HTTP status codes of websites or services in real-time, helping users track service availability.
10+
- **TCP Port Detection**: Checks the open/closed status of TCP ports on target hosts.
11+
- **GFW Detection**: Quickly Detect if a Website or IP is Blocked by the Great Firewall of China.
12+
- **Online Dig Command**: Provides an online DNS query tool that supports multiple DNS record types, enabling users to quickly diagnose DNS issues.
13+
- **Dual-Region IP Detection**: Detects the IPs used for accessing domestic and international networks.
14+
- **Global Ping**: Initiates Ping probes from various locations worldwide to comprehensively assess network latency and connectivity.
15+
- **Traceroute**: Initiates route tracing from any global node to precisely analyze network routing.
16+
- **API**: We provide an API interface for IP queries.
17+
18+
### Notice
19+
20+
This project implements the node functionality of TCP.mk. For the full feature set, please visit: [https://tcp.mk](https://tcp.mk)

api/appinfo.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package api
2+
3+
import "github.com/gin-gonic/gin"
4+
5+
// 声明程序版本
6+
var Version = "1.1.0"
7+
8+
// 声明一个结构体
9+
type appinfo struct {
10+
Version string `json:"version"`
11+
}
12+
13+
func Appinfo(c *gin.Context) {
14+
var info appinfo
15+
info.Version = Version
16+
c.JSON(200, gin.H{
17+
"code": 200,
18+
"msg": "success",
19+
"data": info,
20+
})
21+
}

api/health.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package api
2+
3+
import "github.com/gin-gonic/gin"
4+
5+
func Pong(c *gin.Context) {
6+
// 返回pong 文本
7+
c.String(200, "pong")
8+
}

api/ping.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package api
22

33
import (
4+
"fmt"
45
"strconv"
56
"tcpmk_node/utils"
67
"time"
@@ -18,9 +19,14 @@ type icmpinfo struct {
1819
PingCount int `json:"ping_count"`
1920
Host string `json:"host"`
2021
IP string `json:"ip"`
22+
Area string `json:"area"`
2123
}
2224

2325
func IcmpPing(c *gin.Context) {
26+
// 获取所有请求头并打印
27+
// fmt.Println(c.Request.Header)
28+
// fmt.Println(c.Request.Host)
29+
2430
// 获取用户输入的IP
2531
host := c.DefaultPostForm("host", "")
2632
// 获取ping的次数
@@ -84,7 +90,7 @@ func IcmpPing(c *gin.Context) {
8490
pinger.Timeout = 11 * time.Second
8591
err = pinger.Run() // Blocks until finished.
8692
if err != nil {
87-
// fmt.Println(err)
93+
fmt.Println(err)
8894
c.JSON(200, gin.H{
8995
"code": 500,
9096
"msg": "Running failed!",
@@ -106,6 +112,26 @@ func IcmpPing(c *gin.Context) {
106112
info.PingCount = pinger.Count
107113
info.Host = host
108114
info.IP = ip
115+
// 查询IP所在地区,判断是否是国内IP
116+
var ipArea IPInfo
117+
// 国内IP,且用户语言为中文
118+
if utils.IsIPInCN(ip) && utils.GetUserLang(c) == "zh" {
119+
// 如果是国内IP,则用纯真接口解析
120+
ipArea, err = ParseQQwryNew(ip)
121+
info.Area = ipArea.Country + "/" + ipArea.Region + "/" + ipArea.City + "/" + ipArea.ISP
122+
} else {
123+
// 如果是国外IP,则用IP2Location解析
124+
ipArea, err = ParseIP2Location(ip)
125+
info.Area = ipArea.City + "," + ipArea.Region + "," + ipArea.Country
126+
}
127+
128+
if err != nil {
129+
c.JSON(200, gin.H{
130+
"code": 500,
131+
"msg": "Failed to get IP area",
132+
"data": "",
133+
})
134+
}
109135

110136
// 获取丢包率
111137
c.JSON(200, gin.H{

api/qqwry.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package api
2+
3+
import (
4+
"github.com/ipipdotnet/ipdb-go"
5+
)
6+
7+
// 新版解析纯真IP数据库,使用的ipip数据库格式
8+
func ParseQQwryNew(ip string) (IPInfo, error) {
9+
db, err := ipdb.NewCity("data/ipdb/qqwry.ipdb")
10+
if err != nil {
11+
return IPInfo{}, err
12+
}
13+
14+
// fmt.Println(db.FindMap(ip, "CN")) // return map[string]string
15+
result, err := db.FindMap(ip, "CN")
16+
// 如果IP查找失败
17+
if err != nil {
18+
return IPInfo{}, err
19+
}
20+
21+
var ipInfo = IPInfo{
22+
IP: ip,
23+
Country: result["country_name"],
24+
Region: result["region_name"],
25+
City: result["city_name"],
26+
County: "",
27+
ISP: result["isp_domain"],
28+
Area: "",
29+
}
30+
31+
return ipInfo, nil
32+
}

api/traceroute.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,19 @@ func Traceroute(c *gin.Context) {
6363
continue
6464
}
6565
// fmt.Printf("%d\t%s\t", result.Hop, result.IP)
66-
// 解析IP得到归属地
67-
address, _ := ParseIP2Location(result.IP.String())
66+
// 解析IP得到归属地,判断是否是国内IP
67+
var address IPInfo
68+
var area string
69+
if utils.IsIPInCN(result.IP.String()) && utils.GetUserLang(c) == "zh" {
70+
// 国内IP则用纯真接口解析
71+
address, _ = ParseQQwryNew(result.IP.String())
72+
area = address.Country + "/" + address.Region + "/" + address.City + "/" + address.ISP
73+
} else {
74+
// 国外接口用ip2location解析
75+
address, _ = ParseIP2Location(result.IP.String())
76+
area = address.City + "," + address.Region + "," + address.Country
77+
}
78+
6879
// 获取时间
6980
// 拼接为字符串
7081
var formattedString string
@@ -74,7 +85,7 @@ func Traceroute(c *gin.Context) {
7485
formattedString = fmt.Sprintf("%.0fms", rttSeconds)
7586
}
7687
// 拼接归属地
77-
output := result.IP.String() + "," + address.Country + " " + address.Region + " " + address.City + "," + formattedString
88+
output := result.IP.String() + "|" + area + "|" + formattedString
7889
// 可选:发送完成标志
7990
c.SSEvent("data", output)
8091
c.Writer.Flush() // 添加这一行来刷新缓冲区

api/upgrade.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package api
2+
3+
import (
4+
"io"
5+
"net/http"
6+
"os"
7+
8+
"github.com/gin-gonic/gin"
9+
)
10+
11+
// 升级接口
12+
func Upgrade(c *gin.Context) {
13+
// 升级包地址
14+
url := "https://soft.xiaoz.org/UniBin/tcpmk_node/tcpmk_node.tar.gz"
15+
// 下载这个升级包到/opt/tcpmk_node/目录下
16+
// 下载这个升级包到/opt/tcpmk_node/目录下
17+
filePath := "/opt/tcpmk_node/tcpmk_node.tar.gz"
18+
// 创建目录(如果不存在)
19+
if err := os.MkdirAll("/opt/tcpmk_node", 0755); err != nil {
20+
c.JSON(200, gin.H{
21+
"code": 500,
22+
"msg": "Failed to create directory",
23+
"data": "",
24+
})
25+
return
26+
}
27+
// 下载文件
28+
resp, err := http.Get(url)
29+
if err != nil {
30+
c.JSON(200, gin.H{
31+
"code": 500,
32+
"msg": "Failed to download file",
33+
"data": "",
34+
})
35+
return
36+
}
37+
defer resp.Body.Close()
38+
39+
// 创建文件
40+
out, err := os.Create(filePath)
41+
if err != nil {
42+
c.JSON(200, gin.H{
43+
"code": 500,
44+
"msg": "Failed to create file",
45+
"data": "",
46+
})
47+
return
48+
}
49+
defer out.Close()
50+
51+
// 将下载的内容写入文件
52+
_, err = io.Copy(out, resp.Body)
53+
if err != nil {
54+
c.JSON(200, gin.H{
55+
"code": 500,
56+
"msg": "Failed to write file",
57+
"data": "",
58+
})
59+
return
60+
}
61+
62+
c.JSON(200, gin.H{
63+
"code": 200,
64+
"msg": "Upgrade successful",
65+
"data": "",
66+
})
67+
68+
// 退出进程
69+
os.Exit(0)
70+
}

data/config/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)