diff --git a/.gitignore b/.gitignore index 3b735ec..82c4ca1 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,5 @@ # Go workspace file go.work + +.idea \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..7d3a40a --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# Health Check + +### Run +```bash +go run main.go --domain github.com --port 80 +``` + ++ --domain: 目标 (必须) ++ --port: 端口 \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..487a80a --- /dev/null +++ b/go.mod @@ -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 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..5b37c41 --- /dev/null +++ b/go.sum @@ -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= diff --git a/internal/check.go b/internal/check.go new file mode 100644 index 0000000..eb999c5 --- /dev/null +++ b/internal/check.go @@ -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 +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..03eaea9 --- /dev/null +++ b/main.go @@ -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) + } +}