connections_tcp.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright (C) 2015 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"
  10. "net/url"
  11. "strings"
  12. "github.com/syncthing/syncthing/lib/dialer"
  13. "github.com/syncthing/syncthing/lib/model"
  14. )
  15. func init() {
  16. dialers["tcp"] = tcpDialer
  17. listeners["tcp"] = tcpListener
  18. }
  19. func tcpDialer(uri *url.URL, tlsCfg *tls.Config) (*tls.Conn, error) {
  20. // Check that there is a port number in uri.Host, otherwise add one.
  21. host, port, err := net.SplitHostPort(uri.Host)
  22. if err != nil && strings.HasPrefix(err.Error(), "missing port") {
  23. // addr is on the form "1.2.3.4"
  24. uri.Host = net.JoinHostPort(uri.Host, "22000")
  25. } else if err == nil && port == "" {
  26. // addr is on the form "1.2.3.4:"
  27. uri.Host = net.JoinHostPort(host, "22000")
  28. }
  29. // Don't try to resolve the address before dialing. The dialer may be a
  30. // proxy, and we should let the proxy do the resolving in that case.
  31. conn, err := dialer.Dial("tcp", uri.Host)
  32. if err != nil {
  33. l.Debugln(err)
  34. return nil, err
  35. }
  36. tc := tls.Client(conn, tlsCfg)
  37. err = tc.Handshake()
  38. if err != nil {
  39. tc.Close()
  40. return nil, err
  41. }
  42. return tc, nil
  43. }
  44. func tcpListener(uri *url.URL, tlsCfg *tls.Config, conns chan<- model.IntermediateConnection) {
  45. tcaddr, err := net.ResolveTCPAddr("tcp", uri.Host)
  46. if err != nil {
  47. l.Fatalln("listen (BEP/tcp):", err)
  48. return
  49. }
  50. listener, err := net.ListenTCP("tcp", tcaddr)
  51. if err != nil {
  52. l.Fatalln("listen (BEP/tcp):", err)
  53. return
  54. }
  55. for {
  56. conn, err := listener.Accept()
  57. if err != nil {
  58. l.Warnln("Accepting connection (BEP/tcp):", err)
  59. continue
  60. }
  61. l.Debugln("connect from", conn.RemoteAddr())
  62. err = dialer.SetTCPOptions(conn.(*net.TCPConn))
  63. if err != nil {
  64. l.Infoln(err)
  65. }
  66. tc := tls.Server(conn, tlsCfg)
  67. err = tc.Handshake()
  68. if err != nil {
  69. l.Infoln("TLS handshake (BEP/tcp):", err)
  70. tc.Close()
  71. continue
  72. }
  73. conns <- model.IntermediateConnection{
  74. tc, model.ConnectionTypeDirectAccept,
  75. }
  76. }
  77. }