From 7d968348f5efd71b2faea6f8763fb9e275d5d576 Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Thu, 1 Jun 2023 18:27:53 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20ssh=20=E7=99=BB=E5=BD=95=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E5=BD=92=E5=B1=9E=E5=9C=B0=E8=8E=B7=E5=8F=96=E6=96=B9?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E6=94=B9=20(#1221)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + backend/app/dto/ssh.go | 3 +- backend/app/service/ssh.go | 37 ++++++++----------- cmd/server/docs/docs.go | 9 +++-- cmd/server/docs/swagger.json | 9 +++-- cmd/server/docs/swagger.yaml | 6 ++- frontend/src/api/interface/host.ts | 3 +- frontend/src/views/host/ssh/log/log.vue | 4 +- .../src/views/host/ssh/ssh/address/index.vue | 6 +-- frontend/src/views/host/ssh/ssh/index.vue | 17 +++++++-- 10 files changed, 52 insertions(+), 43 deletions(-) diff --git a/.gitignore b/.gitignore index 05c220fd1..c475be831 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,4 @@ dist/ 1panel.service install.sh docker.sh +cmd/server/web/.DS_Store diff --git a/backend/app/dto/ssh.go b/backend/app/dto/ssh.go index 6686e031b..15eb4926d 100644 --- a/backend/app/dto/ssh.go +++ b/backend/app/dto/ssh.go @@ -4,6 +4,7 @@ import "time" type SSHInfo struct { Status string `json:"status"` + Message string `json:"message"` Port string `json:"port"` ListenAddress string `json:"listenAddress"` PasswordAuthentication string `json:"passwordAuthentication"` @@ -38,7 +39,7 @@ type SSHLog struct { type SSHHistory struct { Date time.Time `json:"date"` DateStr string `json:"dateStr"` - IsLocal bool `json:"isLocal"` + Area string `json:"area"` User string `json:"user"` AuthMode string `json:"authMode"` Address string `json:"address"` diff --git a/backend/app/service/ssh.go b/backend/app/service/ssh.go index af6e3fb78..1c8853108 100644 --- a/backend/app/service/ssh.go +++ b/backend/app/service/ssh.go @@ -2,7 +2,6 @@ package service import ( "fmt" - "net" "os" "os/user" "path" @@ -13,9 +12,11 @@ import ( "github.com/1Panel-dev/1Panel/backend/app/dto" "github.com/1Panel-dev/1Panel/backend/constant" + "github.com/1Panel-dev/1Panel/backend/global" "github.com/1Panel-dev/1Panel/backend/utils/cmd" "github.com/1Panel-dev/1Panel/backend/utils/common" "github.com/1Panel-dev/1Panel/backend/utils/files" + "github.com/1Panel-dev/1Panel/backend/utils/qqwry" ) const sshPath = "/etc/ssh/sshd_config" @@ -38,7 +39,8 @@ func NewISSHService() ISSHService { func (u *SSHService) GetSSHInfo() (*dto.SSHInfo, error) { data := dto.SSHInfo{ - Status: constant.StatusDisable, + Status: constant.StatusEnable, + Message: "", Port: "22", ListenAddress: "0.0.0.0", PasswordAuthentication: "yes", @@ -49,7 +51,8 @@ func (u *SSHService) GetSSHInfo() (*dto.SSHInfo, error) { sudo := cmd.SudoHandleCmd() stdout, err := cmd.Execf("%s systemctl status sshd", sudo) if err != nil { - return &data, nil + data.Message = stdout + data.Status = constant.StatusDisable } stdLines := strings.Split(stdout, "\n") for _, stdline := range stdLines { @@ -58,12 +61,10 @@ func (u *SSHService) GetSSHInfo() (*dto.SSHInfo, error) { break } } - if data.Status == constant.StatusDisable { - return &data, nil - } sshConf, err := os.ReadFile(sshPath) if err != nil { - return &data, err + data.Message = err.Error() + data.Status = constant.StatusDisable } lines := strings.Split(string(sshConf), "\n") for _, line := range lines { @@ -86,7 +87,7 @@ func (u *SSHService) GetSSHInfo() (*dto.SSHInfo, error) { data.UseDNS = strings.ReplaceAll(line, "UseDNS ", "") } } - return &data, err + return &data, nil } func (u *SSHService) OperateSSH(operation string) error { @@ -254,8 +255,13 @@ func (u *SSHService) LoadLog(req dto.SearchSSHLog) (*dto.SSHLog, error) { timeNow := time.Now() nyc, _ := time.LoadLocation(common.LoadTimeZone()) + + qqWry, err := qqwry.NewQQwry() + if err != nil { + global.LOG.Errorf("load qqwry datas failed: %s", err) + } for i := 0; i < len(data.Logs); i++ { - data.Logs[i].IsLocal = isPrivateIP(net.ParseIP(data.Logs[i].Address)) + data.Logs[i].Area = qqWry.Find(data.Logs[i].Address).Area data.Logs[i].Date, _ = time.ParseInLocation("2006 Jan 2 15:04:05", fmt.Sprintf("%d %s", timeNow.Year(), data.Logs[i].DateStr), nyc) if data.Logs[i].Date.After(timeNow) { data.Logs[i].Date = data.Logs[i].Date.AddDate(-1, 0, 0) @@ -394,16 +400,3 @@ func handleGunzip(path string) error { } return nil } -func isPrivateIP(ip net.IP) bool { - if ip4 := ip.To4(); ip4 != nil { - switch true { - case ip4[0] == 10: - return true - case ip4[0] == 172 && ip4[1] >= 16 && ip4[1] <= 31: - return true - case ip4[0] == 192 && ip4[1] == 168: - return true - } - } - return false -} diff --git a/cmd/server/docs/docs.go b/cmd/server/docs/docs.go index fde7fefd1..4670f0d06 100644 --- a/cmd/server/docs/docs.go +++ b/cmd/server/docs/docs.go @@ -12347,6 +12347,9 @@ var doc = `{ "dto.SSHHistory": { "type": "object", "properties": { + "Area": { + "type": "string" + }, "address": { "type": "string" }, @@ -12359,9 +12362,6 @@ var doc = `{ "dateStr": { "type": "string" }, - "isLocal": { - "type": "boolean" - }, "message": { "type": "string" }, @@ -12382,6 +12382,9 @@ var doc = `{ "listenAddress": { "type": "string" }, + "message": { + "type": "string" + }, "passwordAuthentication": { "type": "string" }, diff --git a/cmd/server/docs/swagger.json b/cmd/server/docs/swagger.json index e1a17b870..e7671c0b3 100644 --- a/cmd/server/docs/swagger.json +++ b/cmd/server/docs/swagger.json @@ -12333,6 +12333,9 @@ "dto.SSHHistory": { "type": "object", "properties": { + "Area": { + "type": "string" + }, "address": { "type": "string" }, @@ -12345,9 +12348,6 @@ "dateStr": { "type": "string" }, - "isLocal": { - "type": "boolean" - }, "message": { "type": "string" }, @@ -12368,6 +12368,9 @@ "listenAddress": { "type": "string" }, + "message": { + "type": "string" + }, "passwordAuthentication": { "type": "string" }, diff --git a/cmd/server/docs/swagger.yaml b/cmd/server/docs/swagger.yaml index 551e260cf..81be0fce5 100644 --- a/cmd/server/docs/swagger.yaml +++ b/cmd/server/docs/swagger.yaml @@ -1437,6 +1437,8 @@ definitions: type: object dto.SSHHistory: properties: + Area: + type: string address: type: string authMode: @@ -1445,8 +1447,6 @@ definitions: type: string dateStr: type: string - isLocal: - type: boolean message: type: string port: @@ -1460,6 +1460,8 @@ definitions: properties: listenAddress: type: string + message: + type: string passwordAuthentication: type: string permitRootLogin: diff --git a/frontend/src/api/interface/host.ts b/frontend/src/api/interface/host.ts index 68e0ad117..deeb8e118 100644 --- a/frontend/src/api/interface/host.ts +++ b/frontend/src/api/interface/host.ts @@ -106,6 +106,7 @@ export namespace Host { export interface SSHInfo { status: string; + message: string; port: string; listenAddress: string; passwordAuthentication: string; @@ -130,7 +131,7 @@ export namespace Host { } export interface sshHistory { date: Date; - isLocal: boolean; + area: string; user: string; authMode: string; address: string; diff --git a/frontend/src/views/host/ssh/log/log.vue b/frontend/src/views/host/ssh/log/log.vue index 9cc6e881c..994aba5e0 100644 --- a/frontend/src/views/host/ssh/log/log.vue +++ b/frontend/src/views/host/ssh/log/log.vue @@ -37,9 +37,7 @@