mirror of
https://github.com/fosrl/gerbil.git
synced 2026-02-08 05:56:40 +00:00
Add notify
This commit is contained in:
36
main.go
36
main.go
@@ -30,6 +30,7 @@ var (
|
|||||||
mtuInt int
|
mtuInt int
|
||||||
lastReadings = make(map[string]PeerReading)
|
lastReadings = make(map[string]PeerReading)
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
|
notifyURL string
|
||||||
)
|
)
|
||||||
|
|
||||||
type WgConfig struct {
|
type WgConfig struct {
|
||||||
@@ -111,6 +112,7 @@ func main() {
|
|||||||
reachableAt = os.Getenv("REACHABLE_AT")
|
reachableAt = os.Getenv("REACHABLE_AT")
|
||||||
logLevel = os.Getenv("LOG_LEVEL")
|
logLevel = os.Getenv("LOG_LEVEL")
|
||||||
mtu = os.Getenv("MTU")
|
mtu = os.Getenv("MTU")
|
||||||
|
notifyURL = os.Getenv("NOTIFY_URL")
|
||||||
|
|
||||||
if interfaceName == "" {
|
if interfaceName == "" {
|
||||||
flag.StringVar(&interfaceName, "interface", "wg0", "Name of the WireGuard interface")
|
flag.StringVar(&interfaceName, "interface", "wg0", "Name of the WireGuard interface")
|
||||||
@@ -141,6 +143,9 @@ func main() {
|
|||||||
if mtu == "" {
|
if mtu == "" {
|
||||||
flag.StringVar(&mtu, "mtu", "1280", "MTU of the WireGuard interface")
|
flag.StringVar(&mtu, "mtu", "1280", "MTU of the WireGuard interface")
|
||||||
}
|
}
|
||||||
|
if notifyURL == "" {
|
||||||
|
flag.StringVar(¬ifyURL, "notify", "", "URL to notify on peer changes")
|
||||||
|
}
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
logger.Init()
|
logger.Init()
|
||||||
@@ -578,6 +583,9 @@ func handleAddPeer(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Notify if notifyURL is set
|
||||||
|
go notifyPeerChange("add", peer.PublicKey)
|
||||||
|
|
||||||
w.WriteHeader(http.StatusCreated)
|
w.WriteHeader(http.StatusCreated)
|
||||||
json.NewEncoder(w).Encode(map[string]string{"status": "Peer added successfully"})
|
json.NewEncoder(w).Encode(map[string]string{"status": "Peer added successfully"})
|
||||||
}
|
}
|
||||||
@@ -629,6 +637,9 @@ func handleRemovePeer(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Notify if notifyURL is set
|
||||||
|
go notifyPeerChange("remove", publicKey)
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
json.NewEncoder(w).Encode(map[string]string{"status": "Peer removed successfully"})
|
json.NewEncoder(w).Encode(map[string]string{"status": "Peer removed successfully"})
|
||||||
}
|
}
|
||||||
@@ -776,3 +787,28 @@ func reportPeerBandwidth(apiURL string) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// notifyPeerChange sends a POST request to notifyURL with the action and public key.
|
||||||
|
func notifyPeerChange(action, publicKey string) {
|
||||||
|
if notifyURL == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
payload := map[string]string{
|
||||||
|
"action": action,
|
||||||
|
"publicKey": publicKey,
|
||||||
|
}
|
||||||
|
data, err := json.Marshal(payload)
|
||||||
|
if err != nil {
|
||||||
|
logger.Warn("Failed to marshal notify payload: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
resp, err := http.Post(notifyURL, "application/json", bytes.NewBuffer(data))
|
||||||
|
if err != nil {
|
||||||
|
logger.Warn("Failed to notify peer change: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
logger.Warn("Notify server returned non-OK: %s", resp.Status)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user