syncthing/lib/relay/client/client.go

54 lines
1.3 KiB
Go
Raw Normal View History

2015-06-28 02:52:01 +02:00
// Copyright (C) 2015 Audrius Butkevicius and Contributors (see the CONTRIBUTORS file).
package client
import (
"context"
2015-06-28 02:52:01 +02:00
"crypto/tls"
"fmt"
"net/url"
"time"
2015-09-22 19:38:46 +02:00
"github.com/syncthing/syncthing/lib/relay/protocol"
"github.com/syncthing/syncthing/lib/svcutil"
2020-11-17 13:19:04 +01:00
"github.com/thejerf/suture/v4"
2015-06-28 02:52:01 +02:00
)
type RelayClient interface {
suture.Service
Error() error
String() string
Invitations() <-chan protocol.SessionInvitation
URI() *url.URL
2015-06-28 02:52:01 +02:00
}
func NewClient(uri *url.URL, certs []tls.Certificate, timeout time.Duration) (RelayClient, error) {
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:
return nil, fmt.Errorf("unsupported scheme: %s", uri.Scheme)
2015-06-28 02:52:01 +02:00
}
2015-07-20 11:56:10 +02:00
}
type commonClient struct {
svcutil.ServiceWithError
invitations chan protocol.SessionInvitation
}
func newCommonClient(invitations chan protocol.SessionInvitation, serve func(context.Context) error, creator string) commonClient {
return commonClient{
ServiceWithError: svcutil.AsService(serve, creator),
invitations: invitations,
}
}
func (c *commonClient) Invitations() <-chan protocol.SessionInvitation {
return c.invitations
}