apisrv.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. // Copyright (C) 2018 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package main
  7. import (
  8. "bytes"
  9. "context"
  10. "crypto/tls"
  11. "encoding/json"
  12. "encoding/pem"
  13. "fmt"
  14. "log"
  15. "math/rand"
  16. "net"
  17. "net/http"
  18. "net/url"
  19. "strconv"
  20. "strings"
  21. "sync"
  22. "time"
  23. "github.com/syncthing/syncthing/lib/protocol"
  24. )
  25. // announcement is the format received from and sent to clients
  26. type announcement struct {
  27. Seen time.Time `json:"seen"`
  28. Addresses []string `json:"addresses"`
  29. }
  30. type apiSrv struct {
  31. addr string
  32. cert tls.Certificate
  33. db database
  34. listener net.Listener
  35. repl replicator // optional
  36. useHTTP bool
  37. mapsMut sync.Mutex
  38. misses map[string]int32
  39. }
  40. type requestID int64
  41. func (i requestID) String() string {
  42. return fmt.Sprintf("%016x", int64(i))
  43. }
  44. type contextKey int
  45. const idKey contextKey = iota
  46. func newAPISrv(addr string, cert tls.Certificate, db database, repl replicator, useHTTP bool) *apiSrv {
  47. return &apiSrv{
  48. addr: addr,
  49. cert: cert,
  50. db: db,
  51. repl: repl,
  52. useHTTP: useHTTP,
  53. misses: make(map[string]int32),
  54. }
  55. }
  56. func (s *apiSrv) Serve() {
  57. if s.useHTTP {
  58. listener, err := net.Listen("tcp", s.addr)
  59. if err != nil {
  60. log.Println("Listen:", err)
  61. return
  62. }
  63. s.listener = listener
  64. } else {
  65. tlsCfg := &tls.Config{
  66. Certificates: []tls.Certificate{s.cert},
  67. ClientAuth: tls.RequestClientCert,
  68. SessionTicketsDisabled: true,
  69. MinVersion: tls.VersionTLS12,
  70. CipherSuites: []uint16{
  71. tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  72. tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
  73. tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
  74. tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
  75. tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
  76. tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
  77. },
  78. }
  79. tlsListener, err := tls.Listen("tcp", s.addr, tlsCfg)
  80. if err != nil {
  81. log.Println("Listen:", err)
  82. return
  83. }
  84. s.listener = tlsListener
  85. }
  86. http.HandleFunc("/", s.handler)
  87. http.HandleFunc("/ping", handlePing)
  88. srv := &http.Server{
  89. ReadTimeout: httpReadTimeout,
  90. WriteTimeout: httpWriteTimeout,
  91. MaxHeaderBytes: httpMaxHeaderBytes,
  92. }
  93. if err := srv.Serve(s.listener); err != nil {
  94. log.Println("Serve:", err)
  95. }
  96. }
  97. var topCtx = context.Background()
  98. func (s *apiSrv) handler(w http.ResponseWriter, req *http.Request) {
  99. t0 := time.Now()
  100. lw := NewLoggingResponseWriter(w)
  101. defer func() {
  102. diff := time.Since(t0)
  103. apiRequestsSeconds.WithLabelValues(req.Method).Observe(diff.Seconds())
  104. apiRequestsTotal.WithLabelValues(req.Method, strconv.Itoa(lw.statusCode)).Inc()
  105. }()
  106. reqID := requestID(rand.Int63())
  107. ctx := context.WithValue(topCtx, idKey, reqID)
  108. if debug {
  109. log.Println(reqID, req.Method, req.URL)
  110. }
  111. var remoteIP net.IP
  112. if s.useHTTP {
  113. remoteIP = net.ParseIP(req.Header.Get("X-Forwarded-For"))
  114. } else {
  115. addr, err := net.ResolveTCPAddr("tcp", req.RemoteAddr)
  116. if err != nil {
  117. log.Println("remoteAddr:", err)
  118. lw.Header().Set("Retry-After", errorRetryAfterString())
  119. http.Error(lw, "Internal Server Error", http.StatusInternalServerError)
  120. apiRequestsTotal.WithLabelValues("no_remote_addr").Inc()
  121. return
  122. }
  123. remoteIP = addr.IP
  124. }
  125. switch req.Method {
  126. case "GET":
  127. s.handleGET(ctx, lw, req)
  128. case "POST":
  129. s.handlePOST(ctx, remoteIP, lw, req)
  130. default:
  131. http.Error(lw, "Method Not Allowed", http.StatusMethodNotAllowed)
  132. }
  133. }
  134. func (s *apiSrv) handleGET(ctx context.Context, w http.ResponseWriter, req *http.Request) {
  135. reqID := ctx.Value(idKey).(requestID)
  136. deviceID, err := protocol.DeviceIDFromString(req.URL.Query().Get("device"))
  137. if err != nil {
  138. if debug {
  139. log.Println(reqID, "bad device param")
  140. }
  141. lookupRequestsTotal.WithLabelValues("bad_request").Inc()
  142. w.Header().Set("Retry-After", errorRetryAfterString())
  143. http.Error(w, "Bad Request", http.StatusBadRequest)
  144. return
  145. }
  146. key := deviceID.String()
  147. rec, err := s.db.get(key)
  148. if err != nil {
  149. // some sort of internal error
  150. lookupRequestsTotal.WithLabelValues("internal_error").Inc()
  151. w.Header().Set("Retry-After", errorRetryAfterString())
  152. http.Error(w, "Internal Server Error", http.StatusInternalServerError)
  153. return
  154. }
  155. if len(rec.Addresses) == 0 {
  156. lookupRequestsTotal.WithLabelValues("not_found").Inc()
  157. s.mapsMut.Lock()
  158. misses := s.misses[key]
  159. if misses < rec.Misses {
  160. misses = rec.Misses + 1
  161. } else {
  162. misses++
  163. }
  164. s.misses[key] = misses
  165. s.mapsMut.Unlock()
  166. if misses%notFoundMissesWriteInterval == 0 {
  167. rec.Misses = misses
  168. rec.Missed = time.Now().UnixNano()
  169. rec.Addresses = nil
  170. // rec.Seen retained from get
  171. s.db.put(key, rec)
  172. }
  173. w.Header().Set("Retry-After", notFoundRetryAfterString(int(misses)))
  174. http.Error(w, "Not Found", http.StatusNotFound)
  175. return
  176. }
  177. lookupRequestsTotal.WithLabelValues("success").Inc()
  178. bs, _ := json.Marshal(announcement{
  179. Seen: time.Unix(0, rec.Seen),
  180. Addresses: addressStrs(rec.Addresses),
  181. })
  182. w.Header().Set("Content-Type", "application/json")
  183. w.Write(bs)
  184. }
  185. func (s *apiSrv) handlePOST(ctx context.Context, remoteIP net.IP, w http.ResponseWriter, req *http.Request) {
  186. reqID := ctx.Value(idKey).(requestID)
  187. rawCert := certificateBytes(req)
  188. if rawCert == nil {
  189. if debug {
  190. log.Println(reqID, "no certificates")
  191. }
  192. announceRequestsTotal.WithLabelValues("no_certificate").Inc()
  193. w.Header().Set("Retry-After", errorRetryAfterString())
  194. http.Error(w, "Forbidden", http.StatusForbidden)
  195. return
  196. }
  197. var ann announcement
  198. if err := json.NewDecoder(req.Body).Decode(&ann); err != nil {
  199. if debug {
  200. log.Println(reqID, "decode:", err)
  201. }
  202. announceRequestsTotal.WithLabelValues("bad_request").Inc()
  203. w.Header().Set("Retry-After", errorRetryAfterString())
  204. http.Error(w, "Bad Request", http.StatusBadRequest)
  205. return
  206. }
  207. deviceID := protocol.NewDeviceID(rawCert)
  208. addresses := fixupAddresses(remoteIP, ann.Addresses)
  209. if len(addresses) == 0 {
  210. announceRequestsTotal.WithLabelValues("bad_request").Inc()
  211. w.Header().Set("Retry-After", errorRetryAfterString())
  212. http.Error(w, "Bad Request", http.StatusBadRequest)
  213. return
  214. }
  215. if err := s.handleAnnounce(remoteIP, deviceID, addresses); err != nil {
  216. announceRequestsTotal.WithLabelValues("internal_error").Inc()
  217. w.Header().Set("Retry-After", errorRetryAfterString())
  218. http.Error(w, "Internal Server Error", http.StatusInternalServerError)
  219. return
  220. }
  221. announceRequestsTotal.WithLabelValues("success").Inc()
  222. w.Header().Set("Reannounce-After", reannounceAfterString())
  223. w.WriteHeader(http.StatusNoContent)
  224. }
  225. func (s *apiSrv) Stop() {
  226. s.listener.Close()
  227. }
  228. func (s *apiSrv) handleAnnounce(remote net.IP, deviceID protocol.DeviceID, addresses []string) error {
  229. key := deviceID.String()
  230. now := time.Now()
  231. expire := now.Add(addressExpiryTime).UnixNano()
  232. dbAddrs := make([]DatabaseAddress, len(addresses))
  233. for i := range addresses {
  234. dbAddrs[i].Address = addresses[i]
  235. dbAddrs[i].Expires = expire
  236. }
  237. seen := now.UnixNano()
  238. if s.repl != nil {
  239. s.repl.send(key, dbAddrs, seen)
  240. }
  241. return s.db.merge(key, dbAddrs, seen)
  242. }
  243. func handlePing(w http.ResponseWriter, r *http.Request) {
  244. w.WriteHeader(204)
  245. }
  246. func certificateBytes(req *http.Request) []byte {
  247. if req.TLS != nil && len(req.TLS.PeerCertificates) > 0 {
  248. return req.TLS.PeerCertificates[0].Raw
  249. }
  250. if hdr := req.Header.Get("X-SSL-Cert"); hdr != "" {
  251. bs := []byte(hdr)
  252. // The certificate is in PEM format but with spaces for newlines. We
  253. // need to reinstate the newlines for the PEM decoder. But we need to
  254. // leave the spaces in the BEGIN and END lines - the first and last
  255. // space - alone.
  256. firstSpace := bytes.Index(bs, []byte(" "))
  257. lastSpace := bytes.LastIndex(bs, []byte(" "))
  258. for i := firstSpace + 1; i < lastSpace; i++ {
  259. if bs[i] == ' ' {
  260. bs[i] = '\n'
  261. }
  262. }
  263. block, _ := pem.Decode(bs)
  264. if block == nil {
  265. // Decoding failed
  266. return nil
  267. }
  268. return block.Bytes
  269. }
  270. return nil
  271. }
  272. // fixupAddresses checks the list of addresses, removing invalid ones and
  273. // replacing unspecified IPs with the given remote IP.
  274. func fixupAddresses(remote net.IP, addresses []string) []string {
  275. fixed := make([]string, 0, len(addresses))
  276. for _, annAddr := range addresses {
  277. uri, err := url.Parse(annAddr)
  278. if err != nil {
  279. continue
  280. }
  281. host, port, err := net.SplitHostPort(uri.Host)
  282. if err != nil {
  283. continue
  284. }
  285. ip := net.ParseIP(host)
  286. // Some classes of IP are no-go.
  287. if ip.IsLoopback() || ip.IsMulticast() {
  288. continue
  289. }
  290. if host == "" || ip.IsUnspecified() {
  291. // Replace the unspecified IP with the request source.
  292. // ... unless the request source is the loopback address or
  293. // multicast/unspecified (can't happen, really).
  294. if remote.IsLoopback() || remote.IsMulticast() || remote.IsUnspecified() {
  295. continue
  296. }
  297. // Do not use IPv6 remote address if requested scheme is ...4
  298. // (i.e., tcp4, etc.)
  299. if strings.HasSuffix(uri.Scheme, "4") && remote.To4() == nil {
  300. continue
  301. }
  302. // Do not use IPv4 remote address if requested scheme is ...6
  303. if strings.HasSuffix(uri.Scheme, "6") && remote.To4() != nil {
  304. continue
  305. }
  306. host = remote.String()
  307. }
  308. uri.Host = net.JoinHostPort(host, port)
  309. fixed = append(fixed, uri.String())
  310. }
  311. return fixed
  312. }
  313. type loggingResponseWriter struct {
  314. http.ResponseWriter
  315. statusCode int
  316. }
  317. func NewLoggingResponseWriter(w http.ResponseWriter) *loggingResponseWriter {
  318. return &loggingResponseWriter{w, http.StatusOK}
  319. }
  320. func (lrw *loggingResponseWriter) WriteHeader(code int) {
  321. lrw.statusCode = code
  322. lrw.ResponseWriter.WriteHeader(code)
  323. }
  324. func addressStrs(dbAddrs []DatabaseAddress) []string {
  325. res := make([]string, len(dbAddrs))
  326. for i, a := range dbAddrs {
  327. res[i] = a.Address
  328. }
  329. return res
  330. }
  331. func errorRetryAfterString() string {
  332. return strconv.Itoa(errorRetryAfterSeconds + rand.Intn(errorRetryFuzzSeconds))
  333. }
  334. func notFoundRetryAfterString(misses int) string {
  335. retryAfterS := notFoundRetryMinSeconds + notFoundRetryIncSeconds*misses
  336. if retryAfterS > notFoundRetryMaxSeconds {
  337. retryAfterS = notFoundRetryMaxSeconds
  338. }
  339. retryAfterS += rand.Intn(notFoundRetryFuzzSeconds)
  340. return strconv.Itoa(retryAfterS)
  341. }
  342. func reannounceAfterString() string {
  343. return strconv.Itoa(reannounceAfterSeconds + rand.Intn(reannounzeFuzzSeconds))
  344. }