master
sre 4 years ago
parent 6f11316233
commit 5f636b60b4

@ -2,6 +2,7 @@ package dial
import ( import (
"fmt" "fmt"
"github.com/fatih/color"
"net" "net"
"time" "time"
) )
@ -14,13 +15,15 @@ func PortDial(address, port, protocol string, timeout int) {
} }
fmt.Printf("%v ping %v (%v) via port %v (timeout: %vs)...\n", protocol, address, ip, port, timeout) fmt.Printf("%v ping %v (%v) via port %v (timeout: %vs)...\n", protocol, address, ip, port, timeout)
dialTarget := ip + ":" + port dialTarget := ip + ":" + port
red := color.New(color.FgRed).SprintFunc()
green := color.New(color.FgGreen).SprintFunc()
for { for {
startTime := time.Now() startTime := time.Now()
conn, err := net.DialTimeout(protocol, dialTarget, time.Duration(timeout)*time.Second) conn, err := net.DialTimeout(protocol, dialTarget, time.Duration(timeout)*time.Second)
endTime := time.Now() endTime := time.Now()
elapsedTime := float64(endTime.Sub(startTime)) / float64(time.Millisecond) elapsedTime := float64(endTime.Sub(startTime)) / float64(time.Millisecond)
if err != nil { if err != nil {
fmt.Printf("TIMEOUT via %v:%v time=%v ms\n", ip, port, elapsedTime) fmt.Printf("%s via %v:%v time=%v ms\n", red("TIMEOUT"), ip, port, elapsedTime)
time.Sleep(time.Duration(timeout) * time.Second) time.Sleep(time.Duration(timeout) * time.Second)
} else { } else {
@ -28,7 +31,7 @@ func PortDial(address, port, protocol string, timeout int) {
if err != nil { if err != nil {
return return
} }
fmt.Printf("OK via %v:%v time=%v ms\n", ip, port, elapsedTime) fmt.Printf("%s via %v:%v time=%v ms\n", green("OK"), ip, port, elapsedTime)
time.Sleep(time.Duration(1) * time.Second) time.Sleep(time.Duration(1) * time.Second)
continue continue
} }

@ -1,3 +1,10 @@
module pong module pong
go 1.19 go 1.19
require (
github.com/fatih/color v1.13.0 // indirect
github.com/mattn/go-colorable v0.1.9 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
)

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"github.com/fatih/color"
"net" "net"
"os" "os"
"os/signal" "os/signal"
@ -162,8 +163,9 @@ func Ping(domain string, PS int) {
} }
func foot(address string, icmp_seq int, dropPack, min_lan, avg_lan, max_lan float64, ret_list []float64) { func foot(address string, icmp_seq int, dropPack, min_lan, avg_lan, max_lan float64, ret_list []float64) {
red := color.New(color.FgRed).SprintFunc()
fmt.Printf("\n--- %s ping statistics ---\n", address) fmt.Printf("\n--- %s ping statistics ---\n", address)
fmt.Printf("%d packets transmitted, %f lost, %.2f%% packet loss, time 1000ms\n", icmp_seq, dropPack, dropPack/float64(icmp_seq)*100) fmt.Printf("%d packets transmitted, %f lost, %.2f%% packet loss, time 1000ms\n", icmp_seq, red(dropPack), dropPack/float64(icmp_seq)*100)
if len(ret_list) == 0 { if len(ret_list) == 0 {
avg_lan = 3000.0 avg_lan = 3000.0
} else { } else {

@ -2,12 +2,30 @@ package goping
import ( import (
"fmt" "fmt"
"github.com/fatih/color"
"os" "os"
"os/signal" "os/signal"
"testing" "testing"
"time" "time"
) )
func TestColor(tt *testing.T) {
// Create SprintXxx functions to mix strings with other non-colorized strings:
yellow := color.New(color.FgYellow).SprintFunc()
red := color.New(color.FgRed).SprintFunc()
fmt.Printf("This is a %s and this is %s.\n", yellow("warning"), red("error"))
info := color.New(color.FgWhite, color.BgGreen).SprintFunc()
fmt.Printf("This %s rocks!\n", info("package"))
// Use helper functions
fmt.Println("This", color.RedString("warning"), "should be not neglected.")
fmt.Printf("%v %v\n", color.GreenString("Info:"), "an important message.")
// Windows supported too! Just don't forget to change the output to color.Output
fmt.Fprintf(color.Output, "Windows support: %s", color.GreenString("PASS"))
}
func TestSign(tt *testing.T) { func TestSign(tt *testing.T) {
c := make(chan os.Signal, 1) c := make(chan os.Signal, 1)
for { for {

@ -4,11 +4,9 @@ import (
"flag" "flag"
"fmt" "fmt"
"os" "os"
"os/signal"
"pong/dial" "pong/dial"
"pong/goping" "pong/goping"
"strconv" "strconv"
"time"
) )
// 定义命令行参数对应的变量,这三个变量都是指针类型 // 定义命令行参数对应的变量,这三个变量都是指针类型
@ -22,22 +20,6 @@ pong www.baidu.com 443 -t 1 -k udp
pong -a www.baidu.com -p 443 -k tcp -t 3 pong -a www.baidu.com -p 443 -k tcp -t 3
` `
func main2() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill)
for {
select {
case s := <-c:
fmt.Println("ctc")
fmt.Println("Got signal:", s)
os.Exit(0)
default:
fmt.Println("okok")
time.Sleep(time.Second * 1)
}
}
}
func main() { func main() {
address := flag.String("a", "", "domain or ip address") address := flag.String("a", "", "domain or ip address")
port := flag.Int("p", 0, "port") port := flag.Int("p", 0, "port")

@ -7,6 +7,7 @@ pong -a www.baidu.com -p 443 -k tcp -t 3
## build ## build
```bash
cd /root cd /root
rm -rf pong rm -rf pong
git clone https://git.sre.ink/go/pong.git git clone https://git.sre.ink/go/pong.git
@ -14,4 +15,5 @@ cd pong
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on GOPROXY="https://goproxy.cn,direct" go build -v -a -o pong pong.go CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on GOPROXY="https://goproxy.cn,direct" go build -v -a -o pong pong.go
rm -rf /usr/bin/pong rm -rf /usr/bin/pong
mv pong /usr/bin/ mv pong /usr/bin/
pong www.baidu.com 443 pong www.baidu.com 443
```
Loading…
Cancel
Save