Skip to content

Commit

Permalink
add machine id (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
dogancanbakir authored Sep 22, 2023
1 parent 8b5a952 commit a78f808
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ require (
github.com/VividCortex/ewma v1.2.0 // indirect
github.com/alecthomas/chroma v0.10.0 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/denisbrodbeck/machineid v1.0.1 // indirect
github.com/dlclark/regexp2 v1.8.1 // indirect
github.com/dsnet/compress v0.0.1 // indirect
github.com/fatih/color v1.15.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ github.com/cheggaaa/pb/v3 v3.1.4/go.mod h1:6wVjILNBaXMs8c21qRiaUM8BR82erfgau1DQ4
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/denisbrodbeck/machineid v1.0.1 h1:geKr9qtkB876mXguW2X6TU4ZynleN6ezuMSRhl4D7AQ=
github.com/denisbrodbeck/machineid v1.0.1/go.mod h1:dJUwb7PTidGDeYyUBmXZ2GphQBbjJCrnectwCyxcUSI=
github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/dlclark/regexp2 v1.8.1 h1:6Lcdwya6GjPUNsBct8Lg/yRPwMhABj269AAzdGSiR+0=
github.com/dlclark/regexp2 v1.8.1/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
Expand Down
16 changes: 13 additions & 3 deletions update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/Masterminds/semver/v3"
"github.com/charmbracelet/glamour"
"github.com/denisbrodbeck/machineid"
"github.com/minio/selfupdate"
"github.com/projectdiscovery/gologger"
errorutil "github.com/projectdiscovery/utils/errors"
Expand Down Expand Up @@ -110,7 +111,7 @@ func GetUpdateToolFromRepoCallback(toolName, version, repoName string) func() {
// if repoName is empty then tool name is considered as repoName
func GetToolVersionCallback(toolName, version string) func() (string, error) {
return func() (string, error) {
updateURL := fmt.Sprintf(UpdateCheckEndpoint, toolName) + "?" + getpdtmParams(version)
updateURL := fmt.Sprintf(UpdateCheckEndpoint, toolName) + "?" + GetpdtmParams(version)
if DefaultHttpClient == nil {
// not needed but as a precaution to avoid nil panics
DefaultHttpClient = http.DefaultClient
Expand Down Expand Up @@ -145,16 +146,25 @@ func GetToolVersionCallback(toolName, version string) func() (string, error) {
}
}

// getpdtmParams returns encoded query parameters sent to update check endpoint
func getpdtmParams(version string) string {
// GetpdtmParams returns encoded query parameters sent to update check endpoint
func GetpdtmParams(version string) string {
params := &url.Values{}
params.Add("os", runtime.GOOS)
params.Add("arch", runtime.GOARCH)
params.Add("go_version", runtime.Version())
params.Add("v", version)
params.Add("machine_id", buildMachineId())
return params.Encode()
}

func buildMachineId() string {
machineId, err := machineid.ProtectedID("pdtm")
if err != nil {
return "unknown"
}
return machineId
}

// Deprecated: use GetToolVersionCheckCallback instead
func GetVersionCheckCallback(toolName string) func() (string, error) {
return GetToolVersionCallback(toolName, "")
Expand Down

0 comments on commit a78f808

Please sign in to comment.