client.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright (C) 2015 Audrius Butkevicius and Contributors (see the CONTRIBUTORS file).
  2. package client
  3. import (
  4. "context"
  5. "crypto/tls"
  6. "fmt"
  7. "net/url"
  8. "time"
  9. "github.com/syncthing/syncthing/lib/relay/protocol"
  10. "github.com/syncthing/syncthing/lib/svcutil"
  11. "github.com/thejerf/suture/v4"
  12. )
  13. type RelayClient interface {
  14. suture.Service
  15. Error() error
  16. String() string
  17. Invitations() <-chan protocol.SessionInvitation
  18. URI() *url.URL
  19. }
  20. func NewClient(uri *url.URL, certs []tls.Certificate, timeout time.Duration) (RelayClient, error) {
  21. invitations := make(chan protocol.SessionInvitation)
  22. switch uri.Scheme {
  23. case "relay":
  24. return newStaticClient(uri, certs, invitations, timeout), nil
  25. case "dynamic+http", "dynamic+https":
  26. return newDynamicClient(uri, certs, invitations, timeout), nil
  27. default:
  28. return nil, fmt.Errorf("unsupported scheme: %s", uri.Scheme)
  29. }
  30. }
  31. type commonClient struct {
  32. svcutil.ServiceWithError
  33. invitations chan protocol.SessionInvitation
  34. }
  35. func newCommonClient(invitations chan protocol.SessionInvitation, serve func(context.Context) error, creator string) commonClient {
  36. return commonClient{
  37. ServiceWithError: svcutil.AsService(serve, creator),
  38. invitations: invitations,
  39. }
  40. }
  41. func (c *commonClient) Invitations() <-chan protocol.SessionInvitation {
  42. return c.invitations
  43. }