diff --git a/app.go b/app.go index 7788abf..09d8a60 100644 --- a/app.go +++ b/app.go @@ -6,6 +6,7 @@ import ( "goexplore/internal/config" "goexplore/internal/explorer" "goexplore/internal/keychain" + "goexplore/internal/protocols/ftp" "goexplore/internal/protocols/local" "goexplore/internal/protocols/nfs" "goexplore/internal/protocols/s3" @@ -123,6 +124,8 @@ func (a *App) getExplorerForConnection(id string) (explorer.Explorer, error) { return webdav.New(conn, secret), nil case "nfs": return nfs.New(conn, secret) + case "ftp": + return ftp.New(conn, secret), nil default: return nil, fmt.Errorf("protocol %s not fully implemented", conn.Protocol) } diff --git a/frontend/index.html b/frontend/index.html index d0b6558..c7bf3ee 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -83,6 +83,7 @@ + + +
diff --git a/frontend/src/main.js b/frontend/src/main.js index b3051b5..7a1dcbe 100644 --- a/frontend/src/main.js +++ b/frontend/src/main.js @@ -46,6 +46,7 @@ async function init() { bucket: document.getElementById('conn-bucket').value, region: document.getElementById('conn-region').value, path_style: document.getElementById('conn-pathstyle').checked, + secure: document.getElementById('conn-secure').checked, username: document.getElementById('conn-username').value }; let secret = document.getElementById('conn-secret').value; @@ -105,6 +106,7 @@ window.editConnection = (id) => { document.getElementById('conn-bucket').value = c.bucket || ''; document.getElementById('conn-region').value = c.region || ''; document.getElementById('conn-pathstyle').checked = c.path_style || false; + document.getElementById('conn-secure').checked = c.secure || false; document.getElementById('conn-username').value = c.username || ''; document.getElementById('conn-secret').value = ''; document.getElementById('conn-secret-key').value = ''; @@ -119,6 +121,7 @@ window.openConnModal = () => { document.getElementById('modal-title').innerText = "Add Connection"; document.getElementById('conn-form').reset(); document.getElementById('conn-id').value = uuidv4(); + document.getElementById('conn-secure').checked = false; document.getElementById('conn-secret').value = ''; document.getElementById('conn-secret-key').value = ''; document.getElementById('conn-sftp-auth-type').value = 'password'; @@ -142,6 +145,10 @@ window.updateProtocolFields = () => { document.getElementById('region-group').style.display = showS3Specific ? 'flex' : 'none'; document.getElementById('pathstyle-group').style.display = showS3Specific ? 'flex' : 'none'; + // FTP specific fields + const showFTP = protocol === 'ftp'; + document.getElementById('ftp-secure-group').style.display = showFTP ? 'flex' : 'none'; + // SFTP Auth Type toggle const authTypeSelect = document.getElementById('conn-sftp-auth-type'); const secretInput = document.getElementById('conn-secret'); diff --git a/go.mod b/go.mod index 6f8b7e5..124f57e 100644 --- a/go.mod +++ b/go.mod @@ -41,6 +41,7 @@ require ( github.com/godbus/dbus/v5 v5.2.2 // indirect github.com/gorilla/websocket v1.5.3 // indirect github.com/jchv/go-winloader v0.0.0-20210711035445-715c2860da7e // indirect + github.com/jlaffaye/ftp v0.2.1 // indirect github.com/kr/fs v0.1.0 // indirect github.com/labstack/echo/v4 v4.13.3 // indirect github.com/labstack/gommon v0.4.2 // indirect diff --git a/go.sum b/go.sum index 3017996..1b59b9d 100644 --- a/go.sum +++ b/go.sum @@ -56,6 +56,8 @@ github.com/hirochachacha/go-smb2 v1.1.0 h1:b6hs9qKIql9eVXAiN0M2wSFY5xnhbHAQoCwRK github.com/hirochachacha/go-smb2 v1.1.0/go.mod h1:8F1A4d5EZzrGu5R7PU163UcMRDJQl4FtcxjBfsY8TZE= github.com/jchv/go-winloader v0.0.0-20210711035445-715c2860da7e h1:Q3+PugElBCf4PFpxhErSzU3/PY5sFL5Z6rfv4AbGAck= github.com/jchv/go-winloader v0.0.0-20210711035445-715c2860da7e/go.mod h1:alcuEEnZsY1WQsagKhZDsoPCRoOijYqhZvPwLG0kzVs= +github.com/jlaffaye/ftp v0.2.1 h1:AICcTYPMkaXlmjLMm9I+lB36f6jXCsCvBqVQc6EfC1Y= +github.com/jlaffaye/ftp v0.2.1/go.mod h1:gXSIr1pA9NhynDNigiFHs4+yL7o7I6bGF9Za9wi9tcE= github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8= github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= diff --git a/internal/config/config.go b/internal/config/config.go index e67a603..63b00b9 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -16,6 +16,7 @@ type ConnectionConfig struct { Bucket string `yaml:"bucket,omitempty" json:"bucket,omitempty"` Region string `yaml:"region,omitempty" json:"region,omitempty"` PathStyle bool `yaml:"path_style,omitempty" json:"path_style,omitempty"` + Secure bool `yaml:"secure,omitempty" json:"secure,omitempty"` Username string `yaml:"username,omitempty" json:"username,omitempty"` KeychainKey string `yaml:"keychain_key,omitempty" json:"keychain_key,omitempty"` } diff --git a/internal/protocols/ftp/ftp.go b/internal/protocols/ftp/ftp.go new file mode 100644 index 0000000..3b03a18 --- /dev/null +++ b/internal/protocols/ftp/ftp.go @@ -0,0 +1,179 @@ +package ftp + +import ( + "crypto/tls" + "fmt" + "io" + "path/filepath" + "time" + + "github.com/jlaffaye/ftp" + "goexplore/internal/config" + "goexplore/internal/explorer" +) + +type FTPExplorer struct { + cfg *config.ConnectionConfig + secret string + client *ftp.ServerConn +} + +func New(c *config.ConnectionConfig, secret string) *FTPExplorer { + return &FTPExplorer{cfg: c, secret: secret} +} + +func (e *FTPExplorer) Connect() error { + port := e.cfg.Port + if port == 0 { + port = 21 + } + + addr := fmt.Sprintf("%s:%d", e.cfg.Host, port) + var c *ftp.ServerConn + var err error + + if e.cfg.Secure { + c, err = ftp.Dial(addr, ftp.DialWithExplicitTLS(&tls.Config{ + InsecureSkipVerify: true, + })) + } else { + c, err = ftp.Dial(addr) + } + + if err != nil { + return err + } + + if err := c.Login(e.cfg.Username, e.secret); err != nil { + c.Quit() + return err + } + + e.client = c + return nil +} + +func (e *FTPExplorer) Disconnect() error { + if e.client != nil { + return e.client.Quit() + } + return nil +} + +func (e *FTPExplorer) ListDir(path string) ([]explorer.FileEntry, error) { + if path == "" { + path = "." + } + + entries, err := e.client.List(path) + if err != nil { + return nil, err + } + + var res []explorer.FileEntry + for _, f := range entries { + if f.Name == "." || f.Name == ".." { + continue + } + + isDir := f.Type == ftp.EntryTypeFolder + res = append(res, explorer.FileEntry{ + Name: f.Name, + Path: filepath.ToSlash(filepath.Join(path, f.Name)), + Size: int64(f.Size), + Modified: f.Time.Format(time.RFC3339), + IsDir: isDir, + Permissions: "", + }) + } + return res, nil +} + +func (e *FTPExplorer) Stat(path string) (explorer.FileEntry, error) { + if path == "" { + path = "." + } + + dir := filepath.Dir(path) + base := filepath.Base(path) + + entries, err := e.client.List(dir) + if err != nil { + return explorer.FileEntry{}, err + } + + for _, f := range entries { + if f.Name == base { + return explorer.FileEntry{ + Name: f.Name, + Path: filepath.ToSlash(path), + Size: int64(f.Size), + Modified: f.Time.Format(time.RFC3339), + IsDir: f.Type == ftp.EntryTypeFolder, + Permissions: "", + }, nil + } + } + + return explorer.FileEntry{}, fmt.Errorf("file not found: %s", path) +} + +func (e *FTPExplorer) MkDir(path string) error { + return e.client.MakeDir(path) +} + +func (e *FTPExplorer) removeAll(path string) error { + entries, err := e.client.List(path) + if err != nil { + // If it fails to list, it might be a file + return e.client.Delete(path) + } + + // Try treating it as a directory to remove contents + isDir := false + for _, f := range entries { + if f.Name == filepath.Base(path) && f.Type == ftp.EntryTypeFolder { + isDir = true + break + } + } + + if !isDir && len(entries) == 1 && entries[0].Name == filepath.Base(path) { + return e.client.Delete(path) + } + + // Remove contents + for _, f := range entries { + if f.Name == "." || f.Name == ".." { + continue + } + subPath := filepath.ToSlash(filepath.Join(path, f.Name)) + if f.Type == ftp.EntryTypeFolder { + if err := e.removeAll(subPath); err != nil { + return err + } + } else { + if err := e.client.Delete(subPath); err != nil { + return err + } + } + } + + return e.client.RemoveDir(path) +} + +func (e *FTPExplorer) Delete(path string) error { + return e.removeAll(path) +} + +func (e *FTPExplorer) Rename(src, dst string) error { + return e.client.Rename(src, dst) +} + +func (e *FTPExplorer) ReadFile(path string) (io.ReadCloser, error) { + return e.client.Retr(path) +} + +func (e *FTPExplorer) WriteFile(path string, r io.Reader, size int64) error { + return e.client.Stor(path, r) +}