Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use projectdiscovery/machineid #352

Merged
merged 3 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
github.com/Masterminds/semver/v3 v3.2.1
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2
github.com/charmbracelet/glamour v0.6.0
github.com/denisbrodbeck/machineid v1.0.1
github.com/google/go-github/v30 v30.1.0
github.com/hdm/jarm-go v0.0.7
github.com/julienschmidt/httprouter v1.3.0
Expand All @@ -18,6 +17,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/projectdiscovery/blackrock v0.0.1
github.com/projectdiscovery/fdmax v0.0.4
github.com/projectdiscovery/machineid v0.0.0-20240226150047-2e2c51e35983
github.com/remeh/sizedwaitgroup v1.0.0
github.com/saintfish/chardet v0.0.0-20230101081208-5e3ef4b5456d
github.com/shirou/gopsutil/v3 v3.23.7
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBS
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/dimchansky/utfbom v1.1.1 h1:vV6w1AhK4VMnhBno/TPVCoK9U/LP0PkLCS9tbxHdi/U=
github.com/dimchansky/utfbom v1.1.1/go.mod h1:SxdoEBH5qIqFocHMyGOXVAybYJdr71b1Q/j0mACtrfE=
github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
Expand Down Expand Up @@ -190,6 +188,8 @@ github.com/projectdiscovery/gologger v1.1.12 h1:uX/QkQdip4PubJjjG0+uk5DtyAi1ANPJ
github.com/projectdiscovery/gologger v1.1.12/go.mod h1:DI8nywPLERS5mo8QEA9E7gd5HZ3Je14SjJBH3F5/kLw=
github.com/projectdiscovery/hmap v0.0.39 h1:R33JJzPz8TMUm1TJQ/X5zVJbmyTS64EttZ085thq19g=
github.com/projectdiscovery/hmap v0.0.39/go.mod h1:wEPoEIVGPdHsc9EnE+ERUdgNyp29zZn6gACOALylHOg=
github.com/projectdiscovery/machineid v0.0.0-20240226150047-2e2c51e35983 h1:ZScLodGSezQVwsQDtBSMFp72WDq0nNN+KE/5DHKY5QE=
github.com/projectdiscovery/machineid v0.0.0-20240226150047-2e2c51e35983/go.mod h1:3G3BRKui7nMuDFAZKR/M2hiOLtaOmyukT20g88qRQjI=
github.com/projectdiscovery/networkpolicy v0.0.7 h1:AwHqBRXBqDQgnWzBMuoJtHBNEYBw+NFp/4qIK688x7o=
github.com/projectdiscovery/networkpolicy v0.0.7/go.mod h1:CK0CnFoLF1Nou6mY7P4WODSAxhPN8g8g7XpapgEP8tI=
github.com/projectdiscovery/retryabledns v1.0.56 h1:Rk/fvBSNjw4vzbHRSSoFz3Bkn9uaRSk0UE/IsEBl0cQ=
Expand Down
53 changes: 53 additions & 0 deletions process/docker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package process

import (
"bufio"
"os"
"strings"

fileutil "github.com/projectdiscovery/utils/file"
)

// RunningInContainer checks if the process is running in a docker container
// and returns true if it is.
// reference: https://www.baeldung.com/linux/is-process-running-inside-container
func RunningInContainer() (bool, string) {
if fileutil.FileOrFolderExists("/.dockerenv") {
return true, "docker"
}
// fallback and check using controlgroup 1 detect
if !fileutil.FileExists("/proc/1/cgroup") {
return false, ""
}
f, err := os.Open("/proc/1/cgroup")
if err != nil {
return false, ""
}
defer f.Close()
buff := bufio.NewScanner(f)
for buff.Scan() {
if strings.Contains(buff.Text(), "/docker") {
return true, "docker"
}
if strings.Contains(buff.Text(), "/lxc") {
return true, "lxc"
}
}
// fallback and check using controlgroup 2 detect
f2, err := os.Open("/proc/self/mountinfo")
if err != nil {
return false, ""
}
defer f2.Close()
buff2 := bufio.NewScanner(f2)
for buff2.Scan() {
if strings.Contains(buff2.Text(), "/docker") {
return true, "docker"
}
if strings.Contains(buff2.Text(), "/lxc") {
return true, "lxc"
}
}

return false, ""
}
7 changes: 5 additions & 2 deletions update/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/Masterminds/semver/v3"
"github.com/logrusorgru/aurora"
"github.com/projectdiscovery/utils/process"
)

type AssetFormat uint
Expand Down Expand Up @@ -103,7 +104,7 @@ func IsDevReleaseOutdated(current string, latest string) bool {

// getUtmSource returns utm_source from environment variable or "unknown" value
func getUtmSource() string {
value := ""
value := "unknown"
switch {
case os.Getenv("GH_ACTION") != "":
value = "ghci"
Expand Down Expand Up @@ -144,7 +145,9 @@ func getUtmSource() string {
case os.Getenv("GAE_RUNTIME") != "":
value = os.Getenv("GAE_RUNTIME")
default:
value = "unknown"
if ok, val := process.RunningInContainer(); ok {
return val
}
}
return value
}
8 changes: 5 additions & 3 deletions update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (

"github.com/Masterminds/semver/v3"
"github.com/charmbracelet/glamour"
"github.com/denisbrodbeck/machineid"
"github.com/minio/selfupdate"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/machineid"
errorutil "github.com/projectdiscovery/utils/errors"
)

Expand Down Expand Up @@ -153,12 +153,14 @@ func GetpdtmParams(version string) string {
params.Add("arch", runtime.GOARCH)
params.Add("go_version", runtime.Version())
params.Add("v", version)
params.Add("machine_id", buildMachineId())
params.Add("machine_id", GetMachineID())
params.Add("utm_source", getUtmSource())
return params.Encode()
}

func buildMachineId() string {
// GetMachineID return a unique identifier that is unique to the machine
// it is a sha256 hashed value with pdtm as salt
func GetMachineID() string {
machineId, err := machineid.ProtectedID("pdtm")
if err != nil {
return "unknown"
Expand Down