From 8077bf912a74470b3780b1a23981cdf95bec6aa8 Mon Sep 17 00:00:00 2001
From: Eric <xplzv@126.com>
Date: Thu, 12 Sep 2019 17:10:47 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=85=8D=E7=BD=AE,=20?=
 =?UTF-8?q?=E5=B9=B6=E7=A7=BB=E9=99=A4=E6=97=A0=E6=95=88=E7=9A=84=E9=85=8D?=
 =?UTF-8?q?=E7=BD=AE=E9=A1=B9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 cmd/config_example.yml |  7 ------
 pkg/config/config.go   |  7 +++---
 pkg/koko/koko.go       |  2 +-
 pkg/koko/task.go       | 55 ++++++++++++++++++++----------------------
 4 files changed, 31 insertions(+), 40 deletions(-)

diff --git a/cmd/config_example.yml b/cmd/config_example.yml
index 8400b5c..9e52586 100644
--- a/cmd/config_example.yml
+++ b/cmd/config_example.yml
@@ -24,16 +24,9 @@ BOOTSTRAP_TOKEN: <PleasgeChangeSameWithJumpserver>
 # ACCESS KEY 保存的地址, 默认注册后会保存到该文件中
 # ACCESS_KEY_FILE: data/keys/.access_key
 
-# 加密密钥
-# SECRET_KEY: null
-
 # 设置日志级别 [DEBUG, INFO, WARN, ERROR, FATAL, CRITICAL]
 # LOG_LEVEL: INFO
 
-
-# 和Jumpserver 保持心跳时间间隔 (seconds)
-# HEARTBEAT_INTERVAL: 5
-
 # SSH连接超时时间 (default 15 seconds)
 # SSH_TIMEOUT: 15
 
diff --git a/pkg/config/config.go b/pkg/config/config.go
index 2bf10e0..a4b29c3 100644
--- a/pkg/config/config.go
+++ b/pkg/config/config.go
@@ -24,11 +24,11 @@ type Config struct {
 	SessionKeepDuration time.Duration          `json:"TERMINAL_SESSION_KEEP_DURATION"`
 	TelnetRegex         string                 `json:"TERMINAL_TELNET_REGEX"`
 	MaxIdleTime         time.Duration          `json:"SECURITY_MAX_IDLE_TIME"`
+	HeartbeatDuration   time.Duration          `json:"TERMINAL_HEARTBEAT_INTERVAL"`
 	SftpRoot            string                 `json:"TERMINAL_SFTP_ROOT" yaml:"SFTP_ROOT"`
 	ShowHiddenFile      bool                   `yaml:"SFTP_SHOW_HIDDEN_FILE"`
 	ReuseConnection     bool                   `yaml:"REUSE_CONNECTION"`
 	Name                string                 `yaml:"NAME"`
-	SecretKey           string                 `yaml:"SECRET_KEY"`
 	HostKeyFile         string                 `yaml:"HOST_KEY_FILE"`
 	CoreHost            string                 `yaml:"CORE_HOST"`
 	BootstrapToken      string                 `yaml:"BOOTSTRAP_TOKEN"`
@@ -39,7 +39,6 @@ type Config struct {
 	AccessKey           string                 `yaml:"ACCESS_KEY"`
 	AccessKeyFile       string                 `yaml:"ACCESS_KEY_FILE"`
 	LogLevel            string                 `yaml:"LOG_LEVEL"`
-	HeartbeatDuration   time.Duration          `yaml:"HEARTBEAT_INTERVAL"`
 	RootPath            string                 `yaml:"ROOT_PATH"`
 	Comment             string                 `yaml:"COMMENT"`
 	Language            string                 `yaml:"LANG"`
@@ -55,6 +54,9 @@ func (c *Config) EnsureConfigValid() {
 	if c.LanguageCode != "" && c.Language == "" {
 		c.Language = c.LanguageCode
 	}
+	if c.Language == ""{
+		c.Language = "zh"
+	}
 	// 确保至少有一个认证
 	if !c.PublicKeyAuth && !c.PasswordAuth {
 		c.PasswordAuth = true
@@ -129,7 +131,6 @@ var Conf = &Config{
 	HostKey:            "",
 	RootPath:           rootPath,
 	Comment:            "Coco",
-	Language:           "zh",
 	ReplayStorage:      map[string]interface{}{"TYPE": "server"},
 	CommandStorage:     map[string]interface{}{"TYPE": "server"},
 	UploadFailedReplay: true,
diff --git a/pkg/koko/koko.go b/pkg/koko/koko.go
index 8461e54..7d90bbb 100644
--- a/pkg/koko/koko.go
+++ b/pkg/koko/koko.go
@@ -31,7 +31,7 @@ func (c *Coco) Start() {
 func (c *Coco) Stop() {
 	sshd.StopServer()
 	httpd.StopHTTPServer()
-	logger.Debug("Quit The Coco")
+	logger.Info("Quit The Coco")
 }
 
 func RunForever() {
diff --git a/pkg/koko/task.go b/pkg/koko/task.go
index f3db362..002e0cf 100644
--- a/pkg/koko/task.go
+++ b/pkg/koko/task.go
@@ -19,7 +19,7 @@ func Initial() {
 		go uploadRemainReplay(conf.RootPath)
 	}
 
-	go keepHeartbeat(conf.HeartbeatDuration)
+	go keepHeartbeat()
 }
 
 // uploadRemainReplay 上传遗留的录像
@@ -37,7 +37,7 @@ func uploadRemainReplay(rootPath string) {
 		}
 		var sid string
 		filename := info.Name()
-		if len(filename) == 36{
+		if len(filename) == 36 {
 			sid = filename
 		}
 		if strings.HasSuffix(filename, ".replay.gz") {
@@ -46,8 +46,8 @@ func uploadRemainReplay(rootPath string) {
 				sid = sidName
 			}
 		}
-		if sid != ""{
-			data := map[string]interface{}{"id":sid,"date_end":info.ModTime().UTC().Format(
+		if sid != "" {
+			data := map[string]interface{}{"id": sid, "date_end": info.ModTime().UTC().Format(
 				"2006-01-02 15:04:05 +0000")}
 			service.FinishSession(data)
 			allRemainFiles[sid] = path
@@ -56,21 +56,21 @@ func uploadRemainReplay(rootPath string) {
 		return nil
 	})
 
-	for sid, path := range allRemainFiles{
+	for sid, path := range allRemainFiles {
 		var absGzPath string
 		if strings.HasSuffix(path, ".replay.gz") {
 			absGzPath = path
-		}else if strings.HasSuffix(path, sid)  {
-			if err := ValidateRemainReplayFile(path); err != nil{
+		} else if strings.HasSuffix(path, sid) {
+			if err := ValidateRemainReplayFile(path); err != nil {
 				continue
 			}
 			absGzPath = path + ".replay.gz"
-			if err := common.GzipCompressFile(path,absGzPath); err != nil{
+			if err := common.GzipCompressFile(path, absGzPath); err != nil {
 				continue
 			}
 			_ = os.Remove(path)
 		}
-		relayRecord := &proxy.ReplyRecorder{SessionID:sid}
+		relayRecord := &proxy.ReplyRecorder{SessionID: sid}
 		relayRecord.AbsGzFilePath = absGzPath
 		relayRecord.Target, _ = filepath.Rel(path, rootPath)
 		relayRecord.UploadGzipFile(3)
@@ -79,44 +79,41 @@ func uploadRemainReplay(rootPath string) {
 }
 
 // keepHeartbeat 保持心跳
-func keepHeartbeat(interval time.Duration) {
-	tick := time.Tick(interval * time.Second)
+func keepHeartbeat() {
 	for {
-		select {
-		case <-tick:
-			data := proxy.GetAliveSessions()
-			tasks := service.TerminalHeartBeat(data)
-			if len(tasks) != 0 {
-				for _, task := range tasks {
-					proxy.HandleSessionTask(task)
-				}
+		time.Sleep(config.GetConf().HeartbeatDuration * time.Second)
+		data := proxy.GetAliveSessions()
+		tasks := service.TerminalHeartBeat(data)
+		if len(tasks) != 0 {
+			for _, task := range tasks {
+				proxy.HandleSessionTask(task)
 			}
 		}
 	}
 }
 
-func ValidateRemainReplayFile(path string) error{
-	f, err := os.OpenFile(path, os.O_RDWR|os.O_APPEND,os.ModePerm)
-	if err != nil{
+func ValidateRemainReplayFile(path string) error {
+	f, err := os.OpenFile(path, os.O_RDWR|os.O_APPEND, os.ModePerm)
+	if err != nil {
 		return err
 	}
 	defer f.Close()
-	tmp := make([]byte,1)
-	_, err = f.Seek(-1,2)
-	if err != nil{
+	tmp := make([]byte, 1)
+	_, err = f.Seek(-1, 2)
+	if err != nil {
 		return err
 	}
 	_, err = f.Read(tmp)
-	if err != nil{
+	if err != nil {
 		return err
 	}
 	switch string(tmp) {
 	case "}":
 		return nil
 	case ",":
-		_,err = f.Write([]byte(`"0":""}`))
+		_, err = f.Write([]byte(`"0":""}`))
 	default:
-		_,err = f.Write([]byte(`}`))
+		_, err = f.Write([]byte(`}`))
 	}
 	return err
-}
\ No newline at end of file
+}
-- 
2.18.0