4 Commits

Author SHA1 Message Date
8228932df7 feat: dist 2024-04-22 16:05:16 +08:00
48669bd197 fix: release name 2024-04-22 15:37:00 +08:00
33685bfc8a feat: average delay 2024-04-22 15:22:37 +08:00
27eebf9f25 update: README.md 2024-04-22 14:48:01 +08:00
4 changed files with 54 additions and 13 deletions

2
.gitignore vendored
View File

@@ -2,3 +2,5 @@ bin/
*.exe *.exe
tcping tcping
dist/

View File

@@ -29,7 +29,7 @@ releases: linux darwin-amd64 darwin-arm64 win64 win32
tar zcf $(BINDIR)/$(NAME)-darwin-arm64-$(VERSION).tar.gz -C $(BINDIR) $(NAME)-darwin-arm64 tar zcf $(BINDIR)/$(NAME)-darwin-arm64-$(VERSION).tar.gz -C $(BINDIR) $(NAME)-darwin-arm64
zip -j $(BINDIR)/$(NAME)-win32-$(VERSION).zip $(BINDIR)/$(NAME)-win32.exe zip -j $(BINDIR)/$(NAME)-win32-$(VERSION).zip $(BINDIR)/$(NAME)-win32.exe
zip -j $(BINDIR)/$(NAME)-win64-$(VERSION).zip $(BINDIR)/$(NAME)-win64.exe zip -j $(BINDIR)/$(NAME)-win64-$(VERSION).zip $(BINDIR)/$(NAME)-win64.exe
rm -f $(BINDIR)/socks5-darwin-amd64 $(BINDIR)/socks5-darwin-arm64 $(BINDIR)/socks5-linux $(BINDIR)/socks5-win32.exe $(BINDIR)/socks5-win64.exe rm -f $(BINDIR)/$(NAME)-darwin-amd64 $(BINDIR)/$(NAME)-darwin-arm64 $(BINDIR)/$(NAME)-linux $(BINDIR)/$(NAME)-win32.exe $(BINDIR)/$(NAME)-win64.exe
clean: clean:
rm $(BINDIR)/* rm $(BINDIR)/*

View File

@@ -1,17 +1,50 @@
# Tcping GO # Tcping GO
一个使用go写的小工具用于检测TCP端口是否开放 一个使用go写的小工具用于检测TCP端口是否开放
## 使用方法 ## 使用方法
```shell 你可以简单的测试一个地址的某个端口是否开放可以使用域名或者IP地址默认测试该地址的80端口
tcping -h
Usage of tcping: ```shell
-c, --count int 测试次数默认为4次 (default 4) tcping www.baidu.com
-h, --help 显示帮助信息 使用 www.qq.com 的 A 记录: 175.27.8.138
-t, --infinite 无限次测试 [1] 来自 175.27.8.138:80 的响应: 时间=29.256ms
-6, --ipv6 使用 IPv6需搭配域名使用 [2] 来自 175.27.8.138:80 的响应: 时间=29.086ms
-p, --port int 端口默认为80 (default 80) [3] 来自 175.27.8.138:80 的响应: 时间=37.761ms
-s, --timeout duration 超时时间默认为2秒 (default 2s) [4] 来自 175.27.8.138:80 的响应: 时间=30.259ms
测试完成,成功次数: 4/4
``` ```
使用 `-p` 指定端口号,如下
```shell
tcping www.qq.com -p 443
使用 www.qq.com 的 A 记录: 175.27.8.138
[1] 来自 175.27.8.138:443 的响应: 时间=28.835ms
[2] 来自 175.27.8.138:443 的响应: 时间=27.419ms
[3] 来自 175.27.8.138:443 的响应: 时间=44.382ms
[4] 来自 175.27.8.138:443 的响应: 时间=28.356ms
测试完成,成功次数: 4/4
```
使用 `-6` 指定使用IPv6仅对域名有效
```shell
tcping www.qq.com -6
使用 www.qq.com 的 AAAA 记录: 240e:97c:2f:1::5c
[1] 来自 [240e:97c:2f:1::5c]:80 的响应: 时间=30.795ms
[2] 来自 [240e:97c:2f:1::5c]:80 的响应: 时间=30.247ms
[3] 来自 [240e:97c:2f:1::5c]:80 的响应: 时间=30.856ms
[4] 来自 [240e:97c:2f:1::5c]:80 的响应: 时间=30.275ms
测试完成,成功次数: 4/4
```
使用 `-c` 指定测试次数默认为4次
使用 `-t` 指定超时时间默认为2秒
使用 `-f` 来启用快速模式,降低两次成功测试之间的间隔时间

View File

@@ -21,6 +21,7 @@ func main() {
fast bool fast bool
successCount int successCount int
successDelay time.Duration
attemptCount int attemptCount int
stopped bool stopped bool
) )
@@ -109,6 +110,7 @@ func main() {
fmt.Printf("测试到 %s 的连接失败: %s\n", address, "连接超时") fmt.Printf("测试到 %s 的连接失败: %s\n", address, "连接超时")
} else { } else {
successCount++ successCount++
successDelay += duration
fmt.Printf("来自 %s 的响应: 时间=%s\n", address, fmt.Sprintf("%.3fms", float64(duration)/float64(time.Millisecond))) fmt.Printf("来自 %s 的响应: 时间=%s\n", address, fmt.Sprintf("%.3fms", float64(duration)/float64(time.Millisecond)))
conn.Close() conn.Close()
} }
@@ -137,7 +139,11 @@ func main() {
if !stopped { if !stopped {
fmt.Println() fmt.Println()
} }
fmt.Printf("测试完成,成功次数: %d/%d\n", successCount, attemptCount) avgDelay := 0.0
if successCount > 0 {
avgDelay = float64(successDelay) / float64(time.Duration(successCount * int(time.Millisecond)))
}
fmt.Printf("测试完成,成功次数: %d/%d平均延迟%.3fms\n", successCount, attemptCount, avgDelay)
} }
func filterIP(ips []net.IP, ipv6 bool) (string, error) { func filterIP(ips []net.IP, ipv6 bool) (string, error) {