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
b38e8c9c
Commit
b38e8c9c
authored
May 07, 2019
by
ibuler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[Update] 设置recorder
parent
2be75889
Show whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
125 additions
and
79 deletions
+125
-79
config.go
pkg/config/config.go
+8
-4
session.go
pkg/handler/session.go
+3
-0
parser.go
pkg/proxy/parser.go
+8
-0
proxy.go
pkg/proxy/proxy.go
+52
-21
switch.go
pkg/proxy/switch.go
+1
-9
userconn.go
pkg/proxy/userconn.go
+6
-7
cmd.go
pkg/record/cmd.go
+0
-10
interface.go
pkg/record/interface.go
+0
-15
cmd.go
pkg/recorder/cmd.go
+25
-0
replay.go
pkg/recorder/replay.go
+11
-11
interface.go
pkg/recorder/storage/interface.go
+9
-0
jms.go
pkg/recorder/storage/jms.go
+2
-2
No files found.
pkg/config/config.go
View file @
b38e8c9c
...
...
@@ -25,20 +25,23 @@ type Config struct {
SessionKeepDuration
int
`json:"TERMINAL_SESSION_KEEP_DURATION"`
TelnetRegex
string
`json:"TERMINAL_TELNET_REGEX"`
MaxIdleTime
time
.
Duration
`json:"SECURITY_MAX_IDLE_TIME"`
SftpRoot
string
`json:"TERMINAL_SFTP_ROOT" yaml:"SFTP_ROOT"`
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"`
BindHost
string
`yaml:"BIND_HOST"`
SSHPort
int
`yaml:"SSHD_PORT"`
HTTPPort
int
`yaml:"HTTPD_PORT"`
SSHTimeout
int
`yaml:"SSH_TIMEOUT"`
AccessKey
string
`yaml:"ACCESS_KEY"`
AccessKeyFile
string
`yaml:"ACCESS_KEY_FILE"`
LogLevel
string
`yaml:"LOG_LEVEL"`
HeartbeatDuration
time
.
Duration
`yaml:"HEARTBEAT_INTERVAL"`
RootPath
string
Comment
string
Language
string
RootPath
string
`yaml:"ROOT_PATH"`
Comment
string
`yaml:"COMMENT"`
Language
string
`yaml:"LANG"`
mux
sync
.
RWMutex
}
...
...
@@ -107,6 +110,7 @@ var Conf = &Config{
BootstrapToken
:
""
,
BindHost
:
"0.0.0.0"
,
SSHPort
:
2222
,
SSHTimeout
:
60
,
HTTPPort
:
5000
,
AccessKey
:
""
,
AccessKeyFile
:
"access_key"
,
...
...
@@ -115,7 +119,7 @@ var Conf = &Config{
HostKey
:
""
,
RootPath
:
rootPath
,
Comment
:
"Coco"
,
Language
:
"zh
_CN
"
,
Language
:
"zh"
,
ReplayStorage
:
map
[
string
]
string
{},
CommandStorage
:
map
[
string
]
string
{},
}
pkg/handler/session.go
View file @
b38e8c9c
...
...
@@ -323,8 +323,11 @@ func (i *InteractiveHandler) searchNodeAssets(num int) (assets []model.Asset) {
}
func
(
i
*
InteractiveHandler
)
Proxy
(
ctx
context
.
Context
)
{
i
.
assetSelect
=
&
model
.
Asset
{
Hostname
:
"centos"
,
Port
:
22
,
Ip
:
"192.168.244.185"
}
i
.
systemUserSelect
=
&
model
.
SystemUser
{
Name
:
"web"
,
UserName
:
"web"
,
Password
:
"redhat"
}
p
:=
proxy
.
ProxyServer
{
Session
:
i
.
sess
,
User
:
i
.
user
,
Asset
:
i
.
assetSelect
,
SystemUser
:
i
.
systemUserSelect
,
}
...
...
pkg/proxy/parser.go
View file @
b38e8c9c
...
...
@@ -171,3 +171,11 @@ func (p *Parser) ParseServerOutput(b []byte) []byte {
func
(
p
*
Parser
)
SetCMDFilterRules
(
rules
[]
model
.
SystemUserFilterRule
)
{
p
.
cmdFilterRules
=
rules
}
func
(
p
*
Parser
)
SetReplayRecorder
()
{
}
func
(
p
*
Parser
)
SetCommandRecorder
()
{
}
pkg/proxy/proxy.go
View file @
b38e8c9c
package
proxy
import
(
"fmt"
"io"
"strings"
"time"
"github.com/ibuler/ssh"
"cocogo/pkg/config"
"cocogo/pkg/i18n"
"cocogo/pkg/logger"
"cocogo/pkg/model"
"cocogo/pkg/service"
...
...
@@ -37,39 +44,63 @@ func (p *ProxyServer) validatePermission() bool {
return
true
}
func
(
p
*
ProxyServer
)
getServerConn
()
{
func
(
p
*
ProxyServer
)
getServerConn
()
(
srvConn
ServerConnection
,
err
error
)
{
srvConn
=
&
ServerSSHConnection
{
host
:
"192.168.244.145"
,
port
:
"22"
,
user
:
"root"
,
password
:
"redhat"
,
}
pty
,
_
,
ok
:=
p
.
Session
.
Pty
()
if
!
ok
{
logger
.
Error
(
"User not request Pty"
)
return
}
done
:=
make
(
chan
struct
{})
go
p
.
sendConnectingMsg
(
done
)
err
=
srvConn
.
Connect
(
pty
.
Window
.
Height
,
pty
.
Window
.
Width
,
pty
.
Term
)
_
,
_
=
io
.
WriteString
(
p
.
Session
,
"
\r\n
"
)
done
<-
struct
{}{}
return
}
func
(
p
*
ProxyServer
)
sendConnectingMsg
()
{
func
(
p
*
ProxyServer
)
sendConnectingMsg
(
done
chan
struct
{})
{
delay
:=
0.0
msg
:=
fmt
.
Sprintf
(
i18n
.
T
(
"Connecting to %s@%s %.1f"
),
p
.
SystemUser
.
UserName
,
p
.
Asset
.
Ip
,
delay
)
_
,
_
=
io
.
WriteString
(
p
.
Session
,
msg
)
for
int
(
delay
)
<
config
.
Conf
.
SSHTimeout
{
select
{
case
<-
done
:
return
default
:
delayS
:=
fmt
.
Sprintf
(
"%.1f"
,
delay
)
data
:=
strings
.
Repeat
(
"
\x08
"
,
len
(
delayS
))
+
delayS
_
,
_
=
io
.
WriteString
(
p
.
Session
,
data
)
time
.
Sleep
(
100
*
time
.
Millisecond
)
delay
+=
0.1
}
}
}
func
(
p
*
ProxyServer
)
Proxy
()
{
if
!
p
.
checkProtocol
()
{
return
}
conn
:=
ServerSSHConnection
{
host
:
"192.168.244.185"
,
port
:
"22"
,
user
:
"root"
,
password
:
"redhat"
,
}
ptyReq
,
_
,
ok
:=
p
.
Session
.
Pty
()
if
!
ok
{
logger
.
Error
(
"Pty not ok"
)
return
}
err
:=
conn
.
Connect
(
ptyReq
.
Window
.
Height
,
ptyReq
.
Window
.
Width
,
ptyReq
.
Term
)
srvConn
,
err
:=
p
.
getServerConn
()
if
err
!=
nil
{
logger
.
Errorf
(
"Connect host error: %s
\n
"
,
err
)
return
}
sw
:=
Switch
{
userConn
:
p
.
Session
,
serverConn
:
&
conn
,
parser
:
parser
,
userConn
:=
&
UserSSHConnection
{
Session
:
p
.
Session
,
winch
:
make
(
chan
ssh
.
Window
)}
sw
:=
NewSwitch
(
userConn
,
srvConn
)
cmdRules
,
err
:=
service
.
GetSystemUserFilterRules
(
p
.
SystemUser
.
Id
)
if
err
!=
nil
{
logger
.
Error
(
"Get system user filter rule error: "
,
err
)
}
sw
.
parser
.
SetCMDFilterRules
(
cmdRules
)
sw
.
parser
.
SetReplayRecorder
()
sw
.
parser
.
SetCommandRecorder
()
_
=
sw
.
Bridge
()
_
=
c
onn
.
Close
()
_
=
srvC
onn
.
Close
()
}
pkg/proxy/switch.go
View file @
b38e8c9c
...
...
@@ -2,7 +2,6 @@ package proxy
import
(
"cocogo/pkg/logger"
"cocogo/pkg/service"
"context"
"github.com/ibuler/ssh"
"github.com/satori/go.uuid"
...
...
@@ -10,13 +9,7 @@ import (
)
func
NewSwitch
(
userConn
UserConnection
,
serverConn
ServerConnection
)
(
sw
*
Switch
)
{
rules
,
err
:=
service
.
GetSystemUserFilterRules
(
""
)
if
err
!=
nil
{
logger
.
Error
(
"Get system user filter rule error: "
,
err
)
}
parser
:=
&
Parser
{
cmdFilterRules
:
rules
,
}
parser
:=
new
(
Parser
)
parser
.
Initial
()
sw
=
&
Switch
{
userConn
:
userConn
,
serverConn
:
serverConn
,
parser
:
parser
}
return
sw
...
...
@@ -98,7 +91,6 @@ func (s *Switch) readUserToServer(ctx context.Context) {
s
.
cancelFunc
()
}
buf2
:=
s
.
parser
.
ParseUserInput
(
p
)
logger
.
Debug
(
"Send to server: "
,
string
(
buf2
))
_
,
err
:=
s
.
serverTran
.
Write
(
buf2
)
if
err
!=
nil
{
return
...
...
pkg/proxy/userconn.go
View file @
b38e8c9c
...
...
@@ -12,25 +12,24 @@ type UserConnection interface {
Protocol
()
string
WinCh
()
<-
chan
ssh
.
Window
User
()
string
Name
()
string
LoginFrom
()
string
RemoteAddr
()
string
}
type
SSHUser
Connection
struct
{
type
UserSSH
Connection
struct
{
ssh
.
Session
winch
<-
chan
ssh
.
Window
}
func
(
uc
*
SSHUser
Connection
)
Protocol
()
string
{
func
(
uc
*
UserSSH
Connection
)
Protocol
()
string
{
return
"ssh"
}
func
(
uc
*
SSHUser
Connection
)
User
()
string
{
func
(
uc
*
UserSSH
Connection
)
User
()
string
{
return
uc
.
Session
.
User
()
}
func
(
uc
*
SSHUser
Connection
)
WinCh
()
(
winch
<-
chan
ssh
.
Window
)
{
func
(
uc
*
UserSSH
Connection
)
WinCh
()
(
winch
<-
chan
ssh
.
Window
)
{
_
,
winch
,
ok
:=
uc
.
Pty
()
if
ok
{
return
...
...
@@ -38,10 +37,10 @@ func (uc *SSHUserConnection) WinCh() (winch <-chan ssh.Window) {
return
nil
}
func
(
uc
*
SSHUser
Connection
)
LoginFrom
()
string
{
func
(
uc
*
UserSSH
Connection
)
LoginFrom
()
string
{
return
"T"
}
func
(
uc
*
SSHUser
Connection
)
RemoteAddr
()
string
{
func
(
uc
*
UserSSH
Connection
)
RemoteAddr
()
string
{
return
strings
.
Split
(
uc
.
Session
.
RemoteAddr
()
.
String
(),
":"
)[
0
]
}
pkg/record/cmd.go
deleted
100644 → 0
View file @
2be75889
package
record
import
(
"time"
)
type
Command
struct
{
SessionID
string
StartTime
time
.
Time
}
pkg/record/interface.go
deleted
100644 → 0
View file @
2be75889
package
record
type
Storage
interface
{
Upload
(
gZipFile
,
target
string
)
}
func
NewStorageServer
()
Storage
{
//conf := config.GetGlobalConfig()
//
//switch conf.TermConfig.RePlayStorage["TYPE"] {
//case "server":
// return NewJmsStorage()
//}
return
nil
}
pkg/recorder/cmd.go
0 → 100644
View file @
b38e8c9c
package
recorder
import
(
"time"
)
type
CommandRecorder
struct
{
SessionID
string
StartTime
time
.
Time
}
type
Command
struct
{
SessionId
string
`json:"session"`
OrgId
string
`json:"org_id"`
Input
string
`json:"input"`
Output
string
`json:"output"`
User
string
`json:"user"`
Server
string
`json:"asset"`
SystemUser
string
`json:"system_user"`
Timestamp
time
.
Time
`json:"timestamp"`
}
func
(
c
*
CommandRecorder
)
Record
(
cmd
*
Command
)
{
}
pkg/record/replay.go
→
pkg/record
er
/replay.go
View file @
b38e8c9c
package
record
package
record
er
import
(
"compress/gzip"
...
...
@@ -12,12 +12,12 @@ import (
"time"
"cocogo/pkg/config"
"cocogo/pkg/storage"
"cocogo/pkg/
recorder/
storage"
)
var
conf
=
config
.
Conf
func
NewReplyRecord
(
sessionID
string
)
*
Reply
{
func
NewReplyRecord
(
sessionID
string
)
*
Reply
Recorder
{
rootPath
:=
conf
.
RootPath
currentData
:=
time
.
Now
()
.
UTC
()
.
Format
(
"2006-01-02"
)
gzFileName
:=
sessionID
+
".replay.gz"
...
...
@@ -25,7 +25,7 @@ func NewReplyRecord(sessionID string) *Reply {
absGzFilePath
:=
filepath
.
Join
(
rootPath
,
"data"
,
"replays"
,
currentData
,
gzFileName
)
target
:=
strings
.
Join
([]
string
{
currentData
,
gzFileName
},
"/"
)
return
&
Reply
{
return
&
Reply
Recorder
{
SessionID
:
sessionID
,
FileName
:
sessionID
,
absFilePath
:
absFilePath
,
...
...
@@ -36,7 +36,7 @@ func NewReplyRecord(sessionID string) *Reply {
}
}
type
Reply
struct
{
type
Reply
Recorder
struct
{
SessionID
string
FileName
string
gzFileName
string
...
...
@@ -47,19 +47,19 @@ type Reply struct {
StartTime
time
.
Time
}
func
(
r
*
Reply
)
Record
(
b
[]
byte
)
{
func
(
r
*
Reply
Recorder
)
Record
(
b
[]
byte
)
{
interval
:=
time
.
Now
()
.
UTC
()
.
Sub
(
r
.
StartTime
)
.
Seconds
()
data
,
_
:=
json
.
Marshal
(
string
(
b
))
_
,
_
=
r
.
WriteF
.
WriteString
(
fmt
.
Sprintf
(
"
\"
%0.6f
\"
:%s,"
,
interval
,
data
))
}
func
(
r
*
Reply
)
StartRecord
()
{
func
(
r
*
Reply
Recorder
)
Start
()
{
//auth.MakeSureDirExit(r.absFilePath)
//r.WriteF, _ = os.Create(r.absFilePath)
//_, _ = r.WriteF.Write([]byte("{"))
}
func
(
r
*
Reply
)
EndRecor
d
(
ctx
context
.
Context
)
{
func
(
r
*
Reply
Recorder
)
En
d
(
ctx
context
.
Context
)
{
select
{
case
<-
ctx
.
Done
()
:
_
,
_
=
r
.
WriteF
.
WriteString
(
`"0":""}`
)
...
...
@@ -68,10 +68,10 @@ func (r *Reply) EndRecord(ctx context.Context) {
r
.
uploadReplay
()
}
func
(
r
*
Reply
)
uploadReplay
()
{
func
(
r
*
Reply
Recorder
)
uploadReplay
()
{
_
=
GzipCompressFile
(
r
.
absFilePath
,
r
.
absGzFilePath
)
if
sto
:=
storage
.
NewStorageServer
();
sto
!=
nil
{
sto
.
Upload
(
r
.
absGzFilePath
,
r
.
target
)
if
sto
re
:=
storage
.
NewStorageServer
();
store
!=
nil
{
sto
re
.
Upload
(
r
.
absGzFilePath
,
r
.
target
)
}
_
=
os
.
Remove
(
r
.
absFilePath
)
_
=
os
.
Remove
(
r
.
absGzFilePath
)
...
...
pkg/recorder/storage/interface.go
0 → 100644
View file @
b38e8c9c
package
storage
type
ReplayStorage
interface
{
Upload
(
gZipFile
,
target
string
)
}
func
NewStorageServer
()
ReplayStorage
{
return
nil
}
pkg/record/jms.go
→
pkg/record
er/storage
/jms.go
View file @
b38e8c9c
package
record
package
storage
//var client = service.Client
func
NewJmsStorage
()
Storage
{
func
NewJmsStorage
()
Replay
Storage
{
//appService := auth.GetGlobalService()
//return &Server{
// StorageType: "jms",
...
...
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