diff --git a/pkg/webconsole/command/adb_command.go b/pkg/webconsole/command/adb_command.go index 084fb5a62c..beebfb6428 100644 --- a/pkg/webconsole/command/adb_command.go +++ b/pkg/webconsole/command/adb_command.go @@ -18,6 +18,7 @@ import ( "fmt" "os/exec" + "yunion.io/x/log" "yunion.io/x/pkg/errors" "yunion.io/x/onecloud/pkg/mcclient" @@ -29,6 +30,10 @@ type SAdbShellInfo struct { HostPort int `json:"host_port"` } +func (info SAdbShellInfo) connStr() string { + return fmt.Sprintf("%s:%d", info.HostIp, info.HostPort) +} + type SAdbShellCommand struct { *BaseCommand Info *SAdbShellInfo @@ -37,13 +42,13 @@ type SAdbShellCommand struct { func NewAdbShellCommand(info *SAdbShellInfo, s *mcclient.ClientSession) (*SAdbShellCommand, error) { name := o.Options.AdbPath - var connStr string - initCmd := exec.Command(name, "connect", fmt.Sprintf("%s:%d", info.HostIp, info.HostPort)) + connStr := info.connStr() + initCmd := exec.Command(name, "connect", connStr) if err := initCmd.Run(); err != nil { + log.Errorf("adb connect %s fail %s", connStr, err) return nil, errors.Wrap(err, "connect adb") - } else { - connStr = fmt.Sprintf("%s:%d", info.HostIp, info.HostPort) } + log.Infof("adb connect %s success!", connStr) cmd := NewBaseCommand(s, name, "-s", connStr, "shell") tool := &SAdbShellCommand{ BaseCommand: cmd, @@ -61,9 +66,12 @@ func (c SAdbShellCommand) GetProtocol() string { } func (c *SAdbShellCommand) Cleanup() error { - initCmd := exec.Command(o.Options.AdbPath, "disconnect", fmt.Sprintf("%s:%d", c.Info.HostIp, c.Info.HostPort)) + connStr := c.Info.connStr() + initCmd := exec.Command(o.Options.AdbPath, "disconnect", connStr) if err := initCmd.Run(); err != nil { + log.Errorf("adb disconnect %s fail %s", connStr, err) return errors.Wrap(err, "disconnect adb") } + log.Infof("adb disconnect %s success!", connStr) return nil } diff --git a/pkg/webconsole/service/handlers.go b/pkg/webconsole/service/handlers.go index 4516bd1e85..55c0a1c044 100644 --- a/pkg/webconsole/service/handlers.go +++ b/pkg/webconsole/service/handlers.go @@ -21,6 +21,7 @@ import ( "net/http" "net/url" "os" + "strconv" "strings" "yunion.io/x/jsonutils" @@ -137,7 +138,7 @@ func fetchCloudEnv(ctx context.Context, w http.ResponseWriter, r *http.Request) if userCred == nil { return nil, httperrors.NewUnauthorizedError("No token founded") } - if !gotypes.IsNil(body) { + if !gotypes.IsNil(body) && body.Contains("webconsole") { body, _ = body.Get("webconsole") } s := auth.Client().NewSession(ctx, o.Options.Region, "", "internal", userCred) @@ -425,45 +426,61 @@ func handleAdbShell(ctx context.Context, w http.ResponseWriter, r *http.Request) phoneIp := "" adbPort := -1 - if len(serverDetails.Nics) > 0 { - phoneIp = serverDetails.Nics[0].IpAddr - portMaps := serverDetails.Nics[0].PortMappings - for i := range portMaps { - portMap := portMaps[i] - if portMap.Port == 5555 && portMap.Protocol == "tcp" { - adbPort = *portMap.HostPort - break + connStr, _ := env.Body.GetString("conn") + if len(connStr) > 0 { + parts := strings.Split(connStr, ":") + if len(parts) > 0 { + phoneIp = parts[0] + if len(parts) > 1 { + adbPort, _ = strconv.Atoi(parts[1]) + } + if adbPort == 0 { + adbPort = 5555 } } - var errMsgs []string - if !regutils.MatchIP4Addr(phoneIp) { - errMsgs = append(errMsgs, "invalid phone IP") - } - if adbPort < 0 { - errMsgs = append(errMsgs, "adb port not found") - } - if len(errMsgs) > 0 { - httperrors.GeneralServerError(ctx, w, httperrors.NewNotSupportedError(strings.Join(errMsgs, ";"))) - return - } - } else { - httperrors.GeneralServerError(ctx, w, httperrors.NewNotSupportedError("porting_mapping not supported")) - return } - - { - // test phoneIP is accessible - err := netutils2.TestTcpPort(phoneIp, 5555, 3, 3) - if err != nil { - log.Errorf("TestTcpPort %s:%d fail %s", phoneIp, 5555, err) - phoneIp = serverDetails.HostEIP - if len(phoneIp) == 0 { - phoneIp = serverDetails.HostAccessIp + if len(phoneIp) == 0 { + // fallback + if len(serverDetails.Nics) > 0 { + phoneIp = serverDetails.Nics[0].IpAddr + portMaps := serverDetails.Nics[0].PortMappings + for i := range portMaps { + portMap := portMaps[i] + if portMap.Port == 5555 && portMap.Protocol == "tcp" { + adbPort = *portMap.HostPort + break + } + } + var errMsgs []string + if !regutils.MatchIP4Addr(phoneIp) { + errMsgs = append(errMsgs, "invalid phone IP") + } + if adbPort < 0 { + errMsgs = append(errMsgs, "adb port not found") + } + if len(errMsgs) > 0 { + httperrors.GeneralServerError(ctx, w, httperrors.NewNotSupportedError(strings.Join(errMsgs, ";"))) + return } } else { - adbPort = 5555 + httperrors.GeneralServerError(ctx, w, httperrors.NewNotSupportedError("porting_mapping not supported")) + return + } + { + // test phoneIP is accessible + err := netutils2.TestTcpPort(phoneIp, 5555, 3, 3) + if err != nil { + log.Errorf("TestTcpPort %s:%d fail %s", phoneIp, 5555, err) + phoneIp = serverDetails.HostEIP + if len(phoneIp) == 0 { + phoneIp = serverDetails.HostAccessIp + } + } else { + adbPort = 5555 + } } } + info := command.SAdbShellInfo{ HostIp: phoneIp, HostPort: adbPort,