mirror of
https://github.com/fosrl/gerbil.git
synced 2026-02-07 21:46:40 +00:00
Add notify
This commit is contained in:
36
main.go
36
main.go
@@ -30,6 +30,7 @@ var (
|
||||
mtuInt int
|
||||
lastReadings = make(map[string]PeerReading)
|
||||
mu sync.Mutex
|
||||
notifyURL string
|
||||
)
|
||||
|
||||
type WgConfig struct {
|
||||
@@ -111,6 +112,7 @@ func main() {
|
||||
reachableAt = os.Getenv("REACHABLE_AT")
|
||||
logLevel = os.Getenv("LOG_LEVEL")
|
||||
mtu = os.Getenv("MTU")
|
||||
notifyURL = os.Getenv("NOTIFY_URL")
|
||||
|
||||
if interfaceName == "" {
|
||||
flag.StringVar(&interfaceName, "interface", "wg0", "Name of the WireGuard interface")
|
||||
@@ -141,6 +143,9 @@ func main() {
|
||||
if mtu == "" {
|
||||
flag.StringVar(&mtu, "mtu", "1280", "MTU of the WireGuard interface")
|
||||
}
|
||||
if notifyURL == "" {
|
||||
flag.StringVar(¬ifyURL, "notify", "", "URL to notify on peer changes")
|
||||
}
|
||||
flag.Parse()
|
||||
|
||||
logger.Init()
|
||||
@@ -578,6 +583,9 @@ func handleAddPeer(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// Notify if notifyURL is set
|
||||
go notifyPeerChange("add", peer.PublicKey)
|
||||
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
json.NewEncoder(w).Encode(map[string]string{"status": "Peer added successfully"})
|
||||
}
|
||||
@@ -629,6 +637,9 @@ func handleRemovePeer(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// Notify if notifyURL is set
|
||||
go notifyPeerChange("remove", publicKey)
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(map[string]string{"status": "Peer removed successfully"})
|
||||
}
|
||||
@@ -776,3 +787,28 @@ func reportPeerBandwidth(apiURL string) error {
|
||||
|
||||
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