github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/common/version.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"os"
     7  	"os/user"
     8  	"runtime"
     9  	"strings"
    10  	"time"
    11  
    12  	"github.com/denisbrodbeck/machineid"
    13  )
    14  
    15  var clientSessionIdentity *ClientSessionIdentityT
    16  var SessionID string // global because we use in mlog fns to inject for all data points
    17  
    18  func init() {
    19  	initClientSessionIdentity()
    20  }
    21  
    22  // ClientSessionIdentityT holds values describing the client, environment, and session.
    23  type ClientSessionIdentityT struct {
    24  	Version   string    `json:"version"`
    25  	Hostname  string    `json:"host"`
    26  	Username  string    `json:"user"`
    27  	MachineID string    `json:"machineid"`
    28  	Goos      string    `json:"goos"`
    29  	Goarch    string    `json:"goarch"`
    30  	Goversion string    `json:"goversion"`
    31  	Pid       int       `json:"pid"`
    32  	SessionID string    `json:"session"`
    33  	StartTime time.Time `json:"start"`
    34  }
    35  
    36  // String is the stringer fn for ClientSessionIdentityT
    37  func (s *ClientSessionIdentityT) String() string {
    38  	return fmt.Sprintf("VERSION=%s GO=%s GOOS=%s GOARCH=%s SESSIONID=%s HOSTNAME=%s USER=%s MACHINE=%s PID=%d",
    39  		s.Version, s.Goversion, s.Goos, s.Goarch, s.SessionID, s.Hostname, s.Username, s.MachineID, s.Pid)
    40  }
    41  
    42  // Helpers for random sessionid string.
    43  const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    44  
    45  func randStringBytes(rng *rand.Rand, n int) string {
    46  	b := make([]byte, n)
    47  	for i := range b {
    48  		b[i] = letterBytes[rng.Intn(len(letterBytes))]
    49  	}
    50  	return string(b)
    51  }
    52  
    53  // initClientSessionIdentity sets the global variable describing details about the client and session.
    54  func initClientSessionIdentity() {
    55  	rng := rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
    56  	SessionID = randStringBytes(rng, 4)
    57  
    58  	var hostname, userName string
    59  	var err error
    60  
    61  	hostname, err = os.Hostname()
    62  	if err != nil {
    63  		hostname = "unknown"
    64  	}
    65  
    66  	current, err := user.Current()
    67  	if err == nil {
    68  		userName = current.Username
    69  	} else {
    70  		userName = "unknown"
    71  	}
    72  	// Sanitize userName since it may contain filepath separators on Windows.
    73  	userName = strings.Replace(userName, `\`, "_", -1)
    74  
    75  	var mid string
    76  	var e error
    77  	mid, e = machineid.ID()
    78  	if e == nil {
    79  		mid, e = machineid.ProtectedID(mid)
    80  	}
    81  	if e != nil {
    82  		mid = hostname + "." + userName
    83  	}
    84  
    85  	clientSessionIdentity = &ClientSessionIdentityT{
    86  		Version:   "unknown",
    87  		Hostname:  hostname,
    88  		Username:  userName,
    89  		MachineID: mid[:8], // because we don't care that much
    90  		Goos:      runtime.GOOS,
    91  		Goarch:    runtime.GOARCH,
    92  		Goversion: runtime.Version(),
    93  		Pid:       os.Getpid(),
    94  		SessionID: SessionID,
    95  		StartTime: time.Now(),
    96  	}
    97  }
    98  
    99  func SetClientVersion(version string) {
   100  	if clientSessionIdentity != nil {
   101  		clientSessionIdentity.Version = version
   102  	}
   103  }
   104  
   105  // GetClientSessionIdentity is the getter fn for a the clientSessionIdentity value.
   106  func GetClientSessionIdentity() *ClientSessionIdentityT {
   107  	return clientSessionIdentity
   108  }