first commit

This commit is contained in:
Kenneth 2023-11-22 14:48:29 +08:00
parent faa44d2c74
commit e78f59fbf0
6 changed files with 99 additions and 0 deletions

2
.gitignore vendored
View File

@ -19,3 +19,5 @@
# Go workspace file
go.work
.idea

9
README.md Normal file
View File

@ -0,0 +1,9 @@
# Health Check
### Run
```bash
go run main.go --domain github.com --port 80
```
+ --domain: 目标 (必须)
+ --port: 端口

11
go.mod Normal file
View File

@ -0,0 +1,11 @@
module github.com/zhang2092/health-check
go 1.21.0
require github.com/urfave/cli/v2 v2.25.7
require (
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
)

8
go.sum Normal file
View File

@ -0,0 +1,8 @@
github.com/cpuguy83/go-md2man/v2 v2.0.3 h1:qMCsGGgs+MAzDFyp9LpAe1Lqy/fY/qCovCm0qnXZOBM=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs=
github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=

24
internal/check.go Normal file
View File

@ -0,0 +1,24 @@
package internal
import (
"fmt"
"net"
"time"
)
func Check(destination, port string) string {
address := destination + ":" + port
timeout := 5 * time.Second
var status string
conn, err := net.DialTimeout("tcp", address, timeout)
if err != nil {
status = fmt.Sprintf("[下线] %s 无法访问 %v", destination, err)
return status
}
defer func(conn net.Conn) {
_ = conn.Close()
}(conn)
status = fmt.Sprintf("[上线] %s 访问正常\n从: %s\n到: %s", destination, conn.LocalAddr(), conn.RemoteAddr())
return status
}

45
main.go Normal file
View File

@ -0,0 +1,45 @@
package main
import (
"fmt"
"log"
"os"
"github.com/urfave/cli/v2"
"github.com/zhang2092/health-check/internal"
)
func main() {
app := cli.App{
Name: "健康检查",
Usage: "健康检查小工具",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "domain",
Aliases: []string{"d"},
Usage: "目标",
Required: true,
},
&cli.StringFlag{
Name: "port",
Aliases: []string{"p"},
Usage: "端口",
Required: false,
},
},
Action: func(ctx *cli.Context) error {
port := ctx.String("port")
if len(port) == 0 {
port = "80"
}
status := internal.Check(ctx.String("domain"), port)
fmt.Println(status)
return nil
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}