Skip to content

Commit

Permalink
misc updates (removed interactive login in library)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarunKoyalwar committed Feb 26, 2024
1 parent 348beff commit 11f734f
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@
# Dependency directories (remove the comment below to include it)
# vendor/
dist/
/asnmap
/cmd/asnmap/asnmap
13 changes: 10 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@ GOCMD=go
GOBUILD=$(GOCMD) build
GOMOD=$(GOCMD) mod
GOTEST=$(GOCMD) test
GOCLEAN=$(GOCMD) clean
GOFLAGS := -v
# This should be disabled if the binary uses pprof
LDFLAGS := -s -w

ifneq ($(shell go env GOOS),darwin)
LDFLAGS := -extldflags "-static"
endif


.PHONY: cli test tidy

cli:
$(GOBUILD) -v -ldflags="-extldflags=-static" -o "asnmap" ./cmd/asnmap/
cli, build:
$(GOBUILD) $(GOFLAGS) -ldflags '$(LDFLAGS)' -o "asnmap" ./cmd/asnmap/
test:
$(GOCLEAN) -testcache
$(GOTEST) -v ./...
Expand Down
12 changes: 12 additions & 0 deletions cmd/asnmap/asnmap.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package main

import (
"errors"
"os"
"os/signal"

asnmap "github.com/projectdiscovery/asnmap/libs"
"github.com/projectdiscovery/asnmap/runner"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/utils/auth/pdcp"
)

func main() {
Expand Down Expand Up @@ -34,6 +37,15 @@ func main() {
}()

if err := asnmapRunner.Run(); err != nil {
if errors.Is(err, asnmap.ErrUnAuthorized) {
gologger.Info().Msgf("Try again after authenticating with PDCP\n\n")
// trigger auth callback
pdcp.CheckNValidateCredentials("asnmap")
// run again
if err := asnmapRunner.Run(); err != nil {
gologger.Fatal().Msgf("%s\n", err)
}
}
gologger.Fatal().Msgf("%s\n", err)
}
}
34 changes: 19 additions & 15 deletions libs/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,21 @@ import (
"strings"
"time"

"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/utils/auth/pdcp"
"github.com/projectdiscovery/utils/env"
fileutil "github.com/projectdiscovery/utils/file"
stringsutil "github.com/projectdiscovery/utils/strings"
updateutils "github.com/projectdiscovery/utils/update"
urlutil "github.com/projectdiscovery/utils/url"
"golang.org/x/net/proxy"
)

const serverURL = "https://asn.projectdiscovery.io/"

var (
UsingAsLibrary = false
RequestForAuthOnUnauthorizedErr = true
PDCPApiKey = env.GetEnvOrDefault("PDCP_API_KEY", "")
ErrUnAuthorized = errors.New("unauthorized: 401 (get your free api key from https://cloud.projectdiscovery.io)")
PDCPApiKey = env.GetEnvOrDefault("PDCP_API_KEY", "")
ErrUnAuthorized = errors.New("unauthorized: 401 (get your free api key from https://cloud.projectdiscovery.io)")
)

func init() {
Expand Down Expand Up @@ -162,10 +163,6 @@ func (c *Client) setProxy(proxyString string) (*url.URL, error) {
}
}

func generateRawQuery(query, value string) string {
return query + "=" + value
}

func (c Client) makeRequest() ([]byte, error) {
if c.http == nil {
return nil, errors.New("http client is not initialized")
Expand All @@ -175,16 +172,18 @@ func (c Client) makeRequest() ([]byte, error) {
if err != nil {
return nil, err
}
if PDCPApiKey == "" {
gologger.Error().Label("asnmap-api").Msgf("missing or invalid api key (get your free api key from https://cloud.projectdiscovery.io)")
return nil, ErrUnAuthorized
}
req.Header.Set("X-PDCP-Key", PDCPApiKey)
res, err := c.http.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode == http.StatusUnauthorized {
if UsingAsLibrary && RequestForAuthOnUnauthorizedErr {
pdcp.CheckNValidateCredentials("asnmap")
}
gologger.Error().Msgf("missing or invalid api key (get your free api key from https://cloud.projectdiscovery.io)")
return nil, ErrUnAuthorized
}

Expand All @@ -205,20 +204,25 @@ func (c Client) GetDataWithCustomInput(inputToQuery, inputToUseInResponse string

func (c Client) GetData(input string, medatadas ...string) ([]*Response, error) {
inputToStore := input
params := urlutil.NewOrderedParams()
switch IdentifyInput(input) {
case ASN:
inputToStore = strings.TrimPrefix(strings.ToLower(input), "as")
c.url.RawQuery = generateRawQuery("asn", inputToStore)
params.Add("asn", inputToStore)
case ASNID:
c.url.RawQuery = generateRawQuery("asn", input)
params.Add("asn", input)
case IP:
c.url.RawQuery = generateRawQuery("ip", input)
params.Add("ip", input)
case Org:
c.url.RawQuery = generateRawQuery("org", input)
params.Add("org", input)
case Unknown:
return nil, errors.New("unknown type")
}

params.Decode(updateutils.GetpdtmParams(Version))

c.url.RawQuery = params.Encode()

resp, err := c.makeRequest()
if err != nil {
return nil, err
Expand Down
4 changes: 4 additions & 0 deletions libs/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package asnmap

// Version is the current Version of asnmap
const Version = `v1.0.6`
6 changes: 2 additions & 4 deletions runner/banner.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package runner

import (
asnmap "github.com/projectdiscovery/asnmap/libs"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/utils/auth/pdcp"
updateutils "github.com/projectdiscovery/utils/update"
Expand All @@ -14,9 +15,6 @@ const banner = `
/_/
`

// version is the current version of asnmap
const version = `v1.0.6`

// showBanner is used to show the banner to the user
func showBanner() {
gologger.Print().Msgf("%s\n", banner)
Expand All @@ -27,7 +25,7 @@ func showBanner() {
func GetUpdateCallback() func() {
return func() {
showBanner()
updateutils.GetUpdateToolCallback("asnmap", version)()
updateutils.GetUpdateToolCallback("asnmap", asnmap.Version)()
}
}

Expand Down
10 changes: 5 additions & 5 deletions runner/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ func (options *Options) validateOptions() error {
}

if options.DisplayInJSON && options.DisplayInCSV {
return errors.New("can either display in json or csv.")
return errors.New("can either display in json or csv")
}

// validate asn input
if options.Asn != nil {
for _, asn := range options.Asn {
if !strings.HasPrefix(strings.ToUpper(asn), "AS") {
return errors.New("Invalid ASN given. it should start with prefix 'AS', example : AS14421")
return errors.New("invalid ASN given. it should start with prefix 'AS', example : AS14421")
}
}
}
Expand Down Expand Up @@ -157,20 +157,20 @@ func ParseOptions() *Options {
}

if options.Version {
gologger.Info().Msgf("Current Version: %s\n", version)
gologger.Info().Msgf("Current Version: %s\n", asnmap.Version)
os.Exit(0)
}

showBanner()

if !options.DisableUpdateCheck {
latestVersion, err := updateutils.GetToolVersionCallback("asnmap", version)()
latestVersion, err := updateutils.GetToolVersionCallback("asnmap", asnmap.Version)()
if err != nil {
if options.Verbose {
gologger.Error().Msgf("asnmap version check failed: %v", err.Error())
}
} else {
gologger.Info().Msgf("Current asnmap version %v %v", version, updateutils.GetVersionDescription(version, latestVersion))
gologger.Info().Msgf("Current asnmap version %v %v", asnmap.Version, updateutils.GetVersionDescription(asnmap.Version, latestVersion))
}
}

Expand Down

0 comments on commit 11f734f

Please sign in to comment.