Commit d4c7be16 authored by ibuler's avatar ibuler

[Update] 修改日志

parent 96ee0fb8
...@@ -182,6 +182,14 @@ ...@@ -182,6 +182,14 @@
pruneopts = "UT" pruneopts = "UT"
revision = "2d0786266e9cd132da844109aa05016f11f3df28" revision = "2d0786266e9cd132da844109aa05016f11f3df28"
[[projects]]
digest = "1:c805e517269b0ba4c21ded5836019ed7d16953d4026cb7d00041d039c7906be9"
name = "gopkg.in/natefinch/lumberjack.v2"
packages = ["."]
pruneopts = "UT"
revision = "a96e63847dc3c67d17befa69c303767e2f84e54f"
version = "v2.1"
[[projects]] [[projects]]
digest = "1:4d2e5a73dc1500038e504a8d78b986630e3626dc027bc030ba5c75da257cdb96" digest = "1:4d2e5a73dc1500038e504a8d78b986630e3626dc027bc030ba5c75da257cdb96"
name = "gopkg.in/yaml.v2" name = "gopkg.in/yaml.v2"
...@@ -207,6 +215,7 @@ ...@@ -207,6 +215,7 @@
"github.com/xlab/treeprint", "github.com/xlab/treeprint",
"golang.org/x/crypto/ssh", "golang.org/x/crypto/ssh",
"golang.org/x/crypto/ssh/terminal", "golang.org/x/crypto/ssh/terminal",
"gopkg.in/natefinch/lumberjack.v2",
"gopkg.in/yaml.v2", "gopkg.in/yaml.v2",
] ]
solver-name = "gps-cdcl" solver-name = "gps-cdcl"
......
...@@ -70,3 +70,7 @@ ...@@ -70,3 +70,7 @@
go-tests = true go-tests = true
unused-packages = true unused-packages = true
[[constraint]]
name = "gopkg.in/natefinch/lumberjack.v2"
version = "2.1.0"
package main package main
import ( import (
"cocogo/pkg/config" "cocogo/pkg/coco"
"cocogo/pkg/service"
"cocogo/pkg/sshd"
) )
func init() {
config.Initial()
}
func main() { func main() {
service.Initial() app := &coco.Coco{}
sshd.StartServer() app.Start()
} }
NAME: coco2 NAME: coco2
BOOTSTRAP_TOKEN: PleaseChangeMe BOOTSTRAP_TOKEN: PleaseChangeMe
CORE_HOST: http://127.0.0.1:8080 CORE_HOST: http://127.0.0.1:8080
LOG_LEVEL: INFO
package coco
import (
"fmt"
"time"
"cocogo/pkg/sshd"
)
const version = "1.4.0"
type Coco struct {
}
func (c *Coco) Start() {
fmt.Println(time.Now().Format("2006-01-02 15:04:05"))
fmt.Printf("Coco version %s, more see https://www.jumpserver.org\n", version)
fmt.Println("Quit the server with CONTROL-C.")
sshd.StartServer()
}
func (c *Coco) Stop() {
}
package coco
import (
"cocogo/pkg/config"
"cocogo/pkg/logger"
"cocogo/pkg/service"
)
func init() {
config.Initial()
logger.Initial()
service.Initial()
}
package config package config
import ( import (
"cocogo/pkg/logger"
"encoding/json" "encoding/json"
"io/ioutil" "io/ioutil"
"log"
"os" "os"
"strings" "strings"
"sync" "sync"
"time" "time"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
) )
...@@ -54,7 +53,7 @@ func (c *Config) LoadFromYAML(body []byte) error { ...@@ -54,7 +53,7 @@ func (c *Config) LoadFromYAML(body []byte) error {
defer c.mux.Unlock() defer c.mux.Unlock()
err := yaml.Unmarshal(body, c) err := yaml.Unmarshal(body, c)
if err != nil { if err != nil {
log.Errorf("Load yaml error: %v", err) log.Printf("Load yaml error: %v", err)
} }
return err return err
} }
...@@ -62,8 +61,7 @@ func (c *Config) LoadFromYAML(body []byte) error { ...@@ -62,8 +61,7 @@ func (c *Config) LoadFromYAML(body []byte) error {
func (c *Config) LoadFromYAMLPath(filepath string) error { func (c *Config) LoadFromYAMLPath(filepath string) error {
body, err := ioutil.ReadFile(filepath) body, err := ioutil.ReadFile(filepath)
if err != nil { if err != nil {
log.Errorf("Not found file: %s", filepath) log.Printf("Not found file: %s", filepath)
os.Exit(1)
} }
return c.LoadFromYAML(body) return c.LoadFromYAML(body)
} }
...@@ -73,8 +71,7 @@ func (c *Config) LoadFromJSON(body []byte) error { ...@@ -73,8 +71,7 @@ func (c *Config) LoadFromJSON(body []byte) error {
defer c.mux.Unlock() defer c.mux.Unlock()
err := json.Unmarshal(body, c) err := json.Unmarshal(body, c)
if err != nil { if err != nil {
logger.Error("Config load yaml error") log.Printf("Config load yaml error")
os.Exit(1)
} }
return nil return nil
} }
...@@ -88,7 +85,7 @@ func (c *Config) LoadFromEnv() error { ...@@ -88,7 +85,7 @@ func (c *Config) LoadFromEnv() error {
} }
envYAML, err := yaml.Marshal(envMap) envYAML, err := yaml.Marshal(envMap)
if err != nil { if err != nil {
log.Errorf("Error occur: %v", err) log.Fatalf("Error occur: %v", err)
} }
return c.LoadFromYAML(envYAML) return c.LoadFromYAML(envYAML)
} }
......
...@@ -41,14 +41,14 @@ func SessionHandler(sess ssh.Session) { ...@@ -41,14 +41,14 @@ func SessionHandler(sess ssh.Session) {
} }
} }
func newInteractiveHandler(sess ssh.Session, user *model.User) *InteractiveHandler { func newInteractiveHandler(sess ssh.Session, user *model.User) *interactiveHandler {
term := terminal.NewTerminal(sess, "Opt> ") term := terminal.NewTerminal(sess, "Opt> ")
handler := &InteractiveHandler{sess: sess, user: user, term: term} handler := &interactiveHandler{sess: sess, user: user, term: term}
handler.Initial() handler.Initial()
return handler return handler
} }
type InteractiveHandler struct { type interactiveHandler struct {
sess ssh.Session sess ssh.Session
user *model.User user *model.User
term *terminal.Terminal term *terminal.Terminal
...@@ -61,17 +61,17 @@ type InteractiveHandler struct { ...@@ -61,17 +61,17 @@ type InteractiveHandler struct {
mu *sync.RWMutex mu *sync.RWMutex
} }
func (h *InteractiveHandler) Initial() { func (h *interactiveHandler) Initial() {
h.displayBanner() h.displayBanner()
h.loadUserAssets() h.loadUserAssets()
h.loadUserAssetNodes() h.loadUserAssetNodes()
} }
func (h *InteractiveHandler) displayBanner() { func (h *interactiveHandler) displayBanner() {
displayBanner(h.sess, h.user.Name) displayBanner(h.sess, h.user.Name)
} }
func (h *InteractiveHandler) watchWinSizeChange(winCh <-chan ssh.Window, done <-chan struct{}) { func (h *interactiveHandler) watchWinSizeChange(winCh <-chan ssh.Window, done <-chan struct{}) {
for { for {
select { select {
case <-done: case <-done:
...@@ -81,13 +81,13 @@ func (h *InteractiveHandler) watchWinSizeChange(winCh <-chan ssh.Window, done <- ...@@ -81,13 +81,13 @@ func (h *InteractiveHandler) watchWinSizeChange(winCh <-chan ssh.Window, done <-
if !ok { if !ok {
return return
} }
logger.Debugf("Term change: %d*%d", win.Height, win.Width) logger.Debugf("Term window size change: %d*%d", win.Height, win.Width)
_ = h.term.SetSize(win.Width, win.Height) _ = h.term.SetSize(win.Width, win.Height)
} }
} }
} }
func (h *InteractiveHandler) Dispatch(ctx cctx.Context) { func (h *interactiveHandler) Dispatch(ctx cctx.Context) {
_, winCh, _ := h.sess.Pty() _, winCh, _ := h.sess.Pty()
for { for {
doneChan := make(chan struct{}) doneChan := make(chan struct{})
...@@ -149,7 +149,7 @@ func (h *InteractiveHandler) Dispatch(ctx cctx.Context) { ...@@ -149,7 +149,7 @@ func (h *InteractiveHandler) Dispatch(ctx cctx.Context) {
} }
} }
func (h *InteractiveHandler) chooseSystemUser(systemUsers []model.SystemUser) model.SystemUser { func (h *interactiveHandler) chooseSystemUser(systemUsers []model.SystemUser) model.SystemUser {
table := tablewriter.NewWriter(h.sess) table := tablewriter.NewWriter(h.sess)
table.SetHeader([]string{"ID", "Username"}) table.SetHeader([]string{"ID", "Username"})
for i := 0; i < len(systemUsers); i++ { for i := 0; i < len(systemUsers); i++ {
...@@ -175,7 +175,7 @@ func (h *InteractiveHandler) chooseSystemUser(systemUsers []model.SystemUser) mo ...@@ -175,7 +175,7 @@ func (h *InteractiveHandler) chooseSystemUser(systemUsers []model.SystemUser) mo
} }
// 当资产的数量为1的时候,就进行代理转化 // 当资产的数量为1的时候,就进行代理转化
func (h *InteractiveHandler) displayAssetsOrProxy(assets []model.Asset) { func (h *interactiveHandler) displayAssetsOrProxy(assets []model.Asset) {
//if len(assets) == 1 { //if len(assets) == 1 {
// var systemUser model.SystemUser // var systemUser model.SystemUser
// switch len(assets[0].SystemUsers) { // switch len(assets[0].SystemUsers) {
...@@ -208,7 +208,7 @@ func (h *InteractiveHandler) displayAssetsOrProxy(assets []model.Asset) { ...@@ -208,7 +208,7 @@ func (h *InteractiveHandler) displayAssetsOrProxy(assets []model.Asset) {
//} //}
} }
func (h *InteractiveHandler) displayAssets(assets model.AssetList) { func (h *interactiveHandler) displayAssets(assets model.AssetList) {
if len(assets) == 0 { if len(assets) == 0 {
_, _ = io.WriteString(h.sess, "\r\n No Assets\r\n\r") _, _ = io.WriteString(h.sess, "\r\n No Assets\r\n\r")
} else { } else {
...@@ -229,7 +229,7 @@ func (h *InteractiveHandler) displayAssets(assets model.AssetList) { ...@@ -229,7 +229,7 @@ func (h *InteractiveHandler) displayAssets(assets model.AssetList) {
} }
func (h *InteractiveHandler) displayNodes(nodes []model.Node) { func (h *interactiveHandler) displayNodes(nodes []model.Node) {
tree := ConstructAssetNodeTree(nodes) tree := ConstructAssetNodeTree(nodes)
tipHeaderMsg := "\r\nNode: [ ID.Name(Asset amount) ]" tipHeaderMsg := "\r\nNode: [ ID.Name(Asset amount) ]"
tipEndMsg := "Tips: Enter g+NodeID to display the host under the node, such as g1\r\n\r" tipEndMsg := "Tips: Enter g+NodeID to display the host under the node, such as g1\r\n\r"
...@@ -243,26 +243,26 @@ func (h *InteractiveHandler) displayNodes(nodes []model.Node) { ...@@ -243,26 +243,26 @@ func (h *InteractiveHandler) displayNodes(nodes []model.Node) {
} }
func (h *InteractiveHandler) refreshAssetsAndNodesData() { func (h *interactiveHandler) refreshAssetsAndNodesData() {
_, err := io.WriteString(h.sess, "Refresh done\r\n") _, err := io.WriteString(h.sess, "Refresh done\r\n")
if err != nil { if err != nil {
logger.Error("refresh Assets Nodes err:", err) logger.Error("refresh Assets Nodes err:", err)
} }
} }
func (h *InteractiveHandler) loadUserAssets() { func (h *interactiveHandler) loadUserAssets() {
h.assets = service.GetUserAssets(h.user.ID, "1") h.assets = service.GetUserAssets(h.user.ID, "1")
} }
func (h *InteractiveHandler) loadUserAssetNodes() { func (h *interactiveHandler) loadUserAssetNodes() {
h.nodes = service.GetUserNodes(h.user.ID, "1") h.nodes = service.GetUserNodes(h.user.ID, "1")
} }
func (h *InteractiveHandler) changeLanguage() { func (h *interactiveHandler) changeLanguage() {
} }
func (h *InteractiveHandler) JoinShareRoom(roomID string) { func (h *interactiveHandler) JoinShareRoom(roomID string) {
//sshConn := userhome.NewSSHConn(h.sess) //sshConn := userhome.NewSSHConn(h.sess)
//ctx, cancelFuc := context.WithCancel(h.sess.Context()) //ctx, cancelFuc := context.WithCancel(h.sess.Context())
// //
...@@ -286,7 +286,7 @@ func (h *InteractiveHandler) JoinShareRoom(roomID string) { ...@@ -286,7 +286,7 @@ func (h *InteractiveHandler) JoinShareRoom(roomID string) {
} }
func (h *InteractiveHandler) searchAsset(key string) (assets []model.Asset) { func (h *interactiveHandler) searchAsset(key string) (assets []model.Asset) {
//if indexNum, err := strconv.Atoi(key); err == nil { //if indexNum, err := strconv.Atoi(key); err == nil {
// if indexNum > 0 && indexNum <= len(h.searchResult) { // if indexNum > 0 && indexNum <= len(h.searchResult) {
// return []model.Asset{h.searchResult[indexNum-1]} // return []model.Asset{h.searchResult[indexNum-1]}
...@@ -311,7 +311,7 @@ func (h *InteractiveHandler) searchAsset(key string) (assets []model.Asset) { ...@@ -311,7 +311,7 @@ func (h *InteractiveHandler) searchAsset(key string) (assets []model.Asset) {
return assets return assets
} }
func (h *InteractiveHandler) searchNodeAssets(num int) (assets []model.Asset) { func (h *interactiveHandler) searchNodeAssets(num int) (assets []model.Asset) {
//var assetNodesData []model.Node //var assetNodesData []model.Node
//if assetNodes, ok := h.assetData.Load(AssetNodesMapKey); ok { //if assetNodes, ok := h.assetData.Load(AssetNodesMapKey); ok {
// assetNodesData = assetNodes.([]model.Node) // assetNodesData = assetNodes.([]model.Node)
...@@ -324,7 +324,7 @@ func (h *InteractiveHandler) searchNodeAssets(num int) (assets []model.Asset) { ...@@ -324,7 +324,7 @@ func (h *InteractiveHandler) searchNodeAssets(num int) (assets []model.Asset) {
} }
func (h *InteractiveHandler) Proxy(ctx context.Context) { func (h *interactiveHandler) Proxy(ctx context.Context) {
h.assetSelect = &model.Asset{Hostname: "centos", Port: 22, Ip: "192.168.244.185", Protocol: "ssh"} h.assetSelect = &model.Asset{Hostname: "centos", Port: 22, Ip: "192.168.244.185", Protocol: "ssh"}
h.systemUserSelect = &model.SystemUser{Id: "5dd8b5a0-8cdb-4857-8629-faf811c525e1", Name: "web", Username: "root", Password: "redhat", Protocol: "telnet"} h.systemUserSelect = &model.SystemUser{Id: "5dd8b5a0-8cdb-4857-8629-faf811c525e1", Name: "web", Username: "root", Password: "redhat", Protocol: "telnet"}
......
/*
Using https://github.com/t-tomalak/logrus-easy-formatter/ as formatter
*/
package logger
import (
"strings"
"time"
"github.com/sirupsen/logrus"
)
const (
// Default log format will output [INFO]: 2006-01-02T15:04:05Z07:00 - Log message
defaultLogFormat = "[%lvl%]: %time% - %msg%"
defaultTimestampFormat = time.RFC3339
)
// Formatter implements logrus.Formatter interface.
type Formatter struct {
// Timestamp format
TimestampFormat string
// Available standard keys: time, msg, lvl
// Also can include custom fields but limited to strings.
// All of fields need to be wrapped inside %% i.e %time% %msg%
LogFormat string
// Disables the truncation of the level text to 4 characters.
DisableLevelTruncation bool
}
// Format building log message.
func (f *Formatter) Format(entry *logrus.Entry) ([]byte, error) {
output := f.LogFormat
if output == "" {
output = defaultLogFormat
}
timestampFormat := f.TimestampFormat
if timestampFormat == "" {
timestampFormat = defaultTimestampFormat
}
output = strings.Replace(output, "%time%", entry.Time.Format(timestampFormat), 1)
output = strings.Replace(output, "%msg%", entry.Message, 1)
level := strings.ToUpper(entry.Level.String())
if !f.DisableLevelTruncation {
level = level[:4]
}
output = strings.Replace(output, "%lvl%", level, 1)
for k, v := range entry.Data {
if s, ok := v.(string); ok {
output = strings.Replace(output, "%"+k+"%", s, 1)
}
}
output += "\n"
return []byte(output), nil
}
package logger package logger
import ( import (
"cocogo/pkg/common"
"fmt"
"os" "os"
"path"
"strings"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"cocogo/pkg/config"
) )
var logger = logrus.New() var logger = logrus.New()
var logLevels = map[string]logrus.Level{
"DEBUG": logrus.DebugLevel,
"INFO": logrus.InfoLevel,
"WARN": logrus.WarnLevel,
"ERROR": logrus.ErrorLevel,
}
func init() { func Initial() {
customFormatter := &logrus.TextFormatter{ formatter := &Formatter{
DisableColors: false, LogFormat: "%time% [%lvl%] %msg%",
FullTimestamp: true, TimestampFormat: "2006-01-02 15:04:05",
DisableLevelTruncation: false, }
level, ok := logLevels[strings.ToUpper(config.Conf.LogLevel)]
if !ok {
level = logrus.InfoLevel
} }
customFormatter.TimestampFormat = "2006-01-02 15:04:05"
logger.SetFormatter(customFormatter)
// Output to stdout instead of the default stderr // Output to stdout instead of the default stderr
// Can be any io.Writer, see below for File example // Can be any io.Writer, see below for File example
logger.SetFormatter(formatter)
logger.SetOutput(os.Stdout) logger.SetOutput(os.Stdout)
logger.SetLevel(level)
// Output to file
logFilePath := path.Join(config.Conf.RootPath, "logs", "coco.log")
logDirPath := path.Dir(logFilePath)
if common.FileExists(logDirPath) {
err := os.MkdirAll(logDirPath, os.ModePerm)
if err != nil {
fmt.Printf("Create log dir %s error: %s\n", logDirPath, err)
return
}
}
// Only logger the warning severity or above. rotateFileHook, err := NewRotateFileHook(RotateFileConfig{
logger.SetLevel(logrus.DebugLevel) Filename: logFilePath,
MaxSize: 50,
MaxBackups: 7,
MaxAge: 7,
LocalTime: true,
Level: level,
Formatter: formatter,
})
if err != nil {
fmt.Printf("Create log rotate hook error: %s\n", err)
return
}
logger.AddHook(rotateFileHook)
} }
func Debug(args ...interface{}) { func Debug(args ...interface{}) {
......
package logger
import (
"io"
"github.com/sirupsen/logrus"
"gopkg.in/natefinch/lumberjack.v2"
)
type RotateFileConfig struct {
Filename string
MaxSize int
MaxBackups int
MaxAge int
Level logrus.Level
LocalTime bool
Formatter logrus.Formatter
}
type RotateFileHook struct {
Config RotateFileConfig
logWriter io.Writer
}
func NewRotateFileHook(config RotateFileConfig) (logrus.Hook, error) {
hook := RotateFileHook{
Config: config,
}
hook.logWriter = &lumberjack.Logger{
Filename: config.Filename,
MaxSize: config.MaxSize,
MaxBackups: config.MaxBackups,
MaxAge: config.MaxAge,
LocalTime: config.LocalTime,
}
return &hook, nil
}
func (hook *RotateFileHook) Levels() []logrus.Level {
return logrus.AllLevels[:hook.Config.Level+1]
}
func (hook *RotateFileHook) Fire(entry *logrus.Entry) (err error) {
b, err := hook.Config.Formatter.Format(entry)
if err != nil {
return err
}
hook.logWriter.Write(b)
return nil
}
...@@ -26,7 +26,7 @@ var ( ...@@ -26,7 +26,7 @@ var (
// Parse 解析用户输入输出, 拦截过滤用户输入输出 // Parse 解析用户输入输出, 拦截过滤用户输入输出
type Parser struct { type Parser struct {
session *Session session *SwitchSession
inputBuf *bytes.Buffer inputBuf *bytes.Buffer
cmdBuf *bytes.Buffer cmdBuf *bytes.Buffer
outputBuf *bytes.Buffer outputBuf *bytes.Buffer
......
...@@ -132,7 +132,7 @@ func (p *ProxyServer) Proxy() { ...@@ -132,7 +132,7 @@ func (p *ProxyServer) Proxy() {
return return
} }
sw := NewSwitch(p.UserConn, srvConn) sw := NewSwitchSession(p.UserConn, srvConn)
cmdRules, err := service.GetSystemUserFilterRules(p.SystemUser.Id) cmdRules, err := service.GetSystemUserFilterRules(p.SystemUser.Id)
if err != nil { if err != nil {
logger.Error("Get system user filter rule error: ", err) logger.Error("Get system user filter rule error: ", err)
......
...@@ -16,10 +16,10 @@ import ( ...@@ -16,10 +16,10 @@ import (
) )
type CommandRecorder struct { type CommandRecorder struct {
Session *Session Session *SwitchSession
} }
func NewCommandRecorder(sess *Session) (recorder *CommandRecorder) { func NewCommandRecorder(sess *SwitchSession) (recorder *CommandRecorder) {
return &CommandRecorder{Session: sess} return &CommandRecorder{Session: sess}
} }
......
...@@ -10,15 +10,15 @@ import ( ...@@ -10,15 +10,15 @@ import (
"cocogo/pkg/logger" "cocogo/pkg/logger"
) )
func NewSwitch(userConn UserConnection, serverConn ServerConnection) (sw *Session) { func NewSwitchSession(userConn UserConnection, serverConn ServerConnection) (sw *SwitchSession) {
parser := new(Parser) parser := new(Parser)
parser.Initial() parser.Initial()
sw = &Session{userConn: userConn, serverConn: serverConn, parser: parser} sw = &SwitchSession{userConn: userConn, serverConn: serverConn, parser: parser}
parser.session = sw parser.session = sw
return sw return sw
} }
type Session struct { type SwitchSession struct {
Id string Id string
User string `json:"user"` User string `json:"user"`
Server string `json:"asset"` Server string `json:"asset"`
...@@ -40,7 +40,7 @@ type Session struct { ...@@ -40,7 +40,7 @@ type Session struct {
cancelFunc context.CancelFunc cancelFunc context.CancelFunc
} }
func (s *Session) Initial() { func (s *SwitchSession) Initial() {
s.Id = uuid.NewV4().String() s.Id = uuid.NewV4().String()
s.User = s.userConn.User() s.User = s.userConn.User()
s.Server = s.serverConn.Name() s.Server = s.serverConn.Name()
...@@ -50,15 +50,15 @@ func (s *Session) Initial() { ...@@ -50,15 +50,15 @@ func (s *Session) Initial() {
s.DateStart = time.Now() s.DateStart = time.Now()
} }
func (s *Session) preBridge() { func (s *SwitchSession) preBridge() {
} }
func (s *Session) postBridge() { func (s *SwitchSession) postBridge() {
} }
func (s *Session) watchWindowChange(ctx context.Context, winCh <-chan ssh.Window) { func (s *SwitchSession) watchWindowChange(ctx context.Context, winCh <-chan ssh.Window) {
defer func() { defer func() {
logger.Debug("Watch window change routine end") logger.Debug("Watch window change routine end")
}() }()
...@@ -80,7 +80,7 @@ func (s *Session) watchWindowChange(ctx context.Context, winCh <-chan ssh.Window ...@@ -80,7 +80,7 @@ func (s *Session) watchWindowChange(ctx context.Context, winCh <-chan ssh.Window
} }
} }
func (s *Session) readUserToServer(ctx context.Context) { func (s *SwitchSession) readUserToServer(ctx context.Context) {
defer func() { defer func() {
logger.Debug("Read user to server end") logger.Debug("Read user to server end")
}() }()
...@@ -102,7 +102,7 @@ func (s *Session) readUserToServer(ctx context.Context) { ...@@ -102,7 +102,7 @@ func (s *Session) readUserToServer(ctx context.Context) {
} }
} }
func (s *Session) readServerToUser(ctx context.Context) { func (s *SwitchSession) readServerToUser(ctx context.Context) {
defer func() { defer func() {
logger.Debug("Read server to user end") logger.Debug("Read server to user end")
}() }()
...@@ -124,7 +124,7 @@ func (s *Session) readServerToUser(ctx context.Context) { ...@@ -124,7 +124,7 @@ func (s *Session) readServerToUser(ctx context.Context) {
} }
} }
func (s *Session) Bridge() (err error) { func (s *SwitchSession) Bridge() (err error) {
winCh := s.userConn.WinCh() winCh := s.userConn.WinCh()
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
s.cancelFunc = cancel s.cancelFunc = cancel
......
...@@ -25,10 +25,10 @@ func Initial() { ...@@ -25,10 +25,10 @@ func Initial() {
ak := AccessKey{Value: config.Conf.AccessKey, Path: keyPath} ak := AccessKey{Value: config.Conf.AccessKey, Path: keyPath}
_ = ak.Load() _ = ak.Load()
authClient.Auth = ak authClient.Auth = ak
ValidateAccessAuth() validateAccessAuth()
} }
func ValidateAccessAuth() { func validateAccessAuth() {
maxTry := 30 maxTry := 30
count := 0 count := 0
for count < maxTry { for count < maxTry {
......
package sshd package sshd
import ( import (
"fmt"
"strconv" "strconv"
"time"
"github.com/gliderlabs/ssh" "github.com/gliderlabs/ssh"
...@@ -13,34 +11,36 @@ import ( ...@@ -13,34 +11,36 @@ import (
"cocogo/pkg/logger" "cocogo/pkg/logger"
) )
const version = "v1.4.0" var conf = config.Conf
var (
conf = config.Conf
)
func StartServer() { func StartServer() {
logger.Debug("Load host key")
hostKey := HostKey{Value: conf.HostKey, Path: conf.HostKeyFile} hostKey := HostKey{Value: conf.HostKey, Path: conf.HostKeyFile}
logger.Debug("Loading host key")
signer, err := hostKey.Load() signer, err := hostKey.Load()
if err != nil { if err != nil {
logger.Fatal("Load host key error: ", err) logger.Fatal("Load host key error: ", err)
} }
fmt.Println(time.Now().Format("2006-01-02 15:04:05"))
fmt.Printf("Coco version %s, more see https://www.jumpserver.org\n", version)
fmt.Printf("Start ssh server at %s:%d\n", conf.BindHost, conf.SSHPort)
fmt.Println("Quit the server with CONTROL-C.")
logger.Infof("Start ssh server at %s:%d", conf.BindHost, conf.SSHPort)
srv := ssh.Server{ srv := ssh.Server{
Addr: conf.BindHost + ":" + strconv.Itoa(conf.SSHPort), Addr: conf.BindHost + ":" + strconv.Itoa(conf.SSHPort),
PasswordHandler: auth.CheckUserPassword,
PublicKeyHandler: auth.CheckUserPublicKey,
KeyboardInteractiveHandler: auth.CheckMFA, KeyboardInteractiveHandler: auth.CheckMFA,
NextAuthMethodsHandler: auth.CheckUserNeedMFA, NextAuthMethodsHandler: auth.CheckUserNeedMFA,
HostSigners: []ssh.Signer{signer}, HostSigners: []ssh.Signer{signer},
Handler: handler.SessionHandler, Handler: handler.SessionHandler,
SubsystemHandlers: map[string]ssh.SubsystemHandler{}, SubsystemHandlers: map[string]ssh.SubsystemHandler{},
} }
// Set Auth Handler
if conf.PasswordAuth {
srv.PasswordHandler = auth.CheckUserPassword
}
if conf.PublicKeyAuth {
srv.PublicKeyHandler = auth.CheckUserPublicKey
}
if !conf.PasswordAuth && !conf.PublicKeyAuth {
srv.PasswordHandler = auth.CheckUserPassword
}
srv.SetSubsystemHandler("sftp", handler.SftpHandler) srv.SetSubsystemHandler("sftp", handler.SftpHandler)
logger.Fatal(srv.ListenAndServe()) logger.Fatal(srv.ListenAndServe())
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment