Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sundowndev
GitHub Repository: sundowndev/phoneinfoga
Path: blob/master/cmd/serve.go
988 views
1
package cmd
2
3
import (
4
"fmt"
5
"github.com/gin-gonic/gin"
6
"github.com/joho/godotenv"
7
"github.com/sirupsen/logrus"
8
"github.com/spf13/cobra"
9
"github.com/sundowndev/phoneinfoga/v2/build"
10
"github.com/sundowndev/phoneinfoga/v2/lib/filter"
11
"github.com/sundowndev/phoneinfoga/v2/lib/remote"
12
"github.com/sundowndev/phoneinfoga/v2/web"
13
"github.com/sundowndev/phoneinfoga/v2/web/v2/api/handlers"
14
"log"
15
"net/http"
16
"os"
17
)
18
19
type ServeCmdOptions struct {
20
HttpPort int
21
DisableClient bool
22
DisabledScanners []string
23
PluginPaths []string
24
EnvFiles []string
25
}
26
27
func init() {
28
// Register command
29
opts := &ServeCmdOptions{}
30
cmd := NewServeCmd(opts)
31
rootCmd.AddCommand(cmd)
32
33
// Register flags
34
cmd.PersistentFlags().IntVarP(&opts.HttpPort, "port", "p", 5000, "HTTP port")
35
cmd.PersistentFlags().BoolVar(&opts.DisableClient, "no-client", false, "Disable web client (REST API only)")
36
cmd.PersistentFlags().StringArrayVarP(&opts.DisabledScanners, "disable", "D", []string{}, "Scanner to skip for the scans")
37
cmd.PersistentFlags().StringArrayVar(&opts.PluginPaths, "plugin", []string{}, "Extra scanner plugin to use for the scans")
38
cmd.PersistentFlags().StringSliceVar(&opts.EnvFiles, "env-file", []string{}, "Env files to parse environment variables from (looks for .env by default)")
39
}
40
41
func NewServeCmd(opts *ServeCmdOptions) *cobra.Command {
42
return &cobra.Command{
43
Use: "serve",
44
Short: "Serve web client",
45
PreRun: func(cmd *cobra.Command, args []string) {
46
err := godotenv.Load(opts.EnvFiles...)
47
if err != nil {
48
logrus.WithField("error", err).Debug("Error loading .env file")
49
}
50
51
for _, p := range opts.PluginPaths {
52
err := remote.OpenPlugin(p)
53
if err != nil {
54
exitWithError(err)
55
}
56
}
57
58
// Initialize remote library
59
f := filter.NewEngine()
60
f.AddRule(opts.DisabledScanners...)
61
handlers.Init(f)
62
},
63
Run: func(cmd *cobra.Command, args []string) {
64
if build.IsRelease() && os.Getenv("GIN_MODE") == "" {
65
gin.SetMode(gin.ReleaseMode)
66
}
67
68
srv, err := web.NewServer(opts.DisableClient)
69
if err != nil {
70
log.Fatal(err)
71
}
72
73
addr := fmt.Sprintf(":%d", opts.HttpPort)
74
fmt.Printf("Listening on %s\n", addr)
75
if err := srv.ListenAndServe(addr); err != nil && err != http.ErrServerClosed {
76
log.Fatalf("listen: %s\n", err)
77
}
78
},
79
}
80
}
81
82