From c7d099b6fbab03020c497e3ceb88229c16418e8f Mon Sep 17 00:00:00 2001 From: Yousong Zhou Date: Wed, 23 Dec 2020 11:23:24 +0800 Subject: [PATCH 1/4] refactor(webconsole): use structured response --- pkg/apis/webconsole/types.go | 26 ++++++++++++++++++++++++++ pkg/webconsole/handlers.go | 17 ++++++++++------- 2 files changed, 36 insertions(+), 7 deletions(-) create mode 100644 pkg/apis/webconsole/types.go diff --git a/pkg/apis/webconsole/types.go b/pkg/apis/webconsole/types.go new file mode 100644 index 0000000000..8a9a03fe72 --- /dev/null +++ b/pkg/apis/webconsole/types.go @@ -0,0 +1,26 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package webconsole + +import ( + "yunion.io/x/onecloud/pkg/apis" +) + +type ServerRemoteConsoleResponse struct { + ConnectParams string `json:"connect_params"` + Session string `json:"session,omitempty"` + + apis.Meta +} diff --git a/pkg/webconsole/handlers.go b/pkg/webconsole/handlers.go index de9ff40ded..6cf84bce26 100644 --- a/pkg/webconsole/handlers.go +++ b/pkg/webconsole/handlers.go @@ -24,6 +24,7 @@ import ( "yunion.io/x/jsonutils" + webconsole_api "yunion.io/x/onecloud/pkg/apis/webconsole" "yunion.io/x/onecloud/pkg/appsrv" "yunion.io/x/onecloud/pkg/cloudcommon/policy" "yunion.io/x/onecloud/pkg/httperrors" @@ -214,14 +215,15 @@ func handleServerRemoteConsole(ctx context.Context, w http.ResponseWriter, r *ht } func responsePublicCloudConsole(ctx context.Context, info *session.RemoteConsoleInfo, w http.ResponseWriter) { - data := jsonutils.NewDict() params, err := info.GetConnectParams() if err != nil { httperrors.GeneralServerError(ctx, w, err) return } - data.Add(jsonutils.NewString(params), "connect_params") - sendJSON(w, data) + resp := webconsole_api.ServerRemoteConsoleResponse{ + ConnectParams: params, + } + sendJSON(w, resp.JSON(resp)) } func handleDataSession(ctx context.Context, sData session.ISessionData, w http.ResponseWriter, connParams url.Values, b64Encode bool) { @@ -230,7 +232,6 @@ func handleDataSession(ctx context.Context, sData session.ISessionData, w http.R httperrors.GeneralServerError(ctx, w, err) return } - data := jsonutils.NewDict() params, err := s.GetConnectParams(connParams) if err != nil { httperrors.GeneralServerError(ctx, w, err) @@ -239,9 +240,11 @@ func handleDataSession(ctx context.Context, sData session.ISessionData, w http.R if b64Encode { params = base64.StdEncoding.EncodeToString([]byte(params)) } - data.Add(jsonutils.NewString(params), "connect_params") - data.Add(jsonutils.NewString(s.Id), "session") - sendJSON(w, data) + resp := webconsole_api.ServerRemoteConsoleResponse{ + ConnectParams: params, + Session: s.Id, + } + sendJSON(w, resp.JSON(resp)) } func handleCommandSession(ctx context.Context, cmd command.ICommand, w http.ResponseWriter) { From 4cd1d4ba7c6e10abf9d36046cb0917cca7faaadc Mon Sep 17 00:00:00 2001 From: Yousong Zhou Date: Wed, 23 Dec 2020 13:23:47 +0800 Subject: [PATCH 2/4] refactor(webconsole): api: add helper methods for response struct --- pkg/apis/webconsole/types.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/pkg/apis/webconsole/types.go b/pkg/apis/webconsole/types.go index 8a9a03fe72..dc4e7d1f0d 100644 --- a/pkg/apis/webconsole/types.go +++ b/pkg/apis/webconsole/types.go @@ -15,6 +15,9 @@ package webconsole import ( + "encoding/base64" + "net/url" + "yunion.io/x/onecloud/pkg/apis" ) @@ -24,3 +27,27 @@ type ServerRemoteConsoleResponse struct { apis.Meta } + +func (resp *ServerRemoteConsoleResponse) GetConnectParams() string { + params := resp.ConnectParams + if data, err := base64.StdEncoding.DecodeString(params); err == nil { + params = string(data) + } + return params +} + +func (resp *ServerRemoteConsoleResponse) GetConnectProtocol() (string, error) { + var ( + params = resp.GetConnectParams() + query url.Values + ) + if uri, err := url.ParseRequestURI(params); err == nil { + query = uri.Query() + } else if q, err := url.ParseQuery(params); err == nil { + query = q + } else { + return "", err + } + protocol := query.Get("protocol") + return protocol, nil +} From db8307822f0acbeec197a28c05223d9ea7924bf4 Mon Sep 17 00:00:00 2001 From: Yousong Zhou Date: Wed, 23 Dec 2020 13:24:15 +0800 Subject: [PATCH 3/4] refactor(climc): webconsole: use helper methods of response struct --- cmd/climc/shell/compute/webconsole.go | 30 +++++++++------------------ 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/cmd/climc/shell/compute/webconsole.go b/cmd/climc/shell/compute/webconsole.go index dcc3f7ec47..0d82bf8b83 100644 --- a/cmd/climc/shell/compute/webconsole.go +++ b/cmd/climc/shell/compute/webconsole.go @@ -42,34 +42,24 @@ func init() { if err != nil { return err } - connParams, err := obj.GetString("connect_params") - if err != nil { + + resp := &webconsole_api.ServerRemoteConsoleResponse{} + if err := obj.Unmarshal(resp); err != nil { return err } - if decodeStr, err := base64.StdEncoding.DecodeString(connParams); err == nil { - connParams = string(decodeStr) - } - var query url.Values - connUrl, err := url.ParseRequestURI(connParams) - if err == nil { - query = connUrl.Query() - } else { - query, err = url.ParseQuery(connParams) - if err != nil { - return err - } - } - protocol := query.Get("protocol") - if !utils.IsInStringArray(protocol, []string{ + connectParams := resp.GetConnectParams() + if protocol, err := resp.GetConnectProtocol(); err != nil { + return err + } else if !utils.IsInStringArray(protocol, []string{ command.PROTOCOL_TTY, webconsole_api.VNC, webconsole_api.SPICE, webconsole_api.WMKS, }) { - fmt.Println(connParams) + fmt.Println(connectParams) return nil } - newQuery := url.Values(make(map[string][]string)) - newQuery.Set("data", base64.StdEncoding.EncodeToString([]byte(connParams))) + newQuery := url.Values{} + newQuery.Set("data", base64.StdEncoding.EncodeToString([]byte(connectParams))) u.RawQuery = newQuery.Encode() fmt.Println(u.String()) return nil From b06be9ea5ca267abc658736ee4d4abf8011f9da9 Mon Sep 17 00:00:00 2001 From: Yousong Zhou Date: Wed, 23 Dec 2020 13:26:29 +0800 Subject: [PATCH 4/4] fix(mcclient): options: webconsole: fix passing ssh port --- pkg/mcclient/options/webconsole.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pkg/mcclient/options/webconsole.go b/pkg/mcclient/options/webconsole.go index 89052fb5ac..bd06eb7092 100644 --- a/pkg/mcclient/options/webconsole.go +++ b/pkg/mcclient/options/webconsole.go @@ -71,12 +71,18 @@ func (opt *WebConsoleBaremetalOptions) Params() (*jsonutils.JSONDict, error) { type WebConsoleSshOptions struct { WebConsoleOptions - IP string `help:"IP to connect"` + IP string `help:"IP to connect" json:"-"` Port int `help:"Remote server port"` } func (opt *WebConsoleSshOptions) Params() (*jsonutils.JSONDict, error) { - return StructToParams(opt) + data, err := StructToParams(opt) + if err != nil { + return nil, err + } + params := jsonutils.NewDict() + params.Set("webconsole", data) + return params, nil } type WebConsoleServerOptions struct {