2015-06-28 00:52:01 +00:00
|
|
|
// Copyright (C) 2015 Audrius Butkevicius and Contributors (see the CONTRIBUTORS file).
|
|
|
|
|
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
2015-09-22 17:38:46 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/relay/protocol"
|
2015-06-28 00:52:01 +00:00
|
|
|
)
|
|
|
|
|
2015-11-23 21:14:46 +00:00
|
|
|
type relayClientFactory func(uri *url.URL, certs []tls.Certificate, invitations chan protocol.SessionInvitation, timeout time.Duration) RelayClient
|
2015-06-28 00:52:01 +00:00
|
|
|
|
2015-10-16 22:59:24 +00:00
|
|
|
var (
|
|
|
|
supportedSchemes = map[string]relayClientFactory{
|
|
|
|
"relay": newStaticClient,
|
|
|
|
"dynamic+http": newDynamicClient,
|
|
|
|
"dynamic+https": newDynamicClient,
|
2015-06-28 00:52:01 +00:00
|
|
|
}
|
2015-10-16 22:59:24 +00:00
|
|
|
)
|
2015-06-28 00:52:01 +00:00
|
|
|
|
2015-10-16 22:59:24 +00:00
|
|
|
type RelayClient interface {
|
|
|
|
Serve()
|
|
|
|
Stop()
|
|
|
|
StatusOK() bool
|
|
|
|
Latency() time.Duration
|
|
|
|
String() string
|
|
|
|
Invitations() chan protocol.SessionInvitation
|
|
|
|
URI() *url.URL
|
2015-06-28 00:52:01 +00:00
|
|
|
}
|
|
|
|
|
2015-11-23 21:14:46 +00:00
|
|
|
func NewClient(uri *url.URL, certs []tls.Certificate, invitations chan protocol.SessionInvitation, timeout time.Duration) (RelayClient, error) {
|
2015-10-16 22:59:24 +00:00
|
|
|
factory, ok := supportedSchemes[uri.Scheme]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Unsupported scheme: %s", uri.Scheme)
|
2015-06-28 00:52:01 +00:00
|
|
|
}
|
|
|
|
|
2015-11-23 21:14:46 +00:00
|
|
|
return factory(uri, certs, invitations, timeout), nil
|
2015-07-20 09:56:10 +00:00
|
|
|
}
|