]> Sergey Matveev's repositories - btrtrc.git/blob - version/version.go
Add testdata/The-Fanimatrix-(DivX-5.1-HQ).avi.torrent
[btrtrc.git] / version / version.go
1 // Package version provides default versions, user-agents etc. for client identification.
2 package version
3
4 import (
5         "fmt"
6         "reflect"
7         "runtime/debug"
8 )
9
10 var (
11         DefaultExtendedHandshakeClientVersion string
12         // This should be updated when client behaviour changes in a way that other peers could care
13         // about.
14         DefaultBep20Prefix   = "-GT0003-"
15         DefaultHttpUserAgent string
16         DefaultUpnpId        string
17 )
18
19 func init() {
20         const (
21                 longNamespace   = "anacrolix"
22                 longPackageName = "torrent"
23         )
24         type Newtype struct{}
25         var newtype Newtype
26         thisPkg := reflect.TypeOf(newtype).PkgPath()
27         var (
28                 mainPath       = "unknown"
29                 mainVersion    = "unknown"
30                 torrentVersion = "unknown"
31         )
32         if buildInfo, ok := debug.ReadBuildInfo(); ok {
33                 mainPath = buildInfo.Main.Path
34                 mainVersion = buildInfo.Main.Version
35                 // Note that if the main module is the same as this module, we get a version of "(devel)".
36                 for _, dep := range append(buildInfo.Deps, &buildInfo.Main) {
37                         if dep.Path == thisPkg {
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 }