Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
K
koko
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ops
koko
Commits
42b78fa9
Commit
42b78fa9
authored
May 10, 2019
by
Eric
Browse files
Options
Browse Files
Download
Plain Diff
fix conflicts
parents
6240cc0d
d4c7be16
Hide whitespace changes
Inline
Side-by-side
Showing
17 changed files
with
266 additions
and
74 deletions
+266
-74
Gopkg.lock
Gopkg.lock
+9
-0
Gopkg.toml
Gopkg.toml
+4
-0
coco.go
cmd/coco.go
+3
-9
config_example.yml
cmd/config_example.yml
+1
-0
coco.go
pkg/coco/coco.go
+24
-0
init.go
pkg/coco/init.go
+13
-0
config.go
pkg/config/config.go
+5
-8
session.go
pkg/handler/session.go
+18
-18
formatter.go
pkg/logger/formatter.go
+61
-0
logger.go
pkg/logger/logger.go
+47
-9
rotate.go
pkg/logger/rotate.go
+51
-0
parser.go
pkg/proxy/parser.go
+1
-1
proxy.go
pkg/proxy/proxy.go
+1
-1
recorder.go
pkg/proxy/recorder.go
+2
-2
switch.go
pkg/proxy/switch.go
+10
-10
init.go
pkg/service/init.go
+2
-2
server.go
pkg/sshd/server.go
+14
-14
No files found.
Gopkg.lock
View file @
42b78fa9
...
@@ -182,6 +182,14 @@
...
@@ -182,6 +182,14 @@
pruneopts = "UT"
pruneopts = "UT"
revision = "a5b02f93d862f065920dd6a40dddc66b60d0dec4"
revision = "a5b02f93d862f065920dd6a40dddc66b60d0dec4"
[[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"
...
...
Gopkg.toml
View file @
42b78fa9
...
@@ -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"
cmd/coco.go
View file @
42b78fa9
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
()
}
}
cmd/config_example.yml
View file @
42b78fa9
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
pkg/coco/coco.go
0 → 100644
View file @
42b78fa9
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
()
{
}
pkg/coco/init.go
0 → 100644
View file @
42b78fa9
package
coco
import
(
"cocogo/pkg/config"
"cocogo/pkg/logger"
"cocogo/pkg/service"
)
func
init
()
{
config
.
Initial
()
logger
.
Initial
()
service
.
Initial
()
}
pkg/config/config.go
View file @
42b78fa9
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
.
Error
f
(
"Load yaml error: %v"
,
err
)
log
.
Print
f
(
"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
.
Error
f
(
"Error occur: %v"
,
err
)
log
.
Fatal
f
(
"Error occur: %v"
,
err
)
}
}
return
c
.
LoadFromYAML
(
envYAML
)
return
c
.
LoadFromYAML
(
envYAML
)
}
}
...
...
pkg/handler/session.go
View file @
42b78fa9
...
@@ -36,14 +36,14 @@ func SessionHandler(sess ssh.Session) {
...
@@ -36,14 +36,14 @@ func SessionHandler(sess ssh.Session) {
}
}
}
}
func
newInteractiveHandler
(
sess
ssh
.
Session
,
user
*
model
.
User
)
*
I
nteractiveHandler
{
func
newInteractiveHandler
(
sess
ssh
.
Session
,
user
*
model
.
User
)
*
i
nteractiveHandler
{
term
:=
terminal
.
NewTerminal
(
sess
,
"Opt> "
)
term
:=
terminal
.
NewTerminal
(
sess
,
"Opt> "
)
handler
:=
&
I
nteractiveHandler
{
sess
:
sess
,
user
:
user
,
term
:
term
}
handler
:=
&
i
nteractiveHandler
{
sess
:
sess
,
user
:
user
,
term
:
term
}
handler
.
Initial
()
handler
.
Initial
()
return
handler
return
handler
}
}
type
I
nteractiveHandler
struct
{
type
i
nteractiveHandler
struct
{
sess
ssh
.
Session
sess
ssh
.
Session
user
*
model
.
User
user
*
model
.
User
term
*
terminal
.
Terminal
term
*
terminal
.
Terminal
...
@@ -56,18 +56,18 @@ type InteractiveHandler struct {
...
@@ -56,18 +56,18 @@ type InteractiveHandler struct {
mu
*
sync
.
RWMutex
mu
*
sync
.
RWMutex
}
}
func
(
h
*
I
nteractiveHandler
)
Initial
()
{
func
(
h
*
i
nteractiveHandler
)
Initial
()
{
h
.
displayBanner
()
h
.
displayBanner
()
h
.
loadUserAssets
()
h
.
loadUserAssets
()
h
.
loadUserAssetNodes
()
h
.
loadUserAssetNodes
()
h
.
searchResult
=
h
.
assets
h
.
searchResult
=
h
.
assets
}
}
func
(
h
*
I
nteractiveHandler
)
displayBanner
()
{
func
(
h
*
i
nteractiveHandler
)
displayBanner
()
{
displayBanner
(
h
.
sess
,
h
.
user
.
Name
)
displayBanner
(
h
.
sess
,
h
.
user
.
Name
)
}
}
func
(
h
*
I
nteractiveHandler
)
watchWinSizeChange
(
winCh
<-
chan
ssh
.
Window
,
done
<-
chan
struct
{})
{
func
(
h
*
i
nteractiveHandler
)
watchWinSizeChange
(
winCh
<-
chan
ssh
.
Window
,
done
<-
chan
struct
{})
{
for
{
for
{
select
{
select
{
case
<-
done
:
case
<-
done
:
...
@@ -77,13 +77,13 @@ func (h *InteractiveHandler) watchWinSizeChange(winCh <-chan ssh.Window, done <-
...
@@ -77,13 +77,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
*
I
nteractiveHandler
)
Dispatch
(
ctx
cctx
.
Context
)
{
func
(
h
*
i
nteractiveHandler
)
Dispatch
(
ctx
cctx
.
Context
)
{
_
,
winCh
,
_
:=
h
.
sess
.
Pty
()
_
,
winCh
,
_
:=
h
.
sess
.
Pty
()
for
{
for
{
doneChan
:=
make
(
chan
struct
{})
doneChan
:=
make
(
chan
struct
{})
...
@@ -147,7 +147,7 @@ func (h *InteractiveHandler) Dispatch(ctx cctx.Context) {
...
@@ -147,7 +147,7 @@ func (h *InteractiveHandler) Dispatch(ctx cctx.Context) {
}
}
}
}
func
(
h
*
I
nteractiveHandler
)
chooseSystemUser
(
systemUsers
[]
model
.
SystemUser
)
model
.
SystemUser
{
func
(
h
*
i
nteractiveHandler
)
chooseSystemUser
(
systemUsers
[]
model
.
SystemUser
)
model
.
SystemUser
{
length
:=
len
(
systemUsers
)
length
:=
len
(
systemUsers
)
switch
length
{
switch
length
{
case
0
:
case
0
:
...
@@ -195,7 +195,7 @@ func (h *InteractiveHandler) chooseSystemUser(systemUsers []model.SystemUser) mo
...
@@ -195,7 +195,7 @@ func (h *InteractiveHandler) chooseSystemUser(systemUsers []model.SystemUser) mo
}
}
// 当资产的数量为1的时候,就进行代理转化
// 当资产的数量为1的时候,就进行代理转化
func
(
h
*
I
nteractiveHandler
)
displayAssetsOrProxy
(
assets
[]
model
.
Asset
)
{
func
(
h
*
i
nteractiveHandler
)
displayAssetsOrProxy
(
assets
[]
model
.
Asset
)
{
if
len
(
assets
)
==
1
{
if
len
(
assets
)
==
1
{
logger
.
Debug
(
assets
[
0
]
.
SystemUsers
)
logger
.
Debug
(
assets
[
0
]
.
SystemUsers
)
systemUser
:=
h
.
chooseSystemUser
(
assets
[
0
]
.
SystemUsers
)
systemUser
:=
h
.
chooseSystemUser
(
assets
[
0
]
.
SystemUsers
)
...
@@ -207,7 +207,7 @@ func (h *InteractiveHandler) displayAssetsOrProxy(assets []model.Asset) {
...
@@ -207,7 +207,7 @@ func (h *InteractiveHandler) displayAssetsOrProxy(assets []model.Asset) {
}
}
}
}
func
(
h
*
I
nteractiveHandler
)
displayAssets
(
assets
model
.
AssetList
)
{
func
(
h
*
i
nteractiveHandler
)
displayAssets
(
assets
model
.
AssetList
)
{
if
len
(
assets
)
==
0
{
if
len
(
assets
)
==
0
{
_
,
_
=
io
.
WriteString
(
h
.
term
,
"
\r\n
No Assets
\r\n\r
"
)
_
,
_
=
io
.
WriteString
(
h
.
term
,
"
\r\n
No Assets
\r\n\r
"
)
}
else
{
}
else
{
...
@@ -228,7 +228,7 @@ func (h *InteractiveHandler) displayAssets(assets model.AssetList) {
...
@@ -228,7 +228,7 @@ func (h *InteractiveHandler) displayAssets(assets model.AssetList) {
}
}
func
(
h
*
I
nteractiveHandler
)
displayNodes
(
nodes
[]
model
.
Node
)
{
func
(
h
*
i
nteractiveHandler
)
displayNodes
(
nodes
[]
model
.
Node
)
{
tree
:=
ConstructAssetNodeTree
(
nodes
)
tree
:=
ConstructAssetNodeTree
(
nodes
)
tipHeaderMsg
:=
"
\r\n
Node: [ ID.Name(Asset amount) ]"
tipHeaderMsg
:=
"
\r\n
Node: [ 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
"
...
@@ -242,22 +242,22 @@ func (h *InteractiveHandler) displayNodes(nodes []model.Node) {
...
@@ -242,22 +242,22 @@ func (h *InteractiveHandler) displayNodes(nodes []model.Node) {
}
}
func
(
h
*
I
nteractiveHandler
)
refreshAssetsAndNodesData
()
{
func
(
h
*
i
nteractiveHandler
)
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
*
I
nteractiveHandler
)
loadUserAssets
()
{
func
(
h
*
i
nteractiveHandler
)
loadUserAssets
()
{
h
.
assets
=
service
.
GetUserAssets
(
h
.
user
.
ID
,
"1"
)
h
.
assets
=
service
.
GetUserAssets
(
h
.
user
.
ID
,
"1"
)
}
}
func
(
h
*
I
nteractiveHandler
)
loadUserAssetNodes
()
{
func
(
h
*
i
nteractiveHandler
)
loadUserAssetNodes
()
{
h
.
nodes
=
service
.
GetUserNodes
(
h
.
user
.
ID
,
"1"
)
h
.
nodes
=
service
.
GetUserNodes
(
h
.
user
.
ID
,
"1"
)
}
}
func
(
h
*
I
nteractiveHandler
)
searchAsset
(
key
string
)
(
assets
[]
model
.
Asset
)
{
func
(
h
*
i
nteractiveHandler
)
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
)
{
assets
=
[]
model
.
Asset
{
h
.
searchResult
[
indexNum
-
1
]}
assets
=
[]
model
.
Asset
{
h
.
searchResult
[
indexNum
-
1
]}
...
@@ -281,7 +281,7 @@ func (h *InteractiveHandler) searchAsset(key string) (assets []model.Asset) {
...
@@ -281,7 +281,7 @@ func (h *InteractiveHandler) searchAsset(key string) (assets []model.Asset) {
return
assets
return
assets
}
}
func
(
h
*
I
nteractiveHandler
)
searchNodeAssets
(
num
int
)
(
assets
[]
model
.
Asset
)
{
func
(
h
*
i
nteractiveHandler
)
searchNodeAssets
(
num
int
)
(
assets
[]
model
.
Asset
)
{
if
num
>
len
(
h
.
nodes
)
||
num
==
0
{
if
num
>
len
(
h
.
nodes
)
||
num
==
0
{
return
assets
return
assets
}
}
...
@@ -289,7 +289,7 @@ func (h *InteractiveHandler) searchNodeAssets(num int) (assets []model.Asset) {
...
@@ -289,7 +289,7 @@ func (h *InteractiveHandler) searchNodeAssets(num int) (assets []model.Asset) {
}
}
func
(
h
*
I
nteractiveHandler
)
Proxy
(
ctx
context
.
Context
)
{
func
(
h
*
i
nteractiveHandler
)
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"}
...
...
pkg/logger/formatter.go
0 → 100644
View file @
42b78fa9
/*
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
}
pkg/logger/logger.go
View file @
42b78fa9
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
{})
{
...
...
pkg/logger/rotate.go
0 → 100644
View file @
42b78fa9
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
}
pkg/proxy/parser.go
View file @
42b78fa9
...
@@ -26,7 +26,7 @@ var (
...
@@ -26,7 +26,7 @@ var (
// Parse 解析用户输入输出, 拦截过滤用户输入输出
// Parse 解析用户输入输出, 拦截过滤用户输入输出
type
Parser
struct
{
type
Parser
struct
{
session
*
Session
session
*
S
witchS
ession
inputBuf
*
bytes
.
Buffer
inputBuf
*
bytes
.
Buffer
cmdBuf
*
bytes
.
Buffer
cmdBuf
*
bytes
.
Buffer
outputBuf
*
bytes
.
Buffer
outputBuf
*
bytes
.
Buffer
...
...
pkg/proxy/proxy.go
View file @
42b78fa9
...
@@ -132,7 +132,7 @@ func (p *ProxyServer) Proxy() {
...
@@ -132,7 +132,7 @@ func (p *ProxyServer) Proxy() {
return
return
}
}
sw
:=
NewSwitch
(
p
.
UserConn
,
srvConn
)
sw
:=
NewSwitch
Session
(
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
)
...
...
pkg/proxy/recorder.go
View file @
42b78fa9
...
@@ -16,10 +16,10 @@ import (
...
@@ -16,10 +16,10 @@ import (
)
)
type
CommandRecorder
struct
{
type
CommandRecorder
struct
{
Session
*
Session
Session
*
S
witchS
ession
}
}
func
NewCommandRecorder
(
sess
*
Session
)
(
recorder
*
CommandRecorder
)
{
func
NewCommandRecorder
(
sess
*
S
witchS
ession
)
(
recorder
*
CommandRecorder
)
{
return
&
CommandRecorder
{
Session
:
sess
}
return
&
CommandRecorder
{
Session
:
sess
}
}
}
...
...
pkg/proxy/switch.go
View file @
42b78fa9
...
@@ -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
NewSwitch
Session
(
userConn
UserConnection
,
serverConn
ServerConnection
)
(
sw
*
Switch
Session
)
{
parser
:=
new
(
Parser
)
parser
:=
new
(
Parser
)
parser
.
Initial
()
parser
.
Initial
()
sw
=
&
Session
{
userConn
:
userConn
,
serverConn
:
serverConn
,
parser
:
parser
}
sw
=
&
S
witchS
ession
{
userConn
:
userConn
,
serverConn
:
serverConn
,
parser
:
parser
}
parser
.
session
=
sw
parser
.
session
=
sw
return
sw
return
sw
}
}
type
Session
struct
{
type
S
witchS
ession
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
*
S
witchS
ession
)
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
*
S
witchS
ession
)
preBridge
()
{
}
}
func
(
s
*
Session
)
postBridge
()
{
func
(
s
*
S
witchS
ession
)
postBridge
()
{
}
}
func
(
s
*
Session
)
watchWindowChange
(
ctx
context
.
Context
,
winCh
<-
chan
ssh
.
Window
)
{
func
(
s
*
S
witchS
ession
)
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
*
S
witchS
ession
)
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
*
S
witchS
ession
)
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
*
S
witchS
ession
)
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
...
...
pkg/service/init.go
View file @
42b78fa9
...
@@ -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
V
alidateAccessAuth
()
v
alidateAccessAuth
()
}
}
func
V
alidateAccessAuth
()
{
func
v
alidateAccessAuth
()
{
maxTry
:=
30
maxTry
:=
30
count
:=
0
count
:=
0
for
count
<
maxTry
{
for
count
<
maxTry
{
...
...
pkg/sshd/server.go
View file @
42b78fa9
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
())
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment