mirror of https://github.com/jetkvm/kvm.git
Compare commits
3 Commits
62e830908d
...
787044e5bf
Author | SHA1 | Date |
---|---|---|
|
787044e5bf | |
|
3e7d8fb0f5 | |
|
97b00c6ffc |
3
cloud.go
3
cloud.go
|
@ -223,6 +223,7 @@ func handleCloudRegister(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
apiReq.Header.Set("Content-Type", "application/json")
|
||||
apiReq.Header.Set("User-Agent", httpUserAgent)
|
||||
|
||||
apiResp, err := client.Do(apiReq)
|
||||
if err != nil {
|
||||
|
@ -537,6 +538,8 @@ func rpcDeregisterDevice() error {
|
|||
}
|
||||
|
||||
req.Header.Set("Authorization", "Bearer "+config.CloudToken)
|
||||
req.Header.Set("User-Agent", httpUserAgent)
|
||||
|
||||
client := &http.Client{Timeout: CloudAPIRequestTimeout}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
|
|
|
@ -57,6 +57,7 @@ func (t *TimeSync) queryMultipleHttp(urls []string, timeout time.Duration) (now
|
|||
ctx,
|
||||
url,
|
||||
timeout,
|
||||
t.httpUserAgent,
|
||||
)
|
||||
duration := time.Since(startTime)
|
||||
|
||||
|
@ -122,6 +123,7 @@ func queryHttpTime(
|
|||
ctx context.Context,
|
||||
url string,
|
||||
timeout time.Duration,
|
||||
httpUserAgent string,
|
||||
) (now *time.Time, response *http.Response, err error) {
|
||||
client := http.Client{
|
||||
Timeout: timeout,
|
||||
|
@ -130,6 +132,10 @@ func queryHttpTime(
|
|||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if httpUserAgent != "" {
|
||||
req.Header.Set("User-Agent", httpUserAgent)
|
||||
}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
|
|
|
@ -32,6 +32,8 @@ type TimeSync struct {
|
|||
httpUrls []string
|
||||
networkConfig *network.NetworkConfig
|
||||
|
||||
httpUserAgent string
|
||||
|
||||
rtcDevicePath string
|
||||
rtcDevice *os.File //nolint:unused
|
||||
rtcLock *sync.Mutex
|
||||
|
@ -45,6 +47,7 @@ type TimeSyncOptions struct {
|
|||
PreCheckFunc func() (bool, error)
|
||||
Logger *zerolog.Logger
|
||||
NetworkConfig *network.NetworkConfig
|
||||
HttpUserAgent string
|
||||
}
|
||||
|
||||
type SyncMode struct {
|
||||
|
@ -71,6 +74,7 @@ func NewTimeSync(opts *TimeSyncOptions) *TimeSync {
|
|||
preCheckFunc: opts.PreCheckFunc,
|
||||
ntpServers: defaultNTPServers,
|
||||
httpUrls: defaultHTTPUrls,
|
||||
httpUserAgent: opts.HttpUserAgent,
|
||||
networkConfig: opts.NetworkConfig,
|
||||
}
|
||||
|
||||
|
|
|
@ -143,15 +143,21 @@ func (u *UsbGadget) listenKeyboardEvents() {
|
|||
default:
|
||||
l.Trace().Msg("reading from keyboard")
|
||||
if u.keyboardHidFile == nil {
|
||||
l.Error().Msg("keyboardHidFile is nil")
|
||||
u.logWithSupression("keyboardHidFileNil", 100, &l, nil, "keyboardHidFile is nil")
|
||||
// show the error every 100 times to avoid spamming the logs
|
||||
time.Sleep(time.Second)
|
||||
continue
|
||||
}
|
||||
// reset the counter
|
||||
u.resetLogSuppressionCounter("keyboardHidFileNil")
|
||||
|
||||
n, err := u.keyboardHidFile.Read(buf)
|
||||
if err != nil {
|
||||
l.Error().Err(err).Msg("failed to read")
|
||||
u.logWithSupression("keyboardHidFileRead", 100, &l, err, "failed to read")
|
||||
continue
|
||||
}
|
||||
u.resetLogSuppressionCounter("keyboardHidFileRead")
|
||||
|
||||
l.Trace().Int("n", n).Bytes("buf", buf).Msg("got data from keyboard")
|
||||
if n != 1 {
|
||||
l.Trace().Int("n", n).Msg("expected 1 byte, got")
|
||||
|
@ -195,12 +201,12 @@ func (u *UsbGadget) keyboardWriteHidFile(data []byte) error {
|
|||
|
||||
_, err := u.keyboardHidFile.Write(data)
|
||||
if err != nil {
|
||||
u.log.Error().Err(err).Msg("failed to write to hidg0")
|
||||
u.logWithSupression("keyboardWriteHidFile", 100, u.log, err, "failed to write to hidg0")
|
||||
u.keyboardHidFile.Close()
|
||||
u.keyboardHidFile = nil
|
||||
return err
|
||||
}
|
||||
|
||||
u.resetLogSuppressionCounter("keyboardWriteHidFile")
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -75,11 +75,12 @@ func (u *UsbGadget) absMouseWriteHidFile(data []byte) error {
|
|||
|
||||
_, err := u.absMouseHidFile.Write(data)
|
||||
if err != nil {
|
||||
u.log.Error().Err(err).Msg("failed to write to hidg1")
|
||||
u.logWithSupression("absMouseWriteHidFile", 100, u.log, err, "failed to write to hidg1")
|
||||
u.absMouseHidFile.Close()
|
||||
u.absMouseHidFile = nil
|
||||
return err
|
||||
}
|
||||
u.resetLogSuppressionCounter("absMouseWriteHidFile")
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -65,11 +65,12 @@ func (u *UsbGadget) relMouseWriteHidFile(data []byte) error {
|
|||
|
||||
_, err := u.relMouseHidFile.Write(data)
|
||||
if err != nil {
|
||||
u.log.Error().Err(err).Msg("failed to write to hidg2")
|
||||
u.logWithSupression("relMouseWriteHidFile", 100, u.log, err, "failed to write to hidg2")
|
||||
u.relMouseHidFile.Close()
|
||||
u.relMouseHidFile = nil
|
||||
return err
|
||||
}
|
||||
u.resetLogSuppressionCounter("relMouseWriteHidFile")
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -79,6 +79,8 @@ type UsbGadget struct {
|
|||
onKeyboardStateChange *func(state KeyboardState)
|
||||
|
||||
log *zerolog.Logger
|
||||
|
||||
logSuppressionCounter map[string]int
|
||||
}
|
||||
|
||||
const configFSPath = "/sys/kernel/config"
|
||||
|
@ -126,6 +128,8 @@ func newUsbGadget(name string, configMap map[string]gadgetConfigItem, enabledDev
|
|||
|
||||
strictMode: config.strictMode,
|
||||
|
||||
logSuppressionCounter: make(map[string]int),
|
||||
|
||||
absMouseAccumulatedWheelY: 0,
|
||||
}
|
||||
if err := g.Init(); err != nil {
|
||||
|
|
|
@ -6,6 +6,8 @@ import (
|
|||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
func joinPath(basePath string, paths []string) string {
|
||||
|
@ -78,3 +80,27 @@ func compareFileContent(oldContent []byte, newContent []byte, looserMatch bool)
|
|||
|
||||
return false
|
||||
}
|
||||
|
||||
func (u *UsbGadget) logWithSupression(counterName string, every int, logger *zerolog.Logger, err error, msg string, args ...interface{}) {
|
||||
if _, ok := u.logSuppressionCounter[counterName]; !ok {
|
||||
u.logSuppressionCounter[counterName] = 0
|
||||
} else {
|
||||
u.logSuppressionCounter[counterName]++
|
||||
}
|
||||
|
||||
l := logger.With().Int("counter", u.logSuppressionCounter[counterName]).Logger()
|
||||
|
||||
if u.logSuppressionCounter[counterName]%every == 0 {
|
||||
if err != nil {
|
||||
l.Error().Err(err).Msgf(msg, args...)
|
||||
} else {
|
||||
l.Error().Msgf(msg, args...)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (u *UsbGadget) resetLogSuppressionCounter(counterName string) {
|
||||
if _, ok := u.logSuppressionCounter[counterName]; !ok {
|
||||
u.logSuppressionCounter[counterName] = 0
|
||||
}
|
||||
}
|
||||
|
|
4
main.go
4
main.go
|
@ -2,6 +2,7 @@ package kvm
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
|
@ -12,6 +13,7 @@ import (
|
|||
)
|
||||
|
||||
var appCtx context.Context
|
||||
var httpUserAgent string = fmt.Sprintf("jetkvm-app/%s", builtAppVersion)
|
||||
|
||||
func Main() {
|
||||
LoadConfig()
|
||||
|
@ -25,6 +27,8 @@ func Main() {
|
|||
logger.Warn().Err(err).Msg("failed to get local version")
|
||||
}
|
||||
|
||||
httpUserAgent = fmt.Sprintf("jetkvm-app/%s jetkvm-system/%s", appVersionLocal, systemVersionLocal)
|
||||
|
||||
logger.Info().
|
||||
Interface("system_version", systemVersionLocal).
|
||||
Interface("app_version", appVersionLocal).
|
||||
|
|
3
ota.go
3
ota.go
|
@ -88,6 +88,7 @@ func fetchUpdateMetadata(ctx context.Context, deviceId string, includePreRelease
|
|||
if err != nil {
|
||||
return nil, fmt.Errorf("error creating request: %w", err)
|
||||
}
|
||||
req.Header.Set("User-Agent", httpUserAgent)
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
|
@ -131,7 +132,7 @@ func downloadFile(ctx context.Context, path string, url string, downloadProgress
|
|||
if err != nil {
|
||||
return fmt.Errorf("error creating request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Set("User-Agent", httpUserAgent)
|
||||
client := http.Client{
|
||||
Timeout: 10 * time.Minute,
|
||||
Transport: &http.Transport{
|
||||
|
|
Loading…
Reference in New Issue