This commit is contained in:
cangui 2025-07-27 21:49:30 +02:00
parent d2d4f36443
commit e61403b138

36
main.go
View File

@ -7,6 +7,7 @@ import (
"crypto/tls" "crypto/tls"
"errors" "errors"
"log" "log"
"net"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -50,31 +51,29 @@ func (d *mainDriver) ClientDisconnected(cc ftpserver.ClientContext) {
func (d *mainDriver) GetTLSConfig() (*tls.Config, error) { func (d *mainDriver) GetTLSConfig() (*tls.Config, error) {
return nil, nil // Pas de TLS, à gérer si tu veux FTPS return nil, nil // Pas de TLS, à gérer si tu veux FTPS
} }
import ( func (d *mainDriver) AuthUser(cc ftpserver.ClientContext, user, pass string) (ftpserver.ClientDriver, error) {
"errors" log.Printf("[FTP] Tentative login user='%s' pass='%s'", user, pass)
"net"
// ... autres imports
)
func (d *ftpMainDriver) AuthUser(cc ftpserverlib.ClientContext, user, pass string) (ftpserverlib.ClientDriver, error) {
remoteAddr := cc.RemoteAddr().String() remoteAddr := cc.RemoteAddr().String()
host, _, _ := net.SplitHostPort(remoteAddr) host, _, _ := net.SplitHostPort(remoteAddr)
log.Printf("[FTP] Tentative login user='%s' pass='%s' depuis %s", user, pass, host)
// 1. Login classique
if user == "cangui2089" && pass == "GHT30k7!" { if user == "cangui2089" && pass == "GHT30k7!" {
base := filepath.Clean("upload") uploadPath, _ := filepath.Abs("upload")
fs := afero.NewBasePathFs(afero.NewOsFs(), base) fi, err := os.Stat(uploadPath)
log.Printf("[FTP] OK user %s", user) if err != nil || !fi.IsDir() {
return &ftpClientDriver{fs: fs}, nil log.Printf("[FTP] Le dossier upload/ est manquant ou non valide: %v", err)
return nil, errors.New("le dossier upload/ doit exister")
}
log.Printf("[FTP] Connexion OK, exposé: %s", uploadPath)
return &clientDriver{
fs: afero.NewBasePathFs(afero.NewOsFs(), uploadPath),
}, nil
} }
// 2. Autoriser anonymous depuis 192.168.1.123 uniquement // 2. Autoriser anonymous depuis 192.168.1.123 uniquement
if user == "anonymous" && host == "82.65.73.115" { if user == "anonymous" && host == "192.168.1.123" {
base := filepath.Clean("upload") base := filepath.Clean("upload")
fs := afero.NewBasePathFs(afero.NewOsFs(), base) fs := afero.NewBasePathFs(afero.NewOsFs(), base)
log.Printf("[FTP] Login ANONYMOUS autorisé pour %s", host) log.Printf("[FTP] Login ANONYMOUS autorisé pour %s", host)
return &ftpClientDriver{fs: fs}, nil return &clientDriver{fs: fs}, nil
} }
// 3. Sinon refuse tout anonyme ou mauvais login // 3. Sinon refuse tout anonyme ou mauvais login
@ -83,11 +82,9 @@ func (d *ftpMainDriver) AuthUser(cc ftpserverlib.ClientContext, user, pass strin
return nil, errors.New("anonymous login not allowed") return nil, errors.New("anonymous login not allowed")
} }
log.Printf("[FTP] Refus user=%s pass=%s", user, pass) return nil, errors.New("identifiants invalides")
return nil, errors.New("invalid login")
} }
// ---------- clientDriver = wrapper sur afero.Fs ---------- // ---------- clientDriver = wrapper sur afero.Fs ----------
func (c *clientDriver) Name() string { return "aferofs" } func (c *clientDriver) Name() string { return "aferofs" }
@ -103,7 +100,6 @@ func (c *clientDriver) Remove(name string) error { return c.fs.Remove(
func (c *clientDriver) RemoveAll(path string) error { return c.fs.RemoveAll(path) } func (c *clientDriver) RemoveAll(path string) error { return c.fs.RemoveAll(path) }
func (c *clientDriver) Rename(old, new string) error { return c.fs.Rename(old, new) } func (c *clientDriver) Rename(old, new string) error { return c.fs.Rename(old, new) }
func (c *clientDriver) Stat(name string) (os.FileInfo, error) { func (c *clientDriver) Stat(name string) (os.FileInfo, error) {
log.Printf("[FTP][DEBUG] Stat: %s", name)
return c.fs.Stat(name) return c.fs.Stat(name)
} }
func (c *clientDriver) LstatIfPossible(name string) (os.FileInfo, bool, error) { func (c *clientDriver) LstatIfPossible(name string) (os.FileInfo, bool, error) {