up
This commit is contained in:
parent
e656363dd9
commit
5932d612c3
174
main.go
174
main.go
@ -9,10 +9,6 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/huin/goupnp/dcps/internetgateway1"
|
|
||||||
"github.com/jackpal/gateway"
|
|
||||||
"github.com/jackpal/go-nat-pmp"
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -37,7 +33,7 @@ func main() {
|
|||||||
protected.Use(handlers.AuthMiddleware)
|
protected.Use(handlers.AuthMiddleware)
|
||||||
// 6. Enregistrer les routes protégées sur ce sous-routeur
|
// 6. Enregistrer les routes protégées sur ce sous-routeur
|
||||||
route.RoutesProtected(protected, bd)
|
route.RoutesProtected(protected, bd)
|
||||||
setupPortMappingWithFallback(4000, 4000)
|
//setupPortMappingWithFallback(4000, 4000)
|
||||||
// 6.5. Exposer le dossier upload
|
// 6.5. Exposer le dossier upload
|
||||||
fs := http.StripPrefix("/upload/", http.FileServer(http.Dir("/app/upload")))
|
fs := http.StripPrefix("/upload/", http.FileServer(http.Dir("/app/upload")))
|
||||||
r.PathPrefix("/upload/").Handler(fs)
|
r.PathPrefix("/upload/").Handler(fs)
|
||||||
@ -48,99 +44,99 @@ func main() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
// Fonction principale à appeler dans main()
|
// Fonction principale à appeler dans main()
|
||||||
func setupPortMappingWithFallback(internalPort, externalPort uint16) {
|
// func setupPortMappingWithFallback(internalPort, externalPort uint16) {
|
||||||
success := setupUPnPWithRenewal(internalPort, externalPort)
|
// success := setupUPnPWithRenewal(internalPort, externalPort)
|
||||||
if !success {
|
// if !success {
|
||||||
log.Println("🔁 UPnP indisponible, tentative NAT-PMP...")
|
// log.Println("🔁 UPnP indisponible, tentative NAT-PMP...")
|
||||||
setupNATPMP(internalPort, externalPort)
|
// setupNATPMP(internalPort, externalPort)
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
// UPnP avec renouvellement automatique
|
// // UPnP avec renouvellement automatique
|
||||||
func setupUPnPWithRenewal(internalPort, externalPort uint16) bool {
|
// func setupUPnPWithRenewal(internalPort, externalPort uint16) bool {
|
||||||
clients, _, err := internetgateway1.NewWANIPConnection1Clients()
|
// clients, _, err := internetgateway1.NewWANIPConnection1Clients()
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
log.Println("Erreur découverte UPnP :", err)
|
// log.Println("Erreur découverte UPnP :", err)
|
||||||
return false
|
// return false
|
||||||
}
|
// }
|
||||||
if len(clients) == 0 {
|
// if len(clients) == 0 {
|
||||||
log.Println("Aucun client UPnP détecté.")
|
// log.Println("Aucun client UPnP détecté.")
|
||||||
return false
|
// return false
|
||||||
}
|
// }
|
||||||
|
|
||||||
localIP := getLocalIP()
|
// localIP := getLocalIP()
|
||||||
log.Println("IP locale détectée :", localIP)
|
// log.Println("IP locale détectée :", localIP)
|
||||||
if localIP == "" {
|
// if localIP == "" {
|
||||||
log.Println("Impossible d’obtenir l’IP locale")
|
// log.Println("Impossible d’obtenir l’IP locale")
|
||||||
return false
|
// return false
|
||||||
}
|
// }
|
||||||
|
|
||||||
client := clients[0]
|
// client := clients[0]
|
||||||
protocol := "TCP"
|
// protocol := "TCP"
|
||||||
description := "GoAutoExposition"
|
// description := "GoAutoExposition"
|
||||||
duration := uint32(3600)
|
// duration := uint32(3600)
|
||||||
|
|
||||||
mapPort := func() error {
|
// mapPort := func() error {
|
||||||
err := client.AddPortMapping("", externalPort, protocol, internalPort, localIP, true, description, duration)
|
// err := client.AddPortMapping("", externalPort, protocol, internalPort, localIP, true, description, duration)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
log.Println("UPnP mapping échoué :", err)
|
// log.Println("UPnP mapping échoué :", err)
|
||||||
return err
|
// return err
|
||||||
}
|
// }
|
||||||
log.Printf("✅ [UPnP] Port %d externe redirigé vers %s:%d", externalPort, localIP, internalPort)
|
// log.Printf("✅ [UPnP] Port %d externe redirigé vers %s:%d", externalPort, localIP, internalPort)
|
||||||
return nil
|
// return nil
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Premier essai
|
// // Premier essai
|
||||||
if err := mapPort(); err != nil {
|
// if err := mapPort(); err != nil {
|
||||||
return false
|
// return false
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Renouvellement
|
// // Renouvellement
|
||||||
go func() {
|
// go func() {
|
||||||
for {
|
// for {
|
||||||
time.Sleep(55 * time.Minute)
|
// time.Sleep(55 * time.Minute)
|
||||||
mapPort()
|
// mapPort()
|
||||||
}
|
// }
|
||||||
}()
|
// }()
|
||||||
|
|
||||||
return true
|
// return true
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
|
||||||
// NAT-PMP fallback
|
// // NAT-PMP fallback
|
||||||
func setupNATPMP(internalPort, externalPort uint16) {
|
// func setupNATPMP(internalPort, externalPort uint16) {
|
||||||
gatewayIP := getGatewayIP()
|
// gatewayIP := getGatewayIP()
|
||||||
if gatewayIP == "" {
|
// if gatewayIP == "" {
|
||||||
log.Println("Impossible d’obtenir la gateway pour NAT-PMP")
|
// log.Println("Impossible d’obtenir la gateway pour NAT-PMP")
|
||||||
return
|
// return
|
||||||
}
|
// }
|
||||||
|
|
||||||
client := natpmp.NewClient(net.ParseIP(gatewayIP))
|
// client := natpmp.NewClient(net.ParseIP(gatewayIP))
|
||||||
_, err := client.AddPortMapping("tcp", int(internalPort), int(externalPort), 3600)
|
// _, err := client.AddPortMapping("tcp", int(internalPort), int(externalPort), 3600)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
log.Println("NAT-PMP mapping échoué :", err)
|
// log.Println("NAT-PMP mapping échoué :", err)
|
||||||
} else {
|
// } else {
|
||||||
log.Printf("✅ [NAT-PMP] Port %d externe redirigé vers local :%d via NAT-PMP", externalPort, internalPort)
|
// log.Printf("✅ [NAT-PMP] Port %d externe redirigé vers local :%d via NAT-PMP", externalPort, internalPort)
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Obtenir IP locale
|
// // Obtenir IP locale
|
||||||
func getLocalIP() string {
|
// func getLocalIP() string {
|
||||||
conn, err := net.Dial("udp", "8.8.8.8:80")
|
// conn, err := net.Dial("udp", "8.8.8.8:80")
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return ""
|
// return ""
|
||||||
}
|
// }
|
||||||
defer conn.Close()
|
// defer conn.Close()
|
||||||
localAddr := conn.LocalAddr().(*net.UDPAddr)
|
// localAddr := conn.LocalAddr().(*net.UDPAddr)
|
||||||
return localAddr.IP.String()
|
// return localAddr.IP.String()
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Obtenir IP de la passerelle (gateway) locale
|
// // Obtenir IP de la passerelle (gateway) locale
|
||||||
func getGatewayIP() string {
|
// func getGatewayIP() string {
|
||||||
ip, err := gateway.DiscoverGateway()
|
// ip, err := gateway.DiscoverGateway()
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
log.Println("Erreur récupération gateway réelle :", err)
|
// log.Println("Erreur récupération gateway réelle :", err)
|
||||||
return ""
|
// return ""
|
||||||
}
|
// }
|
||||||
return ip.String()
|
// return ip.String()
|
||||||
}
|
// }
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user