Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Time Restriction #555

Open
ctv99 opened this issue Dec 12, 2024 · 4 comments
Open

Time Restriction #555

ctv99 opened this issue Dec 12, 2024 · 4 comments
Assignees
Labels
enhancement New feature or request

Comments

@ctv99
Copy link

ctv99 commented Dec 12, 2024

Is there an option to Time Restrict Peer not just Date?

Such as Peer cannot Connect After 8pm But at 6Am they can Connect Again?

@ctv99 ctv99 added the enhancement New feature or request label Dec 12, 2024
@donaldzou
Copy link
Owner

I think is better to add repeating job, like repeating restrict peer at 8pm everyday and unrestrict peer at 6am everyday?

@ctv99
Copy link
Author

ctv99 commented Dec 14, 2024 via email

@donaldzou donaldzou self-assigned this Dec 15, 2024
@donaldzou
Copy link
Owner

donaldzou commented Dec 15, 2024

Hi! Unfortunately the current version does not have this functionality. But.. we can use cron utility to schedule recurring jobs, by utilizing the WGDashboard API with curl :)

  1. Get a WGDashboard API key: Settings > WGDashboard Settings > (Scroll down) > Enable API Key > Add a key and copy it
  2. Create 2 shell script to restrict & allow access under WGDashboard/src/

I will use my private domain, you'll need to replace it with your server's IP or domain. Also replace the wg-dashboard-apikey with the api key you just created.

restrict.sh

curl --location 'wg.local:10086/api/restrictPeers/wg0' \
--header 'wg-dashboard-apikey: twkl03ZfNz3WMMjsxs1xGXDZ1ruMaN8_0YKmGcDXvFI' \
--header 'Content-Type: application/json' \
--data '{
    "peers":[
        "Ee3BTE9vFAGgtKkI8d6xLe2wwFPRijnIzZ5bdrCc6Ek="
    ]
}'

allow.sh

curl --location 'wg.local:10086/api/allowAccessPeers/wg0' \
--header 'wg-dashboard-apikey: twkl03ZfNz3WMMjsxs1xGXDZ1ruMaN8_0YKmGcDXvFI' \
--header 'Content-Type: application/json' \
--data '{
    "peers":[
        "Ee3BTE9vFAGgtKkI8d6xLe2wwFPRijnIzZ5bdrCc6Ek="
    ]
}'

Save them in a known location. You can do so by running pwd and you will see the path. In the example I will be using /root/WGDashboard/src

  1. Create cron rules
sudo crontab -e

Then you might ask to choose an editor, nano is my preference so I will choose that.

Then, we will need to create 2 rules. But before that, you might want to know more about cron syntax: https://docs.gitlab.com/ee/topics/cron/

# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of the month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday)
# │ │ │ │ │
# │ │ │ │ │
# │ │ │ │ │
# * * * * * <command to execute>

In your case it would be

0 8 * * * /root/WGDashboard/src/allow.sh
0 18 * * * /root/WGDashboard/src/restrict.sh

Which means, at 8:00 every day, it will run /root/WGDashboard/src/allow.sh. And at 18:00 it will run /root/WGDashboard/src/restrict.sh

Then save it. It will tells you whether your rules are successfully installed or not.

Let me know if you have any questions :)

@donaldzou
Copy link
Owner

@ctv99 Hi! Is this working for you?

@donaldzou donaldzou added this to the v4.3 (Planned) milestone Feb 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants