syncthing/cmd/relaysrv/main.go

143 lines
4.8 KiB
Go
Raw Normal View History

2015-06-24 13:39:46 +02:00
// Copyright (C) 2015 Audrius Butkevicius and Contributors (see the CONTRIBUTORS file).
package main
import (
"crypto/tls"
"flag"
2015-09-07 10:21:23 +02:00
"fmt"
2015-06-24 13:39:46 +02:00
"log"
2015-06-28 02:52:01 +02:00
"net"
2015-09-07 10:21:23 +02:00
"net/url"
2015-06-24 13:39:46 +02:00
"path/filepath"
2015-09-07 10:21:23 +02:00
"strings"
2015-06-24 13:39:46 +02:00
"time"
"github.com/juju/ratelimit"
"github.com/syncthing/syncthing/lib/relay/protocol"
"github.com/syncthing/syncthing/lib/tlsutil"
2015-06-28 02:52:01 +02:00
syncthingprotocol "github.com/syncthing/syncthing/lib/protocol"
2015-06-24 13:39:46 +02:00
)
var (
2015-09-02 22:35:52 +02:00
listen string
debug bool = false
2015-06-24 13:39:46 +02:00
sessionAddress []byte
sessionPort uint16
networkTimeout time.Duration = 2 * time.Minute
pingInterval time.Duration = time.Minute
messageTimeout time.Duration = time.Minute
sessionLimitBps int
globalLimitBps int
sessionLimiter *ratelimit.Bucket
globalLimiter *ratelimit.Bucket
2015-08-20 12:59:44 +02:00
2015-09-07 10:21:23 +02:00
statusAddr string
poolAddrs string
2015-10-18 17:57:13 +02:00
providedBy string
2015-10-17 01:07:01 +02:00
defaultPoolAddrs string = "https://relays.syncthing.net/endpoint"
2015-06-24 13:39:46 +02:00
)
func main() {
log.SetFlags(log.Lshortfile | log.LstdFlags)
2015-06-24 13:39:46 +02:00
var dir, extAddress string
2015-09-02 22:35:52 +02:00
flag.StringVar(&listen, "listen", ":22067", "Protocol listen address")
2015-06-24 13:39:46 +02:00
flag.StringVar(&dir, "keys", ".", "Directory where cert.pem and key.pem is stored")
flag.DurationVar(&networkTimeout, "network-timeout", networkTimeout, "Timeout for network operations between the client and the relay.\n\tIf no data is received between the client and the relay in this period of time, the connection is terminated.\n\tFurthermore, if no data is sent between either clients being relayed within this period of time, the session is also terminated.")
flag.DurationVar(&pingInterval, "ping-interval", pingInterval, "How often pings are sent")
flag.DurationVar(&messageTimeout, "message-timeout", messageTimeout, "Maximum amount of time we wait for relevant messages to arrive")
flag.IntVar(&sessionLimitBps, "per-session-rate", sessionLimitBps, "Per session rate limit, in bytes/s")
flag.IntVar(&globalLimitBps, "global-rate", globalLimitBps, "Global rate limit, in bytes/s")
flag.BoolVar(&debug, "debug", debug, "Enable debug output")
2015-08-20 12:59:44 +02:00
flag.StringVar(&statusAddr, "status-srv", ":22070", "Listen address for status service (blank to disable)")
flag.StringVar(&poolAddrs, "pools", defaultPoolAddrs, "Comma separated list of relay pool addresses to join")
2015-10-18 17:57:13 +02:00
flag.StringVar(&providedBy, "provided-by", "", "An optional description about who provides the relay")
flag.Parse()
2015-06-24 13:39:46 +02:00
2015-06-28 02:52:01 +02:00
if extAddress == "" {
2015-09-02 22:35:52 +02:00
extAddress = listen
2015-06-28 02:52:01 +02:00
}
addr, err := net.ResolveTCPAddr("tcp", extAddress)
if err != nil {
log.Fatal(err)
}
sessionAddress = addr.IP[:]
sessionPort = uint16(addr.Port)
2015-06-24 13:39:46 +02:00
certFile, keyFile := filepath.Join(dir, "cert.pem"), filepath.Join(dir, "key.pem")
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
log.Println("Failed to load keypair. Generating one, this might take a while...")
cert, err = tlsutil.NewCertificate(certFile, keyFile, "relaysrv", 3072)
if err != nil {
log.Fatalln("Failed to generate X509 key pair:", err)
}
2015-06-24 13:39:46 +02:00
}
tlsCfg := &tls.Config{
Certificates: []tls.Certificate{cert},
NextProtos: []string{protocol.ProtocolName},
ClientAuth: tls.RequestClientCert,
SessionTicketsDisabled: true,
InsecureSkipVerify: true,
MinVersion: tls.VersionTLS12,
CipherSuites: []uint16{
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
},
}
2015-06-28 02:52:01 +02:00
id := syncthingprotocol.NewDeviceID(cert.Certificate[0])
if debug {
log.Println("ID:", id)
}
2015-06-24 13:39:46 +02:00
if sessionLimitBps > 0 {
sessionLimiter = ratelimit.NewBucketWithRate(float64(sessionLimitBps), int64(2*sessionLimitBps))
}
if globalLimitBps > 0 {
globalLimiter = ratelimit.NewBucketWithRate(float64(globalLimitBps), int64(2*globalLimitBps))
}
2015-08-20 12:59:44 +02:00
if statusAddr != "" {
go statusService(statusAddr)
}
2015-10-18 17:57:13 +02:00
uri, err := url.Parse(fmt.Sprintf("relay://%s/?id=%s&pingInterval=%s&networkTimeout=%s&sessionLimitBps=%d&globalLimitBps=%d&statusAddr=%s&providedBy=%s", extAddress, id, pingInterval, networkTimeout, sessionLimitBps, globalLimitBps, statusAddr, providedBy))
2015-09-07 10:21:23 +02:00
if err != nil {
log.Fatalln("Failed to construct URI", err)
}
2015-09-21 23:15:29 +02:00
log.Println("URI:", uri.String())
2015-09-07 10:21:23 +02:00
if poolAddrs == defaultPoolAddrs {
log.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
log.Println("!! Joining default relay pools, this relay will be available for public use. !!")
log.Println(`!! Use the -pools="" command line option to make the relay private. !!`)
log.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
}
pools := strings.Split(poolAddrs, ",")
for _, pool := range pools {
pool = strings.TrimSpace(pool)
if len(pool) > 0 {
go poolHandler(pool, uri)
}
}
2015-09-02 22:35:52 +02:00
listener(listen, tlsCfg)
2015-06-24 13:39:46 +02:00
}