2015-06-28 00:52:01 +00:00
|
|
|
// Copyright (C) 2015 Audrius Butkevicius and Contributors (see the CONTRIBUTORS file).
|
|
|
|
|
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2019-11-26 07:39:51 +00:00
|
|
|
"context"
|
2015-06-28 00:52:01 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2015-10-12 18:30:14 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/dialer"
|
2015-09-22 17:38:46 +00:00
|
|
|
syncthingprotocol "github.com/syncthing/syncthing/lib/protocol"
|
|
|
|
"github.com/syncthing/syncthing/lib/relay/protocol"
|
2015-06-28 00:52:01 +00:00
|
|
|
)
|
|
|
|
|
2020-04-04 07:21:52 +00:00
|
|
|
type incorrectResponseCodeErr struct {
|
|
|
|
code int32
|
|
|
|
msg string
|
|
|
|
}
|
|
|
|
|
2020-06-16 07:27:34 +00:00
|
|
|
func (e *incorrectResponseCodeErr) Error() string {
|
2020-04-04 07:21:52 +00:00
|
|
|
return fmt.Sprintf("incorrect response code %d: %s", e.code, e.msg)
|
|
|
|
}
|
|
|
|
|
2019-11-26 07:39:51 +00:00
|
|
|
func GetInvitationFromRelay(ctx context.Context, uri *url.URL, id syncthingprotocol.DeviceID, certs []tls.Certificate, timeout time.Duration) (protocol.SessionInvitation, error) {
|
2015-06-28 19:34:28 +00:00
|
|
|
if uri.Scheme != "relay" {
|
2020-03-03 21:40:00 +00:00
|
|
|
return protocol.SessionInvitation{}, fmt.Errorf("unsupported relay scheme: %v", uri.Scheme)
|
2015-06-28 19:34:28 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 07:39:51 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, timeout)
|
|
|
|
defer cancel()
|
|
|
|
rconn, err := dialer.DialContext(ctx, "tcp", uri.Host)
|
2015-06-28 00:52:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return protocol.SessionInvitation{}, err
|
|
|
|
}
|
2015-10-12 18:30:14 +00:00
|
|
|
|
|
|
|
conn := tls.Client(rconn, configForCerts(certs))
|
2019-02-02 11:16:27 +00:00
|
|
|
conn.SetDeadline(time.Now().Add(timeout))
|
2015-06-28 00:52:01 +00:00
|
|
|
|
|
|
|
if err := performHandshakeAndValidation(conn, uri); err != nil {
|
|
|
|
return protocol.SessionInvitation{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
request := protocol.ConnectRequest{
|
|
|
|
ID: id[:],
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := protocol.WriteMessage(conn, request); err != nil {
|
|
|
|
return protocol.SessionInvitation{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
message, err := protocol.ReadMessage(conn)
|
|
|
|
if err != nil {
|
|
|
|
return protocol.SessionInvitation{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch msg := message.(type) {
|
|
|
|
case protocol.Response:
|
2020-06-16 07:27:34 +00:00
|
|
|
return protocol.SessionInvitation{}, &incorrectResponseCodeErr{msg.Code, msg.Message}
|
2015-06-28 00:52:01 +00:00
|
|
|
case protocol.SessionInvitation:
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln("Received invitation", msg, "via", conn.LocalAddr())
|
2015-06-28 00:52:01 +00:00
|
|
|
ip := net.IP(msg.Address)
|
|
|
|
if len(ip) == 0 || ip.IsUnspecified() {
|
2016-05-31 14:42:10 +00:00
|
|
|
msg.Address = remoteIPBytes(conn)
|
2015-06-28 00:52:01 +00:00
|
|
|
}
|
|
|
|
return msg, nil
|
|
|
|
default:
|
|
|
|
return protocol.SessionInvitation{}, fmt.Errorf("protocol error: unexpected message %v", msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-26 07:39:51 +00:00
|
|
|
func JoinSession(ctx context.Context, invitation protocol.SessionInvitation) (net.Conn, error) {
|
2015-06-28 00:52:01 +00:00
|
|
|
addr := net.JoinHostPort(net.IP(invitation.Address).String(), strconv.Itoa(int(invitation.Port)))
|
|
|
|
|
2019-11-26 07:39:51 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
conn, err := dialer.DialContext(ctx, "tcp", addr)
|
2015-06-28 00:52:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
request := protocol.JoinSessionRequest{
|
|
|
|
Key: invitation.Key,
|
|
|
|
}
|
|
|
|
|
2019-02-02 11:16:27 +00:00
|
|
|
conn.SetDeadline(time.Now().Add(10 * time.Second))
|
2015-06-28 00:52:01 +00:00
|
|
|
err = protocol.WriteMessage(conn, request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
message, err := protocol.ReadMessage(conn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-02-02 11:16:27 +00:00
|
|
|
conn.SetDeadline(time.Time{})
|
2015-06-28 00:52:01 +00:00
|
|
|
|
|
|
|
switch msg := message.(type) {
|
|
|
|
case protocol.Response:
|
|
|
|
if msg.Code != 0 {
|
2020-03-03 21:40:00 +00:00
|
|
|
return nil, fmt.Errorf("incorrect response code %d: %s", msg.Code, msg.Message)
|
2015-06-28 00:52:01 +00:00
|
|
|
}
|
|
|
|
return conn, nil
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("protocol error: expecting response got %v", msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-04 07:21:52 +00:00
|
|
|
func TestRelay(ctx context.Context, uri *url.URL, certs []tls.Certificate, sleep, timeout time.Duration, times int) error {
|
2015-09-06 17:35:38 +00:00
|
|
|
id := syncthingprotocol.NewDeviceID(certs[0].Certificate[0])
|
2015-09-06 19:25:53 +00:00
|
|
|
invs := make(chan protocol.SessionInvitation, 1)
|
2015-11-23 21:14:46 +00:00
|
|
|
c, err := NewClient(uri, certs, invs, timeout)
|
2015-10-16 22:59:24 +00:00
|
|
|
if err != nil {
|
|
|
|
close(invs)
|
2020-04-04 07:21:52 +00:00
|
|
|
return fmt.Errorf("creating client: %w", err)
|
2015-10-16 22:59:24 +00:00
|
|
|
}
|
2015-09-06 17:35:38 +00:00
|
|
|
go c.Serve()
|
2015-09-06 19:25:53 +00:00
|
|
|
defer func() {
|
|
|
|
c.Stop()
|
2015-10-22 22:09:02 +00:00
|
|
|
close(invs)
|
2015-09-06 19:25:53 +00:00
|
|
|
}()
|
2015-09-06 17:35:38 +00:00
|
|
|
|
2015-09-06 19:25:53 +00:00
|
|
|
for i := 0; i < times; i++ {
|
2020-04-04 07:21:52 +00:00
|
|
|
_, err = GetInvitationFromRelay(ctx, uri, id, certs, timeout)
|
2015-09-06 17:35:38 +00:00
|
|
|
if err == nil {
|
2020-04-04 07:21:52 +00:00
|
|
|
return nil
|
2015-09-06 17:35:38 +00:00
|
|
|
}
|
2020-06-16 07:27:34 +00:00
|
|
|
if _, ok := err.(*incorrectResponseCodeErr); !ok {
|
2020-04-04 07:21:52 +00:00
|
|
|
return fmt.Errorf("getting invitation: %w", err)
|
2015-09-06 17:35:38 +00:00
|
|
|
}
|
2015-09-06 19:25:53 +00:00
|
|
|
time.Sleep(sleep)
|
2015-09-06 17:35:38 +00:00
|
|
|
}
|
2020-04-04 07:21:52 +00:00
|
|
|
|
|
|
|
return fmt.Errorf("getting invitation: %w", err) // last of the above errors
|
2015-09-06 17:35:38 +00:00
|
|
|
}
|
|
|
|
|
2015-06-28 00:52:01 +00:00
|
|
|
func configForCerts(certs []tls.Certificate) *tls.Config {
|
|
|
|
return &tls.Config{
|
|
|
|
Certificates: certs,
|
|
|
|
NextProtos: []string{protocol.ProtocolName},
|
|
|
|
ClientAuth: tls.RequestClientCert,
|
|
|
|
SessionTicketsDisabled: true,
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
CipherSuites: []uint16{
|
|
|
|
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
|
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
|
|
|
|
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
|
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
|
|
|
|
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
|
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2016-05-31 14:42:10 +00:00
|
|
|
|
|
|
|
func remoteIPBytes(conn net.Conn) []byte {
|
|
|
|
addr := conn.RemoteAddr().String()
|
|
|
|
if host, _, err := net.SplitHostPort(addr); err == nil {
|
|
|
|
addr = host
|
|
|
|
}
|
|
|
|
return net.ParseIP(addr)[:]
|
|
|
|
}
|