|
| 1 | +package protondrive |
| 2 | + |
| 3 | +/* |
| 4 | +Package protondrive |
| 5 | +Author: Da3zKi7<[email protected]> |
| 6 | +Date: 2025-09-18 |
| 7 | +
|
| 8 | +Thanks to @henrybear327 for modded go-proton-api & Proton-API-Bridge |
| 9 | +
|
| 10 | +The power of open-source, the force of teamwork and the magic of reverse engineering! |
| 11 | +
|
| 12 | +
|
| 13 | +D@' 3z K!7 - The King Of Cracking |
| 14 | +
|
| 15 | +Да здравствует Родина)) |
| 16 | +*/ |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "fmt" |
| 21 | + "io" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/OpenListTeam/OpenList/v4/internal/conf" |
| 25 | + "github.com/OpenListTeam/OpenList/v4/internal/driver" |
| 26 | + "github.com/OpenListTeam/OpenList/v4/internal/model" |
| 27 | + "github.com/OpenListTeam/OpenList/v4/internal/op" |
| 28 | + "github.com/OpenListTeam/OpenList/v4/internal/setting" |
| 29 | + "github.com/OpenListTeam/OpenList/v4/internal/stream" |
| 30 | + "github.com/OpenListTeam/OpenList/v4/pkg/http_range" |
| 31 | + "github.com/OpenListTeam/OpenList/v4/pkg/utils" |
| 32 | + "github.com/ProtonMail/gopenpgp/v2/crypto" |
| 33 | + proton_api_bridge "github.com/henrybear327/Proton-API-Bridge" |
| 34 | + "github.com/henrybear327/Proton-API-Bridge/common" |
| 35 | + "github.com/henrybear327/go-proton-api" |
| 36 | +) |
| 37 | + |
| 38 | +type ProtonDrive struct { |
| 39 | + model.Storage |
| 40 | + Addition |
| 41 | + |
| 42 | + protonDrive *proton_api_bridge.ProtonDrive |
| 43 | + |
| 44 | + apiBase string |
| 45 | + appVersion string |
| 46 | + protonJson string |
| 47 | + userAgent string |
| 48 | + sdkVersion string |
| 49 | + webDriveAV string |
| 50 | + |
| 51 | + c *proton.Client |
| 52 | + |
| 53 | + // userKR *crypto.KeyRing |
| 54 | + addrKRs map[string]*crypto.KeyRing |
| 55 | + addrData map[string]proton.Address |
| 56 | + |
| 57 | + MainShare *proton.Share |
| 58 | + |
| 59 | + DefaultAddrKR *crypto.KeyRing |
| 60 | + MainShareKR *crypto.KeyRing |
| 61 | +} |
| 62 | + |
| 63 | +func (d *ProtonDrive) Config() driver.Config { |
| 64 | + return config |
| 65 | +} |
| 66 | + |
| 67 | +func (d *ProtonDrive) GetAddition() driver.Additional { |
| 68 | + return &d.Addition |
| 69 | +} |
| 70 | + |
| 71 | +func (d *ProtonDrive) Init(ctx context.Context) (err error) { |
| 72 | + defer func() { |
| 73 | + if r := recover(); err == nil && r != nil { |
| 74 | + err = fmt.Errorf("ProtonDrive initialization panic: %v", r) |
| 75 | + } |
| 76 | + }() |
| 77 | + |
| 78 | + if d.Email == "" { |
| 79 | + return fmt.Errorf("email is required") |
| 80 | + } |
| 81 | + if d.Password == "" { |
| 82 | + return fmt.Errorf("password is required") |
| 83 | + } |
| 84 | + |
| 85 | + config := &common.Config{ |
| 86 | + AppVersion: d.appVersion, |
| 87 | + UserAgent: d.userAgent, |
| 88 | + FirstLoginCredential: &common.FirstLoginCredentialData{ |
| 89 | + Username: d.Email, |
| 90 | + Password: d.Password, |
| 91 | + TwoFA: d.TwoFACode, |
| 92 | + }, |
| 93 | + EnableCaching: true, |
| 94 | + ConcurrentBlockUploadCount: setting.GetInt(conf.TaskUploadThreadsNum, conf.Conf.Tasks.Upload.Workers), |
| 95 | + //ConcurrentFileCryptoCount: 2, |
| 96 | + UseReusableLogin: d.UseReusableLogin && d.ReusableCredential != (common.ReusableCredentialData{}), |
| 97 | + ReplaceExistingDraft: true, |
| 98 | + ReusableCredential: &d.ReusableCredential, |
| 99 | + } |
| 100 | + |
| 101 | + protonDrive, _, err := proton_api_bridge.NewProtonDrive( |
| 102 | + ctx, |
| 103 | + config, |
| 104 | + d.authHandler, |
| 105 | + func() {}, |
| 106 | + ) |
| 107 | + |
| 108 | + if err != nil && config.UseReusableLogin { |
| 109 | + config.UseReusableLogin = false |
| 110 | + protonDrive, _, err = proton_api_bridge.NewProtonDrive(ctx, |
| 111 | + config, |
| 112 | + d.authHandler, |
| 113 | + func() {}, |
| 114 | + ) |
| 115 | + if err == nil { |
| 116 | + op.MustSaveDriverStorage(d) |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + if err != nil { |
| 121 | + return fmt.Errorf("failed to initialize ProtonDrive: %w", err) |
| 122 | + } |
| 123 | + |
| 124 | + if err := d.initClient(ctx); err != nil { |
| 125 | + return err |
| 126 | + } |
| 127 | + |
| 128 | + d.protonDrive = protonDrive |
| 129 | + d.MainShare = protonDrive.MainShare |
| 130 | + if d.RootFolderID == "root" || d.RootFolderID == "" { |
| 131 | + d.RootFolderID = protonDrive.RootLink.LinkID |
| 132 | + } |
| 133 | + d.MainShareKR = protonDrive.MainShareKR |
| 134 | + d.DefaultAddrKR = protonDrive.DefaultAddrKR |
| 135 | + |
| 136 | + return nil |
| 137 | +} |
| 138 | + |
| 139 | +func (d *ProtonDrive) Drop(ctx context.Context) error { |
| 140 | + return nil |
| 141 | +} |
| 142 | + |
| 143 | +func (d *ProtonDrive) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) { |
| 144 | + entries, err := d.protonDrive.ListDirectory(ctx, dir.GetID()) |
| 145 | + if err != nil { |
| 146 | + return nil, fmt.Errorf("failed to list directory: %w", err) |
| 147 | + } |
| 148 | + |
| 149 | + objects := make([]model.Obj, 0, len(entries)) |
| 150 | + for _, entry := range entries { |
| 151 | + obj := &model.Object{ |
| 152 | + ID: entry.Link.LinkID, |
| 153 | + Name: entry.Name, |
| 154 | + Size: entry.Link.Size, |
| 155 | + Modified: time.Unix(entry.Link.ModifyTime, 0), |
| 156 | + IsFolder: entry.IsFolder, |
| 157 | + } |
| 158 | + objects = append(objects, obj) |
| 159 | + } |
| 160 | + |
| 161 | + return objects, nil |
| 162 | +} |
| 163 | + |
| 164 | +func (d *ProtonDrive) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) { |
| 165 | + link, err := d.getLink(ctx, file.GetID()) |
| 166 | + if err != nil { |
| 167 | + return nil, fmt.Errorf("failed get file link: %+v", err) |
| 168 | + } |
| 169 | + fileSystemAttrs, err := d.protonDrive.GetActiveRevisionAttrs(ctx, link) |
| 170 | + if err != nil { |
| 171 | + return nil, fmt.Errorf("failed get file revision: %+v", err) |
| 172 | + } |
| 173 | + // 解密后的文件大小 |
| 174 | + size := fileSystemAttrs.Size |
| 175 | + |
| 176 | + rangeReaderFunc := func(rangeCtx context.Context, httpRange http_range.Range) (io.ReadCloser, error) { |
| 177 | + length := httpRange.Length |
| 178 | + if length < 0 || httpRange.Start+length > size { |
| 179 | + length = size - httpRange.Start |
| 180 | + } |
| 181 | + reader, _, _, err := d.protonDrive.DownloadFile(rangeCtx, link, httpRange.Start) |
| 182 | + if err != nil { |
| 183 | + return nil, fmt.Errorf("failed start download: %+v", err) |
| 184 | + } |
| 185 | + return utils.ReadCloser{ |
| 186 | + Reader: io.LimitReader(reader, length), |
| 187 | + Closer: reader, |
| 188 | + }, nil |
| 189 | + } |
| 190 | + |
| 191 | + expiration := time.Minute |
| 192 | + return &model.Link{ |
| 193 | + RangeReader: &model.FileRangeReader{ |
| 194 | + RangeReaderIF: stream.RateLimitRangeReaderFunc(rangeReaderFunc), |
| 195 | + }, |
| 196 | + ContentLength: size, |
| 197 | + Expiration: &expiration, |
| 198 | + }, nil |
| 199 | +} |
| 200 | + |
| 201 | +func (d *ProtonDrive) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) (model.Obj, error) { |
| 202 | + id, err := d.protonDrive.CreateNewFolderByID(ctx, parentDir.GetID(), dirName) |
| 203 | + if err != nil { |
| 204 | + return nil, fmt.Errorf("failed to create directory: %w", err) |
| 205 | + } |
| 206 | + |
| 207 | + newDir := &model.Object{ |
| 208 | + ID: id, |
| 209 | + Name: dirName, |
| 210 | + IsFolder: true, |
| 211 | + Modified: time.Now(), |
| 212 | + } |
| 213 | + return newDir, nil |
| 214 | +} |
| 215 | + |
| 216 | +func (d *ProtonDrive) Move(ctx context.Context, srcObj, dstDir model.Obj) (model.Obj, error) { |
| 217 | + return d.DirectMove(ctx, srcObj, dstDir) |
| 218 | +} |
| 219 | + |
| 220 | +func (d *ProtonDrive) Rename(ctx context.Context, srcObj model.Obj, newName string) (model.Obj, error) { |
| 221 | + if d.protonDrive == nil { |
| 222 | + return nil, fmt.Errorf("protonDrive bridge is nil") |
| 223 | + } |
| 224 | + |
| 225 | + return d.DirectRename(ctx, srcObj, newName) |
| 226 | +} |
| 227 | + |
| 228 | +func (d *ProtonDrive) Copy(ctx context.Context, srcObj, dstDir model.Obj) (model.Obj, error) { |
| 229 | + if srcObj.IsDir() { |
| 230 | + return nil, fmt.Errorf("directory copy not supported") |
| 231 | + } |
| 232 | + |
| 233 | + srcLink, err := d.getLink(ctx, srcObj.GetID()) |
| 234 | + if err != nil { |
| 235 | + return nil, err |
| 236 | + } |
| 237 | + |
| 238 | + reader, linkSize, fileSystemAttrs, err := d.protonDrive.DownloadFile(ctx, srcLink, 0) |
| 239 | + if err != nil { |
| 240 | + return nil, fmt.Errorf("failed to download source file: %w", err) |
| 241 | + } |
| 242 | + defer reader.Close() |
| 243 | + |
| 244 | + actualSize := linkSize |
| 245 | + if fileSystemAttrs != nil && fileSystemAttrs.Size > 0 { |
| 246 | + actualSize = fileSystemAttrs.Size |
| 247 | + } |
| 248 | + |
| 249 | + file := &stream.FileStream{ |
| 250 | + Ctx: ctx, |
| 251 | + Obj: &model.Object{ |
| 252 | + Name: srcObj.GetName(), |
| 253 | + // Use the accurate and real size |
| 254 | + Size: actualSize, |
| 255 | + Modified: srcObj.ModTime(), |
| 256 | + }, |
| 257 | + Reader: reader, |
| 258 | + } |
| 259 | + defer file.Close() |
| 260 | + return d.Put(ctx, dstDir, file, func(percentage float64) {}) |
| 261 | +} |
| 262 | + |
| 263 | +func (d *ProtonDrive) Remove(ctx context.Context, obj model.Obj) error { |
| 264 | + if obj.IsDir() { |
| 265 | + return d.protonDrive.MoveFolderToTrashByID(ctx, obj.GetID(), false) |
| 266 | + } else { |
| 267 | + return d.protonDrive.MoveFileToTrashByID(ctx, obj.GetID()) |
| 268 | + } |
| 269 | +} |
| 270 | + |
| 271 | +func (d *ProtonDrive) Put(ctx context.Context, dstDir model.Obj, file model.FileStreamer, up driver.UpdateProgress) (model.Obj, error) { |
| 272 | + return d.uploadFile(ctx, dstDir.GetID(), file, up) |
| 273 | +} |
| 274 | + |
| 275 | +func (d *ProtonDrive) GetDetails(ctx context.Context) (*model.StorageDetails, error) { |
| 276 | + about, err := d.protonDrive.About(ctx) |
| 277 | + if err != nil { |
| 278 | + return nil, err |
| 279 | + } |
| 280 | + total := uint64(about.MaxSpace) |
| 281 | + free := total - uint64(about.UsedSpace) |
| 282 | + return &model.StorageDetails{ |
| 283 | + DiskUsage: model.DiskUsage{ |
| 284 | + TotalSpace: total, |
| 285 | + FreeSpace: free, |
| 286 | + }, |
| 287 | + }, nil |
| 288 | +} |
| 289 | + |
| 290 | +var _ driver.Driver = (*ProtonDrive)(nil) |
0 commit comments