shelfy/main.go

142 lines
3.3 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"
"github.com/jackpal/go-nat-pmp"
2025-06-06 09:55:54 +00:00
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-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()
if err != nil || len(clients) == 0 {
return false
}
localIP := getLocalIP()
if localIP == "" {
log.Println("Impossible dobtenir lIP locale")
return false
}
client := clients[0]
protocol := "TCP"
description := "GoAutoExposition"
duration := uint32(3600)
// Fonction de mapping UPnP
mapPort := func() {
err = client.AddPortMapping("", externalPort, protocol, internalPort, localIP, true, description, duration)
if err != nil {
log.Println("UPnP mapping échoué :", err)
} else {
log.Printf("✅ [UPnP] Port %d externe redirigé vers %s:%d", externalPort, localIP, internalPort)
}
}
mapPort()
// Renouveler toutes les 55 minutes
go func() {
for {
time.Sleep(55 * time.Minute)
mapPort()
}
}()
return true
}
// 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 {
log.Printf("✅ [NAT-PMP] Port %d externe redirigé vers local :%d", externalPort, internalPort)
}
}
// 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 {
// Connexion UDP simulée pour déduire la passerelle
conn, err := net.Dial("udp", "192.168.0.1:80")
if err != nil {
return ""
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
ip := localAddr.IP.To4()
if ip == nil {
return ""
}
// Exemple : 192.168.1.34 → gateway supposée : 192.168.1.1
ip[3] = 1
return ip.String()
2025-06-06 09:55:54 +00:00
}