shelfy/main.go

147 lines
3.5 KiB
Go
Raw Normal View History

2025-06-06 07:42:55 +00:00
package main
import (
"app/shelfly/handlers"
"app/shelfly/internal/db"
2025-06-06 15:05:15 +00:00
"app/shelfly/internal/library"
2025-06-06 07:42:55 +00:00
"app/shelfly/internal/route"
"log"
2025-06-15 19:18:23 +00:00
"net"
2025-06-06 07:42:55 +00:00
"net/http"
2025-06-15 19:18:23 +00:00
"time"
"github.com/huin/goupnp/dcps/internetgateway1"
2025-06-17 20:05:00 +00:00
"github.com/jackpal/gateway"
2025-06-15 19:18:23 +00:00
"github.com/jackpal/go-nat-pmp"
2025-06-06 07:42:55 +00:00
"github.com/gorilla/mux"
)
func main() {
2025-06-12 08:57:10 +00:00
2025-06-06 09:55:54 +00:00
// 1. Démarrer le routeur principal
r := mux.NewRouter()
2025-06-06 07:42:55 +00:00
2025-06-06 09:55:54 +00:00
// 2. Initialiser la DB
bd := db.InitDB()
2025-06-06 07:42:55 +00:00
2025-06-06 15:05:15 +00:00
// 2.1
library.CreateDefaultFolder(bd)
2025-06-06 09:55:54 +00:00
// 3. Routes non protégées : on les monte sur le routeur principal
route.RoutesPublic(r, bd)
2025-06-06 07:42:55 +00:00
2025-06-06 09:55:54 +00:00
// 4. Créer un sous-routeur pour les routes protégées
protected := r.PathPrefix("/").Subrouter()
2025-06-06 07:42:55 +00:00
2025-06-06 09:55:54 +00:00
// 5. Appliquer le middleware JWT à ce sous-routeur
protected.Use(handlers.AuthMiddleware)
// 6. Enregistrer les routes protégées sur ce sous-routeur
route.RoutesProtected(protected, bd)
2025-06-15 19:18:23 +00:00
setupPortMappingWithFallback(4000, 4000)
2025-06-18 13:46:09 +00:00
// 6.5. Exposer le dossier upload
fs := http.StripPrefix("/upload/", http.FileServer(http.Dir("/app/upload")))
r.PathPrefix("/upload/").Handler(fs)
2025-06-06 07:42:55 +00:00
2025-06-06 09:55:54 +00:00
// 7. Lancer le serveur sur le port 4000
log.Fatal(http.ListenAndServe(":4000", r))
2025-06-06 07:42:55 +00:00
2025-06-15 19:18:23 +00:00
}
// Fonction principale à appeler dans main()
func setupPortMappingWithFallback(internalPort, externalPort uint16) {
success := setupUPnPWithRenewal(internalPort, externalPort)
if !success {
log.Println("🔁 UPnP indisponible, tentative NAT-PMP...")
setupNATPMP(internalPort, externalPort)
}
}
// UPnP avec renouvellement automatique
func setupUPnPWithRenewal(internalPort, externalPort uint16) bool {
clients, _, err := internetgateway1.NewWANIPConnection1Clients()
2025-06-17 20:05:00 +00:00
if err != nil {
log.Println("Erreur découverte UPnP :", err)
return false
}
if len(clients) == 0 {
log.Println("Aucun client UPnP détecté.")
2025-06-15 19:18:23 +00:00
return false
}
localIP := getLocalIP()
2025-06-17 20:05:00 +00:00
log.Println("IP locale détectée :", localIP)
2025-06-15 19:18:23 +00:00
if localIP == "" {
log.Println("Impossible dobtenir lIP locale")
return false
}
client := clients[0]
protocol := "TCP"
description := "GoAutoExposition"
duration := uint32(3600)
2025-06-17 20:05:00 +00:00
mapPort := func() error {
err := client.AddPortMapping("", externalPort, protocol, internalPort, localIP, true, description, duration)
2025-06-15 19:18:23 +00:00
if err != nil {
log.Println("UPnP mapping échoué :", err)
2025-06-17 20:05:00 +00:00
return err
2025-06-15 19:18:23 +00:00
}
2025-06-17 20:05:00 +00:00
log.Printf("✅ [UPnP] Port %d externe redirigé vers %s:%d", externalPort, localIP, internalPort)
return nil
2025-06-15 19:18:23 +00:00
}
2025-06-17 20:05:00 +00:00
// Premier essai
if err := mapPort(); err != nil {
return false
}
2025-06-15 19:18:23 +00:00
2025-06-17 20:05:00 +00:00
// Renouvellement
2025-06-15 19:18:23 +00:00
go func() {
for {
time.Sleep(55 * time.Minute)
mapPort()
}
}()
return true
}
2025-06-17 20:05:00 +00:00
2025-06-15 19:18:23 +00:00
// NAT-PMP fallback
func setupNATPMP(internalPort, externalPort uint16) {
gatewayIP := getGatewayIP()
if gatewayIP == "" {
log.Println("Impossible dobtenir la gateway pour NAT-PMP")
return
}
client := natpmp.NewClient(net.ParseIP(gatewayIP))
_, err := client.AddPortMapping("tcp", int(internalPort), int(externalPort), 3600)
if err != nil {
log.Println("NAT-PMP mapping échoué :", err)
} else {
2025-06-17 20:05:00 +00:00
log.Printf("✅ [NAT-PMP] Port %d externe redirigé vers local :%d via NAT-PMP", externalPort, internalPort)
2025-06-15 19:18:23 +00:00
}
}
// Obtenir IP locale
func getLocalIP() string {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
return ""
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP.String()
}
// Obtenir IP de la passerelle (gateway) locale
func getGatewayIP() string {
2025-06-17 20:05:00 +00:00
ip, err := gateway.DiscoverGateway()
2025-06-15 19:18:23 +00:00
if err != nil {
2025-06-17 20:05:00 +00:00
log.Println("Erreur récupération gateway réelle :", err)
2025-06-15 19:18:23 +00:00
return ""
}
return ip.String()
2025-06-06 09:55:54 +00:00
}