diff --git a/server/api/tunnel.go b/server/api/tunnel.go index 60ef47b..25391f3 100644 --- a/server/api/tunnel.go +++ b/server/api/tunnel.go @@ -21,14 +21,14 @@ import ( ) const ( - TunnelClosed int = -1 - Normal int = 0 - NotFoundSession int = 800 - NewTunnelError int = 801 - ForcedDisconnect int = 802 - AccessGatewayUnAvailable int = 803 - AccessGatewayCreateError int = 804 - AccessGatewayConnectError int = 804 + TunnelClosed int = -1 + Normal int = 0 + NotFoundSession int = 800 + NewTunnelError int = 801 + ForcedDisconnect int = 802 + AccessGatewayUnAvailable int = 803 + AccessGatewayCreateError int = 804 + AssetNotActive int = 805 ) func TunEndpoint(c echo.Context) error { @@ -99,6 +99,12 @@ func TunEndpoint(c echo.Context) error { ip = exposedIP port = exposedPort } + active, err := utils.Tcping(ip, port) + if !active { + disconnect(ws, AssetNotActive, "目标资产不在线: "+err.Error()) + return nil + } + configuration.SetParameter("hostname", ip) configuration.SetParameter("port", strconv.Itoa(port)) @@ -141,10 +147,9 @@ func TunEndpoint(c echo.Context) error { if len(s.ConnectionId) == 0 { if configuration.Protocol == constant.SSH { nextTerminal, err := CreateNextTerminalBySession(s) - if err != nil { - return err + if err == nil { + nextSession.NextTerminal = nextTerminal } - nextSession.NextTerminal = nextTerminal } nextSession.Observer = session.NewObserver(sessionId) diff --git a/server/guacd/guacd.go b/server/guacd/guacd.go index 6563057..5b19f4b 100644 --- a/server/guacd/guacd.go +++ b/server/guacd/guacd.go @@ -128,8 +128,9 @@ func (opt *Instruction) Parse(content string) Instruction { } type Tunnel struct { - rw *bufio.ReadWriter conn net.Conn + reader *bufio.Reader + writer *bufio.Writer UUID string Config *Configuration IsOpen bool @@ -144,7 +145,8 @@ func NewTunnel(address string, config *Configuration) (ret *Tunnel, err error) { ret = &Tunnel{} 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 selectArg := config.ConnectionID @@ -226,52 +228,36 @@ func (opt *Tunnel) WriteInstructionAndFlush(instruction Instruction) error { 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) { //fmt.Printf("-> %v\n", string(p)) - nn, err := opt.rw.Write(p) + nn, err := opt.writer.Write(p) if err != nil { return nn, err } - err = opt.rw.Flush() + err = opt.writer.Flush() if err != nil { return nn, err } 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) { - msg, err := opt.rw.ReadString(Delimiter) - //fmt.Printf("<- %v \n", msg) + msg, err := opt.Read() if err != nil { return instruction, err } - return instruction.Parse(msg), err + return instruction.Parse(string(msg)), err } func (opt *Tunnel) Read() (p []byte, err error) { - p, err = opt.rw.ReadBytes(Delimiter) - //fmt.Printf("<- %v \n", string(p)) - s := string(p) + buffer := make([]byte, 8096) + read, err := opt.reader.Read(buffer) + if err != nil { + return + } + buffer = buffer[0:read] + s := string(buffer) + //fmt.Printf("<- %v \n", s) if s == "rate=44100,channels=2;" { return make([]byte, 0), nil } @@ -299,6 +285,5 @@ func (opt *Tunnel) expect(opcode string) (instruction Instruction, err error) { func (opt *Tunnel) Close() error { opt.IsOpen = false - _ = opt.rw.Flush() return opt.conn.Close() }