Graceful server, fix masking AllowedIPs
parent
42eb0ea8ef
commit
2d69e13e21
|
@ -1,6 +1,7 @@
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
@ -93,10 +94,10 @@ func runServer(ctx *cli.Context) error {
|
||||||
}
|
}
|
||||||
_, interfIPNet, err = net.ParseCIDR(interfAddrs[0].String())
|
_, interfIPNet, err = net.ParseCIDR(interfAddrs[0].String())
|
||||||
|
|
||||||
addQueue := make(chan request)
|
addQueue := make(chan request, 1)
|
||||||
go adder(addQueue, inter, config)
|
go adder(addQueue, inter, config)
|
||||||
|
|
||||||
gateQueue := make(chan request)
|
gateQueue := make(chan request, 1)
|
||||||
go gater(gateQueue, addQueue)
|
go gater(gateQueue, addQueue)
|
||||||
|
|
||||||
// TODO: Rate limiting
|
// TODO: Rate limiting
|
||||||
|
@ -106,6 +107,7 @@ func runServer(ctx *cli.Context) error {
|
||||||
case "POST":
|
case "POST":
|
||||||
publicKey := r.PostFormValue("PublicKey")
|
publicKey := r.PostFormValue("PublicKey")
|
||||||
// TODO: Ensure public key is new
|
// TODO: Ensure public key is new
|
||||||
|
// TODO: Validate public key
|
||||||
if len(publicKey) == 0 {
|
if len(publicKey) == 0 {
|
||||||
w.WriteHeader(400)
|
w.WriteHeader(400)
|
||||||
return
|
return
|
||||||
|
@ -134,15 +136,19 @@ func runServer(ctx *cli.Context) error {
|
||||||
IP: ip,
|
IP: ip,
|
||||||
Mask: interfIPNet.Mask,
|
Mask: interfIPNet.Mask,
|
||||||
}
|
}
|
||||||
|
netIPNet := &net.IPNet{
|
||||||
|
IP: interfIPNet.IP.Mask(interfIPNet.Mask),
|
||||||
|
Mask: interfIPNet.Mask,
|
||||||
|
}
|
||||||
resp := struct {
|
resp := struct {
|
||||||
InterfaceIPs string
|
InterfaceIPs []string
|
||||||
AllowedIPs string
|
AllowedIPs []string
|
||||||
PublicKey string
|
PublicKey string
|
||||||
Endpoint string
|
Endpoint string
|
||||||
PersistentKeepaliveInterval int
|
PersistentKeepaliveInterval int
|
||||||
}{
|
}{
|
||||||
ipNet.String(),
|
[]string{ipNet.String()},
|
||||||
interfIPNet.IP.Mask(interfIPNet.Mask).String(),
|
[]string{netIPNet.String()},
|
||||||
serverPublicKey,
|
serverPublicKey,
|
||||||
endpoint,
|
endpoint,
|
||||||
25,
|
25,
|
||||||
|
@ -155,6 +161,11 @@ func runServer(ctx *cli.Context) error {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
server := &http.Server{
|
||||||
|
Addr: listen,
|
||||||
|
Handler: http.DefaultServeMux,
|
||||||
|
}
|
||||||
|
|
||||||
// Shutdown notifier
|
// Shutdown notifier
|
||||||
go func() {
|
go func() {
|
||||||
sigint := make(chan os.Signal, 1)
|
sigint := make(chan os.Signal, 1)
|
||||||
|
@ -162,14 +173,12 @@ func runServer(ctx *cli.Context) error {
|
||||||
<-sigint
|
<-sigint
|
||||||
close(gateQueue)
|
close(gateQueue)
|
||||||
close(addQueue)
|
close(addQueue)
|
||||||
/*
|
if err := server.Shutdown(context.Background()); err != nil {
|
||||||
if err := http.Shutdown(context.Background()); err != nil {
|
log.Printf("Server shutdown error: %v\n", err)
|
||||||
log.Printf("Server shutdown error: %v\n", err)
|
}
|
||||||
}
|
|
||||||
*/
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return http.ListenAndServe(listen, nil)
|
return server.ListenAndServe()
|
||||||
}
|
}
|
||||||
|
|
||||||
func adder(queue chan request, inter string, config string) {
|
func adder(queue chan request, inter string, config string) {
|
||||||
|
@ -183,10 +192,12 @@ func adder(queue chan request, inter string, config string) {
|
||||||
err := configAddPeer(config, req)
|
err := configAddPeer(config, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
err = interAddPeer(inter, req, config)
|
err = interAddPeer(inter, req, config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue