methods.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright (C) 2015 Audrius Butkevicius and Contributors (see the CONTRIBUTORS file).
  2. package client
  3. import (
  4. "crypto/tls"
  5. "fmt"
  6. "net"
  7. "net/url"
  8. "strconv"
  9. "strings"
  10. "time"
  11. syncthingprotocol "github.com/syncthing/syncthing/lib/protocol"
  12. "github.com/syncthing/syncthing/lib/relay/protocol"
  13. )
  14. func GetInvitationFromRelay(uri *url.URL, id syncthingprotocol.DeviceID, certs []tls.Certificate) (protocol.SessionInvitation, error) {
  15. if uri.Scheme != "relay" {
  16. return protocol.SessionInvitation{}, fmt.Errorf("Unsupported relay scheme: %v", uri.Scheme)
  17. }
  18. conn, err := tls.Dial("tcp", uri.Host, configForCerts(certs))
  19. if err != nil {
  20. return protocol.SessionInvitation{}, err
  21. }
  22. conn.SetDeadline(time.Now().Add(10 * time.Second))
  23. if err := performHandshakeAndValidation(conn, uri); err != nil {
  24. return protocol.SessionInvitation{}, err
  25. }
  26. defer conn.Close()
  27. request := protocol.ConnectRequest{
  28. ID: id[:],
  29. }
  30. if err := protocol.WriteMessage(conn, request); err != nil {
  31. return protocol.SessionInvitation{}, err
  32. }
  33. message, err := protocol.ReadMessage(conn)
  34. if err != nil {
  35. return protocol.SessionInvitation{}, err
  36. }
  37. switch msg := message.(type) {
  38. case protocol.Response:
  39. return protocol.SessionInvitation{}, fmt.Errorf("Incorrect response code %d: %s", msg.Code, msg.Message)
  40. case protocol.SessionInvitation:
  41. if debug {
  42. l.Debugln("Received invitation", msg, "via", conn.LocalAddr())
  43. }
  44. ip := net.IP(msg.Address)
  45. if len(ip) == 0 || ip.IsUnspecified() {
  46. msg.Address = conn.RemoteAddr().(*net.TCPAddr).IP[:]
  47. }
  48. return msg, nil
  49. default:
  50. return protocol.SessionInvitation{}, fmt.Errorf("protocol error: unexpected message %v", msg)
  51. }
  52. }
  53. func JoinSession(invitation protocol.SessionInvitation) (net.Conn, error) {
  54. addr := net.JoinHostPort(net.IP(invitation.Address).String(), strconv.Itoa(int(invitation.Port)))
  55. conn, err := net.Dial("tcp", addr)
  56. if err != nil {
  57. return nil, err
  58. }
  59. request := protocol.JoinSessionRequest{
  60. Key: invitation.Key,
  61. }
  62. conn.SetDeadline(time.Now().Add(10 * time.Second))
  63. err = protocol.WriteMessage(conn, request)
  64. if err != nil {
  65. return nil, err
  66. }
  67. message, err := protocol.ReadMessage(conn)
  68. if err != nil {
  69. return nil, err
  70. }
  71. conn.SetDeadline(time.Time{})
  72. switch msg := message.(type) {
  73. case protocol.Response:
  74. if msg.Code != 0 {
  75. return nil, fmt.Errorf("Incorrect response code %d: %s", msg.Code, msg.Message)
  76. }
  77. return conn, nil
  78. default:
  79. return nil, fmt.Errorf("protocol error: expecting response got %v", msg)
  80. }
  81. }
  82. func TestRelay(uri *url.URL, certs []tls.Certificate, sleep time.Duration, times int) bool {
  83. id := syncthingprotocol.NewDeviceID(certs[0].Certificate[0])
  84. invs := make(chan protocol.SessionInvitation, 1)
  85. c := NewProtocolClient(uri, certs, invs)
  86. go c.Serve()
  87. defer func() {
  88. close(invs)
  89. c.Stop()
  90. }()
  91. for i := 0; i < times; i++ {
  92. _, err := GetInvitationFromRelay(uri, id, certs)
  93. if err == nil {
  94. return true
  95. }
  96. if !strings.Contains(err.Error(), "Incorrect response code") {
  97. return false
  98. }
  99. time.Sleep(sleep)
  100. }
  101. return false
  102. }
  103. func configForCerts(certs []tls.Certificate) *tls.Config {
  104. return &tls.Config{
  105. Certificates: certs,
  106. NextProtos: []string{protocol.ProtocolName},
  107. ClientAuth: tls.RequestClientCert,
  108. SessionTicketsDisabled: true,
  109. InsecureSkipVerify: true,
  110. MinVersion: tls.VersionTLS12,
  111. CipherSuites: []uint16{
  112. tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  113. tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
  114. tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
  115. tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
  116. tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
  117. tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
  118. },
  119. }
  120. }