Skip to content

Commit

Permalink
ssh: fix clientVersion and add uname
Browse files Browse the repository at this point in the history
  • Loading branch information
fcharlie committed Jan 9, 2025
1 parent ea82b77 commit 6479a0b
Show file tree
Hide file tree
Showing 13 changed files with 195 additions and 7 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/gorilla/mux v1.8.1
github.com/hinshun/vt10x v0.0.0-20220301184237-5011da428d02
github.com/klauspost/compress v1.17.11
github.com/klauspost/cpuid/v2 v2.2.9
github.com/mattn/go-isatty v0.0.20
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d
github.com/rivo/uniseg v0.4.7
Expand Down Expand Up @@ -59,7 +60,6 @@ require (
github.com/cloudflare/circl v1.5.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/klauspost/cpuid/v2 v2.2.9 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
Expand Down
2 changes: 1 addition & 1 deletion pkg/serve/httpserver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func NewServerConfig(file string, expandEnv bool) (*ServerConfig, error) {
WriteTimeout: serve.Duration{
Duration: DefaultWriteTimeout,
},
BannerVersion: "HugeSCM/" + version.GetVersion(),
BannerVersion: version.GetUserAgent(),
}
if _, err = toml.NewDecoder(r).Decode(sc); err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/serve/sshserver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func NewServerConfig(file string, expandEnv bool) (*ServerConfig, error) {
MaxTimeout: serve.Duration{
Duration: DefaultMaxTimeout,
},
BannerVersion: "HugeSCM-" + version.GetVersion(),
BannerVersion: version.GetBannerVersion(),
}
if _, err = toml.NewDecoder(r).Decode(sc); err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/transport/http/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func newClient(ctx context.Context, endpoint *transport.Endpoint, operation tran
},
baseURL: base,
extraHeader: endpoint.ExtraHeader,
userAgent: "Zeta/" + version.GetVersion(),
userAgent: version.GetUserAgent(),
language: tr.Language(),
termEnv: os.Getenv("TERM"),
verbose: verbose,
Expand Down
2 changes: 1 addition & 1 deletion pkg/transport/http/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func NewDownloader(verbose bool, insecure bool, proxyURL string) Downloader {
},
},
},
userAgent: "Zeta/" + version.GetVersion(),
userAgent: version.GetUserAgent(),
language: tr.Language(),
proxyURL: proxyURL,
termEnv: os.Getenv("TERM"),
Expand Down
2 changes: 1 addition & 1 deletion pkg/transport/ssh/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func filterKnownHostsFiles(files ...string) ([]string, error) {
return out, nil
}

func (c *client) makeAuth() ([]ssh.AuthMethod, error) {
func (c *client) prepareAuthMethod() ([]ssh.AuthMethod, error) {
auth := make([]ssh.AuthMethod, 0, 4)
auth = append(auth, ssh.PublicKeysCallback(c.PublicKeys))
if len(c.Password) != 0 {
Expand Down
8 changes: 7 additions & 1 deletion pkg/transport/ssh/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ import (
"github.com/antgroup/hugescm/pkg/transport/proxy"
"github.com/antgroup/hugescm/pkg/transport/ssh/config"
"github.com/antgroup/hugescm/pkg/transport/ssh/knownhosts"
"github.com/antgroup/hugescm/pkg/version"
"golang.org/x/crypto/ssh"
)

const (
protocolVersionPrefix = "SSH-2.0-"
)

var (
W = tr.W
dialer = net.Dialer{
Expand Down Expand Up @@ -132,13 +137,14 @@ func (c *client) newCommand(conn net.Conn, addr string) (*Command, error) {
return nil, err
}
}
auth, err := c.makeAuth()
auth, err := c.prepareAuthMethod()
if err != nil {
return nil, err
}
cc, chans, reqs, err := ssh.NewClientConn(conn, addr, &ssh.ClientConfig{
User: c.User,
Auth: auth,
ClientVersion: protocolVersionPrefix + version.GetBannerVersion(),
HostKeyCallback: c.HostKeyCallback,
BannerCallback: ssh.BannerDisplayStderr(),
HostKeyAlgorithms: c.supportedHostKeyAlgos(),
Expand Down
12 changes: 12 additions & 0 deletions pkg/version/uname.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package version

type SystemInfo struct {
Name string `json:"name"`
Node string `json:"node"`
Release string `json:"release"`
Version string `json:"version"`
Machine string `json:"machine"`
Domain string `json:"domain,omitempty"`
OS string `json:"os"`
Processor string `json:"processor"`
}
28 changes: 28 additions & 0 deletions pkg/version/uname_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//go:build linux

package version

import (
"runtime"

"github.com/klauspost/cpuid/v2"
"golang.org/x/sys/unix"
)

func GetSystemInfo() (*SystemInfo, error) {
var utsname unix.Utsname
if err := unix.Uname(&utsname); err != nil {
return nil, err
}

return &SystemInfo{
Name: unix.ByteSliceToString(utsname.Sysname[:]),
Node: unix.ByteSliceToString(utsname.Nodename[:]),
Release: unix.ByteSliceToString(utsname.Release[:]),
Version: unix.ByteSliceToString(utsname.Version[:]),
Machine: unix.ByteSliceToString(utsname.Machine[:]),
Domain: unix.ByteSliceToString(utsname.Domainname[:]),
OS: runtime.GOOS,
Processor: cpuid.CPU.BrandName,
}, nil
}
17 changes: 17 additions & 0 deletions pkg/version/uname_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package version

import (
"encoding/json"
"os"
"testing"
)

func TestGetSystemInfo(t *testing.T) {
info, err := GetSystemInfo()
if err != nil {
return
}
enc := json.NewEncoder(os.Stderr)
enc.SetIndent("", " ")
_ = enc.Encode(info)
}
26 changes: 26 additions & 0 deletions pkg/version/uname_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//go:build !windows && !linux

package version

import (
"runtime"

"github.com/klauspost/cpuid/v2"
"golang.org/x/sys/unix"
)

func GetSystemInfo() (*SystemInfo, error) {
var utsname unix.Utsname
if err := unix.Uname(&utsname); err != nil {
return nil, err
}
return &SystemInfo{
Name: unix.ByteSliceToString(utsname.Sysname[:]),
Node: unix.ByteSliceToString(utsname.Nodename[:]),
Release: unix.ByteSliceToString(utsname.Release[:]),
Version: unix.ByteSliceToString(utsname.Version[:]),
Machine: unix.ByteSliceToString(utsname.Machine[:]),
OS: runtime.GOOS,
Processor: cpuid.CPU.BrandName,
}, nil
}
91 changes: 91 additions & 0 deletions pkg/version/uname_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//go:build windows

package version

import (
"fmt"
"runtime"
"strconv"
"unsafe"

"github.com/klauspost/cpuid/v2"
"golang.org/x/sys/windows"
)

const (
PROCESSOR_ARCHITECTURE_AMD64 = 9
PROCESSOR_ARCHITECTURE_ARM = 5
PROCESSOR_ARCHITECTURE_ARM64 = 12
PROCESSOR_ARCHITECTURE_IA64 = 6
PROCESSOR_ARCHITECTURE_INTEL = 0
)

var (
processorArchLists = map[uint16]string{
PROCESSOR_ARCHITECTURE_AMD64: "x64",
PROCESSOR_ARCHITECTURE_ARM: "arm",
PROCESSOR_ARCHITECTURE_ARM64: "arm64",
PROCESSOR_ARCHITECTURE_IA64: "ia64",
PROCESSOR_ARCHITECTURE_INTEL: "x86",
}
)

func machineName(i uint16) string {
if n, ok := processorArchLists[i]; ok {
return n
}
return "unknown"
}

type PROCESSOR_ARCH struct {
ProcessorArchitecture uint16
Reserved uint16
}

type SYSTEM_INFO struct {
Arch PROCESSOR_ARCH
DwPageSize uint32
LpMinimumApplicationAddress uintptr
LpMaximumApplicationAddress uintptr
DwActiveProcessorMask uint
DwNumberOfProcessors uint32
DwProcessorType uint32
DwAllocationGranularity uint32
WProcessorLevel uint16
WProcessorRevision uint16
}

var (
kernel32 = windows.NewLazySystemDLL("kernel32.dll")
procGetNativeSystemInfo = kernel32.NewProc("GetNativeSystemInfo")
)

func GetNativeSystemInfo() *SYSTEM_INFO {
var info SYSTEM_INFO
_, _, _ = procGetNativeSystemInfo.Call(uintptr(unsafe.Pointer(&info)))
return &info
}

func GetComputerName() (string, error) {
var bufferSize uint32 = 1024
var buffer [1024]uint16
if err := windows.GetComputerName(&buffer[0], &bufferSize); err != nil {
return "", nil
}
return windows.UTF16ToString(buffer[:bufferSize]), nil
}

func GetSystemInfo() (*SystemInfo, error) {
sysinfo := GetNativeSystemInfo()
computerName, _ := GetComputerName()
major, minor, build := windows.RtlGetNtVersionNumbers()
return &SystemInfo{
Name: "WindowsNT",
Node: computerName,
Release: strconv.FormatUint(uint64(major), 10),
Version: fmt.Sprintf("%d.%d.%d", major, minor, build),
Machine: machineName(sysinfo.Arch.ProcessorArchitecture),
OS: runtime.GOOS,
Processor: cpuid.CPU.BrandName,
}, nil
}
8 changes: 8 additions & 0 deletions pkg/version/verison.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ func GetVersion() string {
return version
}

func GetUserAgent() string {
return "Zeta/" + version
}

func GetBannerVersion() string {
return "Zeta-" + version
}

// GetBuildTime returns the time at which the build took place
func GetBuildTime() string {
return buildTime
Expand Down

0 comments on commit 6479a0b

Please sign in to comment.