2015-06-28 01:52:01 +01:00
|
|
|
// Copyright (C) 2015 Audrius Butkevicius and Contributors (see the CONTRIBUTORS file).
|
|
|
|
|
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2019-11-21 08:41:15 +01:00
|
|
|
"context"
|
2015-06-28 01:52:01 +01:00
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
2015-09-22 19:38:46 +02:00
|
|
|
"github.com/syncthing/syncthing/lib/relay/protocol"
|
2020-12-22 20:17:14 +01:00
|
|
|
"github.com/syncthing/syncthing/lib/svcutil"
|
2019-07-09 11:40:30 +02:00
|
|
|
|
2020-11-17 13:19:04 +01:00
|
|
|
"github.com/thejerf/suture/v4"
|
2015-06-28 01:52:01 +01:00
|
|
|
)
|
|
|
|
|
2015-10-16 23:59:24 +01:00
|
|
|
type RelayClient interface {
|
2019-07-09 11:40:30 +02:00
|
|
|
suture.Service
|
2016-05-04 19:38:12 +00:00
|
|
|
Error() error
|
2015-10-16 23:59:24 +01:00
|
|
|
String() string
|
2021-07-21 09:49:09 +02:00
|
|
|
Invitations() <-chan protocol.SessionInvitation
|
2015-10-16 23:59:24 +01:00
|
|
|
URI() *url.URL
|
2015-06-28 01:52:01 +01:00
|
|
|
}
|
|
|
|
|
2021-05-10 22:25:43 +02:00
|
|
|
func NewClient(uri *url.URL, certs []tls.Certificate, timeout time.Duration) (RelayClient, error) {
|
2021-07-21 09:49:09 +02:00
|
|
|
invitations := make(chan protocol.SessionInvitation)
|
|
|
|
|
|
|
|
switch uri.Scheme {
|
|
|
|
case "relay":
|
|
|
|
return newStaticClient(uri, certs, invitations, timeout), nil
|
|
|
|
case "dynamic+http", "dynamic+https":
|
|
|
|
return newDynamicClient(uri, certs, invitations, timeout), nil
|
|
|
|
default:
|
2020-03-03 22:40:00 +01:00
|
|
|
return nil, fmt.Errorf("unsupported scheme: %s", uri.Scheme)
|
2015-06-28 01:52:01 +01:00
|
|
|
}
|
2015-07-20 11:56:10 +02:00
|
|
|
}
|
2019-07-09 11:40:30 +02:00
|
|
|
|
|
|
|
type commonClient struct {
|
2020-12-22 20:17:14 +01:00
|
|
|
svcutil.ServiceWithError
|
2021-05-10 22:25:43 +02:00
|
|
|
invitations chan protocol.SessionInvitation
|
2019-07-09 11:40:30 +02:00
|
|
|
}
|
|
|
|
|
2019-11-21 08:41:15 +01:00
|
|
|
func newCommonClient(invitations chan protocol.SessionInvitation, serve func(context.Context) error, creator string) commonClient {
|
2021-07-21 09:49:09 +02:00
|
|
|
return commonClient{
|
|
|
|
ServiceWithError: svcutil.AsService(serve, creator),
|
|
|
|
invitations: invitations,
|
2019-07-09 11:40:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-21 09:49:09 +02:00
|
|
|
func (c *commonClient) Invitations() <-chan protocol.SessionInvitation {
|
2019-07-09 11:40:30 +02:00
|
|
|
return c.invitations
|
|
|
|
}
|