| 1 | package ipfs |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "runtime" |
| 6 | "runtime/debug" |
| 7 | "strings" |
| 8 | |
| 9 | "github.com/ipfs/kubo/core/commands/cmdutils" |
| 10 | ) |
| 11 | |
| 12 | // CurrentCommit is the current git commit, this is set as a ldflag in the Makefile. |
| 13 | var CurrentCommit string |
| 14 | |
| 15 | // taggedRelease is set via ldflag when building from a version-tagged commit |
| 16 | // with a clean tree. When set, the commit hash is omitted from the libp2p |
| 17 | // identify agent version and the HTTP user agent, since the version number |
| 18 | // already identifies the exact source. |
| 19 | var taggedRelease string |
| 20 | |
| 21 | // buildOrigin is the Makefile-injected `host/org/repo` form of |
| 22 | // `git remote get-url origin`. ImplicitAgentSuffix turns a non-upstream |
| 23 | // value into the Version.AgentSuffix default so fork builds self-identify. |
| 24 | var buildOrigin string |
| 25 | |
| 26 | // upstreamModulePath is the canonical upstream module path. Builds whose |
| 27 | // origin matches it contribute no implicit suffix. |
| 28 | const upstreamModulePath = "github.com/ipfs/kubo" |
| 29 | |
| 30 | // CurrentVersionNumber is the current application's version literal. |
| 31 | const CurrentVersionNumber = "0.43.0-dev" |
| 32 | |
| 33 | const ApiVersion = "/kubo/" + CurrentVersionNumber + "/" //nolint |
| 34 | |
| 35 | // RepoVersion is the version number that we are currently expecting to see. |
| 36 | const RepoVersion = 18 |
| 37 | |
| 38 | // GetUserAgentVersion is the libp2p user agent used by go-ipfs. |
| 39 | func GetUserAgentVersion() string { |
| 40 | // For tagged release builds with a clean tree, the commit hash is |
| 41 | // redundant since the version number identifies the exact source. |
| 42 | commit := CurrentCommit |
| 43 | if taggedRelease != "" { |
| 44 | commit = "" |
| 45 | } |
| 46 | |
| 47 | userAgent := "kubo/" + CurrentVersionNumber |
| 48 | if commit != "" { |
| 49 | userAgent += "/" + commit |
| 50 | } |
| 51 | if userAgentSuffix != "" { |
| 52 | userAgent += "/" + userAgentSuffix |
| 53 | } |
| 54 | return cmdutils.CleanAndTrim(userAgent) |
| 55 | } |
| 56 | |
| 57 | var userAgentSuffix string |
| 58 | |
| 59 | func SetUserAgentSuffix(suffix string) { |
| 60 | userAgentSuffix = cmdutils.CleanAndTrim(suffix) |
| 61 | } |
| 62 | |
| 63 | // ImplicitAgentSuffix returns a Version.AgentSuffix default derived from |
| 64 | // the build origin. It prefers the Makefile-injected URL (covers forks |
| 65 | // that keep the upstream `module` line) and falls back to |
| 66 | // debug.ReadBuildInfo's main module path (covers `go install` and forks |
| 67 | // that renamed their module). Returns "" for upstream builds. |
| 68 | func ImplicitAgentSuffix() string { |
| 69 | if s := suffixFromForkPath(buildOrigin); s != "" { |
| 70 | return s |
| 71 | } |
| 72 | if bi, ok := debug.ReadBuildInfo(); ok { |
| 73 | return suffixFromForkPath(bi.Main.Path) |
| 74 | } |
| 75 | return "" |
| 76 | } |
| 77 | |
| 78 | // knownForges lists public git hosts whose hostname is dropped from the |
| 79 | // implicit suffix; other hosts are kept so the origin stays identifiable. |
| 80 | var knownForges = map[string]struct{}{ |
| 81 | "github.com": {}, |
| 82 | "gitlab.com": {}, |
| 83 | "codeberg.org": {}, |
| 84 | "bitbucket.org": {}, |
| 85 | } |
| 86 | |
| 87 | // suffixFromForkPath turns a normalized `host/org/repo` into the implicit |
| 88 | // Version.AgentSuffix. Returns "" for upstream and empty inputs. |
| 89 | func suffixFromForkPath(p string) string { |
| 90 | p = strings.Trim(p, "/") |
| 91 | if p == "" || p == upstreamModulePath { |
| 92 | return "" |
| 93 | } |
| 94 | parts := strings.Split(p, "/") |
| 95 | // Only normalize canonical `host/org/repo`; shorter inputs pass through |
| 96 | // so operators can still identify them. |
| 97 | if len(parts) < 3 { |
| 98 | return p |
| 99 | } |
| 100 | if _, ok := knownForges[parts[0]]; ok { |
| 101 | parts = parts[1:] |
| 102 | } |
| 103 | if parts[len(parts)-1] == "kubo" { |
| 104 | parts = parts[:len(parts)-1] |
| 105 | } |
| 106 | return strings.Join(parts, "/") |
| 107 | } |
| 108 | |
| 109 | type VersionInfo struct { |
| 110 | Version string |
| 111 | Commit string |
| 112 | Repo string |
| 113 | System string |
| 114 | Golang string |
| 115 | } |
| 116 | |
| 117 | func GetVersionInfo() *VersionInfo { |
| 118 | return &VersionInfo{ |
| 119 | Version: CurrentVersionNumber, |
| 120 | Commit: CurrentCommit, |
| 121 | Repo: fmt.Sprint(RepoVersion), |
| 122 | System: runtime.GOARCH + "/" + runtime.GOOS, // TODO: Precise version here |
| 123 | Golang: runtime.Version(), |
| 124 | } |
| 125 | } |