main.go 17 KB

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