Files
funknetz/station/software.go
T

71 lines
1.4 KiB
Go

package station
import (
"bytes"
jsonv2 "encoding/json/v2"
"golang.org/x/sys/unix"
)
// SoftwareName and SoftwareVersion are the identity sent to the reflector
// after login. Edit them here.
const (
SoftwareName = "funknetz"
SoftwareVersion = "1.0.0"
)
type softwareIdentity struct {
SW string `json:"sw"`
SWVer string `json:"swVer"`
ProjVer string `json:"projVer"`
MachineArch string `json:"machineArch,omitzero"`
}
func softwareIdentityBody() ([]byte, error) {
raw, err := marshalIdentity(false)
if err != nil {
return nil, err
}
return appendString(nil, string(raw)), nil
}
func identityV3(iv, key []byte) ([]byte, error) {
raw, err := marshalIdentity(true)
if err != nil {
return nil, err
}
body := appendBlob(nil, iv)
body = appendBlob(body, key)
return appendString(body, string(raw)), nil
}
func marshalIdentity(arch bool) ([]byte, error) {
info := softwareIdentity{
SW: SoftwareName,
SWVer: SoftwareVersion,
ProjVer: SoftwareVersion,
}
if arch {
if name, ok := MachineArch(); ok {
info.MachineArch = name
}
}
return jsonv2.Marshal(info)
}
// MachineArch is the hardware name from uname, when it can be read.
func MachineArch() (string, bool) {
var u unix.Utsname
if err := unix.Uname(&u); err != nil {
return "", false
}
b := u.Machine[:]
if i := bytes.IndexByte(b, 0); i >= 0 {
b = b[:i]
}
if len(b) == 0 {
return "", false
}
return string(b), true
}