2015-06-23 12:55:30 +00:00
|
|
|
// Copyright (C) 2015 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2015-09-23 20:03:36 +00:00
|
|
|
package connections
|
2015-06-23 12:55:30 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"net"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
2015-06-28 15:05:29 +00:00
|
|
|
|
2015-10-12 18:30:14 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/dialer"
|
2015-06-28 15:05:29 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/model"
|
2015-06-23 12:55:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
dialers["tcp"] = tcpDialer
|
|
|
|
listeners["tcp"] = tcpListener
|
|
|
|
}
|
|
|
|
|
|
|
|
func tcpDialer(uri *url.URL, tlsCfg *tls.Config) (*tls.Conn, error) {
|
2016-01-07 11:18:00 +00:00
|
|
|
// Check that there is a port number in uri.Host, otherwise add one.
|
2015-06-23 12:55:30 +00:00
|
|
|
host, port, err := net.SplitHostPort(uri.Host)
|
|
|
|
if err != nil && strings.HasPrefix(err.Error(), "missing port") {
|
|
|
|
// addr is on the form "1.2.3.4"
|
|
|
|
uri.Host = net.JoinHostPort(uri.Host, "22000")
|
|
|
|
} else if err == nil && port == "" {
|
|
|
|
// addr is on the form "1.2.3.4:"
|
|
|
|
uri.Host = net.JoinHostPort(host, "22000")
|
|
|
|
}
|
|
|
|
|
2016-01-07 11:18:00 +00:00
|
|
|
// Don't try to resolve the address before dialing. The dialer may be a
|
|
|
|
// proxy, and we should let the proxy do the resolving in that case.
|
|
|
|
conn, err := dialer.Dial("tcp", uri.Host)
|
2015-06-23 12:55:30 +00:00
|
|
|
if err != nil {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln(err)
|
2015-06-23 12:55:30 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tc := tls.Client(conn, tlsCfg)
|
|
|
|
err = tc.Handshake()
|
|
|
|
if err != nil {
|
|
|
|
tc.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tc, nil
|
|
|
|
}
|
|
|
|
|
2015-07-17 20:22:07 +00:00
|
|
|
func tcpListener(uri *url.URL, tlsCfg *tls.Config, conns chan<- model.IntermediateConnection) {
|
2015-06-23 12:55:30 +00:00
|
|
|
tcaddr, err := net.ResolveTCPAddr("tcp", uri.Host)
|
|
|
|
if err != nil {
|
|
|
|
l.Fatalln("listen (BEP/tcp):", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
listener, err := net.ListenTCP("tcp", tcaddr)
|
|
|
|
if err != nil {
|
|
|
|
l.Fatalln("listen (BEP/tcp):", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
conn, err := listener.Accept()
|
|
|
|
if err != nil {
|
|
|
|
l.Warnln("Accepting connection (BEP/tcp):", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln("connect from", conn.RemoteAddr())
|
2015-06-23 12:55:30 +00:00
|
|
|
|
2016-01-30 01:52:32 +00:00
|
|
|
err = dialer.SetTCPOptions(conn.(*net.TCPConn))
|
2015-07-17 20:22:07 +00:00
|
|
|
if err != nil {
|
|
|
|
l.Infoln(err)
|
|
|
|
}
|
2015-06-23 12:55:30 +00:00
|
|
|
|
|
|
|
tc := tls.Server(conn, tlsCfg)
|
|
|
|
err = tc.Handshake()
|
|
|
|
if err != nil {
|
|
|
|
l.Infoln("TLS handshake (BEP/tcp):", err)
|
|
|
|
tc.Close()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-07-17 20:22:07 +00:00
|
|
|
conns <- model.IntermediateConnection{
|
2015-08-23 06:43:33 +00:00
|
|
|
tc, model.ConnectionTypeDirectAccept,
|
2015-06-28 15:05:29 +00:00
|
|
|
}
|
2015-06-23 12:55:30 +00:00
|
|
|
}
|
|
|
|
}
|