-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1089 from sundowndev/feat/plugins
Add support for plugins
- Loading branch information
Showing
22 changed files
with
328 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Example plugin | ||
|
||
This is an example scanner plugin. | ||
|
||
## Build | ||
|
||
```shell | ||
$ go build -buildmode=plugin ./customscanner.go | ||
``` | ||
|
||
Depending on your OS, it will create a plugin file (e.g. `customscanner.so` for linux). | ||
|
||
## Usage | ||
|
||
You can now use this plugin with phoneinfoga. | ||
|
||
```shell | ||
$ phoneinfoga scan -n <number> --plugin ./customscanner.so | ||
|
||
Running scan for phone number <number>... | ||
|
||
Results for customscanner | ||
Valid: true | ||
Info: This number is known for scams! | ||
|
||
... | ||
``` | ||
|
||
The `--plugin` flag can be used multiple times to use several plugins at once. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/sundowndev/phoneinfoga/v2/lib/number" | ||
"github.com/sundowndev/phoneinfoga/v2/lib/remote" | ||
) | ||
|
||
type customScanner struct{} | ||
|
||
type customScannerResponse struct { | ||
Valid bool `json:"valid" console:"Valid"` | ||
Info string `json:"info" console:"Info"` | ||
Hidden string `json:"-" console:"-"` | ||
} | ||
|
||
// Name returns the unique name this scanner. | ||
func (s *customScanner) Name() string { | ||
return "customscanner" | ||
} | ||
|
||
// ShouldRun returns a boolean indicating whether | ||
// this scanner should be used or not. | ||
// This can be useful to check for authentication or | ||
// country code support for example, and avoid running | ||
// the scanner when it just can't work. | ||
func (s *customScanner) ShouldRun(n number.Number) bool { | ||
return true | ||
} | ||
|
||
// Scan does the actual scan of the phone number. | ||
// Note this function will be executed in a goroutine. | ||
func (s *customScanner) Scan(n number.Number) (interface{}, error) { | ||
data := customScannerResponse{ | ||
Valid: true, | ||
Info: "This number is known for scams!", | ||
Hidden: "This will not appear in the output", | ||
} | ||
return data, nil | ||
} | ||
|
||
func init() { | ||
remote.RegisterPlugin(&customScanner{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"github.com/sundowndev/phoneinfoga/v2/lib/number" | ||
"testing" | ||
) | ||
|
||
func TestCustomScanner(t *testing.T) { | ||
testcases := []struct { | ||
name string | ||
number *number.Number | ||
expected customScannerResponse | ||
wantError string | ||
}{ | ||
{ | ||
name: "test successful scan", | ||
number: func() *number.Number { | ||
n, _ := number.NewNumber("15556661212") | ||
return n | ||
}(), | ||
expected: customScannerResponse{ | ||
Valid: true, | ||
Info: "This number is known for scams!", | ||
Hidden: "This will not appear in the output", | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range testcases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
scanner := &customScanner{} | ||
|
||
if !scanner.ShouldRun(*tt.number) { | ||
t.Fatal("ShouldRun() should be truthy") | ||
} | ||
|
||
got, err := scanner.Scan(*tt.number) | ||
if tt.wantError != "" { | ||
assert.EqualError(t, err, tt.wantError) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
assert.Equal(t, tt.expected, got) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.