main.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. // Copyright (C) 2015 Audrius Butkevicius and Contributors (see the CONTRIBUTORS file).
  2. package main
  3. import (
  4. "context"
  5. "crypto/tls"
  6. "crypto/x509"
  7. "encoding/json"
  8. "flag"
  9. "fmt"
  10. "log"
  11. "net"
  12. "net/http"
  13. "net/url"
  14. "os"
  15. "path/filepath"
  16. "strconv"
  17. "strings"
  18. "sync/atomic"
  19. "time"
  20. lru "github.com/hashicorp/golang-lru/v2"
  21. "github.com/syncthing/syncthing/lib/httpcache"
  22. "github.com/syncthing/syncthing/lib/protocol"
  23. "github.com/oschwald/geoip2-golang"
  24. "github.com/prometheus/client_golang/prometheus"
  25. "github.com/prometheus/client_golang/prometheus/promhttp"
  26. "github.com/syncthing/syncthing/cmd/strelaypoolsrv/auto"
  27. "github.com/syncthing/syncthing/lib/assets"
  28. "github.com/syncthing/syncthing/lib/rand"
  29. "github.com/syncthing/syncthing/lib/relay/client"
  30. "github.com/syncthing/syncthing/lib/sync"
  31. "github.com/syncthing/syncthing/lib/tlsutil"
  32. )
  33. type location struct {
  34. Latitude float64 `json:"latitude"`
  35. Longitude float64 `json:"longitude"`
  36. City string `json:"city"`
  37. Country string `json:"country"`
  38. Continent string `json:"continent"`
  39. }
  40. type relay struct {
  41. URL string `json:"url"`
  42. Location location `json:"location"`
  43. uri *url.URL
  44. Stats *stats `json:"stats"`
  45. StatsRetrieved time.Time `json:"statsRetrieved"`
  46. }
  47. type stats struct {
  48. StartTime time.Time `json:"startTime"`
  49. UptimeSeconds int `json:"uptimeSeconds"`
  50. PendingSessionKeys int `json:"numPendingSessionKeys"`
  51. ActiveSessions int `json:"numActiveSessions"`
  52. Connections int `json:"numConnections"`
  53. Proxies int `json:"numProxies"`
  54. BytesProxied int `json:"bytesProxied"`
  55. GoVersion string `json:"goVersion"`
  56. GoOS string `json:"goOS"`
  57. GoArch string `json:"goArch"`
  58. GoMaxProcs int `json:"goMaxProcs"`
  59. GoRoutines int `json:"goNumRoutine"`
  60. Rates []int64 `json:"kbps10s1m5m15m30m60m"`
  61. Options struct {
  62. NetworkTimeout int `json:"network-timeout"`
  63. PintInterval int `json:"ping-interval"`
  64. MessageTimeout int `json:"message-timeout"`
  65. SessionRate int `json:"per-session-rate"`
  66. GlobalRate int `json:"global-rate"`
  67. Pools []string `json:"pools"`
  68. ProvidedBy string `json:"provided-by"`
  69. } `json:"options"`
  70. }
  71. func (r relay) String() string {
  72. return r.URL
  73. }
  74. type request struct {
  75. relay *relay
  76. result chan result
  77. queueTimer *prometheus.Timer
  78. }
  79. type result struct {
  80. err error
  81. eviction time.Duration
  82. }
  83. var (
  84. testCert tls.Certificate
  85. knownRelaysFile = filepath.Join(os.TempDir(), "strelaypoolsrv_known_relays")
  86. listen = ":80"
  87. dir string
  88. evictionTime = time.Hour
  89. debug bool
  90. permRelaysFile string
  91. ipHeader string
  92. geoipPath string
  93. proto string
  94. statsRefresh = time.Minute
  95. requestQueueLen = 64
  96. requestProcessors = 8
  97. requests chan request
  98. mut = sync.NewRWMutex()
  99. knownRelays = make([]*relay, 0)
  100. permanentRelays = make([]*relay, 0)
  101. evictionTimers = make(map[string]*time.Timer)
  102. globalBlocklist = newErrorTracker(1000)
  103. )
  104. const (
  105. httpStatusEnhanceYourCalm = 429
  106. )
  107. func main() {
  108. log.SetOutput(os.Stdout)
  109. log.SetFlags(log.Lshortfile)
  110. flag.StringVar(&listen, "listen", listen, "Listen address")
  111. flag.StringVar(&dir, "keys", dir, "Directory where http-cert.pem and http-key.pem is stored for TLS listening")
  112. flag.BoolVar(&debug, "debug", debug, "Enable debug output")
  113. flag.DurationVar(&evictionTime, "eviction", evictionTime, "After how long the relay is evicted")
  114. flag.StringVar(&permRelaysFile, "perm-relays", "", "Path to list of permanent relays")
  115. flag.StringVar(&knownRelaysFile, "known-relays", knownRelaysFile, "Path to list of current relays")
  116. flag.StringVar(&ipHeader, "ip-header", "", "Name of header which holds clients ip:port. Only meaningful when running behind a reverse proxy.")
  117. flag.StringVar(&geoipPath, "geoip", "GeoLite2-City.mmdb", "Path to GeoLite2-City database")
  118. flag.StringVar(&proto, "protocol", "tcp", "Protocol used for listening. 'tcp' for IPv4 and IPv6, 'tcp4' for IPv4, 'tcp6' for IPv6")
  119. flag.DurationVar(&statsRefresh, "stats-refresh", statsRefresh, "Interval at which to refresh relay stats")
  120. flag.IntVar(&requestQueueLen, "request-queue", requestQueueLen, "Queue length for incoming test requests")
  121. flag.IntVar(&requestProcessors, "request-processors", requestProcessors, "Number of request processor routines")
  122. flag.Parse()
  123. requests = make(chan request, requestQueueLen)
  124. var listener net.Listener
  125. var err error
  126. if permRelaysFile != "" {
  127. permanentRelays = loadRelays(permRelaysFile)
  128. }
  129. testCert = createTestCertificate()
  130. for i := 0; i < requestProcessors; i++ {
  131. go requestProcessor()
  132. }
  133. // Load relays from cache in the background.
  134. // Load them in a serial fashion to make sure any genuine requests
  135. // are not dropped.
  136. go func() {
  137. for _, relay := range loadRelays(knownRelaysFile) {
  138. resultChan := make(chan result)
  139. requests <- request{relay, resultChan, nil}
  140. result := <-resultChan
  141. if result.err != nil {
  142. relayTestsTotal.WithLabelValues("failed").Inc()
  143. } else {
  144. relayTestsTotal.WithLabelValues("success").Inc()
  145. }
  146. }
  147. // Run the the stats refresher once the relays are loaded.
  148. statsRefresher(statsRefresh)
  149. }()
  150. if dir != "" {
  151. if debug {
  152. log.Println("Starting TLS listener on", listen)
  153. }
  154. certFile, keyFile := filepath.Join(dir, "http-cert.pem"), filepath.Join(dir, "http-key.pem")
  155. var cert tls.Certificate
  156. cert, err = tls.LoadX509KeyPair(certFile, keyFile)
  157. if err != nil {
  158. log.Fatalln("Failed to load HTTP X509 key pair:", err)
  159. }
  160. tlsCfg := &tls.Config{
  161. Certificates: []tls.Certificate{cert},
  162. MinVersion: tls.VersionTLS10, // No SSLv3
  163. ClientAuth: tls.RequestClientCert,
  164. CipherSuites: []uint16{
  165. // No RC4
  166. tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  167. tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
  168. tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
  169. tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
  170. tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
  171. tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
  172. tls.TLS_RSA_WITH_AES_128_CBC_SHA,
  173. tls.TLS_RSA_WITH_AES_256_CBC_SHA,
  174. tls.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
  175. tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA,
  176. },
  177. }
  178. listener, err = tls.Listen(proto, listen, tlsCfg)
  179. } else {
  180. if debug {
  181. log.Println("Starting plain listener on", listen)
  182. }
  183. listener, err = net.Listen(proto, listen)
  184. }
  185. if err != nil {
  186. log.Fatalln("listen:", err)
  187. }
  188. handler := http.NewServeMux()
  189. handler.HandleFunc("/", handleAssets)
  190. handler.Handle("/endpoint", httpcache.SinglePath(http.HandlerFunc(handleRequest), 15*time.Second))
  191. handler.HandleFunc("/metrics", handleMetrics)
  192. srv := http.Server{
  193. Handler: handler,
  194. ReadTimeout: 10 * time.Second,
  195. }
  196. err = srv.Serve(listener)
  197. if err != nil {
  198. log.Fatalln("serve:", err)
  199. }
  200. }
  201. func handleMetrics(w http.ResponseWriter, r *http.Request) {
  202. timer := prometheus.NewTimer(metricsRequestsSeconds)
  203. // Acquire the mutex just to make sure we're not caught mid-way stats collection
  204. mut.RLock()
  205. promhttp.Handler().ServeHTTP(w, r)
  206. mut.RUnlock()
  207. timer.ObserveDuration()
  208. }
  209. func handleAssets(w http.ResponseWriter, r *http.Request) {
  210. w.Header().Set("Cache-Control", "no-cache, must-revalidate")
  211. path := r.URL.Path[1:]
  212. if path == "" {
  213. path = "index.html"
  214. }
  215. as, ok := auto.Assets()[path]
  216. if !ok {
  217. w.WriteHeader(http.StatusNotFound)
  218. return
  219. }
  220. assets.Serve(w, r, as)
  221. }
  222. func handleRequest(w http.ResponseWriter, r *http.Request) {
  223. timer := prometheus.NewTimer(apiRequestsSeconds.WithLabelValues(r.Method))
  224. w = NewLoggingResponseWriter(w)
  225. defer func() {
  226. timer.ObserveDuration()
  227. lw := w.(*loggingResponseWriter)
  228. apiRequestsTotal.WithLabelValues(r.Method, strconv.Itoa(lw.statusCode)).Inc()
  229. }()
  230. if ipHeader != "" {
  231. hdr := r.Header.Get(ipHeader)
  232. fields := strings.Split(hdr, ",")
  233. if len(fields) > 0 {
  234. r.RemoteAddr = strings.TrimSpace(fields[len(fields)-1])
  235. }
  236. }
  237. w.Header().Set("Access-Control-Allow-Origin", "*")
  238. switch r.Method {
  239. case "GET":
  240. handleGetRequest(w, r)
  241. case "POST":
  242. handlePostRequest(w, r)
  243. default:
  244. if debug {
  245. log.Println("Unhandled HTTP method", r.Method)
  246. }
  247. http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
  248. }
  249. }
  250. func handleGetRequest(rw http.ResponseWriter, r *http.Request) {
  251. rw.Header().Set("Content-Type", "application/json; charset=utf-8")
  252. mut.RLock()
  253. relays := make([]*relay, len(permanentRelays)+len(knownRelays))
  254. n := copy(relays, permanentRelays)
  255. copy(relays[n:], knownRelays)
  256. mut.RUnlock()
  257. // Shuffle
  258. rand.Shuffle(relays)
  259. _ = json.NewEncoder(rw).Encode(map[string][]*relay{
  260. "relays": relays,
  261. })
  262. }
  263. func handlePostRequest(w http.ResponseWriter, r *http.Request) {
  264. // Get the IP address of the client
  265. rhost := r.RemoteAddr
  266. if host, _, err := net.SplitHostPort(rhost); err == nil {
  267. rhost = host
  268. }
  269. // Check the black list. A client is blacklisted if their last 10
  270. // attempts to join have all failed. The "Unauthorized" status return
  271. // causes strelaysrv to cease attempting to join.
  272. if globalBlocklist.IsBlocked(rhost) {
  273. log.Println("Rejected blocked client", rhost)
  274. http.Error(w, "Too many errors", http.StatusUnauthorized)
  275. globalBlocklist.ClearErrors(rhost)
  276. return
  277. }
  278. var relayCert *x509.Certificate
  279. if r.TLS != nil && len(r.TLS.PeerCertificates) > 0 {
  280. relayCert = r.TLS.PeerCertificates[0]
  281. log.Printf("Got TLS cert from relay server")
  282. }
  283. var newRelay relay
  284. err := json.NewDecoder(r.Body).Decode(&newRelay)
  285. r.Body.Close()
  286. if err != nil {
  287. if debug {
  288. log.Println("Failed to parse payload")
  289. }
  290. http.Error(w, err.Error(), http.StatusBadRequest)
  291. return
  292. }
  293. uri, err := url.Parse(newRelay.URL)
  294. if err != nil {
  295. if debug {
  296. log.Println("Failed to parse URI", newRelay.URL)
  297. }
  298. http.Error(w, err.Error(), http.StatusBadRequest)
  299. return
  300. }
  301. // Canonicalize the URL. In particular, parse and re-encode the query
  302. // string so that it's guaranteed to be valid.
  303. uri.RawQuery = uri.Query().Encode()
  304. newRelay.URL = uri.String()
  305. if relayCert != nil {
  306. advertisedId := uri.Query().Get("id")
  307. idFromCert := protocol.NewDeviceID(relayCert.Raw).String()
  308. if advertisedId != idFromCert {
  309. log.Println("Warning: Relay server requested to join with an ID different from the join request, rejecting")
  310. http.Error(w, "mismatched advertised id and join request cert", http.StatusBadRequest)
  311. return
  312. }
  313. }
  314. host, port, err := net.SplitHostPort(uri.Host)
  315. if err != nil {
  316. if debug {
  317. log.Println("Failed to split URI", newRelay.URL)
  318. }
  319. http.Error(w, err.Error(), http.StatusBadRequest)
  320. return
  321. }
  322. ip := net.ParseIP(host)
  323. // The client did not provide an IP address, use the IP address of the client.
  324. if ip == nil || ip.IsUnspecified() {
  325. uri.Host = net.JoinHostPort(rhost, port)
  326. newRelay.URL = uri.String()
  327. } else if host != rhost && relayCert == nil {
  328. if debug {
  329. log.Println("IP address advertised does not match client IP address", r.RemoteAddr, uri)
  330. }
  331. http.Error(w, fmt.Sprintf("IP advertised %s does not match client IP %s", host, rhost), http.StatusUnauthorized)
  332. return
  333. }
  334. newRelay.uri = uri
  335. for _, current := range permanentRelays {
  336. if current.uri.Host == newRelay.uri.Host {
  337. if debug {
  338. log.Println("Asked to add a relay", newRelay, "which exists in permanent list")
  339. }
  340. http.Error(w, "Invalid request", http.StatusBadRequest)
  341. return
  342. }
  343. }
  344. reschan := make(chan result)
  345. select {
  346. case requests <- request{&newRelay, reschan, prometheus.NewTimer(relayTestActionsSeconds.WithLabelValues("queue"))}:
  347. result := <-reschan
  348. if result.err != nil {
  349. log.Println("Join from", r.RemoteAddr, "failed:", result.err)
  350. globalBlocklist.AddError(rhost)
  351. relayTestsTotal.WithLabelValues("failed").Inc()
  352. http.Error(w, result.err.Error(), http.StatusBadRequest)
  353. return
  354. }
  355. log.Println("Join from", r.RemoteAddr, "succeeded")
  356. globalBlocklist.ClearErrors(rhost)
  357. relayTestsTotal.WithLabelValues("success").Inc()
  358. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  359. json.NewEncoder(w).Encode(map[string]time.Duration{
  360. "evictionIn": result.eviction,
  361. })
  362. default:
  363. relayTestsTotal.WithLabelValues("dropped").Inc()
  364. if debug {
  365. log.Println("Dropping request")
  366. }
  367. w.WriteHeader(httpStatusEnhanceYourCalm)
  368. }
  369. }
  370. func requestProcessor() {
  371. for request := range requests {
  372. if request.queueTimer != nil {
  373. request.queueTimer.ObserveDuration()
  374. }
  375. timer := prometheus.NewTimer(relayTestActionsSeconds.WithLabelValues("test"))
  376. handleRelayTest(request)
  377. timer.ObserveDuration()
  378. }
  379. }
  380. func handleRelayTest(request request) {
  381. if debug {
  382. log.Println("Request for", request.relay)
  383. }
  384. if err := client.TestRelay(context.TODO(), request.relay.uri, []tls.Certificate{testCert}, time.Second, 2*time.Second, 3); err != nil {
  385. if debug {
  386. log.Println("Test for relay", request.relay, "failed:", err)
  387. }
  388. request.result <- result{err, 0}
  389. return
  390. }
  391. stats := fetchStats(request.relay)
  392. location := getLocation(request.relay.uri.Host)
  393. mut.Lock()
  394. if stats != nil {
  395. updateMetrics(request.relay.uri.Host, *stats, location)
  396. }
  397. request.relay.Stats = stats
  398. request.relay.StatsRetrieved = time.Now().Truncate(time.Second)
  399. request.relay.Location = location
  400. timer, ok := evictionTimers[request.relay.uri.Host]
  401. if ok {
  402. if debug {
  403. log.Println("Stopping existing timer for", request.relay)
  404. }
  405. timer.Stop()
  406. }
  407. for i, current := range knownRelays {
  408. if current.uri.Host == request.relay.uri.Host {
  409. if debug {
  410. log.Println("Relay", request.relay, "already exists")
  411. }
  412. // Evict the old entry anyway, as configuration might have changed.
  413. last := len(knownRelays) - 1
  414. knownRelays[i] = knownRelays[last]
  415. knownRelays = knownRelays[:last]
  416. goto found
  417. }
  418. }
  419. if debug {
  420. log.Println("Adding new relay", request.relay)
  421. }
  422. found:
  423. knownRelays = append(knownRelays, request.relay)
  424. evictionTimers[request.relay.uri.Host] = time.AfterFunc(evictionTime, evict(request.relay))
  425. mut.Unlock()
  426. if err := saveRelays(knownRelaysFile, knownRelays); err != nil {
  427. log.Println("Failed to write known relays: " + err.Error())
  428. }
  429. request.result <- result{nil, evictionTime}
  430. }
  431. func evict(relay *relay) func() {
  432. return func() {
  433. mut.Lock()
  434. defer mut.Unlock()
  435. if debug {
  436. log.Println("Evicting", relay)
  437. }
  438. for i, current := range knownRelays {
  439. if current.uri.Host == relay.uri.Host {
  440. if debug {
  441. log.Println("Evicted", relay)
  442. }
  443. last := len(knownRelays) - 1
  444. knownRelays[i] = knownRelays[last]
  445. knownRelays = knownRelays[:last]
  446. deleteMetrics(current.uri.Host)
  447. }
  448. }
  449. delete(evictionTimers, relay.uri.Host)
  450. }
  451. }
  452. func loadRelays(file string) []*relay {
  453. content, err := os.ReadFile(file)
  454. if err != nil {
  455. log.Println("Failed to load relays: " + err.Error())
  456. return nil
  457. }
  458. var relays []*relay
  459. for _, line := range strings.Split(string(content), "\n") {
  460. if line == "" {
  461. continue
  462. }
  463. uri, err := url.Parse(line)
  464. if err != nil {
  465. if debug {
  466. log.Println("Skipping relay", line, "due to parse error", err)
  467. }
  468. continue
  469. }
  470. relays = append(relays, &relay{
  471. URL: line,
  472. Location: getLocation(uri.Host),
  473. uri: uri,
  474. })
  475. if debug {
  476. log.Println("Adding relay", line)
  477. }
  478. }
  479. return relays
  480. }
  481. func saveRelays(file string, relays []*relay) error {
  482. var content string
  483. for _, relay := range relays {
  484. content += relay.uri.String() + "\n"
  485. }
  486. return os.WriteFile(file, []byte(content), 0o777)
  487. }
  488. func createTestCertificate() tls.Certificate {
  489. tmpDir, err := os.MkdirTemp("", "relaypoolsrv")
  490. if err != nil {
  491. log.Fatal(err)
  492. }
  493. certFile, keyFile := filepath.Join(tmpDir, "cert.pem"), filepath.Join(tmpDir, "key.pem")
  494. cert, err := tlsutil.NewCertificate(certFile, keyFile, "relaypoolsrv", 20*365)
  495. if err != nil {
  496. log.Fatalln("Failed to create test X509 key pair:", err)
  497. }
  498. return cert
  499. }
  500. func getLocation(host string) location {
  501. timer := prometheus.NewTimer(locationLookupSeconds)
  502. defer timer.ObserveDuration()
  503. db, err := geoip2.Open(geoipPath)
  504. if err != nil {
  505. return location{}
  506. }
  507. defer db.Close()
  508. addr, err := net.ResolveTCPAddr("tcp", host)
  509. if err != nil {
  510. return location{}
  511. }
  512. city, err := db.City(addr.IP)
  513. if err != nil {
  514. return location{}
  515. }
  516. return location{
  517. Longitude: city.Location.Longitude,
  518. Latitude: city.Location.Latitude,
  519. City: city.City.Names["en"],
  520. Country: city.Country.IsoCode,
  521. Continent: city.Continent.Code,
  522. }
  523. }
  524. type loggingResponseWriter struct {
  525. http.ResponseWriter
  526. statusCode int
  527. }
  528. func NewLoggingResponseWriter(w http.ResponseWriter) *loggingResponseWriter {
  529. return &loggingResponseWriter{w, http.StatusOK}
  530. }
  531. func (lrw *loggingResponseWriter) WriteHeader(code int) {
  532. lrw.statusCode = code
  533. lrw.ResponseWriter.WriteHeader(code)
  534. }
  535. type errorTracker struct {
  536. errors *lru.TwoQueueCache[string, *errorCounter]
  537. }
  538. type errorCounter struct {
  539. count atomic.Int32
  540. }
  541. func newErrorTracker(size int) *errorTracker {
  542. cache, err := lru.New2Q[string, *errorCounter](size)
  543. if err != nil {
  544. panic(err)
  545. }
  546. return &errorTracker{
  547. errors: cache,
  548. }
  549. }
  550. func (b *errorTracker) AddError(host string) {
  551. entry, ok := b.errors.Get(host)
  552. if !ok {
  553. entry = &errorCounter{}
  554. b.errors.Add(host, entry)
  555. }
  556. c := entry.count.Add(1)
  557. log.Printf("Error count for %s is now %d", host, c)
  558. }
  559. func (b *errorTracker) ClearErrors(host string) {
  560. b.errors.Remove(host)
  561. }
  562. func (b *errorTracker) IsBlocked(host string) bool {
  563. if be, ok := b.errors.Get(host); ok {
  564. return be.count.Load() > 10
  565. }
  566. return false
  567. }