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
b8d1f098
Commit
b8d1f098
authored
May 20, 2019
by
Eric
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[Update] 调整结构
parent
3f681921
Hide whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
96 additions
and
60 deletions
+96
-60
coco.go
cmd/coco.go
+1
-2
coco.go
pkg/coco/coco.go
+26
-1
init.go
pkg/coco/init.go
+0
-13
config.go
pkg/config/config.go
+20
-17
session.go
pkg/handler/session.go
+3
-1
assets.go
pkg/model/assets.go
+0
-1
parser.go
pkg/proxy/parser.go
+15
-7
proxy.go
pkg/proxy/proxy.go
+2
-0
recorder.go
pkg/proxy/recorder.go
+4
-4
switch.go
pkg/proxy/switch.go
+9
-9
terminal.go
pkg/service/terminal.go
+1
-1
server.go
pkg/sshd/server.go
+14
-3
util.go
pkg/utils/util.go
+1
-1
No files found.
cmd/coco.go
View file @
b8d1f098
...
@@ -5,6 +5,5 @@ import (
...
@@ -5,6 +5,5 @@ import (
)
)
func
main
()
{
func
main
()
{
app
:=
&
coco
.
Coco
{}
coco
.
RunForever
()
app
.
Start
()
}
}
pkg/coco/coco.go
View file @
b8d1f098
...
@@ -2,8 +2,15 @@ package coco
...
@@ -2,8 +2,15 @@ package coco
import
(
import
(
"fmt"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"time"
"cocogo/pkg/config"
"cocogo/pkg/logger"
"cocogo/pkg/proxy"
"cocogo/pkg/service"
"cocogo/pkg/sshd"
"cocogo/pkg/sshd"
)
)
...
@@ -16,9 +23,27 @@ func (c *Coco) Start() {
...
@@ -16,9 +23,27 @@ func (c *Coco) Start() {
fmt
.
Println
(
time
.
Now
()
.
Format
(
"2006-01-02 15:04:05"
))
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
(
"Coco version %s, more see https://www.jumpserver.org
\n
"
,
version
)
fmt
.
Println
(
"Quit the server with CONTROL-C."
)
fmt
.
Println
(
"Quit the server with CONTROL-C."
)
sshd
.
StartServer
()
go
sshd
.
StartServer
()
}
}
func
(
c
*
Coco
)
Stop
()
{
func
(
c
*
Coco
)
Stop
()
{
sshd
.
StopServer
()
logger
.
Debug
(
"Quit The Coco"
)
}
func
RunForever
()
{
loadingBoot
()
gracefulStop
:=
make
(
chan
os
.
Signal
)
signal
.
Notify
(
gracefulStop
,
syscall
.
SIGTERM
,
syscall
.
SIGINT
,
syscall
.
SIGQUIT
)
app
:=
&
Coco
{}
app
.
Start
()
<-
gracefulStop
app
.
Stop
()
}
func
loadingBoot
()
{
config
.
Initial
()
logger
.
Initial
()
service
.
Initial
()
proxy
.
Initial
()
}
}
pkg/coco/init.go
deleted
100644 → 0
View file @
3f681921
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 @
b8d1f098
...
@@ -41,6 +41,7 @@ type Config struct {
...
@@ -41,6 +41,7 @@ type Config struct {
Comment
string
`yaml:"COMMENT"`
Comment
string
`yaml:"COMMENT"`
Language
string
`yaml:"LANG"`
Language
string
`yaml:"LANG"`
LanguageCode
string
`yaml:"LANGUAGE_CODE"`
// Abandon
LanguageCode
string
`yaml:"LANGUAGE_CODE"`
// Abandon
UploadFailedReplay
bool
`yaml:"UPLOAD_FAILED_REPLAY_ON_START"`
}
}
func
(
c
*
Config
)
EnsureConfigValid
()
{
func
(
c
*
Config
)
EnsureConfigValid
()
{
...
@@ -105,23 +106,25 @@ var lock = new(sync.RWMutex)
...
@@ -105,23 +106,25 @@ var lock = new(sync.RWMutex)
var
name
,
_
=
os
.
Hostname
()
var
name
,
_
=
os
.
Hostname
()
var
rootPath
,
_
=
os
.
Getwd
()
var
rootPath
,
_
=
os
.
Getwd
()
var
Conf
=
&
Config
{
var
Conf
=
&
Config
{
Name
:
name
,
Name
:
name
,
CoreHost
:
"http://localhost:8080"
,
CoreHost
:
"http://localhost:8080"
,
BootstrapToken
:
""
,
BootstrapToken
:
""
,
BindHost
:
"0.0.0.0"
,
BindHost
:
"0.0.0.0"
,
SSHPort
:
2222
,
SSHPort
:
2222
,
SSHTimeout
:
60
,
SSHTimeout
:
15
,
HTTPPort
:
5000
,
HTTPPort
:
5000
,
AccessKey
:
""
,
HeartbeatDuration
:
10
,
AccessKeyFile
:
"data/keys/.access_key"
,
AccessKey
:
""
,
LogLevel
:
"DEBUG"
,
AccessKeyFile
:
"data/keys/.access_key"
,
HostKeyFile
:
"data/keys/host_key"
,
LogLevel
:
"DEBUG"
,
HostKey
:
""
,
HostKeyFile
:
"data/keys/host_key"
,
RootPath
:
rootPath
,
HostKey
:
""
,
Comment
:
"Coco"
,
RootPath
:
rootPath
,
Language
:
"zh"
,
Comment
:
"Coco"
,
ReplayStorage
:
map
[
string
]
string
{
"TYPE"
:
"server"
},
Language
:
"zh"
,
CommandStorage
:
map
[
string
]
string
{
"TYPE"
:
"server"
},
ReplayStorage
:
map
[
string
]
string
{
"TYPE"
:
"server"
},
CommandStorage
:
map
[
string
]
string
{
"TYPE"
:
"server"
},
UploadFailedReplay
:
true
,
}
}
func
SetConf
(
conf
*
Config
)
{
func
SetConf
(
conf
*
Config
)
{
...
...
pkg/handler/session.go
View file @
b8d1f098
...
@@ -330,8 +330,10 @@ func (h *interactiveHandler) searchAsset(key string) (assets []model.Asset) {
...
@@ -330,8 +330,10 @@ func (h *interactiveHandler) searchAsset(key string) (assets []model.Asset) {
searchData
=
h
.
searchResult
searchData
=
h
.
searchResult
}
}
key
=
strings
.
ToLower
(
key
)
for
_
,
assetValue
:=
range
searchData
{
for
_
,
assetValue
:=
range
searchData
{
contents
:=
[]
string
{
assetValue
.
Hostname
,
assetValue
.
Ip
,
assetValue
.
Comment
}
contents
:=
[]
string
{
strings
.
ToLower
(
assetValue
.
Hostname
),
strings
.
ToLower
(
assetValue
.
Ip
),
strings
.
ToLower
(
assetValue
.
Comment
)}
if
isSubstring
(
contents
,
key
)
{
if
isSubstring
(
contents
,
key
)
{
assets
=
append
(
assets
,
assetValue
)
assets
=
append
(
assets
,
assetValue
)
}
}
...
...
pkg/model/assets.go
View file @
b8d1f098
...
@@ -268,7 +268,6 @@ func (sf *SystemUserFilterRule) Pattern() *regexp.Regexp {
...
@@ -268,7 +268,6 @@ func (sf *SystemUserFilterRule) Pattern() *regexp.Regexp {
func
(
sf
*
SystemUserFilterRule
)
Match
(
cmd
string
)
(
RuleAction
,
string
)
{
func
(
sf
*
SystemUserFilterRule
)
Match
(
cmd
string
)
(
RuleAction
,
string
)
{
found
:=
sf
.
Pattern
()
.
FindString
(
cmd
)
found
:=
sf
.
Pattern
()
.
FindString
(
cmd
)
fmt
.
Println
(
found
)
if
found
==
""
{
if
found
==
""
{
return
ActionUnknown
,
""
return
ActionUnknown
,
""
}
}
...
...
pkg/proxy/parser.go
View file @
b8d1f098
...
@@ -2,10 +2,10 @@ package proxy
...
@@ -2,10 +2,10 @@ package proxy
import
(
import
(
"bytes"
"bytes"
"cocogo/pkg/i18n"
"fmt"
"fmt"
"sync"
"sync"
"cocogo/pkg/i18n"
"cocogo/pkg/logger"
"cocogo/pkg/logger"
"cocogo/pkg/model"
"cocogo/pkg/model"
"cocogo/pkg/utils"
"cocogo/pkg/utils"
...
@@ -114,8 +114,8 @@ func (p *Parser) parseInputState(b []byte) []byte {
...
@@ -114,8 +114,8 @@ func (p *Parser) parseInputState(b []byte) []byte {
p
.
inputState
=
false
p
.
inputState
=
false
// 用户输入了Enter,开始结算命令
// 用户输入了Enter,开始结算命令
p
.
parseCmdInput
()
p
.
parseCmdInput
()
if
p
.
IsCommandForbidden
()
{
if
cmd
,
ok
:=
p
.
IsCommandForbidden
();
!
ok
{
fbdMsg
:=
utils
.
WrapperWarn
(
fmt
.
Sprintf
(
i18n
.
T
(
"Command `%s` is forbidden"
),
p
.
comman
d
))
fbdMsg
:=
utils
.
WrapperWarn
(
fmt
.
Sprintf
(
i18n
.
T
(
"Command `%s` is forbidden"
),
cm
d
))
p
.
outputBuf
.
WriteString
(
fbdMsg
)
p
.
outputBuf
.
WriteString
(
fbdMsg
)
p
.
srvOutputChan
<-
[]
byte
(
"
\r\n
"
+
fbdMsg
)
p
.
srvOutputChan
<-
[]
byte
(
"
\r\n
"
+
fbdMsg
)
return
[]
byte
{
utils
.
CharCleanLine
,
'\r'
}
return
[]
byte
{
utils
.
CharCleanLine
,
'\r'
}
...
@@ -223,12 +223,20 @@ func (p *Parser) SetCMDFilterRules(rules []model.SystemUserFilterRule) {
...
@@ -223,12 +223,20 @@ func (p *Parser) SetCMDFilterRules(rules []model.SystemUserFilterRule) {
p
.
cmdFilterRules
=
rules
p
.
cmdFilterRules
=
rules
}
}
func
(
p
*
Parser
)
IsCommandForbidden
()
bool
{
func
(
p
*
Parser
)
IsCommandForbidden
()
(
string
,
bool
)
{
fmt
.
Println
(
"Command is: "
,
p
.
command
)
fmt
.
Println
(
"Command is: "
,
p
.
command
)
if
p
.
command
==
"ls"
{
for
_
,
rule
:=
range
p
.
cmdFilterRules
{
return
true
allowed
,
cmd
:=
rule
.
Match
(
p
.
command
)
switch
allowed
{
case
model
.
ActionAllow
:
return
""
,
true
case
model
.
ActionDeny
:
return
cmd
,
false
default
:
}
}
}
return
fals
e
return
""
,
tru
e
}
}
func
(
p
*
Parser
)
IsRecvState
()
bool
{
func
(
p
*
Parser
)
IsRecvState
()
bool
{
...
...
pkg/proxy/proxy.go
View file @
b8d1f098
...
@@ -148,8 +148,10 @@ func (p *ProxyServer) Proxy() {
...
@@ -148,8 +148,10 @@ func (p *ProxyServer) Proxy() {
}
}
cmdRules
:=
p
.
GetFilterRules
()
cmdRules
:=
p
.
GetFilterRules
()
sw
.
SetFilterRules
(
cmdRules
)
sw
.
SetFilterRules
(
cmdRules
)
AddSession
(
sw
)
_
=
sw
.
Bridge
(
p
.
UserConn
,
srvConn
)
_
=
sw
.
Bridge
(
p
.
UserConn
,
srvConn
)
p
.
finishSession
(
sw
)
p
.
finishSession
(
sw
)
RemoveSession
(
sw
)
}
}
func
(
p
*
ProxyServer
)
createSession
(
s
*
SwitchSession
)
bool
{
func
(
p
*
ProxyServer
)
createSession
(
s
*
SwitchSession
)
bool
{
...
...
pkg/proxy/recorder.go
View file @
b8d1f098
...
@@ -14,14 +14,14 @@ import (
...
@@ -14,14 +14,14 @@ import (
"cocogo/pkg/model"
"cocogo/pkg/model"
)
)
func
NewCommandRecorder
(
s
ess
*
SwitchSession
)
(
recorder
*
CommandRecorder
)
{
func
NewCommandRecorder
(
s
id
string
)
(
recorder
*
CommandRecorder
)
{
recorder
=
&
CommandRecorder
{
sessionID
:
s
ess
.
I
d
}
recorder
=
&
CommandRecorder
{
sessionID
:
s
i
d
}
recorder
.
initial
()
recorder
.
initial
()
return
recorder
return
recorder
}
}
func
NewReplyRecord
(
s
ess
*
SwitchSession
)
(
recorder
*
ReplyRecorder
)
{
func
NewReplyRecord
(
s
id
string
)
(
recorder
*
ReplyRecorder
)
{
recorder
=
&
ReplyRecorder
{
sessionID
:
s
ess
.
I
d
}
recorder
=
&
ReplyRecorder
{
sessionID
:
s
i
d
}
recorder
.
initial
()
recorder
.
initial
()
return
recorder
return
recorder
}
}
...
...
pkg/proxy/switch.go
View file @
b8d1f098
...
@@ -47,8 +47,8 @@ func (s *SwitchSession) Initial() {
...
@@ -47,8 +47,8 @@ func (s *SwitchSession) Initial() {
s
.
Id
=
uuid
.
NewV4
()
.
String
()
s
.
Id
=
uuid
.
NewV4
()
.
String
()
s
.
DateStart
=
time
.
Now
()
.
UTC
()
.
Format
(
"2006-01-02 15:04:05 +0000"
)
s
.
DateStart
=
time
.
Now
()
.
UTC
()
.
Format
(
"2006-01-02 15:04:05 +0000"
)
s
.
MaxIdleTime
=
config
.
GetConf
()
.
MaxIdleTime
s
.
MaxIdleTime
=
config
.
GetConf
()
.
MaxIdleTime
s
.
cmdRecorder
=
NewCommandRecorder
(
s
)
s
.
cmdRecorder
=
NewCommandRecorder
(
s
.
Id
)
s
.
replayRecorder
=
NewReplyRecord
(
s
)
s
.
replayRecorder
=
NewReplyRecord
(
s
.
Id
)
s
.
parser
=
newParser
()
s
.
parser
=
newParser
()
...
@@ -83,13 +83,12 @@ func (s *SwitchSession) generateCommandResult(command [2]string) *model.Command
...
@@ -83,13 +83,12 @@ func (s *SwitchSession) generateCommandResult(command [2]string) *model.Command
input
=
command
[
0
]
input
=
command
[
0
]
}
}
i
:=
strings
.
LastIndexByte
(
command
[
1
],
'\r'
)
i
:=
strings
.
LastIndexByte
(
command
[
1
],
'\r'
)
if
i
<
0
{
if
i
>
1024
{
output
=
command
[
1
]
output
=
output
[
:
1024
]
}
else
if
i
>
0
&&
i
<
1024
{
}
else
if
i
>
0
{
output
=
command
[
1
][
:
i
]
output
=
command
[
1
][
:
i
]
}
else
{
}
else
{
output
=
command
[
1
]
output
=
command
[
1
]
[
:
1024
]
}
}
return
&
model
.
Command
{
return
&
model
.
Command
{
...
@@ -134,13 +133,14 @@ func (s *SwitchSession) Bridge(userConn UserConnection, srvConn ServerConnection
...
@@ -134,13 +133,14 @@ func (s *SwitchSession) Bridge(userConn UserConnection, srvConn ServerConnection
select
{
select
{
// 检测是否超过最大空闲时间
// 检测是否超过最大空闲时间
case
<-
time
.
After
(
time
.
Duration
(
s
.
MaxIdleTime
)
*
time
.
Minute
)
:
case
<-
time
.
After
(
time
.
Duration
(
s
.
MaxIdleTime
)
*
time
.
Minute
)
:
msg
:=
i18n
.
T
(
fmt
.
Sprintf
(
"Connect idle more than %d minutes, disconnect"
,
s
.
MaxIdleTime
))
msg
:=
i18n
.
T
(
fmt
.
Sprintf
(
"
\n\n
Connect idle more than %d minutes, disconnect"
,
s
.
MaxIdleTime
))
msg
=
utils
.
WrapperWarn
(
msg
)
msg
=
utils
.
WrapperWarn
(
msg
)
utils
.
IgnoreErrWriteString
(
s
.
userTran
,
msg
)
utils
.
IgnoreErrWriteString
(
s
.
userTran
,
msg
)
return
return
// 手动结束
// 手动结束
case
<-
s
.
ctx
.
Done
()
:
case
<-
s
.
ctx
.
Done
()
:
msg
:=
i18n
.
T
(
"Terminated by administrator"
)
msg
:=
i18n
.
T
(
"
\n\r
Terminated by administrator"
)
msg
=
utils
.
WrapperWarn
(
msg
)
utils
.
IgnoreErrWriteString
(
userConn
,
msg
)
utils
.
IgnoreErrWriteString
(
userConn
,
msg
)
return
return
// 监控窗口大小变化
// 监控窗口大小变化
...
...
pkg/service/terminal.go
View file @
b8d1f098
...
@@ -75,7 +75,7 @@ func FinishTask(tid string) bool {
...
@@ -75,7 +75,7 @@ func FinishTask(tid string) bool {
var
res
map
[
string
]
interface
{}
var
res
map
[
string
]
interface
{}
data
:=
map
[
string
]
bool
{
"is_finished"
:
true
}
data
:=
map
[
string
]
bool
{
"is_finished"
:
true
}
Url
:=
fmt
.
Sprintf
(
FinishTaskURL
,
tid
)
Url
:=
fmt
.
Sprintf
(
FinishTaskURL
,
tid
)
err
:=
authClient
.
Patch
(
Url
,
data
,
res
)
err
:=
authClient
.
Patch
(
Url
,
data
,
&
res
)
if
err
!=
nil
{
if
err
!=
nil
{
logger
.
Error
(
err
)
logger
.
Error
(
err
)
return
false
return
false
...
...
pkg/sshd/server.go
View file @
b8d1f098
...
@@ -11,6 +11,8 @@ import (
...
@@ -11,6 +11,8 @@ import (
"cocogo/pkg/logger"
"cocogo/pkg/logger"
)
)
var
sshServer
*
ssh
.
Server
func
StartServer
()
{
func
StartServer
()
{
conf
:=
config
.
GetConf
()
conf
:=
config
.
GetConf
()
hostKey
:=
HostKey
{
Value
:
conf
.
HostKey
,
Path
:
conf
.
HostKeyFile
}
hostKey
:=
HostKey
{
Value
:
conf
.
HostKey
,
Path
:
conf
.
HostKeyFile
}
...
@@ -21,7 +23,7 @@ func StartServer() {
...
@@ -21,7 +23,7 @@ func StartServer() {
}
}
logger
.
Infof
(
"Start ssh server at %s:%d"
,
conf
.
BindHost
,
conf
.
SSHPort
)
logger
.
Infof
(
"Start ssh server at %s:%d"
,
conf
.
BindHost
,
conf
.
SSHPort
)
s
rv
:=
ssh
.
Server
{
s
shServer
=
&
ssh
.
Server
{
Addr
:
conf
.
BindHost
+
":"
+
strconv
.
Itoa
(
conf
.
SSHPort
),
Addr
:
conf
.
BindHost
+
":"
+
strconv
.
Itoa
(
conf
.
SSHPort
),
KeyboardInteractiveHandler
:
auth
.
CheckMFA
,
KeyboardInteractiveHandler
:
auth
.
CheckMFA
,
PasswordHandler
:
auth
.
CheckUserPassword
,
PasswordHandler
:
auth
.
CheckUserPassword
,
...
@@ -32,6 +34,15 @@ func StartServer() {
...
@@ -32,6 +34,15 @@ func StartServer() {
SubsystemHandlers
:
map
[
string
]
ssh
.
SubsystemHandler
{},
SubsystemHandlers
:
map
[
string
]
ssh
.
SubsystemHandler
{},
}
}
// Set Auth Handler
// Set Auth Handler
srv
.
SetSubsystemHandler
(
"sftp"
,
handler
.
SftpHandler
)
sshServer
.
SetSubsystemHandler
(
"sftp"
,
handler
.
SftpHandler
)
logger
.
Fatal
(
srv
.
ListenAndServe
())
logger
.
Fatal
(
sshServer
.
ListenAndServe
())
}
func
StopServer
()
{
err
:=
sshServer
.
Close
()
if
err
!=
nil
{
logger
.
Debugf
(
"ssh server close failed: %s"
,
err
.
Error
())
}
logger
.
Debug
(
"Close ssh Server"
)
}
}
pkg/utils/util.go
View file @
b8d1f098
...
@@ -41,6 +41,6 @@ func WrapperTitle(text string) string {
...
@@ -41,6 +41,6 @@ func WrapperTitle(text string) string {
}
}
func
WrapperWarn
(
text
string
)
string
{
func
WrapperWarn
(
text
string
)
string
{
text
+=
"
\
r\n
"
text
+=
"
\
n\r
"
return
WrapperString
(
text
,
Red
)
return
WrapperString
(
text
,
Red
)
}
}
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