|
| 1 | +// Credits: https://pkg.go.dev/github.com/rclone/rclone@v1.65.2/cmd/serve/s3 |
| 2 | +// Package s3 implements a fake s3 server for openlist |
| 3 | +package s3 |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "net" |
| 8 | + "net/http" |
| 9 | + "path" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/OpenListTeam/OpenList/v4/internal/conf" |
| 13 | + "github.com/OpenListTeam/OpenList/v4/internal/fs" |
| 14 | + "github.com/OpenListTeam/OpenList/v4/internal/model" |
| 15 | + "github.com/OpenListTeam/OpenList/v4/internal/op" |
| 16 | + "github.com/OpenListTeam/OpenList/v4/server/common" |
| 17 | + "github.com/itsHenry35/gofakes3/signature" |
| 18 | +) |
| 19 | + |
| 20 | +func redirectHandler(next http.Handler, authPairs map[string]string) http.Handler { |
| 21 | + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 22 | + if u, ok := directObjectURL(r, authPairs); ok { |
| 23 | + w.Header().Set("Location", u) |
| 24 | + w.WriteHeader(http.StatusFound) |
| 25 | + return |
| 26 | + } |
| 27 | + if u, ok := directUploadURL(r, authPairs); ok { |
| 28 | + w.Header().Set("Location", u) |
| 29 | + w.Header().Set("Cache-Control", "no-store") |
| 30 | + w.WriteHeader(http.StatusTemporaryRedirect) |
| 31 | + return |
| 32 | + } |
| 33 | + next.ServeHTTP(w, r) |
| 34 | + }) |
| 35 | +} |
| 36 | + |
| 37 | +func directObjectURL(r *http.Request, authPairs map[string]string) (string, bool) { |
| 38 | + if r.Method != http.MethodGet { |
| 39 | + return "", false |
| 40 | + } |
| 41 | + if hasNonObjectQuery(r) || !s3RequestAuthorized(r, authPairs) { |
| 42 | + return "", false |
| 43 | + } |
| 44 | + bucketName, objectName, ok := parseObjectPath(r.URL.Path) |
| 45 | + if !ok { |
| 46 | + return "", false |
| 47 | + } |
| 48 | + bucket, err := getBucketByName(bucketName) |
| 49 | + if err != nil { |
| 50 | + return "", false |
| 51 | + } |
| 52 | + reqPath := path.Join(bucket.Path, objectName) |
| 53 | + meta, _ := op.GetNearestMeta(reqPath) |
| 54 | + ctx := context.WithValue(r.Context(), conf.MetaKey, meta) |
| 55 | + storage, err := fs.GetStorage(reqPath, &fs.GetStoragesArgs{}) |
| 56 | + if err != nil || common.ShouldProxy(storage, path.Base(reqPath)) { |
| 57 | + return "", false |
| 58 | + } |
| 59 | + link, file, err := fs.Link(ctx, reqPath, model.LinkArgs{ |
| 60 | + IP: clientIP(r), |
| 61 | + Header: r.Header, |
| 62 | + Redirect: true, |
| 63 | + }) |
| 64 | + if err != nil { |
| 65 | + return "", false |
| 66 | + } |
| 67 | + defer link.Close() |
| 68 | + if file == nil || file.IsDir() || link.URL == "" || link.RangeReader != nil { |
| 69 | + return "", false |
| 70 | + } |
| 71 | + return link.URL, true |
| 72 | +} |
| 73 | + |
| 74 | +func directUploadURL(r *http.Request, authPairs map[string]string) (string, bool) { |
| 75 | + if r.Method != http.MethodPut || r.ContentLength < 0 { |
| 76 | + return "", false |
| 77 | + } |
| 78 | + if hasNonObjectQuery(r) || !s3RequestAuthorized(r, authPairs) { |
| 79 | + return "", false |
| 80 | + } |
| 81 | + if r.Header.Get("X-Amz-Copy-Source") != "" || |
| 82 | + r.Header.Get("X-Amz-Content-Sha256") == "STREAMING-AWS4-HMAC-SHA256-PAYLOAD" { |
| 83 | + return "", false |
| 84 | + } |
| 85 | + bucketName, objectName, ok := parseObjectPath(r.URL.Path) |
| 86 | + if !ok || strings.HasSuffix(objectName, "/") { |
| 87 | + return "", false |
| 88 | + } |
| 89 | + bucket, err := getBucketByName(bucketName) |
| 90 | + if err != nil { |
| 91 | + return "", false |
| 92 | + } |
| 93 | + reqPath := path.Join(bucket.Path, objectName) |
| 94 | + storage, dstDirActualPath, err := op.GetStorageAndActualPath(path.Dir(reqPath)) |
| 95 | + if err != nil || storage.Config().NoUpload { |
| 96 | + return "", false |
| 97 | + } |
| 98 | + info, err := op.GetDirectUploadInfo(r.Context(), "HttpDirect", storage, dstDirActualPath, path.Base(reqPath), r.ContentLength) |
| 99 | + if err != nil { |
| 100 | + return "", false |
| 101 | + } |
| 102 | + httpInfo, ok := asHTTPDirectUploadInfo(info) |
| 103 | + if !ok { |
| 104 | + return "", false |
| 105 | + } |
| 106 | + method := httpInfo.Method |
| 107 | + if method == "" { |
| 108 | + method = http.MethodPut |
| 109 | + } |
| 110 | + if !strings.EqualFold(method, http.MethodPut) || httpInfo.UploadURL == "" || |
| 111 | + httpInfo.ChunkSize > 0 || len(httpInfo.Headers) > 0 { |
| 112 | + return "", false |
| 113 | + } |
| 114 | + return httpInfo.UploadURL, true |
| 115 | +} |
| 116 | + |
| 117 | +func asHTTPDirectUploadInfo(info any) (*model.HttpDirectUploadInfo, bool) { |
| 118 | + switch v := info.(type) { |
| 119 | + case *model.HttpDirectUploadInfo: |
| 120 | + return v, v != nil |
| 121 | + case model.HttpDirectUploadInfo: |
| 122 | + return &v, true |
| 123 | + default: |
| 124 | + return nil, false |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +func parseObjectPath(rawPath string) (bucket, object string, ok bool) { |
| 129 | + parts := strings.SplitN(strings.TrimPrefix(rawPath, "/"), "/", 2) |
| 130 | + if len(parts) != 2 || parts[0] == "" || parts[1] == "" { |
| 131 | + return "", "", false |
| 132 | + } |
| 133 | + return parts[0], parts[1], true |
| 134 | +} |
| 135 | + |
| 136 | +func hasNonObjectQuery(r *http.Request) bool { |
| 137 | + query := r.URL.Query() |
| 138 | + for key := range query { |
| 139 | + lower := strings.ToLower(key) |
| 140 | + if strings.HasPrefix(lower, "x-amz-") || |
| 141 | + lower == "awsaccesskeyid" || |
| 142 | + lower == "signature" || |
| 143 | + lower == "expires" || |
| 144 | + lower == "x-id" { |
| 145 | + continue |
| 146 | + } |
| 147 | + return true |
| 148 | + } |
| 149 | + return false |
| 150 | +} |
| 151 | + |
| 152 | +func s3RequestAuthorized(r *http.Request, authPairs map[string]string) bool { |
| 153 | + if len(authPairs) == 0 { |
| 154 | + return true |
| 155 | + } |
| 156 | + result := signature.V4SignVerify(r) |
| 157 | + if result == signature.ErrUnsupportAlgorithm { |
| 158 | + result = signature.V2SignVerify(r) |
| 159 | + } |
| 160 | + return result == signature.ErrNone |
| 161 | +} |
| 162 | + |
| 163 | +func clientIP(r *http.Request) string { |
| 164 | + host, _, err := net.SplitHostPort(r.RemoteAddr) |
| 165 | + if err != nil { |
| 166 | + return r.RemoteAddr |
| 167 | + } |
| 168 | + return host |
| 169 | +} |
0 commit comments