fixed directory decryption

This commit is contained in:
ston1th 2022-04-06 01:50:28 +02:00
commit 4e1d5b36b2
2 changed files with 33 additions and 10 deletions

View file

@ -16,9 +16,18 @@ type file struct {
nonce [NonceSize]byte nonce [NonceSize]byte
offset int64 offset int64
name string name string
dir bool
} }
func newFile(key []byte, name string, fs *FS, f provider.File) (ef provider.File, err error) { func newFile(key []byte, name string, fs *FS, f provider.File, dir bool) (ef provider.File, err error) {
if dir {
return &file{
File: f,
fs: fs,
name: name,
dir: dir,
}, nil
}
nonce, err := initNonce(f) nonce, err := initNonce(f)
if err != nil { if err != nil {
return return
@ -68,6 +77,9 @@ func (s stat) Name() string {
} }
func (f *file) Close() error { func (f *file) Close() error {
if f.dir {
return f.File.Close()
}
e1 := f.w.Close() e1 := f.w.Close()
e2 := f.File.Close() e2 := f.File.Close()
if e1 != nil { if e1 != nil {
@ -155,7 +167,11 @@ func (f *file) Readdir(n int) ([]fs.FileInfo, error) {
} }
fio := make([]fs.FileInfo, len(fis)) fio := make([]fs.FileInfo, len(fis))
for i, fi := range fis { for i, fi := range fis {
fio[i] = dir{FileInfo: fi, name: f.fs.decPath(fi.Name())} name, err := f.fs.decPath(fi.Name())
if err != nil {
return nil, err
}
fio[i] = dir{FileInfo: fi, name: name}
} }
return fio, nil return fio, nil
} }

View file

@ -47,15 +47,17 @@ func (fs *FS) encPath(path string) string {
return filepath.Join(cparts...) return filepath.Join(cparts...)
} }
func (fs *FS) decPath(path string) string { func (fs *FS) decPath(path string) (string, error) {
parts := strings.Split(path, "/") parts := strings.Split(path, "/")
pparts := make([]string, len(parts)) pparts := make([]string, len(parts))
for i, p := range parts { for i, p := range parts {
pparts[i] = base64.RawURLEncoding.EncodeToString( b, err := base64.RawURLEncoding.DecodeString(p)
fs.cbc.decrypt([]byte(p)), if err != nil {
) return "", err
}
pparts[i] = string(fs.cbc.decrypt(b))
} }
return filepath.Join(pparts...) return filepath.Join(pparts...), nil
} }
func (fs *FS) name(p string) string { func (fs *FS) name(p string) string {
@ -72,11 +74,16 @@ func (fs *FS) Remove(p string) error {
} }
func (fs *FS) Open(p string) (provider.File, error) { func (fs *FS) Open(p string) (provider.File, error) {
f, err := fs.fs.Open(fs.encPath(p)) c := fs.encPath(p)
fi, err := fs.fs.Stat(c)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return newFile(fs.key, p, fs, f) f, err := fs.fs.Open(c)
if err != nil {
return nil, err
}
return newFile(fs.key, p, fs, f, fi.IsDir())
} }
func (fs *FS) OpenFile(p string, flags int, mode os.FileMode) (provider.File, error) { func (fs *FS) OpenFile(p string, flags int, mode os.FileMode) (provider.File, error) {
@ -84,7 +91,7 @@ func (fs *FS) OpenFile(p string, flags int, mode os.FileMode) (provider.File, er
if err != nil { if err != nil {
return nil, err return nil, err
} }
return newFile(fs.key, p, fs, f) return newFile(fs.key, p, fs, f, false)
} }
func (fs *FS) MkdirAll(p string, mode os.FileMode) error { func (fs *FS) MkdirAll(p string, mode os.FileMode) error {