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"
"errors"
"log"
"net"
"os"
"path/filepath"
"strings"
@ -50,31 +51,29 @@ func (d *mainDriver) ClientDisconnected(cc ftpserver.ClientContext) {
func (d *mainDriver) GetTLSConfig() (*tls.Config, error) {
return nil, nil // Pas de TLS, à gérer si tu veux FTPS
}
import (
"errors"
"net"
// ... autres imports
)
func (d *ftpMainDriver) AuthUser(cc ftpserverlib.ClientContext, user, pass string) (ftpserverlib.ClientDriver, error) {
func (d *mainDriver) AuthUser(cc ftpserver.ClientContext, user, pass string) (ftpserver.ClientDriver, error) {
log.Printf("[FTP] Tentative login user='%s' pass='%s'", user, pass)
remoteAddr := cc.RemoteAddr().String()
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!" {
base := filepath.Clean("upload")
fs := afero.NewBasePathFs(afero.NewOsFs(), base)
log.Printf("[FTP] OK user %s", user)
return &ftpClientDriver{fs: fs}, nil
uploadPath, _ := filepath.Abs("upload")
fi, err := os.Stat(uploadPath)
if err != nil || !fi.IsDir() {
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
if user == "anonymous" && host == "82.65.73.115" {
if user == "anonymous" && host == "192.168.1.123" {
base := filepath.Clean("upload")
fs := afero.NewBasePathFs(afero.NewOsFs(), base)
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
@ -83,11 +82,9 @@ func (d *ftpMainDriver) AuthUser(cc ftpserverlib.ClientContext, user, pass strin
return nil, errors.New("anonymous login not allowed")
}
log.Printf("[FTP] Refus user=%s pass=%s", user, pass)
return nil, errors.New("invalid login")
return nil, errors.New("identifiants invalides")
}
// ---------- clientDriver = wrapper sur afero.Fs ----------
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) Rename(old, new string) error { return c.fs.Rename(old, new) }
func (c *clientDriver) Stat(name string) (os.FileInfo, error) {
log.Printf("[FTP][DEBUG] Stat: %s", name)
return c.fs.Stat(name)
}
func (c *clientDriver) LstatIfPossible(name string) (os.FileInfo, bool, error) {