Commit 2b6f937d authored by ibuler's avatar ibuler

[Update] 修改连接

parent 50e6613c
...@@ -2,7 +2,7 @@ module cocogo ...@@ -2,7 +2,7 @@ module cocogo
require ( require (
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 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/jarcoal/httpmock v1.0.3
github.com/konsorten/go-windows-terminal-sequences v1.0.2 github.com/konsorten/go-windows-terminal-sequences v1.0.2
github.com/kr/fs v0.1.0 github.com/kr/fs v0.1.0
......
package proxy package proxy
import ( import (
"cocogo/pkg/logger"
"fmt" "fmt"
"io" "io"
"net" "net"
...@@ -14,6 +15,7 @@ type ServerConnection interface { ...@@ -14,6 +15,7 @@ type ServerConnection interface {
Reader() io.Reader Reader() io.Reader
Protocol() string Protocol() string
Connect() error Connect() error
SetWinSize(w, h int)
Close() Close()
} }
...@@ -30,6 +32,9 @@ type SSHConnection struct { ...@@ -30,6 +32,9 @@ type SSHConnection struct {
client *gossh.Client client *gossh.Client
Session *gossh.Session Session *gossh.Session
proxyConn gossh.Conn proxyConn gossh.Conn
stdin io.WriteCloser
stdout io.Reader
closed bool
} }
func (sc *SSHConnection) Protocol() string { func (sc *SSHConnection) Protocol() string {
...@@ -66,13 +71,13 @@ func (sc *SSHConnection) Config() (config *gossh.ClientConfig, err error) { ...@@ -66,13 +71,13 @@ func (sc *SSHConnection) Config() (config *gossh.ClientConfig, err error) {
return config, nil return config, nil
} }
func (sc *SSHConnection) Connect() (client *gossh.Client, err error) { func (sc *SSHConnection) connect() (client *gossh.Client, err error) {
config, err := sc.Config() config, err := sc.Config()
if err != nil { if err != nil {
return return
} }
if sc.Proxy != nil { if sc.Proxy != nil {
proxyClient, err := sc.Proxy.Connect() proxyClient, err := sc.Proxy.connect()
if err != nil { if err != nil {
return client, err return client, err
} }
...@@ -94,29 +99,58 @@ func (sc *SSHConnection) Connect() (client *gossh.Client, err error) { ...@@ -94,29 +99,58 @@ func (sc *SSHConnection) Connect() (client *gossh.Client, err error) {
} }
} }
sc.client = client 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 { if err != nil {
return return
} }
sc.Session = sess 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) { func (sc *SSHConnection) Reader() (reader io.Reader) {
return sc.Session.StdoutPipe() return sc.stdout
} }
func (sc *SSHConnection) Writer() (writer io.WriteCloser, err error) { func (sc *SSHConnection) Writer() (writer io.WriteCloser) {
return sc.Session.StdinPipe() return sc.stdin
} }
func (sc *SSHConnection) Close() error { func (sc *SSHConnection) SetWinSize(h, w int) error {
err := sc.client.Close() return sc.Session.WindowChange(h, w)
if err != nil { }
return err
func (sc *SSHConnection) Close() {
if sc.closed {
return
} }
_ = sc.Session.Close()
_ = sc.client.Close()
if sc.proxyConn != nil { if sc.proxyConn != nil {
err = sc.proxyConn.Close() _ = sc.proxyConn.Close()
} }
return err sc.closed = true
} }
package proxy package proxy
import ( import (
"fmt"
"time"
"github.com/ibuler/ssh"
gossh "golang.org/x/crypto/ssh"
"cocogo/pkg/logger" "cocogo/pkg/logger"
"cocogo/pkg/sdk" "cocogo/pkg/sdk"
"cocogo/pkg/service" "cocogo/pkg/service"
"fmt"
"github.com/ibuler/ssh"
) )
type ProxyServer struct { type ProxyServer struct {
...@@ -53,56 +49,62 @@ func (p *ProxyServer) Proxy() { ...@@ -53,56 +49,62 @@ func (p *ProxyServer) Proxy() {
if !p.checkProtocol() { if !p.checkProtocol() {
return return
} }
fmt.Println(">>>>>>>>>>>>>>>>>>>>>>>>>Proxy")
ptyReq, winCh, ok := p.Session.Pty()
if !ok {
logger.Error("Pty not ok")
return
}
conn := SSHConnection{ conn := SSHConnection{
Host: "192.168.244.143", Host: "192.168.244.185",
Port: "22", Port: "22",
User: "root", User: "root",
Password: "redhat", Password: "redhat",
} }
_, err := conn.Connect() err := conn.Connect(ptyReq.Window.Height, ptyReq.Window.Width, ptyReq.Term)
if err != nil { if err != nil {
return return
} }
ptyReq, _, ok := p.Session.Pty()
if !ok {
logger.Error("Pty not ok")
return
}
fmt.Println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") go func() {
modes := gossh.TerminalModes{ for {
gossh.ECHO: 1, // enable echoing select {
gossh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud case win, ok := <-winCh:
gossh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud if !ok {
} return
err = conn.Session.RequestPty("xterm", ptyReq.Window.Height, ptyReq.Window.Width, modes) }
if err != nil { err := conn.SetWinSize(win.Height, win.Width)
logger.Errorf("Request pty error: %s", err) if err != nil {
return logger.Error("windowChange err: ", win)
} return
}
logger.Info("windowChange: ", win)
}
}
}()
go func() { go func() {
buf := make([]byte, 1024) buf := make([]byte, 1024)
writer, err := conn.Session.StdinPipe() writer := conn.Writer()
if err != nil {
return
}
for { for {
fmt.Println("Start read from user session")
nr, err := p.Session.Read(buf) nr, err := p.Session.Read(buf)
fmt.Printf("get ddata from user: %s\n", buf)
if err != nil { if err != nil {
writer.Write(buf[:nr]) logger.Error("...............")
} }
writer.Write(buf[:nr])
} }
}() }()
go func() { go func() {
buf := make([]byte, 1024) buf := make([]byte, 1024)
reader, err := conn.Reader() reader := conn.Reader()
if err != nil { fmt.Printf("Go func stdout pip")
return
}
for { for {
fmt.Printf("Start read from server\n")
nr, err := reader.Read(buf) nr, err := reader.Read(buf)
fmt.Printf("Read data from server: %s\n", buf)
if err != nil { if err != nil {
logger.Error("Read error") logger.Error("Read error")
} }
...@@ -110,5 +112,5 @@ func (p *ProxyServer) Proxy() { ...@@ -110,5 +112,5 @@ func (p *ProxyServer) Proxy() {
} }
}() }()
time.Sleep(time.Second * 20) conn.Session.Wait()
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment