Skip to content

Commit

Permalink
feat: port scan cmd's flags to serve cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
sundowndev committed Nov 5, 2022
1 parent 73a31eb commit 1c37ce6
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 27 deletions.
84 changes: 59 additions & 25 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,78 @@ package cmd
import (

This comment has been minimized.

Copy link
@Zamirawad

Zamirawad Jan 24, 2023

073

"fmt"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/sundowndev/phoneinfoga/v2/build"
"github.com/sundowndev/phoneinfoga/v2/lib/filter"
"github.com/sundowndev/phoneinfoga/v2/lib/remote"
"github.com/sundowndev/phoneinfoga/v2/web"
"github.com/sundowndev/phoneinfoga/v2/web/v2/api/handlers"
"log"
"net/http"
"os"
)

var httpPort int
var disableClient bool
type ServeCmdOptions struct {
HttpPort int
DisableClient bool
DisabledScanners []string
PluginPaths []string
EnvFiles []string
}

func init() {
// Register command
rootCmd.AddCommand(serveCmd)
opts := &ServeCmdOptions{}
cmd := NewServeCmd(opts)
rootCmd.AddCommand(cmd)

// Register flags
serveCmd.PersistentFlags().IntVarP(&httpPort, "port", "p", 5000, "HTTP port")
serveCmd.PersistentFlags().BoolVar(&disableClient, "no-client", false, "Disable web client (REST API only)")
cmd.PersistentFlags().IntVarP(&opts.HttpPort, "port", "p", 5000, "HTTP port")
cmd.PersistentFlags().BoolVar(&opts.DisableClient, "no-client", false, "Disable web client (REST API only)")
cmd.PersistentFlags().StringArrayVarP(&opts.DisabledScanners, "disable", "D", []string{}, "Scanner to skip for the scans")
cmd.PersistentFlags().StringArrayVar(&opts.PluginPaths, "plugin", []string{}, "Extra scanner plugin to use for the scans")
cmd.PersistentFlags().StringSliceVar(&opts.EnvFiles, "env-file", []string{}, "Env files to parse environment variables from (looks for .env by default)")
}

var serveCmd = &cobra.Command{
Use: "serve",
Short: "Serve web client",
Run: func(cmd *cobra.Command, args []string) {
if build.IsRelease() && os.Getenv("GIN_MODE") == "" {
gin.SetMode(gin.ReleaseMode)
}

srv, err := web.NewServer(disableClient)
if err != nil {
log.Fatal(err)
}

addr := fmt.Sprintf(":%d", httpPort)

fmt.Printf("Listening on %s\n", addr)
if err := srv.ListenAndServe(addr); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
},
func NewServeCmd(opts *ServeCmdOptions) *cobra.Command {
return &cobra.Command{
Use: "serve",
Short: "Serve web client",
PreRun: func(cmd *cobra.Command, args []string) {
err := godotenv.Load(opts.EnvFiles...)
if err != nil {
logrus.WithField("error", err).Debug("Error loading .env file")
}

for _, p := range opts.PluginPaths {
err := remote.OpenPlugin(p)
if err != nil {
exitWithError(err)
}
}

// Initialize remote library
f := filter.NewEngine()
f.AddRule(opts.DisabledScanners...)
handlers.Init(f)
},
Run: func(cmd *cobra.Command, args []string) {
if build.IsRelease() && os.Getenv("GIN_MODE") == "" {
gin.SetMode(gin.ReleaseMode)
}

srv, err := web.NewServer(opts.DisableClient)
if err != nil {
log.Fatal(err)
}

addr := fmt.Sprintf(":%d", opts.HttpPort)
fmt.Printf("Listening on %s\n", addr)
if err := srv.ListenAndServe(addr); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
},
}
}
4 changes: 2 additions & 2 deletions web/v2/api/handlers/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
var once sync.Once
var RemoteLibrary *remote.Library

func init() {
func Init(filterEngine filter.Filter) {
once.Do(func() {
RemoteLibrary = remote.NewLibrary(filter.NewEngine())
RemoteLibrary = remote.NewLibrary(filterEngine)
remote.InitScanners(RemoteLibrary)
logrus.Debug("Scanners and plugins initialized")
})
Expand Down

0 comments on commit 1c37ce6

Please sign in to comment.