1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-03-14 01:34:47 +08:00

fix: 解决时间同步错误的问题

This commit is contained in:
ssongliu 2022-12-22 18:06:49 +08:00 committed by ssongliu
parent 26eacf81b7
commit 1e46d6359b
5 changed files with 20 additions and 35 deletions

View File

@ -92,13 +92,14 @@ func (b *BaseApi) SyncTime(c *gin.Context) {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return return
} }
ts := ntime.Format(timeLayoutStr) ts := ntime.Format(timeLayoutStr)
if err := ntp.UpdateSystemDate(ts); err != nil { if err := ntp.UpdateSystemDate(ts); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return return
} }
helper.SuccessWithData(c, ts) helper.SuccessWithData(c, ntime.String())
} }
func (b *BaseApi) CleanMonitor(c *gin.Context) { func (b *BaseApi) CleanMonitor(c *gin.Context) {

View File

@ -4,23 +4,23 @@ type SettingInfo struct {
UserName string `json:"userName"` UserName string `json:"userName"`
Email string `json:"email"` Email string `json:"email"`
SessionTimeout int `json:"sessionTimeout"` SessionTimeout string `json:"sessionTimeout"`
LocalTime string `json:"localTime"` LocalTime string `json:"localTime"`
PanelName string `json:"panelName"` PanelName string `json:"panelName"`
Theme string `json:"theme"` Theme string `json:"theme"`
Language string `json:"language"` Language string `json:"language"`
ServerPort int `json:"serverPort"` ServerPort string `json:"serverPort"`
SecurityEntrance string `json:"securityEntrance"` SecurityEntrance string `json:"securityEntrance"`
ExpirationDays int `json:"expirationDays"` ExpirationDays string `json:"expirationDays"`
ExpirationTime string `json:"expirationTime"` ExpirationTime string `json:"expirationTime"`
ComplexityVerification string `json:"complexityVerification"` ComplexityVerification string `json:"complexityVerification"`
MFAStatus string `json:"mfaStatus"` MFAStatus string `json:"mfaStatus"`
MFASecret string `json:"mfaSecret"` MFASecret string `json:"mfaSecret"`
MonitorStatus string `json:"monitorStatus"` MonitorStatus string `json:"monitorStatus"`
MonitorStoreDays int `json:"monitorStoreDays"` MonitorStoreDays string `json:"monitorStoreDays"`
MessageType string `json:"messageType"` MessageType string `json:"messageType"`
EmailVars string `json:"emailVars"` EmailVars string `json:"emailVars"`

View File

@ -40,12 +40,10 @@ func (u *SettingService) GetSettingInfo() (*dto.SettingInfo, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
_ = json.Unmarshal(arr, &info) if err := json.Unmarshal(arr, &info); err != nil {
info.MonitorStoreDays, _ = strconv.Atoi(settingMap["MonitorStoreDays"]) return nil, err
info.ServerPort, _ = strconv.Atoi(settingMap["ServerPort"]) }
info.SessionTimeout, _ = strconv.Atoi(settingMap["SessionTimeout"]) info.LocalTime = time.Now().String()
info.ExpirationDays, _ = strconv.Atoi(settingMap["ExpirationDays"])
info.LocalTime = time.Now().Format("2006-01-02 15:04:05")
return &info, err return &info, err
} }

View File

@ -2,7 +2,6 @@ package ntp
import ( import (
"encoding/binary" "encoding/binary"
"flag"
"fmt" "fmt"
"net" "net"
"runtime" "runtime"
@ -31,29 +30,25 @@ type packet struct {
TxTimeFrac uint32 TxTimeFrac uint32
} }
func Getremotetime() (*time.Time, error) { func Getremotetime() (time.Time, error) {
var host string conn, err := net.Dial("udp", "ntp.aliyun.com:123")
flag.StringVar(&host, "e", "ntp1.aliyun.com:123", "NTP host")
flag.Parse()
conn, err := net.Dial("udp", host)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to connect: %v", err) return time.Time{}, fmt.Errorf("failed to connect: %v", err)
} }
defer conn.Close() defer conn.Close()
if err := conn.SetDeadline(time.Now().Add(15 * time.Second)); err != nil { if err := conn.SetDeadline(time.Now().Add(15 * time.Second)); err != nil {
return nil, fmt.Errorf("failed to set deadline: %v", err) return time.Time{}, fmt.Errorf("failed to set deadline: %v", err)
} }
req := &packet{Settings: 0x1B} req := &packet{Settings: 0x1B}
if err := binary.Write(conn, binary.BigEndian, req); err != nil { if err := binary.Write(conn, binary.BigEndian, req); err != nil {
return nil, fmt.Errorf("failed to set request: %v", err) return time.Time{}, fmt.Errorf("failed to set request: %v", err)
} }
rsp := &packet{} rsp := &packet{}
if err := binary.Read(conn, binary.BigEndian, rsp); err != nil { if err := binary.Read(conn, binary.BigEndian, rsp); err != nil {
return nil, fmt.Errorf("failed to read server response: %v", err) return time.Time{}, fmt.Errorf("failed to read server response: %v", err)
} }
secs := float64(rsp.TxTimeSec) - ntpEpochOffset secs := float64(rsp.TxTimeSec) - ntpEpochOffset
@ -61,7 +56,7 @@ func Getremotetime() (*time.Time, error) {
showtime := time.Unix(int64(secs), nanos) showtime := time.Unix(int64(secs), nanos)
return &showtime, nil return showtime, nil
} }
func UpdateSystemDate(dateTime string) error { func UpdateSystemDate(dateTime string) error {

View File

@ -12,10 +12,10 @@ const settingRouter = {
children: [ children: [
{ {
path: '/setting', path: '/setting',
name: 'Setting', name: 'Panel',
component: () => import('@/views/setting/index.vue'), component: () => import('@/views/setting/panel/index.vue'),
hidden: true,
meta: { meta: {
requiresAuth: true,
key: 'Setting', key: 'Setting',
}, },
}, },
@ -46,15 +46,6 @@ const settingRouter = {
key: 'Setting', key: 'Setting',
}, },
}, },
{
path: '/setting/panel',
name: 'Panel',
component: () => import('@/views/setting/panel/index.vue'),
hidden: true,
meta: {
key: 'Setting',
},
},
{ {
path: '/setting/safe', path: '/setting/safe',
name: 'Safe', name: 'Safe',