socat_command.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package command
  15. import (
  16. "fmt"
  17. "os/exec"
  18. "yunion.io/x/onecloud/pkg/mcclient"
  19. )
  20. type SocatInfo struct {
  21. IP string `json:"ip"`
  22. Port int `json:"port"`
  23. }
  24. type SocatCmd struct {
  25. *BaseCommand
  26. Info *SocatInfo
  27. s *mcclient.ClientSession
  28. }
  29. func NewSocatCommand(info *SocatInfo, s *mcclient.ClientSession) (*SocatCmd, error) {
  30. if info.IP == "" {
  31. return nil, fmt.Errorf("Empty remote ip address")
  32. }
  33. if info.Port <= 0 {
  34. return nil, fmt.Errorf("Invalid port %d", info.Port)
  35. }
  36. name := "bash"
  37. args := []string{
  38. "-c",
  39. fmt.Sprintf("socat FILE:`tty`,raw,echo=0 TCP:%s:%d", info.IP, info.Port),
  40. }
  41. cmd := NewBaseCommand(s, name, args...)
  42. scmd := &SocatCmd{
  43. BaseCommand: cmd,
  44. Info: info,
  45. }
  46. return scmd, nil
  47. }
  48. func (c *SocatCmd) GetCommand() *exec.Cmd {
  49. return c.BaseCommand.GetCommand()
  50. }
  51. func (c *SocatCmd) GetProtocol() string {
  52. return PROTOCOL_TTY
  53. }