Skip to content

Commit

Permalink
Merge pull request #1154 from sundowndev/feat/env-file
Browse files Browse the repository at this point in the history
Add env file support
  • Loading branch information
sundowndev committed Oct 29, 2022
2 parents 816baca + 58ad739 commit ad8fd22
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 24 deletions.
56 changes: 35 additions & 21 deletions cmd/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"github.com/fatih/color"
"github.com/joho/godotenv"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/sundowndev/phoneinfoga/v2/lib/filter"
Expand All @@ -13,56 +14,69 @@ import (
"os"
)

var inputNumber string
var disabledScanners []string
var pluginPaths []string
type ScanCmdOptions struct {
Number string
DisabledScanners []string
PluginPaths []string
EnvFiles []string
}

func init() {
// Register command
rootCmd.AddCommand(scanCmd)
opts := &ScanCmdOptions{}
cmd := NewScanCmd(opts)
rootCmd.AddCommand(cmd)

// Register flags
scanCmd.PersistentFlags().StringVarP(&inputNumber, "number", "n", "", "The phone number to scan (E164 or international format)")
scanCmd.PersistentFlags().StringArrayVarP(&disabledScanners, "disable", "D", []string{}, "A list of scanners to skip for this scan.")
scanCmd.PersistentFlags().StringSliceVar(&pluginPaths, "plugin", []string{}, "Extra scanner plugin to use for the scan")
cmd.PersistentFlags().StringVarP(&opts.Number, "number", "n", "", "The phone number to scan (E164 or international format)")
cmd.PersistentFlags().StringArrayVarP(&opts.DisabledScanners, "disable", "D", []string{}, "A list of scanners to skip for this scan.")
cmd.PersistentFlags().StringSliceVar(&opts.PluginPaths, "plugin", []string{}, "Extra scanner plugin to use for the scan")
cmd.PersistentFlags().StringSliceVar(&opts.EnvFiles, "env-file", []string{}, "Env files to parse environment variables from (looks for .env by default)")
// scanCmd.PersistentFlags().StringVarP(&input, "input", "i", "", "Text file containing a list of phone numbers to scan (one per line)")
// scanCmd.PersistentFlags().StringVarP(&output, "output", "o", "", "Output to save scan results")
}

var scanCmd = &cobra.Command{
Use: "scan",
Short: "Scan a phone number",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
runScan()
},
func NewScanCmd(opts *ScanCmdOptions) *cobra.Command {
return &cobra.Command{
Use: "scan",
Short: "Scan a phone number",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
err := godotenv.Load(opts.EnvFiles...)
if err != nil {
logrus.WithField("error", err).Debug("Error loading .env file")
}

runScan(opts)
},
}
}

func runScan() {
fmt.Printf(color.WhiteString("Running scan for phone number %s...\n\n"), inputNumber)
func runScan(opts *ScanCmdOptions) {
fmt.Printf(color.WhiteString("Running scan for phone number %s...\n\n"), opts.Number)

if valid := number.IsValid(inputNumber); !valid {
if valid := number.IsValid(opts.Number); !valid {
logrus.WithFields(map[string]interface{}{
"input": inputNumber,
"input": opts.Number,
"valid": valid,
}).Debug("Input phone number is invalid")
exitWithError(errors.New("given phone number is not valid"))
}

num, err := number.NewNumber(inputNumber)
num, err := number.NewNumber(opts.Number)
if err != nil {
exitWithError(err)
}

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

f := filter.NewEngine()
f.AddRule(disabledScanners...)
f.AddRule(opts.DisabledScanners...)

remoteLibrary := remote.NewLibrary(f)
remote.InitScanners(remoteLibrary)
Expand Down
2 changes: 1 addition & 1 deletion docs/contribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ We use [mkdocs](https://www.mkdocs.org/) to generate our documentation website.
### Install mkdocs

```shell
python3 -m pip install mkdocs==1.3.0 mkdocs-material==8.3.9 mkdocs-minify-plugin==0.5.0
python3 -m pip install mkdocs==1.3.0 mkdocs-material==8.3.9 mkdocs-minify-plugin==0.5.0 mkdocs-redirects==1.1.0
```

### Serve documentation on localhost
Expand Down
21 changes: 19 additions & 2 deletions docs/getting-started/scanners.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
# Scanners

PhoneInfoga provide several scanners to extract as much information as possible from a given phone number. Those scanners may require authentication, so they're automatically skipped when no authentication credentials are found. Note that all scanners use environment variables for configuration values.
PhoneInfoga provide several scanners to extract as much information as possible from a given phone number. Those scanners may require authentication, so they're automatically skipped when no authentication credentials are found.

## Configuration

Note that all scanners use environment variables for configuration values. You can define an environment variable inline or put them in a file called `.env` in the current directory. The tool will parse it automatically. To specify another filename, use the flag `--env-file`.

**Example**

```shell
# .env.local
NUMVERIFY_API_KEY="value"
GOOGLECSE_CX="value"
GOOGLE_API_KEY="value"
```

```shell
phoneinfoga scan -n +4176418xxxx --env-file=.env.local
```

## Building your own scanner

Expand All @@ -17,7 +34,7 @@ $ phoneinfoga scan -n +4176418xxxx --plugin ./custom_scanner.so

## Local

The local scan is probably the simplest scan of PhoneInfoga. By default, the tool statically parse the phone number and convert it to several formats, it also tries to recognize the country and the carrier. Those information are passed to all scanners in order to provide further analysis. The local scanner simply return those information to the end user so they can exploit it as well.
The local scan is probably the simplest scan of PhoneInfoga. By default, the tool statically parse the phone number and convert it to several formats, it also tries to recognize the country and the carrier. This information are passed to all scanners in order to provide further analysis. The local scanner simply return those information to the end user, so they can exploit it as well.

??? info "Configuration"

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
github.com/fatih/color v1.13.0
github.com/gin-gonic/gin v1.7.0
github.com/joho/godotenv v1.4.0
github.com/nyaruka/phonenumbers v1.1.0
github.com/onlinecity/go-phone-iso3166 v0.0.1
github.com/sirupsen/logrus v1.8.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
Expand Down

0 comments on commit ad8fd22

Please sign in to comment.