Skip to content

Commit

Permalink
add main.gp
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangxiang committed Mar 20, 2019
0 parents commit 4e89683
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Gaping --- A remote host port stability test tool inspired by paping
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module zlx/zlxGO/study/gaping

go 1.12

require (
github.com/labstack/gommon v0.2.8
github.com/mattn/go-colorable v0.1.1 // indirect
github.com/mattn/go-isatty v0.0.7 // indirect
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
github.com/labstack/gommon v0.2.8 h1:JvRqmeZcfrHC5u6uVleB4NxxNbzx6gpbJiQknDbKQu0=
github.com/labstack/gommon v0.2.8/go.mod h1:/tj9csK2iPSBvn+3NLM9e52usepMtrd5ilFYA+wQNJ4=
github.com/mattn/go-colorable v0.1.1 h1:G1f5SKeVxmagw/IyvzvtZE4Gybcc4Tr1tf7I8z0XgOg=
github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ=
github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.7 h1:UvyT9uN+3r7yLEYSlJsbQGdsaB/a0DlgWP3pql6iwOc=
github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
91 changes: 91 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package main

import (
"fmt"
"github.com/labstack/gommon/color"
"net"
"time"
"flag"
"os"
)
var (
help bool
v,V bool
h string
p string
c int
)

func init() {
flag.BoolVar(&help,"help",false,"Show gaping help")
flag.BoolVar(&v,"v",false,"Show Version and exit")
flag.BoolVar(&V,"V",false,"Show Version and exit")
flag.StringVar(&h,"h","","Dest host ipaddress")
flag.StringVar(&p,"p","","Dest host port")
flag.IntVar(&c,"c",100000000,"Tcp test conuts")
}

// version
func Version() {
if v || V {
fmt.Println("gaping v0.01")
os.Exit(3)
}
}

// tcp 端口连通性测试
func TcpConnect(address string) (err error, costTime time.Duration) {
nets := new(net.Dialer)
nets.Timeout = time.Millisecond * 3000
startTime := time.Now()
conn, err := nets.Dial("tcp", address)
if err != nil {
return err, 1000000
}
conn.Write([]byte("hello I'm test!"))
times := time.Since(startTime).Round(time.Microsecond)
defer conn.Close()
return nil, times
}


// help
func Help() {
fmt.Println(`Gaping v0.01- Copyright (c) 2019 Mike chulinx
Example: pping -h 127.0.0.1 -p 80`)
flag.PrintDefaults()
os.Exit(2)
}

// usage
func Usages() {
if help {
Help()
}
}

// 格式化打印输出
func PingPrint(address, port string) {
date := time.Now().Format("2006-01-02 15:04:05")
err, t := TcpConnect(address)
if err != nil {
fmt.Printf("%s %s", date, color.Red("Connection timed out\n"))
} else {
fmt.Printf("%s Connected to %s: time=%s protocol=%s port=%s\n",
date, color.Green(address), color.Green(t), color.Green("TCP"), color.Green(port))
}
time.Sleep(1000 * time.Millisecond)
}

func main() {
flag.Parse()
address := fmt.Sprintf("%s:%s", h, p)
Version()
Usages()
if h == "" || p == "" {
Help()
}
for i := 0;i < c; i++ {
PingPrint(address, p)
}
}

0 comments on commit 4e89683

Please sign in to comment.