mconnect - KDE Connect protocol implementation in Vala/C
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
2.8 KiB

  1. // Licensed under the Apache License, Version 2.0 (the "License");
  2. // you may not use this file except in compliance with the License.
  3. // You may obtain a copy of the License at
  4. //
  5. // http://www.apache.org/licenses/LICENSE-2.0
  6. //
  7. // Unless required by applicable law or agreed to in writing, software
  8. // distributed under the License is distributed on an "AS IS" BASIS,
  9. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. // See the License for the specific language governing permissions and
  11. // limitations under the License.
  12. package main
  13. import (
  14. "context"
  15. "os"
  16. "os/user"
  17. "github.com/jessevdk/go-flags"
  18. "github.com/bboozzoo/mconnect/logger"
  19. "github.com/bboozzoo/mconnect/protocol"
  20. "github.com/bboozzoo/mconnect/protocol/packet"
  21. uflags "github.com/bboozzoo/mconnect/utils/flags"
  22. )
  23. var (
  24. Stderr = os.Stderr
  25. Stdout = os.Stdout
  26. )
  27. func main() {
  28. var opts struct {
  29. Debug bool `short:"d" long:"debug" description:"Show debugging information"`
  30. Address string `short:"a" long:"address" description:"Address of remote device"`
  31. }
  32. _, err := flags.ParseArgs(&opts, os.Args)
  33. if err != nil {
  34. uflags.HandleFlagsError(err)
  35. }
  36. ctx := context.Background()
  37. ctx = logger.WithContext(ctx, logger.New())
  38. log := logger.FromContext(ctx)
  39. log.SetLevel(logger.ErrorLevel)
  40. if opts.Debug {
  41. log.SetLevel(logger.DebugLevel)
  42. }
  43. hostname, err := os.Hostname()
  44. if err != nil {
  45. log.Errorf("cannot obtain hostname: %v", err)
  46. os.Exit(1)
  47. }
  48. u, err := user.Current()
  49. if err != nil {
  50. log.Errorf("cannot obtain current user: %v", err)
  51. os.Exit(1)
  52. }
  53. entity := u.Name + "@" + hostname
  54. deviceCert, err := protocol.GenerateDeviceCertificate(entity)
  55. if err != nil {
  56. log.Errorf("cannot generate device certificate for entity %q: %v",
  57. entity, err)
  58. os.Exit(1)
  59. }
  60. conf := protocol.Configuration{
  61. Identity: &packet.Identity{
  62. DeviceId: "mconnect-" + hostname,
  63. DeviceName: hostname,
  64. DeviceType: "computer",
  65. ProtocolVersion: 7,
  66. TcpPort: 1716,
  67. },
  68. Cert: deviceCert.TLSCertificate(),
  69. }
  70. conn, err := protocol.Dial(ctx, opts.Address, &conf)
  71. if err != nil {
  72. log.Errorf("connection failed: %v", err)
  73. os.Exit(1)
  74. }
  75. defer conn.Close()
  76. for {
  77. var response *packet.Packet
  78. p, err := conn.Receive()
  79. if err != nil {
  80. log.Errorf("failed to receive packet: %v", err)
  81. os.Exit(1)
  82. }
  83. log.Infof("got packet: %+v", p)
  84. log.Infof("packet type: %q", p.Type)
  85. switch p.Type {
  86. case "kdeconnect.pair":
  87. log.Infof("pair request")
  88. pair, err := p.AsPair()
  89. if err != nil {
  90. log.Errorf("cannot decode pair packet: %v", err)
  91. continue
  92. }
  93. if pair.Pair {
  94. response = packet.NewPair()
  95. }
  96. }
  97. if response != nil {
  98. log.Debugf("sending response: %v", response)
  99. if err := conn.Send(*response); err != nil {
  100. log.Errorf("cannot send a response packet: %v", err)
  101. }
  102. }
  103. }
  104. }