peerapi_drive.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. //go:build !ts_omit_drive
  4. package ipnlocal
  5. import (
  6. "net/http"
  7. "path/filepath"
  8. "strings"
  9. "tailscale.com/drive"
  10. "tailscale.com/tailcfg"
  11. "tailscale.com/util/httpm"
  12. )
  13. const (
  14. taildrivePrefix = "/v0/drive"
  15. )
  16. func init() {
  17. peerAPIHandlerPrefixes[taildrivePrefix] = handleServeDrive
  18. }
  19. func handleServeDrive(hi PeerAPIHandler, w http.ResponseWriter, r *http.Request) {
  20. h := hi.(*peerAPIHandler)
  21. h.logfv1("taildrive: got %s request from %s", r.Method, h.peerNode.Key().ShortString())
  22. if !h.ps.b.DriveSharingEnabled() {
  23. h.logf("taildrive: not enabled")
  24. http.Error(w, "taildrive not enabled", http.StatusNotFound)
  25. return
  26. }
  27. capsMap := h.PeerCaps()
  28. driveCaps, ok := capsMap[tailcfg.PeerCapabilityTaildrive]
  29. if !ok {
  30. h.logf("taildrive: not permitted")
  31. http.Error(w, "taildrive not permitted", http.StatusForbidden)
  32. return
  33. }
  34. rawPerms := make([][]byte, 0, len(driveCaps))
  35. for _, cap := range driveCaps {
  36. rawPerms = append(rawPerms, []byte(cap))
  37. }
  38. p, err := drive.ParsePermissions(rawPerms)
  39. if err != nil {
  40. h.logf("taildrive: error parsing permissions: %v", err)
  41. http.Error(w, err.Error(), http.StatusInternalServerError)
  42. return
  43. }
  44. fs, ok := h.ps.b.sys.DriveForRemote.GetOK()
  45. if !ok {
  46. h.logf("taildrive: not supported on platform")
  47. http.Error(w, "taildrive not supported on platform", http.StatusNotFound)
  48. return
  49. }
  50. wr := &httpResponseWrapper{
  51. ResponseWriter: w,
  52. }
  53. bw := &requestBodyWrapper{
  54. ReadCloser: r.Body,
  55. }
  56. r.Body = bw
  57. defer func() {
  58. switch wr.statusCode {
  59. case 304:
  60. // 304s are particularly chatty so skip logging.
  61. default:
  62. log := h.logf
  63. if r.Method != httpm.PUT && r.Method != httpm.GET {
  64. log = h.logfv1
  65. }
  66. contentType := "unknown"
  67. if ct := wr.Header().Get("Content-Type"); ct != "" {
  68. contentType = ct
  69. }
  70. log("taildrive: share: %s from %s to %s: status-code=%d ext=%q content-type=%q tx=%.f rx=%.f", r.Method, h.peerNode.Key().ShortString(), h.selfNode.Key().ShortString(), wr.statusCode, parseDriveFileExtensionForLog(r.URL.Path), contentType, roundTraffic(wr.contentLength), roundTraffic(bw.bytesRead))
  71. }
  72. }()
  73. r.URL.Path = strings.TrimPrefix(r.URL.Path, taildrivePrefix)
  74. fs.ServeHTTPWithPerms(p, wr, r)
  75. }
  76. // parseDriveFileExtensionForLog parses the file extension, if available.
  77. // If a file extension is not present or parsable, the file extension is
  78. // set to "unknown". If the file extension contains a double quote, it is
  79. // replaced with "removed".
  80. // All whitespace is removed from a parsed file extension.
  81. // File extensions including the leading ., e.g. ".gif".
  82. func parseDriveFileExtensionForLog(path string) string {
  83. fileExt := "unknown"
  84. if fe := filepath.Ext(path); fe != "" {
  85. if strings.Contains(fe, "\"") {
  86. // Do not log include file extensions with quotes within them.
  87. return "removed"
  88. }
  89. // Remove white space from user defined inputs.
  90. fileExt = strings.ReplaceAll(fe, " ", "")
  91. }
  92. return fileExt
  93. }