server.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 server
  15. import (
  16. "context"
  17. "net/http"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. "yunion.io/x/pkg/appctx"
  21. "yunion.io/x/pkg/utils"
  22. api "yunion.io/x/onecloud/pkg/apis/compute"
  23. "yunion.io/x/onecloud/pkg/httperrors"
  24. "yunion.io/x/onecloud/pkg/webconsole/session"
  25. )
  26. type ConnectionServer struct {
  27. }
  28. func NewConnectionServer() *ConnectionServer {
  29. return &ConnectionServer{}
  30. }
  31. func (s *ConnectionServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
  32. ctx := context.Background()
  33. ctx = appctx.WithRequestLang(ctx, req)
  34. query, err := jsonutils.ParseQueryString(req.URL.RawQuery)
  35. if err != nil {
  36. httperrors.GeneralServerError(ctx, w, err)
  37. return
  38. }
  39. log.Debugf("[connection] Get query: %v", query)
  40. accessToken, _ := query.GetString("access_token")
  41. if accessToken == "" {
  42. httperrors.BadRequestError(ctx, w, "Empty access_token")
  43. return
  44. }
  45. sessionObj, ok := session.Manager.Get(accessToken)
  46. if !ok {
  47. httperrors.NotFoundError(ctx, w, "session not found")
  48. return
  49. }
  50. var srv http.Handler
  51. protocol := sessionObj.GetProtocol()
  52. switch protocol {
  53. case session.VNC, session.SPICE:
  54. info := sessionObj.ISessionData.(*session.RemoteConsoleInfo)
  55. if utils.IsInStringArray(info.Hypervisor, []string{
  56. api.HYPERVISOR_OPENSTACK,
  57. api.HYPERVISOR_CTYUN,
  58. api.HYPERVISOR_SANGFOR,
  59. }) {
  60. srv, err = NewWebsocketProxyServer(sessionObj)
  61. } else {
  62. srv, err = NewWebsockifyServer(sessionObj)
  63. }
  64. case session.WMKS:
  65. srv, err = NewWebsocketProxyServer(sessionObj)
  66. case session.WS:
  67. srv, err = NewSshServer(sessionObj)
  68. case session.RDP:
  69. srv, err = NewRDPServer(sessionObj)
  70. default:
  71. srv, err = NewTTYServer(sessionObj)
  72. }
  73. if err != nil {
  74. httperrors.GeneralServerError(ctx, w, err)
  75. return
  76. }
  77. srv.ServeHTTP(w, req)
  78. }