version.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Package version provides default versions, user-agents etc. for client identification.
  2. package version
  3. import (
  4. "fmt"
  5. "reflect"
  6. "runtime/debug"
  7. "strings"
  8. )
  9. var (
  10. DefaultExtendedHandshakeClientVersion string
  11. // This should be updated when client behaviour changes in a way that other peers could care
  12. // about.
  13. DefaultBep20Prefix = "-GT0003-"
  14. DefaultHttpUserAgent string
  15. DefaultUpnpId string
  16. )
  17. func init() {
  18. const (
  19. longNamespace = "anacrolix"
  20. longPackageName = "torrent"
  21. )
  22. type Newtype struct{}
  23. var newtype Newtype
  24. thisPkg := reflect.TypeOf(newtype).PkgPath()
  25. var (
  26. mainPath = "unknown"
  27. mainVersion = "unknown"
  28. torrentVersion = "unknown"
  29. )
  30. if buildInfo, ok := debug.ReadBuildInfo(); ok {
  31. mainPath = buildInfo.Main.Path
  32. mainVersion = buildInfo.Main.Version
  33. thisModule := ""
  34. // Note that if the main module is the same as this module, we get a version of "(devel)".
  35. for _, dep := range append(buildInfo.Deps, &buildInfo.Main) {
  36. if strings.HasPrefix(thisPkg, dep.Path) && len(dep.Path) >= len(thisModule) {
  37. thisModule = dep.Path
  38. torrentVersion = dep.Version
  39. }
  40. }
  41. }
  42. DefaultExtendedHandshakeClientVersion = fmt.Sprintf(
  43. "%v %v (%v/%v %v)",
  44. mainPath,
  45. mainVersion,
  46. longNamespace,
  47. longPackageName,
  48. torrentVersion,
  49. )
  50. DefaultUpnpId = fmt.Sprintf("%v %v", mainPath, mainVersion)
  51. // Per https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent#library_and_net_tool_ua_strings
  52. DefaultHttpUserAgent = fmt.Sprintf(
  53. "%v-%v/%v",
  54. longNamespace,
  55. longPackageName,
  56. torrentVersion,
  57. )
  58. }