1
0

tcp_dial.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright (C) 2016 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. package connections
  7. import (
  8. "crypto/tls"
  9. "net/url"
  10. "time"
  11. "github.com/syncthing/syncthing/lib/config"
  12. "github.com/syncthing/syncthing/lib/dialer"
  13. "github.com/syncthing/syncthing/lib/protocol"
  14. )
  15. const tcpPriority = 10
  16. func init() {
  17. factory := &tcpDialerFactory{}
  18. for _, scheme := range []string{"tcp", "tcp4", "tcp6"} {
  19. dialers[scheme] = factory
  20. }
  21. }
  22. type tcpDialer struct {
  23. cfg *config.Wrapper
  24. tlsCfg *tls.Config
  25. }
  26. func (d *tcpDialer) Dial(id protocol.DeviceID, uri *url.URL) (IntermediateConnection, error) {
  27. uri = fixupPort(uri)
  28. conn, err := dialer.DialTimeout(uri.Scheme, uri.Host, 10*time.Second)
  29. if err != nil {
  30. l.Debugln(err)
  31. return IntermediateConnection{}, err
  32. }
  33. tc := tls.Client(conn, d.tlsCfg)
  34. err = tc.Handshake()
  35. if err != nil {
  36. tc.Close()
  37. return IntermediateConnection{}, err
  38. }
  39. return IntermediateConnection{tc, "TCP (Client)", tcpPriority}, nil
  40. }
  41. func (d *tcpDialer) RedialFrequency() time.Duration {
  42. return time.Duration(d.cfg.Options().ReconnectIntervalS) * time.Second
  43. }
  44. type tcpDialerFactory struct{}
  45. func (tcpDialerFactory) New(cfg *config.Wrapper, tlsCfg *tls.Config) genericDialer {
  46. return &tcpDialer{
  47. cfg: cfg,
  48. tlsCfg: tlsCfg,
  49. }
  50. }
  51. func (tcpDialerFactory) Priority() int {
  52. return tcpPriority
  53. }
  54. func (tcpDialerFactory) Enabled(cfg config.Configuration) bool {
  55. return true
  56. }
  57. func (tcpDialerFactory) String() string {
  58. return "TCP Dialer"
  59. }