优化ssh资产一直卡在连接中的问题

This commit is contained in:
dushixiang
2021-11-15 16:23:26 +08:00
parent 1306c9dcd7
commit 569a1ad763
2 changed files with 32 additions and 42 deletions

View File

@ -21,14 +21,14 @@ import (
) )
const ( const (
TunnelClosed int = -1 TunnelClosed int = -1
Normal int = 0 Normal int = 0
NotFoundSession int = 800 NotFoundSession int = 800
NewTunnelError int = 801 NewTunnelError int = 801
ForcedDisconnect int = 802 ForcedDisconnect int = 802
AccessGatewayUnAvailable int = 803 AccessGatewayUnAvailable int = 803
AccessGatewayCreateError int = 804 AccessGatewayCreateError int = 804
AccessGatewayConnectError int = 804 AssetNotActive int = 805
) )
func TunEndpoint(c echo.Context) error { func TunEndpoint(c echo.Context) error {
@ -99,6 +99,12 @@ func TunEndpoint(c echo.Context) error {
ip = exposedIP ip = exposedIP
port = exposedPort port = exposedPort
} }
active, err := utils.Tcping(ip, port)
if !active {
disconnect(ws, AssetNotActive, "目标资产不在线: "+err.Error())
return nil
}
configuration.SetParameter("hostname", ip) configuration.SetParameter("hostname", ip)
configuration.SetParameter("port", strconv.Itoa(port)) configuration.SetParameter("port", strconv.Itoa(port))
@ -141,10 +147,9 @@ func TunEndpoint(c echo.Context) error {
if len(s.ConnectionId) == 0 { if len(s.ConnectionId) == 0 {
if configuration.Protocol == constant.SSH { if configuration.Protocol == constant.SSH {
nextTerminal, err := CreateNextTerminalBySession(s) nextTerminal, err := CreateNextTerminalBySession(s)
if err != nil { if err == nil {
return err nextSession.NextTerminal = nextTerminal
} }
nextSession.NextTerminal = nextTerminal
} }
nextSession.Observer = session.NewObserver(sessionId) nextSession.Observer = session.NewObserver(sessionId)

View File

@ -128,8 +128,9 @@ func (opt *Instruction) Parse(content string) Instruction {
} }
type Tunnel struct { type Tunnel struct {
rw *bufio.ReadWriter
conn net.Conn conn net.Conn
reader *bufio.Reader
writer *bufio.Writer
UUID string UUID string
Config *Configuration Config *Configuration
IsOpen bool IsOpen bool
@ -144,7 +145,8 @@ func NewTunnel(address string, config *Configuration) (ret *Tunnel, err error) {
ret = &Tunnel{} ret = &Tunnel{}
ret.conn = conn ret.conn = conn
ret.rw = bufio.NewReadWriter(bufio.NewReader(conn), bufio.NewWriter(conn)) ret.reader = bufio.NewReader(conn)
ret.writer = bufio.NewWriter(conn)
ret.Config = config ret.Config = config
selectArg := config.ConnectionID selectArg := config.ConnectionID
@ -226,52 +228,36 @@ func (opt *Tunnel) WriteInstructionAndFlush(instruction Instruction) error {
return nil return nil
} }
func (opt *Tunnel) WriteInstruction(instruction Instruction) error {
if _, err := opt.Write([]byte(instruction.String())); err != nil {
return err
}
return nil
}
func (opt *Tunnel) WriteAndFlush(p []byte) (int, error) { func (opt *Tunnel) WriteAndFlush(p []byte) (int, error) {
//fmt.Printf("-> %v\n", string(p)) //fmt.Printf("-> %v\n", string(p))
nn, err := opt.rw.Write(p) nn, err := opt.writer.Write(p)
if err != nil { if err != nil {
return nn, err return nn, err
} }
err = opt.rw.Flush() err = opt.writer.Flush()
if err != nil { if err != nil {
return nn, err return nn, err
} }
return nn, nil return nn, nil
} }
func (opt *Tunnel) Write(p []byte) (int, error) {
//fmt.Printf("-> %v \n", string(p))
nn, err := opt.rw.Write(p)
if err != nil {
return nn, err
}
return nn, nil
}
func (opt *Tunnel) Flush() error {
return opt.rw.Flush()
}
func (opt *Tunnel) ReadInstruction() (instruction Instruction, err error) { func (opt *Tunnel) ReadInstruction() (instruction Instruction, err error) {
msg, err := opt.rw.ReadString(Delimiter) msg, err := opt.Read()
//fmt.Printf("<- %v \n", msg)
if err != nil { if err != nil {
return instruction, err return instruction, err
} }
return instruction.Parse(msg), err return instruction.Parse(string(msg)), err
} }
func (opt *Tunnel) Read() (p []byte, err error) { func (opt *Tunnel) Read() (p []byte, err error) {
p, err = opt.rw.ReadBytes(Delimiter) buffer := make([]byte, 8096)
//fmt.Printf("<- %v \n", string(p)) read, err := opt.reader.Read(buffer)
s := string(p) if err != nil {
return
}
buffer = buffer[0:read]
s := string(buffer)
//fmt.Printf("<- %v \n", s)
if s == "rate=44100,channels=2;" { if s == "rate=44100,channels=2;" {
return make([]byte, 0), nil return make([]byte, 0), nil
} }
@ -299,6 +285,5 @@ func (opt *Tunnel) expect(opcode string) (instruction Instruction, err error) {
func (opt *Tunnel) Close() error { func (opt *Tunnel) Close() error {
opt.IsOpen = false opt.IsOpen = false
_ = opt.rw.Flush()
return opt.conn.Close() return opt.conn.Close()
} }