client.go 973 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright (C) 2015 Audrius Butkevicius and Contributors (see the CONTRIBUTORS file).
  2. package client
  3. import (
  4. "crypto/tls"
  5. "fmt"
  6. "net/url"
  7. "time"
  8. "github.com/syncthing/syncthing/lib/relay/protocol"
  9. )
  10. type relayClientFactory func(uri *url.URL, certs []tls.Certificate, invitations chan protocol.SessionInvitation) RelayClient
  11. var (
  12. supportedSchemes = map[string]relayClientFactory{
  13. "relay": newStaticClient,
  14. "dynamic+http": newDynamicClient,
  15. "dynamic+https": newDynamicClient,
  16. }
  17. )
  18. type RelayClient interface {
  19. Serve()
  20. Stop()
  21. StatusOK() bool
  22. Latency() time.Duration
  23. String() string
  24. Invitations() chan protocol.SessionInvitation
  25. URI() *url.URL
  26. }
  27. func NewClient(uri *url.URL, certs []tls.Certificate, invitations chan protocol.SessionInvitation) (RelayClient, error) {
  28. factory, ok := supportedSchemes[uri.Scheme]
  29. if !ok {
  30. return nil, fmt.Errorf("Unsupported scheme: %s", uri.Scheme)
  31. }
  32. return factory(uri, certs, invitations), nil
  33. }