apisrv.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. "crypto/tls"
  10. "encoding/json"
  11. "encoding/pem"
  12. "fmt"
  13. "log"
  14. "math/rand"
  15. "net"
  16. "net/http"
  17. "net/url"
  18. "strconv"
  19. "strings"
  20. "sync"
  21. "time"
  22. "github.com/syncthing/syncthing/lib/protocol"
  23. "golang.org/x/net/context"
  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.Addresses = nil
  169. // rec.Seen retained from get
  170. s.db.put(key, rec)
  171. }
  172. w.Header().Set("Retry-After", notFoundRetryAfterString(int(misses)))
  173. http.Error(w, "Not Found", http.StatusNotFound)
  174. return
  175. }
  176. lookupRequestsTotal.WithLabelValues("success").Inc()
  177. bs, _ := json.Marshal(announcement{
  178. Seen: time.Unix(0, rec.Seen),
  179. Addresses: addressStrs(rec.Addresses),
  180. })
  181. w.Header().Set("Content-Type", "application/json")
  182. w.Write(bs)
  183. }
  184. func (s *apiSrv) handlePOST(ctx context.Context, remoteIP net.IP, w http.ResponseWriter, req *http.Request) {
  185. reqID := ctx.Value(idKey).(requestID)
  186. rawCert := certificateBytes(req)
  187. if rawCert == nil {
  188. if debug {
  189. log.Println(reqID, "no certificates")
  190. }
  191. announceRequestsTotal.WithLabelValues("no_certificate").Inc()
  192. w.Header().Set("Retry-After", errorRetryAfterString())
  193. http.Error(w, "Forbidden", http.StatusForbidden)
  194. return
  195. }
  196. var ann announcement
  197. if err := json.NewDecoder(req.Body).Decode(&ann); err != nil {
  198. if debug {
  199. log.Println(reqID, "decode:", err)
  200. }
  201. announceRequestsTotal.WithLabelValues("bad_request").Inc()
  202. w.Header().Set("Retry-After", errorRetryAfterString())
  203. http.Error(w, "Bad Request", http.StatusBadRequest)
  204. return
  205. }
  206. deviceID := protocol.NewDeviceID(rawCert)
  207. addresses := fixupAddresses(remoteIP, ann.Addresses)
  208. if len(addresses) == 0 {
  209. announceRequestsTotal.WithLabelValues("bad_request").Inc()
  210. w.Header().Set("Retry-After", errorRetryAfterString())
  211. http.Error(w, "Bad Request", http.StatusBadRequest)
  212. return
  213. }
  214. if err := s.handleAnnounce(remoteIP, deviceID, addresses); err != nil {
  215. announceRequestsTotal.WithLabelValues("internal_error").Inc()
  216. w.Header().Set("Retry-After", errorRetryAfterString())
  217. http.Error(w, "Internal Server Error", http.StatusInternalServerError)
  218. return
  219. }
  220. announceRequestsTotal.WithLabelValues("success").Inc()
  221. w.Header().Set("Reannounce-After", reannounceAfterString())
  222. w.WriteHeader(http.StatusNoContent)
  223. }
  224. func (s *apiSrv) Stop() {
  225. s.listener.Close()
  226. }
  227. func (s *apiSrv) handleAnnounce(remote net.IP, deviceID protocol.DeviceID, addresses []string) error {
  228. key := deviceID.String()
  229. now := time.Now()
  230. expire := now.Add(addressExpiryTime).UnixNano()
  231. dbAddrs := make([]DatabaseAddress, len(addresses))
  232. for i := range addresses {
  233. dbAddrs[i].Address = addresses[i]
  234. dbAddrs[i].Expires = expire
  235. }
  236. seen := now.UnixNano()
  237. if s.repl != nil {
  238. s.repl.send(key, dbAddrs, seen)
  239. }
  240. return s.db.merge(key, dbAddrs, seen)
  241. }
  242. func handlePing(w http.ResponseWriter, r *http.Request) {
  243. w.WriteHeader(204)
  244. }
  245. func certificateBytes(req *http.Request) []byte {
  246. if req.TLS != nil && len(req.TLS.PeerCertificates) > 0 {
  247. return req.TLS.PeerCertificates[0].Raw
  248. }
  249. if hdr := req.Header.Get("X-SSL-Cert"); hdr != "" {
  250. bs := []byte(hdr)
  251. // The certificate is in PEM format but with spaces for newlines. We
  252. // need to reinstate the newlines for the PEM decoder. But we need to
  253. // leave the spaces in the BEGIN and END lines - the first and last
  254. // space - alone.
  255. firstSpace := bytes.Index(bs, []byte(" "))
  256. lastSpace := bytes.LastIndex(bs, []byte(" "))
  257. for i := firstSpace + 1; i < lastSpace; i++ {
  258. if bs[i] == ' ' {
  259. bs[i] = '\n'
  260. }
  261. }
  262. block, _ := pem.Decode(bs)
  263. if block == nil {
  264. // Decoding failed
  265. return nil
  266. }
  267. return block.Bytes
  268. }
  269. return nil
  270. }
  271. // fixupAddresses checks the list of addresses, removing invalid ones and
  272. // replacing unspecified IPs with the given remote IP.
  273. func fixupAddresses(remote net.IP, addresses []string) []string {
  274. fixed := make([]string, 0, len(addresses))
  275. for _, annAddr := range addresses {
  276. uri, err := url.Parse(annAddr)
  277. if err != nil {
  278. continue
  279. }
  280. host, port, err := net.SplitHostPort(uri.Host)
  281. if err != nil {
  282. continue
  283. }
  284. ip := net.ParseIP(host)
  285. if host == "" || ip.IsUnspecified() {
  286. // Do not use IPv6 remote address if requested scheme is ...4
  287. // (i.e., tcp4, etc.)
  288. if strings.HasSuffix(uri.Scheme, "4") && remote.To4() == nil {
  289. continue
  290. }
  291. // Do not use IPv4 remote address if requested scheme is ...6
  292. if strings.HasSuffix(uri.Scheme, "6") && remote.To4() != nil {
  293. continue
  294. }
  295. host = remote.String()
  296. }
  297. uri.Host = net.JoinHostPort(host, port)
  298. fixed = append(fixed, uri.String())
  299. }
  300. return fixed
  301. }
  302. type loggingResponseWriter struct {
  303. http.ResponseWriter
  304. statusCode int
  305. }
  306. func NewLoggingResponseWriter(w http.ResponseWriter) *loggingResponseWriter {
  307. return &loggingResponseWriter{w, http.StatusOK}
  308. }
  309. func (lrw *loggingResponseWriter) WriteHeader(code int) {
  310. lrw.statusCode = code
  311. lrw.ResponseWriter.WriteHeader(code)
  312. }
  313. func addressStrs(dbAddrs []DatabaseAddress) []string {
  314. res := make([]string, len(dbAddrs))
  315. for i, a := range dbAddrs {
  316. res[i] = a.Address
  317. }
  318. return res
  319. }
  320. func errorRetryAfterString() string {
  321. return strconv.Itoa(errorRetryAfterSeconds + rand.Intn(errorRetryFuzzSeconds))
  322. }
  323. func notFoundRetryAfterString(misses int) string {
  324. retryAfterS := notFoundRetryMinSeconds + notFoundRetryIncSeconds*misses
  325. if retryAfterS > notFoundRetryMaxSeconds {
  326. retryAfterS = notFoundRetryMaxSeconds
  327. }
  328. retryAfterS += rand.Intn(notFoundRetryFuzzSeconds)
  329. return strconv.Itoa(retryAfterS)
  330. }
  331. func reannounceAfterString() string {
  332. return strconv.Itoa(reannounceAfterSeconds + rand.Intn(reannounzeFuzzSeconds))
  333. }