Commit 2b6f937d authored by ibuler's avatar ibuler

[Update] 修改连接

parent 50e6613c
......@@ -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
......
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) Connect() (client *gossh.Client, err error) {
func (sc *SSHConnection) connect() (client *gossh.Client, err error) {
config, err := sc.Config()
if err != nil {
return
}
if sc.Proxy != nil {
proxyClient, err := sc.Proxy.Connect()
proxyClient, err := sc.Proxy.connect()
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
}
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.143",
Host: "192.168.244.185",
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()
}
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