mirror of
https://github.com/octoleo/syncthing.git
synced 2025-01-08 09:18:44 +00:00
lib/connections: Allow "tcp4" and "tcp6" addresses
This commit is contained in:
parent
82e80a479a
commit
690837dbe5
@ -17,75 +17,82 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
dialers["tcp"] = tcpDialer
|
for _, network := range []string{"tcp", "tcp4", "tcp6"} {
|
||||||
listeners["tcp"] = tcpListener
|
dialers[network] = makeTcpDialer(network)
|
||||||
|
listeners[network] = makeTcpListener(network)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func tcpDialer(uri *url.URL, tlsCfg *tls.Config) (*tls.Conn, error) {
|
func makeTcpDialer(network string) DialerFactory {
|
||||||
// Check that there is a port number in uri.Host, otherwise add one.
|
return func(uri *url.URL, tlsCfg *tls.Config) (*tls.Conn, error) {
|
||||||
host, port, err := net.SplitHostPort(uri.Host)
|
// Check that there is a port number in uri.Host, otherwise add one.
|
||||||
if err != nil && strings.HasPrefix(err.Error(), "missing port") {
|
host, port, err := net.SplitHostPort(uri.Host)
|
||||||
// addr is on the form "1.2.3.4"
|
if err != nil && strings.HasPrefix(err.Error(), "missing port") {
|
||||||
uri.Host = net.JoinHostPort(uri.Host, "22000")
|
// addr is on the form "1.2.3.4"
|
||||||
} else if err == nil && port == "" {
|
uri.Host = net.JoinHostPort(uri.Host, "22000")
|
||||||
// addr is on the form "1.2.3.4:"
|
} else if err == nil && port == "" {
|
||||||
uri.Host = net.JoinHostPort(host, "22000")
|
// addr is on the form "1.2.3.4:"
|
||||||
}
|
uri.Host = net.JoinHostPort(host, "22000")
|
||||||
|
|
||||||
// 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)
|
|
||||||
if err != nil {
|
|
||||||
l.Debugln(err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
tc := tls.Client(conn, tlsCfg)
|
|
||||||
err = tc.Handshake()
|
|
||||||
if err != nil {
|
|
||||||
tc.Close()
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return tc, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func tcpListener(uri *url.URL, tlsCfg *tls.Config, conns chan<- model.IntermediateConnection) {
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
l.Debugln("connect from", conn.RemoteAddr())
|
// 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.
|
||||||
err = dialer.SetTCPOptions(conn.(*net.TCPConn))
|
conn, err := dialer.Dial(network, uri.Host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Infoln(err)
|
l.Debugln(err)
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
tc := tls.Server(conn, tlsCfg)
|
tc := tls.Client(conn, tlsCfg)
|
||||||
err = tc.Handshake()
|
err = tc.Handshake()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Infoln("TLS handshake (BEP/tcp):", err)
|
|
||||||
tc.Close()
|
tc.Close()
|
||||||
continue
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
conns <- model.IntermediateConnection{
|
return tc, nil
|
||||||
tc, model.ConnectionTypeDirectAccept,
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func makeTcpListener(network string) ListenerFactory {
|
||||||
|
return func(uri *url.URL, tlsCfg *tls.Config, conns chan<- model.IntermediateConnection) {
|
||||||
|
tcaddr, err := net.ResolveTCPAddr(network, uri.Host)
|
||||||
|
if err != nil {
|
||||||
|
l.Fatalln("listen (BEP/tcp):", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
listener, err := net.ListenTCP(network, 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
|
||||||
|
}
|
||||||
|
|
||||||
|
l.Debugln("connect from", conn.RemoteAddr())
|
||||||
|
|
||||||
|
err = dialer.SetTCPOptions(conn.(*net.TCPConn))
|
||||||
|
if err != nil {
|
||||||
|
l.Infoln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
tc := tls.Server(conn, tlsCfg)
|
||||||
|
err = tc.Handshake()
|
||||||
|
if err != nil {
|
||||||
|
l.Infoln("TLS handshake (BEP/tcp):", err)
|
||||||
|
tc.Close()
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
conns <- model.IntermediateConnection{
|
||||||
|
tc, model.ConnectionTypeDirectAccept,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user