Skip to content

Commit bbb3cba

Browse files
committed
add tem-id option
1 parent fb579bd commit bbb3cba

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

cmd/httpx/httpx.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,8 @@ func setupOptionalAssetUpload(opts *runner.Options) *pdcp.UploadWriter {
138138
// silently ignore
139139
writer.SetAssetGroupName(opts.AssetName)
140140
}
141+
if opts.TeamID != "" {
142+
writer.SetTeamID(opts.TeamID)
143+
}
141144
return writer
142145
}

internal/pdcp/writer.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ const (
3535

3636
var (
3737
xidRegex = regexp.MustCompile(xidRe)
38-
// teamID if given
39-
teamID = env.GetEnvOrDefault("PDCP_TEAM_ID", "")
4038
// EnableeUpload if set to true enables the upload feature
4139
HideAutoSaveMsg = env.GetEnvOrDefault("DISABLE_CLOUD_UPLOAD_WRN", false)
4240
EnableCloudUpload = env.GetEnvOrDefault("ENABLE_CLOUD_UPLOAD", false)
@@ -54,6 +52,7 @@ type UploadWriter struct {
5452
assetGroupName string
5553
counter atomic.Int32
5654
closed atomic.Bool
55+
TeamID string
5756
}
5857

5958
// NewUploadWriterCallback creates a new upload writer callback
@@ -63,9 +62,10 @@ func NewUploadWriterCallback(ctx context.Context, creds *pdcpauth.PDCPCredential
6362
return nil, fmt.Errorf("no credentials provided")
6463
}
6564
u := &UploadWriter{
66-
creds: creds,
67-
done: make(chan struct{}, 1),
68-
data: make(chan runner.Result, 8), // default buffer size
65+
creds: creds,
66+
done: make(chan struct{}, 1),
67+
data: make(chan runner.Result, 8), // default buffer size
68+
TeamID: "",
6969
}
7070
var err error
7171
tmp, err := urlutil.Parse(creds.Server)
@@ -111,6 +111,11 @@ func (u *UploadWriter) SetAssetGroupName(name string) {
111111
u.assetGroupName = name
112112
}
113113

114+
// SetTeamID sets the team id for the upload writer
115+
func (u *UploadWriter) SetTeamID(id string) {
116+
u.TeamID = id
117+
}
118+
114119
func (u *UploadWriter) autoCommit(ctx context.Context) {
115120
// wait for context to be done
116121
defer func() {
@@ -244,8 +249,8 @@ func (u *UploadWriter) getRequest(bin []byte) (*retryablehttp.Request, error) {
244249
req.URL.Update()
245250

246251
req.Header.Set(pdcpauth.ApiKeyHeaderName, u.creds.APIKey)
247-
if teamID != "" {
248-
req.Header.Set(teamIDHeader, teamID)
252+
if u.TeamID != "" {
253+
req.Header.Set(teamIDHeader, u.TeamID)
249254
}
250255
req.Header.Set("Content-Type", "application/octet-stream")
251256
req.Header.Set("Accept", "application/json")

runner/options.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"github.com/projectdiscovery/httpx/common/httpx"
2626
"github.com/projectdiscovery/httpx/common/stringz"
2727
"github.com/projectdiscovery/networkpolicy"
28-
"github.com/projectdiscovery/utils/auth/pdcp"
28+
pdcpauth "github.com/projectdiscovery/utils/auth/pdcp"
2929
"github.com/projectdiscovery/utils/env"
3030
fileutil "github.com/projectdiscovery/utils/file"
3131
sliceutil "github.com/projectdiscovery/utils/slice"
@@ -41,7 +41,10 @@ const (
4141
DefaultOutputDirectory = "output"
4242
)
4343

44-
var PDCPApiKey = ""
44+
var (
45+
PDCPApiKey = ""
46+
TeamIDEnv = env.GetEnvOrDefault("PDCP_TEAM_ID", "")
47+
)
4548

4649
// OnResultCallback (hostResult)
4750
type OnResultCallback func(Result)
@@ -318,6 +321,7 @@ type Options struct {
318321
AssetID string
319322
// AssetFileUpload
320323
AssetFileUpload string
324+
TeamID string
321325
// OnClose adds a callback function that is invoked when httpx is closed
322326
// to be exact at end of existing closures
323327
OnClose func()
@@ -509,6 +513,7 @@ func ParseOptions() *Options {
509513
flagSet.CreateGroup("cloud", "Cloud",
510514
flagSet.DynamicVar(&options.PdcpAuth, "auth", "true", "configure projectdiscovery cloud (pdcp) api key"),
511515
flagSet.BoolVarP(&options.AssetUpload, "dashboard", "pd", false, "upload / view output in projectdiscovery cloud (pdcp) UI dashboard"),
516+
flagSet.StringVarP(&options.TeamID, "team-id", "tid", TeamIDEnv, "upload asset results to given team id (optional)"),
512517
flagSet.StringVarP(&options.AssetID, "asset-id", "aid", "", "upload new assets to existing asset id (optional)"),
513518
flagSet.StringVarP(&options.AssetName, "asset-name", "aname", "", "assets group name to set (optional)"),
514519
flagSet.StringVarP(&options.AssetFileUpload, "dashboard-upload", "pdu", "", "upload httpx output file (jsonl) in projectdiscovery cloud (pdcp) UI dashboard"),
@@ -540,9 +545,9 @@ func ParseOptions() *Options {
540545
AuthWithPDCP()
541546
} else if len(options.PdcpAuth) == 36 {
542547
PDCPApiKey = options.PdcpAuth
543-
ph := pdcp.PDCPCredHandler{}
544-
if _, err := ph.GetCreds(); err == pdcp.ErrNoCreds {
545-
apiServer := env.GetEnvOrDefault("PDCP_API_SERVER", pdcp.DefaultApiServer)
548+
ph := pdcpauth.PDCPCredHandler{}
549+
if _, err := ph.GetCreds(); err == pdcpauth.ErrNoCreds {
550+
apiServer := env.GetEnvOrDefault("PDCP_API_SERVER", pdcpauth.DefaultApiServer)
546551
if validatedCreds, err := ph.ValidateAPIKey(PDCPApiKey, apiServer, "httpx"); err == nil {
547552
_ = ph.SaveCreds(validatedCreds)
548553
}

0 commit comments

Comments
 (0)