Skip to content

Commit

Permalink
Merge pull request #1116 from sundowndev/feat/remotelibapi
Browse files Browse the repository at this point in the history
Add scanners cli command
  • Loading branch information
sundowndev committed Sep 5, 2022
2 parents 2dd7657 + a883d10 commit f336b2c
Show file tree
Hide file tree
Showing 17 changed files with 146 additions and 8 deletions.
49 changes: 49 additions & 0 deletions cmd/scanners.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package cmd

import (
"fmt"
"github.com/spf13/cobra"
"github.com/sundowndev/phoneinfoga/v2/lib/filter"
"github.com/sundowndev/phoneinfoga/v2/lib/remote"
)

type ScannersCmdOptions struct {
Plugin []string
}

func init() {
opts := &ScannersCmdOptions{}
scannersCmd := NewScannersCmd(opts)

fl := scannersCmd.Flags()
fl.StringSliceVar(&opts.Plugin, "plugin", []string{}, "Output file")

rootCmd.AddCommand(scannersCmd)
}

func NewScannersCmd(opts *ScannersCmdOptions) *cobra.Command {
cmd := &cobra.Command{
Use: "scanners",
Example: "phoneinfoga scanners",
Short: "Display list of loaded scanners",
Run: func(cmd *cobra.Command, args []string) {
for _, p := range opts.Plugin {
err := remote.OpenPlugin(p)
if err != nil {
exitWithError(err)
}
}

remoteLibrary := remote.NewLibrary(filter.NewEngine())
remote.InitScanners(remoteLibrary)

for i, s := range remoteLibrary.GetAllScanners() {
fmt.Printf("%s\n%s\n", s.Name(), s.Description())
if i < len(remoteLibrary.GetAllScanners()) {
fmt.Printf("\n")
}
}
},
}
return cmd
}
5 changes: 5 additions & 0 deletions examples/plugin/customscanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ func (s *customScanner) Name() string {
return "customscanner"
}

// Description returns a short description for this scanner.
func (s *customScanner) Description() string {
return "This is a dummy scanner"
}

// ShouldRun returns a boolean indicating whether
// this scanner should be used or not.
// This can be useful to check for authentication or
Expand Down
6 changes: 6 additions & 0 deletions examples/plugin/customscanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import (
"testing"
)

func TestCustomScanner_Metadata(t *testing.T) {
scanner := &customScanner{}
assert.Equal(t, "customscanner", scanner.Name())
assert.NotEmpty(t, scanner.Description())
}

func TestCustomScanner(t *testing.T) {
testcases := []struct {
name string
Expand Down
4 changes: 4 additions & 0 deletions lib/remote/googlecse_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ func (s *googleCSEScanner) Name() string {
return GoogleCSE
}

func (s *googleCSEScanner) Description() string {
return "Googlecse searches for footprints of a given phone number on the web using Google Custom Search Engine."
}

func (s *googleCSEScanner) ShouldRun(_ number.Number) bool {
if s.Cx == "" || s.ApiKey == "" {
return false
Expand Down
9 changes: 6 additions & 3 deletions lib/remote/googlecse_scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ import (
"testing"
)

func TestGoogleCSEScanner_Metadata(t *testing.T) {
scanner := NewGoogleCSEScanner(&http.Client{})
assert.Equal(t, GoogleCSE, scanner.Name())
assert.NotEmpty(t, scanner.Description())
}

func TestGoogleCSEScanner_Scan_Success(t *testing.T) {
testcases := []struct {
name string
Expand Down Expand Up @@ -249,9 +255,6 @@ func TestGoogleCSEScanner_Scan_Success(t *testing.T) {

func TestGoogleCSEScanner_ShouldRun(t *testing.T) {
scanner := NewGoogleCSEScanner(&http.Client{})
remote := NewLibrary(filter.NewEngine())
remote.AddScanner(scanner)

assert.False(t, scanner.ShouldRun(*test.NewFakeUSNumber()))
}

Expand Down
6 changes: 5 additions & 1 deletion lib/remote/googlesearch_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,18 @@ type GoogleSearchResponse struct {
General []*GoogleSearchDork `json:"general" console:"General,omitempty"`
}

func NewGoogleSearchScanner() *googlesearchScanner {
func NewGoogleSearchScanner() Scanner {
return &googlesearchScanner{}
}

func (s *googlesearchScanner) Name() string {
return Googlesearch
}

func (s *googlesearchScanner) Description() string {
return "Generate several Google dork requests for a given phone number."
}

func (s *googlesearchScanner) ShouldRun(_ number.Number) bool {
return true
}
Expand Down
6 changes: 6 additions & 0 deletions lib/remote/googlesearch_scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import (
"testing"
)

func TestGoogleSearchScanner_Metadata(t *testing.T) {
scanner := NewGoogleSearchScanner()
assert.Equal(t, Googlesearch, scanner.Name())
assert.NotEmpty(t, scanner.Description())
}

func TestGoogleSearchScanner(t *testing.T) {
testcases := []struct {
name string
Expand Down
6 changes: 5 additions & 1 deletion lib/remote/local_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@ type LocalScannerResponse struct {
Carrier string `json:"carrier,omitempty" console:"Carrier,omitempty"`
}

func NewLocalScanner() *localScanner {
func NewLocalScanner() Scanner {
return &localScanner{}
}

func (s *localScanner) Name() string {
return Local
}

func (s *localScanner) Description() string {
return "Gather offline info about a given phone number."
}

func (s *localScanner) ShouldRun(_ number.Number) bool {
return true
}
Expand Down
6 changes: 6 additions & 0 deletions lib/remote/local_scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import (
"testing"
)

func TestLocalScanner_Metadata(t *testing.T) {
scanner := NewLocalScanner()
assert.Equal(t, Local, scanner.Name())
assert.NotEmpty(t, scanner.Description())
}

func TestLocalScanner(t *testing.T) {
testcases := []struct {
name string
Expand Down
6 changes: 5 additions & 1 deletion lib/remote/numverify_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,18 @@ type NumverifyScannerResponse struct {
LineType string `json:"line_type" console:"Line type"`
}

func NewNumverifyScanner(s suppliers.NumverifySupplierInterface) *numverifyScanner {
func NewNumverifyScanner(s suppliers.NumverifySupplierInterface) Scanner {
return &numverifyScanner{client: s}
}

func (s *numverifyScanner) Name() string {
return Numverify
}

func (s *numverifyScanner) Description() string {
return "Request info about a given phone number through the Numverify API."
}

func (s *numverifyScanner) ShouldRun(_ number.Number) bool {
return s.client.IsAvailable()
}
Expand Down
6 changes: 6 additions & 0 deletions lib/remote/numverify_scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import (
"testing"
)

func TestNumverifyScanner_Metadata(t *testing.T) {
scanner := NewNumverifyScanner(&mocks.NumverifySupplier{})
assert.Equal(t, Numverify, scanner.Name())
assert.NotEmpty(t, scanner.Description())
}

func TestNumverifyScanner(t *testing.T) {
dummyError := errors.New("dummy")

Expand Down
6 changes: 5 additions & 1 deletion lib/remote/ovh_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ type OVHScannerResponse struct {
ZipCode string `json:"zip_code,omitempty" console:"Zip code,omitempty"`
}

func NewOVHScanner(s suppliers.OVHSupplierInterface) *ovhScanner {
func NewOVHScanner(s suppliers.OVHSupplierInterface) Scanner {
return &ovhScanner{client: s}
}

func (s *ovhScanner) Name() string {
return OVH
}

func (s *ovhScanner) Description() string {
return "Search a phone number through the OVH Telecom REST API."
}

func (s *ovhScanner) ShouldRun(n number.Number) bool {
return s.isSupported(n.CountryCode)
}
Expand Down
6 changes: 6 additions & 0 deletions lib/remote/ovh_scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import (
"testing"
)

func TestOVHScanner_Metadata(t *testing.T) {
scanner := NewOVHScanner(&mocks.OVHSupplier{})
assert.Equal(t, OVH, scanner.Name())
assert.NotEmpty(t, scanner.Description())
}

func TestOVHScanner(t *testing.T) {
dummyError := errors.New("dummy")

Expand Down
4 changes: 4 additions & 0 deletions lib/remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ func (r *Library) Scan(n *number.Number) (map[string]interface{}, map[string]err
return r.results, r.errors
}

func (r *Library) GetAllScanners() []Scanner {
return r.scanners
}

func RegisterPlugin(s Scanner) {
mu.Lock()
defer mu.Unlock()
Expand Down
12 changes: 12 additions & 0 deletions lib/remote/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,15 @@ func TestRemoteLibraryEmptyScan(t *testing.T) {

fakeScanner.AssertExpectations(t)
}

func TestRemoteLibrary_GetAllScanners(t *testing.T) {
fakeScanner := &mocks.Scanner{}
fakeScanner2 := &mocks.Scanner{}

lib := NewLibrary(filter.NewEngine())

lib.AddScanner(fakeScanner)
lib.AddScanner(fakeScanner2)

assert.Equal(t, []Scanner{fakeScanner, fakeScanner2}, lib.GetAllScanners())
}
1 change: 1 addition & 0 deletions lib/remote/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Plugin interface {

type Scanner interface {
Name() string
Description() string
Scan(number.Number) (interface{}, error)
ShouldRun(number.Number) bool
}
Expand Down
16 changes: 15 additions & 1 deletion mocks/Scanner.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f336b2c

Please sign in to comment.