guac.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 guac
  15. import (
  16. "fmt"
  17. "net"
  18. )
  19. func NewGuacamoleTunnel(host string, port int, user, password string, id string, w, h, dpi int, uId string) (*GuacamoleTunnel, error) {
  20. opts := NewGuacOptions()
  21. opts.ConnectionId = id
  22. opts.Protocol = "rdp"
  23. if w > 0 && h > 0 {
  24. opts.OptimalScreenHeight = h
  25. opts.OptimalScreenWidth = w
  26. }
  27. if dpi > 0 {
  28. opts.OptimalResolution = dpi
  29. }
  30. opts.AudioMimetypes = []string{"audio/L16", "rate=44100", "channels=2"}
  31. opts.Parameters = map[string]string{
  32. "scheme": opts.Protocol,
  33. "hostname": host,
  34. "port": fmt.Sprintf("%d", port),
  35. "ignore-cert": "true",
  36. "security": "",
  37. "username": user,
  38. "password": password,
  39. "enable-drive": "true",
  40. "drive-name": "Cloudpods",
  41. "drive-path": "/opt/cloudpods/" + uId,
  42. "create-drive-path": "true",
  43. }
  44. conn, err := net.Dial("tcp", "127.0.0.1:4822")
  45. if err != nil {
  46. return nil, err
  47. }
  48. ret := &GuacamoleTunnel{
  49. conn: conn,
  50. opts: opts,
  51. }
  52. err = ret.Handshake()
  53. if err != nil {
  54. return nil, err
  55. }
  56. return ret, nil
  57. }