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

Adding self-diagnostic functionality #260

Merged
merged 1 commit into from
Jan 5, 2023
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
30 changes: 20 additions & 10 deletions cmd/katana/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,25 @@ var (
)

func main() {
if err := readFlags(); err != nil {
flagSet, err := readFlags()
if err != nil {
gologger.Fatal().Msgf("Could not read flags: %s\n", err)
}

runner, err := runner.New(options)
if err != nil || runner == nil {
if options.HealthCheck {
gologger.Print().Msgf("%s\n", runner.DoHealthCheck(options, flagSet))
os.Exit(0)
}

katanaRunner, err := runner.New(options)
if err != nil || katanaRunner == nil {
if options.Version {
return
} else {
gologger.Fatal().Msgf("could not create runner: %s\n", err)
}
}
defer runner.Close()
defer katanaRunner.Close()

// close handler
go func() {
Expand All @@ -42,18 +48,18 @@ func main() {
go func() {
<-c
gologger.DefaultLogger.Info().Msg("- Ctrl+C pressed in Terminal")
runner.Close()
katanaRunner.Close()
os.Exit(0)
}()
}()

if err := runner.ExecuteCrawling(); err != nil {
if err := katanaRunner.ExecuteCrawling(); err != nil {
gologger.Fatal().Msgf("could not execute crawling: %s", err)
}

}

func readFlags() error {
func readFlags() (*goflags.FlagSet, error) {
flagSet := goflags.NewFlagSet()
flagSet.SetDescription(`Katana is a fast crawler focused on execution in automation
pipelines offering both headless and non-headless crawling.`)
Expand All @@ -77,6 +83,10 @@ pipelines offering both headless and non-headless crawling.`)
flagSet.StringVarP(&options.FormConfig, "form-config", "fc", "", "path to custom form configuration file"),
)

flagSet.CreateGroup("debug", "Debug",
flagSet.BoolVarP(&options.HealthCheck, "hc", "health-check", false, "run diagnostic check up"),
)

flagSet.CreateGroup("headless", "Headless",
flagSet.BoolVarP(&options.Headless, "headless", "hl", false, "enable headless hybrid crawling (experimental)"),
flagSet.BoolVarP(&options.UseInstalledChrome, "system-chrome", "sc", false, "use local installed chrome browser instead of katana installed"),
Expand Down Expand Up @@ -123,13 +133,13 @@ pipelines offering both headless and non-headless crawling.`)
)

if err := flagSet.Parse(); err != nil {
return errors.Wrap(err, "could not parse flags")
return nil, errors.Wrap(err, "could not parse flags")
}

if cfgFile != "" {
if err := flagSet.MergeConfigFile(cfgFile); err != nil {
return errors.Wrap(err, "could not read config file")
return nil, errors.Wrap(err, "could not read config file")
}
}
return nil
return flagSet, nil
}
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ golang.org/x/crypto v0.0.0-20201124201722-c8d3bf9c5392/go.mod h1:jdWPYTVW3xRLrWP
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.1.0 h1:MDRAIl0xIo9Io2xV565hzXHw3zVseKrJKodhohM5CjU=
golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw=
golang.org/x/exp v0.0.0-20221019170559-20944726eadf/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
golang.org/x/exp v0.0.0-20221217163422-3c43f8badb15 h1:5oN1Pz/eDhCpbMbLstvIPa0b/BEQo6g6nwV3pLjfM6w=
golang.org/x/exp v0.0.0-20221217163422-3c43f8badb15/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
Expand Down
100 changes: 100 additions & 0 deletions internal/runner/healthcheck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package runner

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

"github.com/projectdiscovery/goflags"
"github.com/projectdiscovery/katana/pkg/types"
fileutil "github.com/projectdiscovery/utils/file"
permissionutil "github.com/projectdiscovery/utils/permission"
)

func DoHealthCheck(options *types.Options, flagSet *goflags.FlagSet) string {
// RW permissions on config file
cfgFilePath, _ := flagSet.GetConfigFilePath()
var test strings.Builder
test.WriteString(fmt.Sprintf("Version: %s\n", version))
test.WriteString(fmt.Sprintf("Operative 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
if permissionutil.IsRoot {
testResult = "Ok"
} else {
testResult = "Ko"
}
test.WriteString(fmt.Sprintf("root: %s\n", testResult))

ok, err := fileutil.IsReadable(cfgFilePath)
if ok {
testResult = "Ok"
} else {
testResult = "Ko"
}
if err != nil {
testResult += fmt.Sprintf(" (%s)", err)
}
test.WriteString(fmt.Sprintf("Config file \"%s\" Read => %s\n", cfgFilePath, testResult))
ok, err = fileutil.IsWriteable(cfgFilePath)
if ok {
testResult = "Ok"
} else {
testResult = "Ko"
}
if err != nil {
testResult += fmt.Sprintf(" (%s)", err)
}
test.WriteString(fmt.Sprintf("Config file \"%s\" Write => %s\n", cfgFilePath, testResult))
c4, err := net.Dial("tcp4", "scanme.sh:80")
if err == nil && c4 != nil {
c4.Close()
}
testResult = "Ok"
if err != nil {
testResult = fmt.Sprintf("Ko (%s)", err)
}
test.WriteString(fmt.Sprintf("TCP IPv4 connectivity to scanme.sh:80 => %s\n", testResult))
c6, err := net.Dial("tcp6", "scanme.sh:80")
if err == nil && c6 != nil {
c6.Close()
}
testResult = "Ok"
if err != nil {
testResult = fmt.Sprintf("Ko (%s)", err)
}
test.WriteString(fmt.Sprintf("TCP IPv6 connectivity to scanme.sh:80 => %s\n", testResult))
u4, err := net.Dial("udp4", "scanme.sh:53")
if err == nil && c4 != nil {
u4.Close()
}
testResult = "Ok"
if err != nil {
testResult = fmt.Sprintf("Ko (%s)", err)
}
test.WriteString(fmt.Sprintf("UDP IPv4 connectivity to scanme.sh:80 => %s\n", testResult))
u6, err := net.Dial("udp6", "scanme.sh:80")
if err == nil && c6 != nil {
u6.Close()
}
testResult = "Ok"
if err != nil {
testResult = fmt.Sprintf("Ko (%s)", err)
}
test.WriteString(fmt.Sprintf("UDP IPv6 connectivity to scanme.sh:80 => %s\n", testResult))

// attempt to identify if chome is installed locally
if chromePath, err := exec.LookPath("chrome"); err == nil {
test.WriteString(fmt.Sprintf("Potential chrome binary path (linux/osx) => %s\n", chromePath))
}
if chromePath, err := exec.LookPath("chrome.exe"); err == nil {
test.WriteString(fmt.Sprintf("Potential chrome.exe binary path (windows) => %s\n", chromePath))
}

return test.String()
}
2 changes: 1 addition & 1 deletion internal/runner/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func validateOptions(options *types.Options) error {
return errors.New("headless mode (-hl) is required if -ho, -nos or -scp are set")
}
if options.SystemChromePath != "" {
if _, err := os.Stat(options.SystemChromePath); errors.Is(err, os.ErrNotExist) {
if !fileutil.FileExists(options.SystemChromePath) {
return errors.New("specified system chrome binary does not exist")
}
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/types/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ type Options struct {
StoreResponseDir string
// HeadlessNoIncognito specifies if chrome should be started without incognito mode
HeadlessNoIncognito bool
// HealthCheck determines if a self-healthcheck should be performed
HealthCheck bool
}

func (options *Options) ParseCustomHeaders() map[string]string {
Expand Down