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
e70545d3
Commit
e70545d3
authored
Jun 05, 2019
by
ibuler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
{update} tij
parent
774c4ac4
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
114 additions
and
103 deletions
+114
-103
Makefile
cmd/Makefile
+11
-9
client.go
pkg/common/client.go
+1
-1
sftp.go
pkg/handler/sftp.go
+46
-54
data.go
pkg/httpd/data.go
+1
-1
elfhandler.go
pkg/httpd/elfhandler.go
+2
-2
handler.go
pkg/httpd/handler.go
+18
-15
server.go
pkg/httpd/server.go
+3
-4
init.go
pkg/service/init.go
+7
-0
urls.go
pkg/service/urls.go
+1
-1
users.go
pkg/service/users.go
+4
-3
connmanager.go
pkg/srvconn/connmanager.go
+19
-12
sshconn.go
pkg/srvconn/sshconn.go
+1
-1
No files found.
cmd/Makefile
View file @
e70545d3
BRANCH
:=
$(
shell
git symbolic-ref HEAD 2>/dev/null |
cut
-d
"/"
-f
3
)
BRANCH
:=
$(
shell
git symbolic-ref HEAD 2>/dev/null |
cut
-d
"/"
-f
3
)
BUILD
:=
$(
shell
git rev-parse
--short
HEAD
)
BUILD
:=
$(
shell
git rev-parse
--short
HEAD
)
VERSION
=
$(BRANCH)
-
$(BUILD)
VERSION
=
$(BRANCH)
-
$(BUILD)
NAME
:=
coco
NAME
:=
coco
BASEPATH
:=
$(
shell
pwd
)
BASEPATH
:=
$(
shell
pwd
)
CGO_ENABLED
=
0
CGO_ENABLED
=
0
GOCMD
=
go
GOCMD
=
go
GOBUILD
=
$(GOCMD)
build
GOBUILD
=
$(GOCMD)
build
ASSETS
=
$(
shell
echo
"locale static templates coco config_example.yml"
)
SOFTWARENAME
=
$(NAME)
-
$(VERSION)
COCOSRCFILE
=
coco.go
SOFTWARENAME
=
$(NAME)
-
$(VERSION)
COCOSRCFILE
=
coco.go
.PHONY
:
windows
.PHONY
:
windows
windows
:
windows
:
@
echo
"编译windows"
@
echo
"编译windows"
mkdir
-p
$(BASEPATH)
/../build
mkdir
-p
$(BASEPATH)
/../build
GOOS
=
windows
GOARCH
=
amd64 go build
-o
$(BASEPATH)
/
$(
SOFTWARENAME)
-windows-amd64
$(COCOSRCFILE)
GOOS
=
windows
GOARCH
=
amd64 go build
-o
$(BASEPATH)
/
$(
NAME)
$(COCOSRCFILE)
tar
czvf
$(BASEPATH)
/../build/
$(SOFTWARENAME)
-windows-amd64
.tar.gz
$(SOFTWARENAME)
-windows-amd64
locale/ config_example.yml
tar
czvf
$(BASEPATH)
/../build/
$(SOFTWARENAME)
-windows-amd64
.tar.gz
$(SOFTWARENAME)
-windows-amd64
locale/ config_example.yml
.PHONY
:
linux
.PHONY
:
linux
linux
:
linux
:
@
echo
"编译linux"
@
echo
"编译linux"
mkdir
-p
$(BASEPATH)
/../build
mkdir
-p
$(BASEPATH)
/../build
GOOS
=
linux
GOARCH
=
amd64 go build
-o
$(BASEPATH)
/
$(
SOFTWARENAME)
-linux-amd64
$(COCOSRCFILE)
GOOS
=
linux
GOARCH
=
amd64 go build
-o
$(BASEPATH)
/
$(
NAME)
$(COCOSRCFILE)
tar
czvf
$(BASEPATH)
/../build/
$(SOFTWARENAME)
-linux-amd64
.tar.gz
$(
SOFTWARENAME)
-linux-amd64
locale/ config_example.yml
tar
czvf
$(BASEPATH)
/../build/
$(SOFTWARENAME)
-linux-amd64
.tar.gz
$(
ASSETS)
.PHONY
:
darwin
.PHONY
:
darwin
darwin
:
darwin
:
@
echo
"编译darwin"
@
echo
"编译darwin"
mkdir
-p
$(BASEPATH)
/../build
mkdir
-p
$(BASEPATH)
/../build
GOOS
=
darwin
GOARCH
=
amd64 go build
-o
$(BASEPATH)
/
$(
SOFTWARENAME)
-darwin-amd64
$(COCOSRCFILE)
GOOS
=
darwin
GOARCH
=
amd64 go build
-o
$(BASEPATH)
/
$(
NAME)
$(COCOSRCFILE)
tar
czvf
$(BASEPATH)
/../build/
$(SOFTWARENAME)
-darwin-amd64
.tar.gz
$(SOFTWARENAME)
-darwin-amd64
locale/ config_example.yml
tar
czvf
$(BASEPATH)
/../build/
$(SOFTWARENAME)
-darwin-amd64
.tar.gz
$(SOFTWARENAME)
-darwin-amd64
locale/ config_example.yml
.PHONY
:
docker
.PHONY
:
docker
...
...
pkg/common/client.go
View file @
e70545d3
...
@@ -38,7 +38,7 @@ type UrlParser interface {
...
@@ -38,7 +38,7 @@ type UrlParser interface {
func
NewClient
(
timeout
time
.
Duration
,
baseHost
string
)
*
Client
{
func
NewClient
(
timeout
time
.
Duration
,
baseHost
string
)
*
Client
{
headers
:=
make
(
map
[
string
]
string
,
0
)
headers
:=
make
(
map
[
string
]
string
,
0
)
client
:=
http
.
DefaultClient
client
:=
new
(
http
.
Client
)
client
.
Timeout
=
timeout
*
time
.
Second
client
.
Timeout
=
timeout
*
time
.
Second
return
&
Client
{
return
&
Client
{
BaseHost
:
baseHost
,
BaseHost
:
baseHost
,
...
...
pkg/handler/sftp.go
View file @
e70545d3
...
@@ -3,9 +3,7 @@ package handler
...
@@ -3,9 +3,7 @@ package handler
import
(
import
(
"fmt"
"fmt"
"io"
"io"
"net"
"os"
"os"
"strconv"
"strings"
"strings"
"sync"
"sync"
"syscall"
"syscall"
...
@@ -13,7 +11,6 @@ import (
...
@@ -13,7 +11,6 @@ import (
"github.com/gliderlabs/ssh"
"github.com/gliderlabs/ssh"
"github.com/pkg/sftp"
"github.com/pkg/sftp"
gossh
"golang.org/x/crypto/ssh"
gossh
"golang.org/x/crypto/ssh"
"cocogo/pkg/cctx"
"cocogo/pkg/cctx"
...
@@ -21,38 +18,47 @@ import (
...
@@ -21,38 +18,47 @@ import (
"cocogo/pkg/logger"
"cocogo/pkg/logger"
"cocogo/pkg/model"
"cocogo/pkg/model"
"cocogo/pkg/service"
"cocogo/pkg/service"
"cocogo/pkg/srvconn"
)
)
func
SftpHandler
(
sess
ssh
.
Session
)
{
func
SftpHandler
(
sess
ssh
.
Session
)
{
ctx
,
cancel
:=
cctx
.
NewContext
(
sess
)
ctx
,
cancel
:=
cctx
.
NewContext
(
sess
)
defer
cancel
()
defer
cancel
()
userhandler
:=
&
userSftpRequests
{
user
:
ctx
.
User
()}
handler
:=
&
sftpHandler
{
user
:
ctx
.
User
()}
userhandler
.
initial
()
handler
.
initial
()
hs
:=
sftp
.
Handlers
{
handlers
:=
sftp
.
Handlers
{
FileGet
:
userhandler
,
FileGet
:
handler
,
FilePut
:
userhandler
,
FilePut
:
handler
,
FileCmd
:
userhandler
,
FileCmd
:
handler
,
FileList
:
userhandler
}
FileList
:
handler
,
}
req
:=
sftp
.
NewRequestServer
(
sess
,
hs
)
req
:=
sftp
.
NewRequestServer
(
sess
,
h
andler
s
)
if
err
:=
req
.
Serve
();
err
==
io
.
EOF
{
if
err
:=
req
.
Serve
();
err
==
io
.
EOF
{
_
=
req
.
Close
()
_
=
req
.
Close
()
hosts
:=
handler
.
hosts
for
hostname
,
dir
:=
range
hosts
{
for
name
,
d
:=
range
dir
.
suMaps
{
srvconn
.
RecycleClient
(
d
.
conn
)
delete
(
dir
.
suMaps
,
name
)
}
delete
(
hosts
,
hostname
)
}
logger
.
Info
(
"sftp client exited session."
)
logger
.
Info
(
"sftp client exited session."
)
}
else
if
err
!=
nil
{
}
else
if
err
!=
nil
{
logger
.
Error
(
"sftp server completed with error:"
,
err
)
logger
.
Error
(
"sftp server completed with error:"
,
err
)
}
}
}
}
type
userSftpRequests
struct
{
type
sftpHandler
struct
{
user
*
model
.
User
user
*
model
.
User
assets
model
.
AssetList
assets
model
.
AssetList
rootPath
string
// tmp || home || ~
rootPath
string
// tmp || home || ~
hosts
map
[
string
]
*
HostNameDir
hosts
map
[
string
]
*
HostNameDir
}
}
func
(
fs
*
userSftpRequests
)
initial
()
{
func
(
fs
*
sftpHandler
)
initial
()
{
fs
.
loadAssets
()
fs
.
loadAssets
()
fs
.
hosts
=
make
(
map
[
string
]
*
HostNameDir
)
fs
.
hosts
=
make
(
map
[
string
]
*
HostNameDir
)
fs
.
rootPath
=
config
.
GetConf
()
.
SftpRoot
fs
.
rootPath
=
config
.
GetConf
()
.
SftpRoot
...
@@ -67,11 +73,11 @@ func (fs *userSftpRequests) initial() {
...
@@ -67,11 +73,11 @@ func (fs *userSftpRequests) initial() {
}
}
}
}
func
(
fs
*
userSftpRequests
)
loadAssets
()
{
func
(
fs
*
sftpHandler
)
loadAssets
()
{
fs
.
assets
=
service
.
GetUserAssets
(
fs
.
user
.
ID
,
"1"
)
fs
.
assets
=
service
.
GetUserAssets
(
fs
.
user
.
ID
,
"1"
)
}
}
func
(
fs
*
userSftpRequests
)
Filelist
(
r
*
sftp
.
Request
)
(
sftp
.
ListerAt
,
error
)
{
func
(
fs
*
sftpHandler
)
Filelist
(
r
*
sftp
.
Request
)
(
sftp
.
ListerAt
,
error
)
{
var
fileInfos
=
listerat
{}
var
fileInfos
=
listerat
{}
var
err
error
var
err
error
logger
.
Debug
(
"list path: "
,
r
.
Filepath
)
logger
.
Debug
(
"list path: "
,
r
.
Filepath
)
...
@@ -116,10 +122,12 @@ func (fs *userSftpRequests) Filelist(r *sftp.Request) (sftp.ListerAt, error) {
...
@@ -116,10 +122,12 @@ func (fs *userSftpRequests) Filelist(r *sftp.Request) (sftp.ListerAt, error) {
}
}
realPath
=
sysUserDir
.
ParsePath
(
r
.
Filepath
)
realPath
=
sysUserDir
.
ParsePath
(
r
.
Filepath
)
if
sysUserDir
.
client
==
nil
{
if
sysUserDir
.
client
==
nil
{
sysUserDir
.
client
,
err
=
fs
.
GetSftpClient
(
hostDir
.
asset
,
sysUserDir
.
systemUser
)
client
,
conn
,
err
:
=
fs
.
GetSftpClient
(
hostDir
.
asset
,
sysUserDir
.
systemUser
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
sftp
.
ErrSshFxPermissionDenied
return
nil
,
sftp
.
ErrSshFxPermissionDenied
}
}
sysUserDir
.
client
=
client
sysUserDir
.
conn
=
conn
}
}
switch
r
.
Method
{
switch
r
.
Method
{
...
@@ -144,7 +152,7 @@ func (fs *userSftpRequests) Filelist(r *sftp.Request) (sftp.ListerAt, error) {
...
@@ -144,7 +152,7 @@ func (fs *userSftpRequests) Filelist(r *sftp.Request) (sftp.ListerAt, error) {
return
fileInfos
,
err
return
fileInfos
,
err
}
}
func
(
fs
*
userSftpRequests
)
Filecmd
(
r
*
sftp
.
Request
)
error
{
func
(
fs
*
sftpHandler
)
Filecmd
(
r
*
sftp
.
Request
)
error
{
logger
.
Debug
(
"File cmd: "
,
r
.
Filepath
)
logger
.
Debug
(
"File cmd: "
,
r
.
Filepath
)
pathNames
:=
strings
.
Split
(
strings
.
TrimPrefix
(
r
.
Filepath
,
"/"
),
"/"
)
pathNames
:=
strings
.
Split
(
strings
.
TrimPrefix
(
r
.
Filepath
,
"/"
),
"/"
)
if
len
(
pathNames
)
<=
2
{
if
len
(
pathNames
)
<=
2
{
...
@@ -153,11 +161,12 @@ func (fs *userSftpRequests) Filecmd(r *sftp.Request) error {
...
@@ -153,11 +161,12 @@ func (fs *userSftpRequests) Filecmd(r *sftp.Request) error {
hostDir
:=
fs
.
hosts
[
pathNames
[
0
]]
hostDir
:=
fs
.
hosts
[
pathNames
[
0
]]
suDir
:=
hostDir
.
suMaps
[
pathNames
[
1
]]
suDir
:=
hostDir
.
suMaps
[
pathNames
[
1
]]
if
suDir
.
client
==
nil
{
if
suDir
.
client
==
nil
{
client
,
err
:=
fs
.
GetSftpClient
(
hostDir
.
asset
,
suDir
.
systemUser
)
client
,
conn
,
err
:=
fs
.
GetSftpClient
(
hostDir
.
asset
,
suDir
.
systemUser
)
if
err
!=
nil
{
if
err
!=
nil
{
return
sftp
.
ErrSshFxPermissionDenied
return
sftp
.
ErrSshFxPermissionDenied
}
}
suDir
.
client
=
client
suDir
.
client
=
client
suDir
.
conn
=
conn
}
}
realPathName
:=
suDir
.
ParsePath
(
r
.
Filepath
)
realPathName
:=
suDir
.
ParsePath
(
r
.
Filepath
)
switch
r
.
Method
{
switch
r
.
Method
{
...
@@ -179,7 +188,7 @@ func (fs *userSftpRequests) Filecmd(r *sftp.Request) error {
...
@@ -179,7 +188,7 @@ func (fs *userSftpRequests) Filecmd(r *sftp.Request) error {
return
nil
return
nil
}
}
func
(
fs
*
userSftpRequests
)
Filewrite
(
r
*
sftp
.
Request
)
(
io
.
WriterAt
,
error
)
{
func
(
fs
*
sftpHandler
)
Filewrite
(
r
*
sftp
.
Request
)
(
io
.
WriterAt
,
error
)
{
logger
.
Debug
(
"File write: "
,
r
.
Filepath
)
logger
.
Debug
(
"File write: "
,
r
.
Filepath
)
pathNames
:=
strings
.
Split
(
strings
.
TrimPrefix
(
r
.
Filepath
,
"/"
),
"/"
)
pathNames
:=
strings
.
Split
(
strings
.
TrimPrefix
(
r
.
Filepath
,
"/"
),
"/"
)
if
len
(
pathNames
)
<=
2
{
if
len
(
pathNames
)
<=
2
{
...
@@ -188,18 +197,19 @@ func (fs *userSftpRequests) Filewrite(r *sftp.Request) (io.WriterAt, error) {
...
@@ -188,18 +197,19 @@ func (fs *userSftpRequests) Filewrite(r *sftp.Request) (io.WriterAt, error) {
hostDir
:=
fs
.
hosts
[
pathNames
[
0
]]
hostDir
:=
fs
.
hosts
[
pathNames
[
0
]]
suDir
:=
hostDir
.
suMaps
[
pathNames
[
1
]]
suDir
:=
hostDir
.
suMaps
[
pathNames
[
1
]]
if
suDir
.
client
==
nil
{
if
suDir
.
client
==
nil
{
client
,
err
:=
fs
.
GetSftpClient
(
hostDir
.
asset
,
suDir
.
systemUser
)
client
,
conn
,
err
:=
fs
.
GetSftpClient
(
hostDir
.
asset
,
suDir
.
systemUser
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
sftp
.
ErrSshFxPermissionDenied
return
nil
,
sftp
.
ErrSshFxPermissionDenied
}
}
suDir
.
client
=
client
suDir
.
client
=
client
suDir
.
conn
=
conn
}
}
realPathName
:=
suDir
.
ParsePath
(
r
.
Filepath
)
realPathName
:=
suDir
.
ParsePath
(
r
.
Filepath
)
f
,
err
:=
suDir
.
client
.
Create
(
realPathName
)
f
,
err
:=
suDir
.
client
.
Create
(
realPathName
)
return
NewWriterAt
(
f
),
err
return
NewWriterAt
(
f
),
err
}
}
func
(
fs
*
userSftpRequests
)
Fileread
(
r
*
sftp
.
Request
)
(
io
.
ReaderAt
,
error
)
{
func
(
fs
*
sftpHandler
)
Fileread
(
r
*
sftp
.
Request
)
(
io
.
ReaderAt
,
error
)
{
logger
.
Debug
(
"File read: "
,
r
.
Filepath
)
logger
.
Debug
(
"File read: "
,
r
.
Filepath
)
pathNames
:=
strings
.
Split
(
strings
.
TrimPrefix
(
r
.
Filepath
,
"/"
),
"/"
)
pathNames
:=
strings
.
Split
(
strings
.
TrimPrefix
(
r
.
Filepath
,
"/"
),
"/"
)
if
len
(
pathNames
)
<=
2
{
if
len
(
pathNames
)
<=
2
{
...
@@ -208,11 +218,12 @@ func (fs *userSftpRequests) Fileread(r *sftp.Request) (io.ReaderAt, error) {
...
@@ -208,11 +218,12 @@ func (fs *userSftpRequests) Fileread(r *sftp.Request) (io.ReaderAt, error) {
hostDir
:=
fs
.
hosts
[
pathNames
[
0
]]
hostDir
:=
fs
.
hosts
[
pathNames
[
0
]]
suDir
:=
hostDir
.
suMaps
[
pathNames
[
1
]]
suDir
:=
hostDir
.
suMaps
[
pathNames
[
1
]]
if
suDir
.
client
==
nil
{
if
suDir
.
client
==
nil
{
client
,
err
:=
fs
.
GetSftpClient
(
hostDir
.
asset
,
suDir
.
systemUser
)
ftpClient
,
client
,
err
:=
fs
.
GetSftpClient
(
hostDir
.
asset
,
suDir
.
systemUser
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
sftp
.
ErrSshFxPermissionDenied
return
nil
,
sftp
.
ErrSshFxPermissionDenied
}
}
suDir
.
client
=
client
suDir
.
client
=
ftpClient
suDir
.
conn
=
client
}
}
realPathName
:=
suDir
.
ParsePath
(
r
.
Filepath
)
realPathName
:=
suDir
.
ParsePath
(
r
.
Filepath
)
f
,
err
:=
suDir
.
client
.
Open
(
realPathName
)
f
,
err
:=
suDir
.
client
.
Open
(
realPathName
)
...
@@ -222,10 +233,16 @@ func (fs *userSftpRequests) Fileread(r *sftp.Request) (io.ReaderAt, error) {
...
@@ -222,10 +233,16 @@ func (fs *userSftpRequests) Fileread(r *sftp.Request) (io.ReaderAt, error) {
return
NewReaderAt
(
f
),
err
return
NewReaderAt
(
f
),
err
}
}
func
(
fs
*
userSftpRequests
)
GetSftpClient
(
asset
*
model
.
Asset
,
sysUser
*
model
.
SystemUser
)
(
*
sftp
.
Client
,
error
)
{
func
(
fs
*
sftpHandler
)
GetSftpClient
(
asset
*
model
.
Asset
,
sysUser
*
model
.
SystemUser
)
(
sftpClient
*
sftp
.
Client
,
sshClient
*
gossh
.
Client
,
err
error
)
{
logger
.
Debug
(
"Get Sftp Client"
)
sshClient
,
err
=
srvconn
.
NewClient
(
fs
.
user
,
asset
,
sysUser
,
config
.
GetConf
()
.
SSHTimeout
*
time
.
Second
)
info
:=
service
.
GetSystemUserAssetAuthInfo
(
sysUser
.
ID
,
asset
.
ID
)
if
err
!=
nil
{
return
CreateSFTPConn
(
sysUser
.
Username
,
info
.
Password
,
info
.
PrivateKey
,
asset
.
IP
,
strconv
.
Itoa
(
asset
.
Port
))
return
}
sftpClient
,
err
=
sftp
.
NewClient
(
sshClient
)
if
err
!=
nil
{
return
}
return
sftpClient
,
sshClient
,
nil
}
}
type
HostNameDir
struct
{
type
HostNameDir
struct
{
...
@@ -259,6 +276,7 @@ type SysUserDir struct {
...
@@ -259,6 +276,7 @@ type SysUserDir struct {
systemUser
*
model
.
SystemUser
systemUser
*
model
.
SystemUser
time
time
.
Time
time
time
.
Time
client
*
sftp
.
Client
client
*
sftp
.
Client
conn
*
gossh
.
Client
}
}
func
(
su
*
SysUserDir
)
Name
()
string
{
return
su
.
systemUser
.
Name
}
func
(
su
*
SysUserDir
)
Name
()
string
{
return
su
.
systemUser
.
Name
}
...
@@ -388,29 +406,3 @@ func (c *clientReadWritAt) ReadAt(p []byte, off int64) (n int, err error) {
...
@@ -388,29 +406,3 @@ func (c *clientReadWritAt) ReadAt(p []byte, off int64) (n int, err error) {
return
nr
,
err
return
nr
,
err
}
}
func
CreateSFTPConn
(
user
,
password
,
privateKey
,
host
,
port
string
)
(
*
sftp
.
Client
,
error
)
{
authMethods
:=
make
([]
gossh
.
AuthMethod
,
0
)
if
password
!=
""
{
authMethods
=
append
(
authMethods
,
gossh
.
Password
(
password
))
}
if
privateKey
!=
""
{
if
signer
,
err
:=
gossh
.
ParsePrivateKey
([]
byte
(
privateKey
));
err
!=
nil
{
err
=
fmt
.
Errorf
(
"parse private key error: %sc"
,
err
)
}
else
{
authMethods
=
append
(
authMethods
,
gossh
.
PublicKeys
(
signer
))
}
}
config
:=
&
gossh
.
ClientConfig
{
User
:
user
,
Auth
:
authMethods
,
HostKeyCallback
:
gossh
.
InsecureIgnoreHostKey
(),
Timeout
:
300
*
time
.
Second
,
}
client
,
err
:=
gossh
.
Dial
(
"tcp"
,
net
.
JoinHostPort
(
host
,
port
),
config
)
if
err
!=
nil
{
return
nil
,
err
}
return
sftp
.
NewClient
(
client
)
}
pkg/httpd/data.go
View file @
e70545d3
...
@@ -23,7 +23,7 @@ type DataMsg struct {
...
@@ -23,7 +23,7 @@ type DataMsg struct {
Room
string
`json:"room"`
Room
string
`json:"room"`
}
}
type
Emit
RoomMsg
struct
{
type
RoomMsg
struct
{
Room
string
`json:"room"`
Room
string
`json:"room"`
Secret
string
`json:"secret"`
Secret
string
`json:"secret"`
}
}
...
...
pkg/httpd/elfhandler.go
View file @
e70545d3
...
@@ -10,7 +10,7 @@ import (
...
@@ -10,7 +10,7 @@ import (
"github.com/gorilla/mux"
"github.com/gorilla/mux"
)
)
func
OnE
lf
inderConnect
(
s
socketio
.
Conn
)
error
{
func
OnE
LF
inderConnect
(
s
socketio
.
Conn
)
error
{
u
:=
s
.
URL
()
u
:=
s
.
URL
()
sid
:=
u
.
Query
()
.
Get
(
"sid"
)
sid
:=
u
.
Query
()
.
Get
(
"sid"
)
data
:=
EmitSidMsg
{
Sid
:
sid
}
data
:=
EmitSidMsg
{
Sid
:
sid
}
...
@@ -18,7 +18,7 @@ func OnElfinderConnect(s socketio.Conn) error {
...
@@ -18,7 +18,7 @@ func OnElfinderConnect(s socketio.Conn) error {
return
nil
return
nil
}
}
func
OnE
lf
inderDisconnect
(
s
socketio
.
Conn
,
msg
string
)
{
func
OnE
LF
inderDisconnect
(
s
socketio
.
Conn
,
msg
string
)
{
u
:=
s
.
URL
()
u
:=
s
.
URL
()
sid
:=
u
.
Query
()
.
Get
(
"sid"
)
sid
:=
u
.
Query
()
.
Get
(
"sid"
)
log
.
Println
(
"disconnect: "
,
sid
)
log
.
Println
(
"disconnect: "
,
sid
)
...
...
pkg/httpd/handler.go
View file @
e70545d3
...
@@ -39,7 +39,7 @@ func AuthDecorator(handler http.HandlerFunc) http.HandlerFunc {
...
@@ -39,7 +39,7 @@ func AuthDecorator(handler http.HandlerFunc) http.HandlerFunc {
// OnConnectHandler 当websocket连接后触发
// OnConnectHandler 当websocket连接后触发
func
OnConnectHandler
(
s
socketio
.
Conn
)
error
{
func
OnConnectHandler
(
s
socketio
.
Conn
)
error
{
// 首次连接 1.获取当前用户的信息
// 首次连接 1.获取当前用户的信息
logger
.
Debug
(
"
On connec
t trigger"
)
logger
.
Debug
(
"
Web terminal on connect even
t trigger"
)
cookies
:=
strings
.
Split
(
s
.
RemoteHeader
()
.
Get
(
"Cookie"
),
";"
)
cookies
:=
strings
.
Split
(
s
.
RemoteHeader
()
.
Get
(
"Cookie"
),
";"
)
var
csrfToken
,
sessionID
,
remoteIP
string
var
csrfToken
,
sessionID
,
remoteIP
string
for
_
,
line
:=
range
cookies
{
for
_
,
line
:=
range
cookies
{
...
@@ -62,25 +62,22 @@ func OnConnectHandler(s socketio.Conn) error {
...
@@ -62,25 +62,22 @@ func OnConnectHandler(s socketio.Conn) error {
}
else
{
}
else
{
remoteIP
=
strings
.
Split
(
remoteAddr
,
","
)[
0
]
remoteIP
=
strings
.
Split
(
remoteAddr
,
","
)[
0
]
}
}
logger
.
Infof
(
"
%s connect websocket from %s
\n
"
,
user
.
Username
,
remoteIP
)
logger
.
Infof
(
"
Accepted %s connect websocket from %s
"
,
user
.
Username
,
remoteIP
)
conn
:=
newWebConn
(
s
.
ID
(),
s
,
remoteIP
,
user
)
conn
:=
newWebConn
(
s
.
ID
(),
s
,
remoteIP
,
user
)
ctx
:=
WebContext
{
User
:
user
,
Connection
:
conn
}
ctx
:=
WebContext
{
User
:
user
,
Connection
:
conn
}
s
.
SetContext
(
ctx
)
s
.
SetContext
(
ctx
)
conns
.
AddWebConn
(
s
.
ID
(),
conn
)
conns
.
AddWebConn
(
s
.
ID
(),
conn
)
logger
.
Info
(
"On Connect handler end"
)
return
nil
return
nil
}
}
// OnErrorHandler 当出现错误时触发
// OnErrorHandler 当出现错误时触发
func
OnErrorHandler
(
e
error
)
{
func
OnErrorHandler
(
e
error
)
{
logger
.
Debug
(
"OnError trigger"
)
logger
.
Debug
(
"Web terminal on error trigger: "
,
e
)
logger
.
Debug
(
e
)
}
}
// OnHostHandler 当用户连接Host时触发
// OnHostHandler 当用户连接Host时触发
func
OnHostHandler
(
s
socketio
.
Conn
,
message
HostMsg
)
{
func
OnHostHandler
(
s
socketio
.
Conn
,
message
HostMsg
)
{
// secret uuid string
logger
.
Debug
(
"Web terminal on host event trigger"
)
logger
.
Debug
(
"On host event trigger"
)
win
:=
ssh
.
Window
{
Height
:
24
,
Width
:
80
}
win
:=
ssh
.
Window
{
Height
:
24
,
Width
:
80
}
assetID
:=
message
.
Uuid
assetID
:=
message
.
Uuid
systemUserID
:=
message
.
UserID
systemUserID
:=
message
.
UserID
...
@@ -93,15 +90,15 @@ func OnHostHandler(s socketio.Conn, message HostMsg) {
...
@@ -93,15 +90,15 @@ func OnHostHandler(s socketio.Conn, message HostMsg) {
win
.
Height
=
height
win
.
Height
=
height
}
}
clientID
:=
uuid
.
NewV4
()
.
String
()
clientID
:=
uuid
.
NewV4
()
.
String
()
emitMsg
:=
Emit
RoomMsg
{
clientID
,
secret
}
emitMsg
:=
RoomMsg
{
clientID
,
secret
}
s
.
Emit
(
"room"
,
emitMsg
)
s
.
Emit
(
"room"
,
emitMsg
)
logger
.
Debug
(
"Asset id: "
,
assetID
)
asset
:=
service
.
GetAsset
(
assetID
)
asset
:=
service
.
GetAsset
(
assetID
)
systemUser
:=
service
.
GetSystemUser
(
systemUserID
)
systemUser
:=
service
.
GetSystemUser
(
systemUserID
)
if
asset
.
ID
==
""
||
systemUser
.
ID
==
""
{
if
asset
.
ID
==
""
||
systemUser
.
ID
==
""
{
return
return
}
}
logger
.
Debug
(
"Web terminal want to connect host: "
,
asset
.
Hostname
)
ctx
:=
s
.
Context
()
.
(
WebContext
)
ctx
:=
s
.
Context
()
.
(
WebContext
)
userR
,
userW
:=
io
.
Pipe
()
userR
,
userW
:=
io
.
Pipe
()
...
@@ -118,12 +115,15 @@ func OnHostHandler(s socketio.Conn, message HostMsg) {
...
@@ -118,12 +115,15 @@ func OnHostHandler(s socketio.Conn, message HostMsg) {
UserConn
:
client
,
User
:
ctx
.
User
,
UserConn
:
client
,
User
:
ctx
.
User
,
Asset
:
&
asset
,
SystemUser
:
&
systemUser
,
Asset
:
&
asset
,
SystemUser
:
&
systemUser
,
}
}
go
proxySrv
.
Proxy
()
go
func
()
{
proxySrv
.
Proxy
()
s
.
Emit
(
"logout"
,
RoomMsg
{
Room
:
clientID
})
}()
}
}
// OnTokenHandler 当使用token连接时触发
// OnTokenHandler 当使用token连接时触发
func
OnTokenHandler
(
s
socketio
.
Conn
,
message
TokenMsg
)
{
func
OnTokenHandler
(
s
socketio
.
Conn
,
message
TokenMsg
)
{
logger
.
Debug
(
"
O
n token event trigger"
)
logger
.
Debug
(
"
Web terminal o
n token event trigger"
)
win
:=
ssh
.
Window
{
Height
:
24
,
Width
:
80
}
win
:=
ssh
.
Window
{
Height
:
24
,
Width
:
80
}
token
:=
message
.
Token
token
:=
message
.
Token
secret
:=
message
.
Secret
secret
:=
message
.
Secret
...
@@ -135,7 +135,7 @@ func OnTokenHandler(s socketio.Conn, message TokenMsg) {
...
@@ -135,7 +135,7 @@ func OnTokenHandler(s socketio.Conn, message TokenMsg) {
win
.
Height
=
height
win
.
Height
=
height
}
}
clientID
:=
uuid
.
NewV4
()
.
String
()
clientID
:=
uuid
.
NewV4
()
.
String
()
emitMs
:=
Emit
RoomMsg
{
clientID
,
secret
}
emitMs
:=
RoomMsg
{
clientID
,
secret
}
s
.
Emit
(
"room"
,
emitMs
)
s
.
Emit
(
"room"
,
emitMs
)
// check token
// check token
...
@@ -177,7 +177,10 @@ func OnTokenHandler(s socketio.Conn, message TokenMsg) {
...
@@ -177,7 +177,10 @@ func OnTokenHandler(s socketio.Conn, message TokenMsg) {
UserConn
:
&
client
,
User
:
currentUser
,
UserConn
:
&
client
,
User
:
currentUser
,
Asset
:
&
asset
,
SystemUser
:
&
systemUser
,
Asset
:
&
asset
,
SystemUser
:
&
systemUser
,
}
}
go
proxySrv
.
Proxy
()
go
func
()
{
proxySrv
.
Proxy
()
s
.
Emit
(
"logout"
,
RoomMsg
{
Room
:
clientID
})
}()
}
}
// OnDataHandler 收发数据时触发
// OnDataHandler 收发数据时触发
...
@@ -193,15 +196,15 @@ func OnDataHandler(s socketio.Conn, message DataMsg) {
...
@@ -193,15 +196,15 @@ func OnDataHandler(s socketio.Conn, message DataMsg) {
// OnResizeHandler 用户窗口改变时触发
// OnResizeHandler 用户窗口改变时触发
func
OnResizeHandler
(
s
socketio
.
Conn
,
message
ResizeMsg
)
{
func
OnResizeHandler
(
s
socketio
.
Conn
,
message
ResizeMsg
)
{
logger
.
Debugf
(
"Web terminal on resize event trigger: %d*%d"
,
message
.
Width
,
message
.
Height
)
winSize
:=
ssh
.
Window
{
Height
:
message
.
Height
,
Width
:
message
.
Width
}
winSize
:=
ssh
.
Window
{
Height
:
message
.
Height
,
Width
:
message
.
Width
}
logger
.
Debugf
(
"On resize event trigger: %d*%d"
,
message
.
Width
,
message
.
Height
)
conn
:=
conns
.
GetWebConn
(
s
.
ID
())
conn
:=
conns
.
GetWebConn
(
s
.
ID
())
conn
.
SetWinSize
(
winSize
)
conn
.
SetWinSize
(
winSize
)
}
}
// OnLogoutHandler 用户登出一个会话时触发
// OnLogoutHandler 用户登出一个会话时触发
func
OnLogoutHandler
(
s
socketio
.
Conn
,
message
string
)
{
func
OnLogoutHandler
(
s
socketio
.
Conn
,
message
string
)
{
logger
.
Debug
(
"
O
n logout event trigger"
)
logger
.
Debug
(
"
Web terminal o
n logout event trigger"
)
conn
:=
conns
.
GetWebConn
(
s
.
ID
())
conn
:=
conns
.
GetWebConn
(
s
.
ID
())
if
conn
==
nil
{
if
conn
==
nil
{
logger
.
Error
(
"No conn found"
)
logger
.
Error
(
"No conn found"
)
...
...
pkg/httpd/server.go
View file @
e70545d3
...
@@ -31,8 +31,8 @@ func StartHTTPServer() {
...
@@ -31,8 +31,8 @@ func StartHTTPServer() {
server
.
OnEvent
(
"/ssh"
,
"resize"
,
OnResizeHandler
)
server
.
OnEvent
(
"/ssh"
,
"resize"
,
OnResizeHandler
)
server
.
OnEvent
(
"/ssh"
,
"logout"
,
OnLogoutHandler
)
server
.
OnEvent
(
"/ssh"
,
"logout"
,
OnLogoutHandler
)
server
.
OnConnect
(
"/elfinder"
,
OnE
lf
inderConnect
)
server
.
OnConnect
(
"/elfinder"
,
OnE
LF
inderConnect
)
server
.
OnDisconnect
(
"/elfinder"
,
OnE
lf
inderDisconnect
)
server
.
OnDisconnect
(
"/elfinder"
,
OnE
LF
inderDisconnect
)
go
server
.
Serve
()
go
server
.
Serve
()
defer
server
.
Close
()
defer
server
.
Close
()
...
@@ -44,8 +44,7 @@ func StartHTTPServer() {
...
@@ -44,8 +44,7 @@ func StartHTTPServer() {
router
.
Handle
(
"/socket.io/"
,
server
)
router
.
Handle
(
"/socket.io/"
,
server
)
router
.
HandleFunc
(
"/coco/elfinder/sftp/{host}/"
,
sftpHostFinder
)
router
.
HandleFunc
(
"/coco/elfinder/sftp/{host}/"
,
sftpHostFinder
)
router
.
HandleFunc
(
"/coco/elfinder/sftp/"
,
sftpFinder
)
router
.
HandleFunc
(
"/coco/elfinder/sftp/"
,
sftpFinder
)
router
.
HandleFunc
(
"/coco/elfinder/sftp/connector/{host}/"
,
router
.
HandleFunc
(
"/coco/elfinder/sftp/connector/{host}/"
,
sftpHostConnectorView
)
.
Methods
(
"GET"
,
"POST"
)
sftpHostConnectorView
)
.
Methods
(
"GET"
,
"POST"
)
addr
:=
net
.
JoinHostPort
(
conf
.
BindHost
,
conf
.
HTTPPort
)
addr
:=
net
.
JoinHostPort
(
conf
.
BindHost
,
conf
.
HTTPPort
)
logger
.
Debug
(
"Start HTTP server at "
,
addr
)
logger
.
Debug
(
"Start HTTP server at "
,
addr
)
...
...
pkg/service/init.go
View file @
e70545d3
...
@@ -34,6 +34,13 @@ func Initial() {
...
@@ -34,6 +34,13 @@ func Initial() {
go
KeepSyncConfigWithServer
()
go
KeepSyncConfigWithServer
()
}
}
func
newClient
()
*
common
.
Client
{
cf
:=
config
.
GetConf
()
cli
:=
common
.
NewClient
(
30
,
""
)
cli
.
BaseHost
=
cf
.
CoreHost
return
cli
}
func
validateAccessAuth
()
{
func
validateAccessAuth
()
{
cf
:=
config
.
GetConf
()
cf
:=
config
.
GetConf
()
maxTry
:=
30
maxTry
:=
30
...
...
pkg/service/urls.go
View file @
e70545d3
package
service
package
service
const
(
const
(
UserAuthURL
=
"/api/
users/v1/auth/"
// post 验证用户登陆
UserAuthURL
=
"/api/
authentication/v1/auth/"
// post 验证用户登陆
UserProfileURL
=
"/api/users/v1/profile/"
// 获取当前用户的基本信息
UserProfileURL
=
"/api/users/v1/profile/"
// 获取当前用户的基本信息
UserListUrl
=
"/api/users/v1/users/"
// 用户列表地址
UserListUrl
=
"/api/users/v1/users/"
// 用户列表地址
UserDetailURL
=
"/api/users/v1/users/%s/"
// 获取用户信息
UserDetailURL
=
"/api/users/v1/users/%s/"
// 获取用户信息
...
...
pkg/service/users.go
View file @
e70545d3
...
@@ -69,8 +69,9 @@ func CheckUserOTP(seed, code string) (resp *AuthResp, err error) {
...
@@ -69,8 +69,9 @@ func CheckUserOTP(seed, code string) (resp *AuthResp, err error) {
}
}
func
CheckUserCookie
(
sessionID
,
csrfToken
string
)
(
user
*
model
.
User
,
err
error
)
{
func
CheckUserCookie
(
sessionID
,
csrfToken
string
)
(
user
*
model
.
User
,
err
error
)
{
client
.
SetCookie
(
"csrftoken"
,
csrfToken
)
cli
:=
newClient
()
client
.
SetCookie
(
"sessionid"
,
sessionID
)
cli
.
SetCookie
(
"csrftoken"
,
csrfToken
)
err
=
client
.
Get
(
UserProfileURL
,
&
user
)
cli
.
SetCookie
(
"sessionid"
,
sessionID
)
err
=
cli
.
Get
(
UserProfileURL
,
&
user
)
return
return
}
}
pkg/srvconn/connmanager.go
View file @
e70545d3
...
@@ -2,6 +2,7 @@ package srvconn
...
@@ -2,6 +2,7 @@ package srvconn
import
(
import
(
"cocogo/pkg/service"
"cocogo/pkg/service"
"encoding/json"
"errors"
"errors"
"fmt"
"fmt"
"net"
"net"
...
@@ -22,13 +23,13 @@ var (
...
@@ -22,13 +23,13 @@ var (
)
)
type
SSHClientConfig
struct
{
type
SSHClientConfig
struct
{
Host
string
Host
string
`json:"host"`
Port
string
Port
string
`json:"port"`
User
string
User
string
`json:"user"`
Password
string
Password
string
`json:"password"`
PrivateKey
string
PrivateKey
string
`json:"private_key"`
PrivateKeyPath
string
PrivateKeyPath
string
`json:"private_key_path"`
Timeout
time
.
Duration
Timeout
time
.
Duration
`json:"timeout"`
Proxy
[]
*
SSHClientConfig
Proxy
[]
*
SSHClientConfig
proxyConn
gossh
.
Conn
proxyConn
gossh
.
Conn
...
@@ -66,13 +67,14 @@ func (sc *SSHClientConfig) Config() (config *gossh.ClientConfig, err error) {
...
@@ -66,13 +67,14 @@ func (sc *SSHClientConfig) Config() (config *gossh.ClientConfig, err error) {
func
(
sc
*
SSHClientConfig
)
DialProxy
()
(
client
*
gossh
.
Client
,
err
error
)
{
func
(
sc
*
SSHClientConfig
)
DialProxy
()
(
client
*
gossh
.
Client
,
err
error
)
{
for
_
,
p
:=
range
sc
.
Proxy
{
for
_
,
p
:=
range
sc
.
Proxy
{
logger
.
Debug
(
"Connect proxy: ......."
)
data
,
_
:=
json
.
Marshal
(
p
)
fmt
.
Println
(
string
(
data
))
client
,
err
=
p
.
Dial
()
client
,
err
=
p
.
Dial
()
if
err
==
nil
{
if
err
==
nil
{
logger
.
Debug
(
"Connect proxy host %s:%s success"
,
p
.
Host
,
p
.
Port
)
logger
.
Debug
f
(
"Connect proxy host %s:%s success"
,
p
.
Host
,
p
.
Port
)
return
return
}
else
{
}
else
{
logger
.
Errorf
(
"Connect proxy host %s:%s error: "
,
p
.
Host
,
p
.
Port
,
err
)
logger
.
Errorf
(
"Connect proxy host %s:%s error:
%s
"
,
p
.
Host
,
p
.
Port
,
err
)
}
}
}
}
return
return
...
@@ -135,6 +137,11 @@ func MakeConfig(asset *model.Asset, systemUser *model.SystemUser, timeout time.D
...
@@ -135,6 +137,11 @@ func MakeConfig(asset *model.Asset, systemUser *model.SystemUser, timeout time.D
}
}
}
}
}
}
if
systemUser
.
Password
==
""
&&
systemUser
.
PrivateKey
==
""
{
info
:=
service
.
GetSystemUserAssetAuthInfo
(
systemUser
.
ID
,
asset
.
ID
)
systemUser
.
Password
=
info
.
Password
systemUser
.
PrivateKey
=
info
.
PrivateKey
}
conf
=
&
SSHClientConfig
{
conf
=
&
SSHClientConfig
{
Host
:
asset
.
IP
,
Host
:
asset
.
IP
,
Port
:
strconv
.
Itoa
(
asset
.
Port
),
Port
:
strconv
.
Itoa
(
asset
.
Port
),
...
@@ -168,7 +175,7 @@ func NewClient(user *model.User, asset *model.Asset, systemUser *model.SystemUse
...
@@ -168,7 +175,7 @@ func NewClient(user *model.User, asset *model.Asset, systemUser *model.SystemUse
clientsRefCounter
[
client
]
++
clientsRefCounter
[
client
]
++
var
counter
=
clientsRefCounter
[
client
]
var
counter
=
clientsRefCounter
[
client
]
logger
.
Infof
(
"Reuse connection: %s->%s@%s
\n
ref: %d"
,
u
,
sysName
,
ip
,
counter
)
logger
.
Infof
(
"Reuse connection: %s->%s@%s ref: %d"
,
u
,
sysName
,
ip
,
counter
)
clientLock
.
Unlock
()
clientLock
.
Unlock
()
return
client
,
nil
return
client
,
nil
}
}
...
@@ -215,8 +222,8 @@ func RecycleClient(client *gossh.Client) {
...
@@ -215,8 +222,8 @@ func RecycleClient(client *gossh.Client) {
delete
(
sshClients
,
key
)
delete
(
sshClients
,
key
)
}
}
}
else
{
}
else
{
logger
.
Debug
(
"Recycle client: ref -1"
)
clientsRefCounter
[
client
]
--
clientsRefCounter
[
client
]
--
logger
.
Debugf
(
"Recycle client: ref -1: %d"
,
clientsRefCounter
[
client
])
}
}
}
}
}
}
pkg/srvconn/sshconn.go
View file @
e70545d3
package
srvconn
package
srvconn
import
(
import
(
"
github.com/pkg/
errors"
"errors"
"io"
"io"
"time"
"time"
...
...
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