Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add health check option #101

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/runner/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ type Options struct {
Silent bool
Verbose bool
Debug bool
HealthCheck bool
}
7 changes: 7 additions & 0 deletions pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ func ParseOptions() *Options {
flagset.BoolVar(&options.Silent, "silent", false, "Silent"),
flagset.BoolVar(&options.Verbose, "verbose", false, "Verbose"),
flagset.BoolVar(&options.Debug, "debug", false, "Debug"),
flagset.BoolVarP(&options.HealthCheck, "health-check", "hc", false, "run diagnostic check up"),
)

if err := flagset.Parse(); err != nil {
Expand All @@ -160,6 +161,12 @@ func ParseOptions() *Options {
os.Exit(0)
}

if options.HealthCheck {
showBanner()
gologger.Print().Msgf("%s\n", DoHealthCheck(options))
os.Exit(0)
}

// api key hierarchy: cli flag > env var > .pdcp/credential file
if options.PdcpAuth == "true" {
AuthWithPDCP()
Expand Down
34 changes: 34 additions & 0 deletions pkg/runner/util.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,48 @@
package runner

import (
"fmt"
"net"
"os"
"os/exec"
"runtime"
"strings"

"github.com/projectdiscovery/cvemap/pkg/types"
fileutil "github.com/projectdiscovery/utils/file"
)

func DoHealthCheck(opts Options) string {
var test strings.Builder
test.WriteString(fmt.Sprintf("Version: %s\n", Version))
test.WriteString(fmt.Sprintf("Operating System: %s\n", runtime.GOOS))
test.WriteString(fmt.Sprintf("Architecture: %s\n", runtime.GOARCH))
test.WriteString(fmt.Sprintf("Go Version: %s\n", runtime.Version()))
test.WriteString(fmt.Sprintf("Compiler: %s\n", runtime.Compiler))

var testResult string
c4, err := net.Dial("tcp4", "cve.projectdiscovery.io:443")
if err == nil && c4 != nil {
c4.Close()
}
testResult = "Ok"
if err != nil {
testResult = fmt.Sprintf("Ko (%s)", err)
}
test.WriteString(fmt.Sprintf("IPv4 connectivity to cve.projectdiscovery.io:443 => %s\n", testResult))

u4, err := net.Dial("udp4", "cve.projectdiscovery.io:53")
if err == nil && u4 != nil {
u4.Close()
}
testResult = "Ok"
if err != nil {
testResult = fmt.Sprintf("Ko (%s)", err)
}
test.WriteString(fmt.Sprintf("IPv4 UDP connectivity to cve.projectdiscovery.io:53 => %s\n", testResult))
return test.String()
}

func getLatestVersionCVSSScore(cvss types.CvssMetrics) float64 {
var highestScore float64
if cvss.Cvss2 != nil {
Expand Down
Loading