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
2be75889
Commit
2be75889
authored
May 06, 2019
by
ibuler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[Update] 修改结构
parent
21379f76
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
149 additions
and
70 deletions
+149
-70
context.go
pkg/cctx/context.go
+2
-2
client.go
pkg/common/client.go
+1
-1
client_test.go
pkg/common/client_test.go
+1
-1
session.go
pkg/handler/session.go
+4
-4
proxy.go
pkg/proxy/proxy.go
+8
-8
srvconn.go
pkg/proxy/srvconn.go
+60
-30
srvconn_test.go
pkg/proxy/srvconn_test.go
+6
-6
switch.go
pkg/proxy/switch.go
+20
-18
userconn.go
pkg/proxy/userconn.go
+47
-0
No files found.
pkg/cctx/context.go
View file @
2be75889
...
...
@@ -12,7 +12,7 @@ type contextKey struct {
}
var
(
ContextKeyUser
=
&
contextKey
{
"
U
ser"
}
ContextKeyUser
=
&
contextKey
{
"
u
ser"
}
ContextKeyAsset
=
&
contextKey
{
"asset"
}
ContextKeySystemUser
=
&
contextKey
{
"systemUser"
}
ContextKeySSHSession
=
&
contextKey
{
"sshSession"
}
...
...
@@ -35,7 +35,7 @@ type CocoContext struct {
context
.
Context
}
//
U
ser 返回当前连接的用户model
//
u
ser 返回当前连接的用户model
func
(
ctx
*
CocoContext
)
User
()
*
model
.
User
{
return
ctx
.
Value
(
ContextKeyUser
)
.
(
*
model
.
User
)
}
...
...
pkg/common/client.go
View file @
2be75889
...
...
@@ -117,7 +117,7 @@ func (c *Client) SetReqHeaders(req *http.Request, params ...map[string]string) {
if
req
.
Header
.
Get
(
"Content-Type"
)
==
""
{
req
.
Header
.
Set
(
"Content-Type"
,
"application/json"
)
}
req
.
Header
.
Set
(
"
U
ser-Agent"
,
"coco-client"
)
req
.
Header
.
Set
(
"
u
ser-Agent"
,
"coco-client"
)
c
.
SetAuthHeader
(
req
)
}
...
...
pkg/common/client_test.go
View file @
2be75889
...
...
@@ -38,7 +38,7 @@ func TestClient_Do(t *testing.T) {
t
.
Errorf
(
"Failed Do() error: %s"
,
err
.
Error
())
}
if
len
(
res
)
!=
2
{
t
.
Errorf
(
"
U
ser not equal 2: %d"
,
len
(
res
))
t
.
Errorf
(
"
u
ser not equal 2: %d"
,
len
(
res
))
}
}
...
...
pkg/handler/session.go
View file @
2be75889
...
...
@@ -97,7 +97,7 @@ func (i *InteractiveHandler) Dispatch(ctx cctx.Context) {
if
err
!=
nil
{
if
err
!=
io
.
EOF
{
logger
.
Debug
(
"
U
ser disconnected"
)
logger
.
Debug
(
"
u
ser disconnected"
)
}
else
{
logger
.
Error
(
"Read from user err: "
,
err
)
}
...
...
@@ -342,10 +342,10 @@ func (i *InteractiveHandler) Proxy(ctx context.Context) {
// serverAuth := transport.ServerAuth{
// SessionID: uuid.NewV4().String(),
// IP: asset.Ip,
//
Port: asset.P
ort,
//
port: asset.p
ort,
// UserName: systemUser.UserName,
//
Password: systemUser.P
assword,
// PublicKey: parsePrivateKey(systemUser.
P
rivateKey)}
//
password: systemUser.p
assword,
// PublicKey: parsePrivateKey(systemUser.
p
rivateKey)}
//
// nodeConn, err := transport.NewNodeConn(i.sess.Context(), serverAuth, ptyReq, winChan)
// if err != nil {
...
...
pkg/proxy/proxy.go
View file @
2be75889
...
...
@@ -49,11 +49,11 @@ func (p *ProxyServer) Proxy() {
if
!
p
.
checkProtocol
()
{
return
}
conn
:=
SSHConnection
{
H
ost
:
"192.168.244.185"
,
P
ort
:
"22"
,
U
ser
:
"root"
,
P
assword
:
"redhat"
,
conn
:=
S
erverS
SHConnection
{
h
ost
:
"192.168.244.185"
,
p
ort
:
"22"
,
u
ser
:
"root"
,
p
assword
:
"redhat"
,
}
ptyReq
,
_
,
ok
:=
p
.
Session
.
Pty
()
if
!
ok
{
...
...
@@ -66,9 +66,9 @@ func (p *ProxyServer) Proxy() {
}
sw
:=
Switch
{
user
Session
:
p
.
Session
,
serverConn
:
&
conn
,
parser
:
parser
,
user
Conn
:
p
.
Session
,
serverConn
:
&
conn
,
parser
:
parser
,
}
_
=
sw
.
Bridge
()
_
=
conn
.
Close
()
...
...
pkg/proxy/
connectio
n.go
→
pkg/proxy/
srvcon
n.go
View file @
2be75889
...
...
@@ -11,20 +11,26 @@ import (
type
ServerConnection
interface
{
io
.
ReadWriteCloser
Name
()
string
Host
()
string
Port
()
string
User
()
string
Timeout
()
time
.
Duration
Protocol
()
string
Connect
(
h
,
w
int
,
term
string
)
error
SetWinSize
(
w
,
h
int
)
error
}
type
SSHConnection
struct
{
Host
string
Port
string
User
string
Password
string
PrivateKey
string
PrivateKeyPath
string
Timeout
time
.
Duration
Proxy
*
SSHConnection
type
ServerSSHConnection
struct
{
name
string
host
string
port
string
user
string
password
string
privateKey
string
privateKeyPath
string
timeout
time
.
Duration
Proxy
*
ServerSSHConnection
client
*
gossh
.
Client
Session
*
gossh
.
Session
...
...
@@ -34,25 +40,49 @@ type SSHConnection struct {
closed
bool
}
func
(
sc
*
SSHConnection
)
Protocol
()
string
{
func
(
sc
*
S
erverS
SHConnection
)
Protocol
()
string
{
return
"ssh"
}
func
(
sc
*
SSHConnection
)
Config
()
(
config
*
gossh
.
ClientConfig
,
err
error
)
{
func
(
sc
*
ServerSSHConnection
)
User
()
string
{
return
sc
.
user
}
func
(
sc
*
ServerSSHConnection
)
Host
()
string
{
return
sc
.
host
}
func
(
sc
*
ServerSSHConnection
)
Name
()
string
{
return
sc
.
name
}
func
(
sc
*
ServerSSHConnection
)
Port
()
string
{
return
sc
.
port
}
func
(
sc
*
ServerSSHConnection
)
Timeout
()
time
.
Duration
{
return
sc
.
timeout
}
func
(
sc
*
ServerSSHConnection
)
String
()
string
{
return
fmt
.
Sprintf
(
"%s@%s:%s"
,
sc
.
user
,
sc
.
host
,
sc
.
port
)
}
func
(
sc
*
ServerSSHConnection
)
Config
()
(
config
*
gossh
.
ClientConfig
,
err
error
)
{
authMethods
:=
make
([]
gossh
.
AuthMethod
,
0
)
if
sc
.
P
assword
!=
""
{
authMethods
=
append
(
authMethods
,
gossh
.
Password
(
sc
.
P
assword
))
if
sc
.
p
assword
!=
""
{
authMethods
=
append
(
authMethods
,
gossh
.
Password
(
sc
.
p
assword
))
}
if
sc
.
P
rivateKeyPath
!=
""
{
if
pubkey
,
err
:=
GetPubKeyFromFile
(
sc
.
P
rivateKeyPath
);
err
!=
nil
{
if
sc
.
p
rivateKeyPath
!=
""
{
if
pubkey
,
err
:=
GetPubKeyFromFile
(
sc
.
p
rivateKeyPath
);
err
!=
nil
{
err
=
fmt
.
Errorf
(
"parse private key from file error: %sc"
,
err
)
return
config
,
err
}
else
{
authMethods
=
append
(
authMethods
,
gossh
.
PublicKeys
(
pubkey
))
}
}
if
sc
.
P
rivateKey
!=
""
{
if
signer
,
err
:=
gossh
.
ParsePrivateKey
([]
byte
(
sc
.
P
rivateKey
));
err
!=
nil
{
if
sc
.
p
rivateKey
!=
""
{
if
signer
,
err
:=
gossh
.
ParsePrivateKey
([]
byte
(
sc
.
p
rivateKey
));
err
!=
nil
{
err
=
fmt
.
Errorf
(
"parse private key error: %sc"
,
err
)
return
config
,
err
}
else
{
...
...
@@ -60,15 +90,15 @@ func (sc *SSHConnection) Config() (config *gossh.ClientConfig, err error) {
}
}
config
=
&
gossh
.
ClientConfig
{
User
:
sc
.
U
ser
,
User
:
sc
.
u
ser
,
Auth
:
authMethods
,
HostKeyCallback
:
gossh
.
InsecureIgnoreHostKey
(),
Timeout
:
sc
.
T
imeout
,
Timeout
:
sc
.
t
imeout
,
}
return
config
,
nil
}
func
(
sc
*
SSHConnection
)
connect
()
(
client
*
gossh
.
Client
,
err
error
)
{
func
(
sc
*
S
erverS
SHConnection
)
connect
()
(
client
*
gossh
.
Client
,
err
error
)
{
config
,
err
:=
sc
.
Config
()
if
err
!=
nil
{
return
...
...
@@ -78,20 +108,20 @@ func (sc *SSHConnection) connect() (client *gossh.Client, err error) {
if
err
!=
nil
{
return
client
,
err
}
proxySock
,
err
:=
proxyClient
.
Dial
(
"tcp"
,
net
.
JoinHostPort
(
sc
.
Host
,
sc
.
P
ort
))
proxySock
,
err
:=
proxyClient
.
Dial
(
"tcp"
,
net
.
JoinHostPort
(
sc
.
host
,
sc
.
p
ort
))
if
err
!=
nil
{
return
client
,
err
}
proxyConn
,
chans
,
reqs
,
err
:=
gossh
.
NewClientConn
(
proxySock
,
net
.
JoinHostPort
(
sc
.
Host
,
sc
.
P
ort
),
config
)
proxyConn
,
chans
,
reqs
,
err
:=
gossh
.
NewClientConn
(
proxySock
,
net
.
JoinHostPort
(
sc
.
host
,
sc
.
p
ort
),
config
)
if
err
!=
nil
{
return
client
,
err
}
sc
.
proxyConn
=
proxyConn
client
=
gossh
.
NewClient
(
proxyConn
,
chans
,
reqs
)
}
else
{
client
,
err
=
gossh
.
Dial
(
"tcp"
,
net
.
JoinHostPort
(
sc
.
Host
,
sc
.
P
ort
),
config
)
client
,
err
=
gossh
.
Dial
(
"tcp"
,
net
.
JoinHostPort
(
sc
.
host
,
sc
.
p
ort
),
config
)
if
err
!=
nil
{
err
=
fmt
.
Errorf
(
"connect host %sc error: %sc"
,
sc
.
H
ost
,
err
)
err
=
fmt
.
Errorf
(
"connect host %sc error: %sc"
,
sc
.
h
ost
,
err
)
return
}
}
...
...
@@ -99,7 +129,7 @@ func (sc *SSHConnection) connect() (client *gossh.Client, err error) {
return
client
,
nil
}
func
(
sc
*
SSHConnection
)
invokeShell
(
h
,
w
int
,
term
string
)
(
err
error
)
{
func
(
sc
*
S
erverS
SHConnection
)
invokeShell
(
h
,
w
int
,
term
string
)
(
err
error
)
{
sess
,
err
:=
sc
.
client
.
NewSession
()
if
err
!=
nil
{
return
...
...
@@ -126,7 +156,7 @@ func (sc *SSHConnection) invokeShell(h, w int, term string) (err error) {
return
err
}
func
(
sc
*
SSHConnection
)
Connect
(
h
,
w
int
,
term
string
)
(
err
error
)
{
func
(
sc
*
S
erverS
SHConnection
)
Connect
(
h
,
w
int
,
term
string
)
(
err
error
)
{
_
,
err
=
sc
.
connect
()
if
err
!=
nil
{
return
...
...
@@ -138,19 +168,19 @@ func (sc *SSHConnection) Connect(h, w int, term string) (err error) {
return
nil
}
func
(
sc
*
SSHConnection
)
SetWinSize
(
h
,
w
int
)
error
{
func
(
sc
*
S
erverS
SHConnection
)
SetWinSize
(
h
,
w
int
)
error
{
return
sc
.
Session
.
WindowChange
(
h
,
w
)
}
func
(
sc
*
SSHConnection
)
Read
(
p
[]
byte
)
(
n
int
,
err
error
)
{
func
(
sc
*
S
erverS
SHConnection
)
Read
(
p
[]
byte
)
(
n
int
,
err
error
)
{
return
sc
.
stdout
.
Read
(
p
)
}
func
(
sc
*
SSHConnection
)
Write
(
p
[]
byte
)
(
n
int
,
err
error
)
{
func
(
sc
*
S
erverS
SHConnection
)
Write
(
p
[]
byte
)
(
n
int
,
err
error
)
{
return
sc
.
stdin
.
Write
(
p
)
}
func
(
sc
*
SSHConnection
)
Close
()
(
err
error
)
{
func
(
sc
*
S
erverS
SHConnection
)
Close
()
(
err
error
)
{
if
sc
.
closed
{
return
}
...
...
pkg/proxy/
connectio
n_test.go
→
pkg/proxy/
srvcon
n_test.go
View file @
2be75889
...
...
@@ -5,12 +5,12 @@ import (
"testing"
)
var
testConnection
=
SSHConnection
{
H
ost
:
"127.0.0.1"
,
P
ort
:
"22"
,
U
ser
:
"root"
,
P
assword
:
"redhat"
,
Proxy
:
&
S
SHConnection
{
Host
:
"192.168.244.185"
,
Port
:
"22"
,
User
:
"root"
,
P
assword
:
"redhat"
},
var
testConnection
=
S
erverS
SHConnection
{
h
ost
:
"127.0.0.1"
,
p
ort
:
"22"
,
u
ser
:
"root"
,
p
assword
:
"redhat"
,
Proxy
:
&
S
erverSSHConnection
{
host
:
"192.168.244.185"
,
port
:
"22"
,
user
:
"root"
,
p
assword
:
"redhat"
},
}
func
TestSSHConnection_Config
(
t
*
testing
.
T
)
{
...
...
pkg/proxy/switch.go
View file @
2be75889
package
proxy
import
(
"cocogo/pkg/logger"
"cocogo/pkg/service"
"context"
"github.com/ibuler/ssh"
"github.com/satori/go.uuid"
"time"
"cocogo/pkg/logger"
)
func
NewSwitch
(
user
Sess
ssh
.
Sess
ion
,
serverConn
ServerConnection
)
(
sw
*
Switch
)
{
func
NewSwitch
(
user
Conn
UserConnect
ion
,
serverConn
ServerConnection
)
(
sw
*
Switch
)
{
rules
,
err
:=
service
.
GetSystemUserFilterRules
(
""
)
if
err
!=
nil
{
logger
.
Error
(
"Get system user filter rule error: "
,
err
)
...
...
@@ -19,14 +18,14 @@ func NewSwitch(userSess ssh.Session, serverConn ServerConnection) (sw *Switch) {
cmdFilterRules
:
rules
,
}
parser
.
Initial
()
sw
=
&
Switch
{
user
Session
:
userSess
,
serverConn
:
serverConn
,
parser
:
parser
}
sw
=
&
Switch
{
user
Conn
:
userConn
,
serverConn
:
serverConn
,
parser
:
parser
}
return
sw
}
type
Switch
Info
struct
{
Id
string
`json:"id"`
type
Switch
struct
{
Id
string
User
string
`json:"user"`
Asset
string
`json:"asset"`
Server
string
`json:"asset"`
SystemUser
string
`json:"system_user"`
Org
string
`json:"org_id"`
LoginFrom
string
`json:"login_from"`
...
...
@@ -36,20 +35,23 @@ type SwitchInfo struct {
DateActive
time
.
Time
`json:"date_last_active"`
Finished
bool
`json:"is_finished"`
Closed
bool
}
type
Switch
struct
{
Info
*
SwitchInfo
parser
*
Parser
userSession
ssh
.
Session
serverConn
ServerConnection
userTran
Transport
serverTran
Transport
cancelFunc
context
.
CancelFunc
parser
*
Parser
userConn
UserConnection
serverConn
ServerConnection
userTran
Transport
serverTran
Transport
cancelFunc
context
.
CancelFunc
}
func
(
s
*
Switch
)
Initial
()
{
s
.
Id
=
uuid
.
NewV4
()
.
String
()
s
.
User
=
s
.
userConn
.
User
()
s
.
Server
=
s
.
serverConn
.
Name
()
s
.
SystemUser
=
s
.
serverConn
.
User
()
s
.
LoginFrom
=
s
.
userConn
.
LoginFrom
()
s
.
RemoteAddr
=
s
.
userConn
.
RemoteAddr
()
s
.
DateStart
=
time
.
Now
()
}
func
(
s
*
Switch
)
preBridge
()
{
...
...
@@ -128,11 +130,11 @@ func (s *Switch) readServerToUser(ctx context.Context) {
}
func
(
s
*
Switch
)
Bridge
()
(
err
error
)
{
_
,
winCh
,
_
:=
s
.
userSession
.
Pty
()
winCh
:=
s
.
userConn
.
WinCh
()
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
s
.
cancelFunc
=
cancel
s
.
userTran
=
NewDirectTransport
(
""
,
s
.
user
Sessio
n
)
s
.
userTran
=
NewDirectTransport
(
""
,
s
.
user
Con
n
)
s
.
serverTran
=
NewDirectTransport
(
""
,
s
.
serverConn
)
go
s
.
watchWindowChange
(
ctx
,
winCh
)
go
s
.
readServerToUser
(
ctx
)
...
...
pkg/proxy/userconn.go
0 → 100644
View file @
2be75889
package
proxy
import
(
"io"
"strings"
"github.com/ibuler/ssh"
)
type
UserConnection
interface
{
io
.
ReadWriteCloser
Protocol
()
string
WinCh
()
<-
chan
ssh
.
Window
User
()
string
Name
()
string
LoginFrom
()
string
RemoteAddr
()
string
}
type
SSHUserConnection
struct
{
ssh
.
Session
winch
<-
chan
ssh
.
Window
}
func
(
uc
*
SSHUserConnection
)
Protocol
()
string
{
return
"ssh"
}
func
(
uc
*
SSHUserConnection
)
User
()
string
{
return
uc
.
Session
.
User
()
}
func
(
uc
*
SSHUserConnection
)
WinCh
()
(
winch
<-
chan
ssh
.
Window
)
{
_
,
winch
,
ok
:=
uc
.
Pty
()
if
ok
{
return
}
return
nil
}
func
(
uc
*
SSHUserConnection
)
LoginFrom
()
string
{
return
"T"
}
func
(
uc
*
SSHUserConnection
)
RemoteAddr
()
string
{
return
strings
.
Split
(
uc
.
Session
.
RemoteAddr
()
.
String
(),
":"
)[
0
]
}
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