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
2b6f937d
Commit
2b6f937d
authored
Apr 28, 2019
by
ibuler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[Update] 修改连接
parent
50e6613c
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
85 additions
and
49 deletions
+85
-49
go.mod
go.mod
+1
-1
connection.go
pkg/proxy/connection.go
+48
-14
proxy.go
pkg/proxy/proxy.go
+36
-34
No files found.
go.mod
View file @
2b6f937d
...
...
@@ -2,7 +2,7 @@ module cocogo
require (
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239
github.com/ibuler/ssh v0.1.
1
github.com/ibuler/ssh v0.1.
5
github.com/jarcoal/httpmock v1.0.3
github.com/konsorten/go-windows-terminal-sequences v1.0.2
github.com/kr/fs v0.1.0
...
...
pkg/proxy/connection.go
View file @
2b6f937d
package
proxy
import
(
"cocogo/pkg/logger"
"fmt"
"io"
"net"
...
...
@@ -14,6 +15,7 @@ type ServerConnection interface {
Reader
()
io
.
Reader
Protocol
()
string
Connect
()
error
SetWinSize
(
w
,
h
int
)
Close
()
}
...
...
@@ -30,6 +32,9 @@ type SSHConnection struct {
client
*
gossh
.
Client
Session
*
gossh
.
Session
proxyConn
gossh
.
Conn
stdin
io
.
WriteCloser
stdout
io
.
Reader
closed
bool
}
func
(
sc
*
SSHConnection
)
Protocol
()
string
{
...
...
@@ -66,13 +71,13 @@ func (sc *SSHConnection) Config() (config *gossh.ClientConfig, err error) {
return
config
,
nil
}
func
(
sc
*
SSHConnection
)
C
onnect
()
(
client
*
gossh
.
Client
,
err
error
)
{
func
(
sc
*
SSHConnection
)
c
onnect
()
(
client
*
gossh
.
Client
,
err
error
)
{
config
,
err
:=
sc
.
Config
()
if
err
!=
nil
{
return
}
if
sc
.
Proxy
!=
nil
{
proxyClient
,
err
:=
sc
.
Proxy
.
C
onnect
()
proxyClient
,
err
:=
sc
.
Proxy
.
c
onnect
()
if
err
!=
nil
{
return
client
,
err
}
...
...
@@ -94,29 +99,58 @@ func (sc *SSHConnection) Connect() (client *gossh.Client, err error) {
}
}
sc
.
client
=
client
sess
,
err
:=
sc
.
client
.
NewSession
()
return
client
,
nil
}
func
(
sc
*
SSHConnection
)
Connect
(
h
,
w
int
,
term
string
)
(
err
error
)
{
client
,
err
:=
sc
.
connect
()
sess
,
err
:=
client
.
NewSession
()
if
err
!=
nil
{
return
}
sc
.
Session
=
sess
return
client
,
nil
modes
:=
gossh
.
TerminalModes
{
gossh
.
ECHO
:
1
,
// enable echoing
gossh
.
TTY_OP_ISPEED
:
14400
,
// input speed = 14.4 kbaud
gossh
.
TTY_OP_OSPEED
:
14400
,
// output speed = 14.4 kbaud
}
err
=
sess
.
RequestPty
(
term
,
h
,
w
,
modes
)
if
err
!=
nil
{
logger
.
Errorf
(
"Request pty error: %s"
,
err
)
return
}
sc
.
stdin
,
err
=
sess
.
StdinPipe
()
if
err
!=
nil
{
return
}
sc
.
stdout
,
err
=
sess
.
StdoutPipe
()
if
err
!=
nil
{
return
}
err
=
sess
.
Shell
()
return
err
}
func
(
sc
*
SSHConnection
)
Reader
()
(
reader
io
.
Reader
,
err
error
)
{
return
sc
.
Session
.
StdoutPipe
()
func
(
sc
*
SSHConnection
)
Reader
()
(
reader
io
.
Reader
)
{
return
sc
.
stdout
}
func
(
sc
*
SSHConnection
)
Writer
()
(
writer
io
.
WriteCloser
,
err
error
)
{
return
sc
.
Session
.
StdinPipe
()
func
(
sc
*
SSHConnection
)
Writer
()
(
writer
io
.
WriteCloser
)
{
return
sc
.
stdin
}
func
(
sc
*
SSHConnection
)
Close
()
error
{
err
:=
sc
.
client
.
Close
()
if
err
!=
nil
{
return
err
func
(
sc
*
SSHConnection
)
SetWinSize
(
h
,
w
int
)
error
{
return
sc
.
Session
.
WindowChange
(
h
,
w
)
}
func
(
sc
*
SSHConnection
)
Close
()
{
if
sc
.
closed
{
return
}
_
=
sc
.
Session
.
Close
()
_
=
sc
.
client
.
Close
()
if
sc
.
proxyConn
!=
nil
{
err
=
sc
.
proxyConn
.
Close
()
_
=
sc
.
proxyConn
.
Close
()
}
return
err
sc
.
closed
=
true
}
pkg/proxy/proxy.go
View file @
2b6f937d
package
proxy
import
(
"fmt"
"time"
"github.com/ibuler/ssh"
gossh
"golang.org/x/crypto/ssh"
"cocogo/pkg/logger"
"cocogo/pkg/sdk"
"cocogo/pkg/service"
"fmt"
"github.com/ibuler/ssh"
)
type
ProxyServer
struct
{
...
...
@@ -53,56 +49,62 @@ func (p *ProxyServer) Proxy() {
if
!
p
.
checkProtocol
()
{
return
}
fmt
.
Println
(
">>>>>>>>>>>>>>>>>>>>>>>>>Proxy"
)
ptyReq
,
winCh
,
ok
:=
p
.
Session
.
Pty
()
if
!
ok
{
logger
.
Error
(
"Pty not ok"
)
return
}
conn
:=
SSHConnection
{
Host
:
"192.168.244.1
43
"
,
Host
:
"192.168.244.1
85
"
,
Port
:
"22"
,
User
:
"root"
,
Password
:
"redhat"
,
}
_
,
err
:=
conn
.
Connect
(
)
err
:=
conn
.
Connect
(
ptyReq
.
Window
.
Height
,
ptyReq
.
Window
.
Width
,
ptyReq
.
Term
)
if
err
!=
nil
{
return
}
ptyReq
,
_
,
ok
:=
p
.
Session
.
Pty
()
if
!
ok
{
logger
.
Error
(
"Pty not ok"
)
return
}
fmt
.
Println
(
">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
)
modes
:=
gossh
.
TerminalModes
{
gossh
.
ECHO
:
1
,
// enable echoing
gossh
.
TTY_OP_ISPEED
:
14400
,
// input speed = 14.4kbaud
gossh
.
TTY_OP_OSPEED
:
14400
,
// output speed = 14.4kbaud
}
err
=
conn
.
Session
.
RequestPty
(
"xterm"
,
ptyReq
.
Window
.
Height
,
ptyReq
.
Window
.
Width
,
modes
)
if
err
!=
nil
{
logger
.
Errorf
(
"Request pty error: %s"
,
err
)
return
}
go
func
()
{
for
{
select
{
case
win
,
ok
:=
<-
winCh
:
if
!
ok
{
return
}
err
:=
conn
.
SetWinSize
(
win
.
Height
,
win
.
Width
)
if
err
!=
nil
{
logger
.
Error
(
"windowChange err: "
,
win
)
return
}
logger
.
Info
(
"windowChange: "
,
win
)
}
}
}()
go
func
()
{
buf
:=
make
([]
byte
,
1024
)
writer
,
err
:=
conn
.
Session
.
StdinPipe
()
if
err
!=
nil
{
return
}
writer
:=
conn
.
Writer
()
for
{
fmt
.
Println
(
"Start read from user session"
)
nr
,
err
:=
p
.
Session
.
Read
(
buf
)
fmt
.
Printf
(
"get ddata from user: %s
\n
"
,
buf
)
if
err
!=
nil
{
writer
.
Write
(
buf
[
:
nr
]
)
logger
.
Error
(
"..............."
)
}
writer
.
Write
(
buf
[
:
nr
])
}
}()
go
func
()
{
buf
:=
make
([]
byte
,
1024
)
reader
,
err
:=
conn
.
Reader
()
if
err
!=
nil
{
return
}
reader
:=
conn
.
Reader
()
fmt
.
Printf
(
"Go func stdout pip"
)
for
{
fmt
.
Printf
(
"Start read from server
\n
"
)
nr
,
err
:=
reader
.
Read
(
buf
)
fmt
.
Printf
(
"Read data from server: %s
\n
"
,
buf
)
if
err
!=
nil
{
logger
.
Error
(
"Read error"
)
}
...
...
@@ -110,5 +112,5 @@ func (p *ProxyServer) Proxy() {
}
}()
time
.
Sleep
(
time
.
Second
*
20
)
conn
.
Session
.
Wait
(
)
}
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