donaldzou / WGDashboard

Simple dashboard for WireGuard VPN written in Python & Vue.js
https://donaldzou.github.io/WGDashboard-Documentation/
Apache License 2.0
1.65k stars 255 forks source link

Restrict Peer API #536

Open sohrabp72 opened 15 hours ago

sohrabp72 commented 15 hours ago

based on this link, I tried 2 times to restrict some peers, but it seems the API in the background works as Delete Peer method, it removes peers instead of just restricting them.

Untitled

All these sections' descriptions are the same for deletion.

donaldzou commented 14 hours ago

Hi! May I see how did you send the request please?

sohrabp72 commented 13 hours ago

Hi! May I see how did you send the request please?

Sure. here is it:

def restrict(peer_name, uid):
    """
    تابعی برای اعمال محدودیت (restrict) بر روی یک peer.
    """
    # دریافت کلید عمومی (public key) از دیتابیس
    public_key = get_public_key_from_db(peer_name,uid)

    if not public_key:
        print(f"❌ کلید عمومی برای peer {peer_name} پیدا نشد.")
        return

    # ساخت URL برای درخواست restrict به API
    api_url = f"https://webpanel/api/restrictPeers/{configName}"  # آدرس API که restrict را انجام می‌دهد

    # داده‌هایی که باید ارسال شوند
    data = {
        "peers": [public_key]
    }

    try:
        # ارسال درخواست HTTP POST به API برای restrict کردن
        response = requests.post(api_url, headers=HEADERS, json=data)

        # بررسی پاسخ API
        if response.status_code == 200:
            print(f"✅ Restrict اعمال شد برای {peer_name} در UID {uid}")
            print(response.json().get('message'))
        else:
            print(f"❌ خطا در اعمال restrict برای {peer_name}: {response.text}")
    except requests.exceptions.RequestException as e:
        print(f"❌ خطا در ارتباط با API: {e}")
sohrabp72 commented 13 hours ago

I realized something weird, if the Peer I'm going to restrict is in a configuration where its name starts with wg, it will be restricted, if it is in a configuration in which its name starts with numbers like 213512, the peer will be removed, and won't restrict.