github.com/anacrolix/torrent@v1.61.0/version/version.go (about)

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