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

Telegram support #488

Merged
merged 11 commits into from
Dec 29, 2023
Merged

Telegram support #488

merged 11 commits into from
Dec 29, 2023

Conversation

0xCA
Copy link
Contributor

@0xCA 0xCA commented Nov 18, 2023

Telegram support

image

Telegram userid settings and config send button
Userid setting is moved to a separate collapsible section

Reason

  • In short, convenience and time saving
  • Mainly intended for quick effortless distribution, especially for large servers and mass config updates

Details

  • You can assign one user id or group id to each client config.
  • Each telegram userid can be assigned to multiple configs, so one user can have multiple configs.
  • You can search by userid on the clients page.
  • There is a configurable global flood wait. It applies to both manual config push and a user request. 1h default.
  • I used dependency injection to avoid circular dependencies with util.
  • There is an in-memory cache of tg userid -> client ids correspondence to avoid heavy db usage.
  • Pre-generated QR codes are kinda low res for telegram. So I generate 512x512 images each time they are needed.

Config request

image

Send any message to the bot to get all the configs associated with your userid

  • If config requests are disabled, bot module will not listen to updates. You can have a separate server handling the incoming messages without conflicts.
  • If you have multiple configs assigned to you, they are sent with a 2s delay between them, to avoid hitting bot API limits.
  • If there are no configs assigned, flood wait is still applied to avoid bot overload.

Configuration

Variable Description Default
TELEGRAM_TOKEN Telegram bot token for distributing configs to clients N/A
TELEGRAM_ALLOW_CONF_REQUEST Allow users to get configs from the bot by sending a message false
TELEGRAM_FLOOD_WAIT Time in minutes before the next conf request is processed 60

None of this configuration is saved to the database. You can change it by restarting the wgui.

Conflicts

I tried to minimize conflicts for this branch, but they are inevitable,

The advised merge order:

  1. Add endpoint field to client #470
  2. Subnet range selector, interface fixes #481
  3. Fixed tag input being too small and unable to fit a CIDR #483
  4. this

If you intend to merge all of the above, you can simplify this by merging https://github.com/0xCA/wireguard-ui/tree/umaster, where all the conflicts are already resolved.

Future

As this repo is currently inactive, it's inconvenient to continue development from the stale master. Further development (if any) is going to be in my repo. Feel free to check and contribute.

@nebulosa2007
Copy link
Contributor

Also thought about adding Telegram ID in clients settings :) For now I use Email field for filling Telegram Id and just parse json files with my own tgbot. Nothing special - it parses wgui configs and send .conf files with generated QR-code to user according his TgID.

This setup is more convenient than yours, because conf files send to user when he wants, but not when you press the button in wgui admin interface (besides user must add tgbot to contacts before, that you could send him an anything).

@0xCA
Copy link
Contributor Author

0xCA commented Nov 19, 2023

@nebulosa2007

This setup is more convenient than yours, because conf files send to user when he wants

You probably missed the Config request section above. Please read the full description carefully.
This PR includes both ways of getting a config. It's just disabled by default.

@X-UIP
Copy link

X-UIP commented Nov 30, 2023

How can update Wireguard-UI to get this ability?

@0xCA
Copy link
Contributor Author

0xCA commented Dec 2, 2023

@X-UIP
Copy link

X-UIP commented Dec 3, 2023

@X-UIP You can build from https://github.com/0xCA/wireguard-ui/tree/umaster

which image ? linuxserver/wireguard:latest ?

@0xCA
Copy link
Contributor Author

0xCA commented Dec 3, 2023

@X-UIP There is no public image built from my branch. You should build it yourself.
Hint: you will need to update Go version in Dockerfile. Use golang:1.20-alpine3.17 as build and alpine:3.17 as runtime.

@X-UIP
Copy link

X-UIP commented Dec 3, 2023

@X-UIP There is no public image built from my branch. You should build it yourself. Hint: you will need to update Go version in Dockerfile. Use golang:1.20-alpine3.17 as build and alpine:3.17 as runtime.

excuse me! if you can create a learning! because I don't know exactly what you say! I can create and run, Wireguard UI with a docker file and special image, but your work i don't know yours!

@ngoduykhanh
Copy link
Owner

Hey @0xCA, thank you for another contribution here. I guess I should touch this PR after solving #481. I've just left a comment there.
Although I am a Telegram user, I don't really see how this feature would be helpful in my daily use case. However, I am happy to merge if somebody else needs it. Let me know if you still want to continue with your contribution here or if you want to stop and keep it in your folks.

@0xCA
Copy link
Contributor Author

0xCA commented Dec 27, 2023

@ngoduykhanh this feature is intended for large servers, and | or environments where the config changes must be rolled out quickly. Also for the pure convenience of clients: anyone can request their configs anytime, and receive it automatically.
As for interest, there is certainly some, as you can see from reactions and questions about how to use this feature.

@ngoduykhanh
Copy link
Owner

@0xCA fair enough. I merged the other PR. could you please resolve this PR conflict as well?

@0xCA
Copy link
Contributor Author

0xCA commented Dec 28, 2023

Lots of conflicts were here. Please test it thoroughly.
I found no errors or bugs, but maybe I missed something.

Copy link
Owner

@ngoduykhanh ngoduykhanh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found an issue. When the TELEGRAM_ALLOW_CONF_REQUEST is not set and it has false value by default, I cannot send the Telegram user the config.

telegram-req-error

When TELEGRAM_ALLOW_CONF_REQUEST=true, everything is fine.

telegram/bot.go Outdated Show resolved Hide resolved
telegram/bot.go Outdated Show resolved Hide resolved
@0xCA
Copy link
Contributor Author

0xCA commented Dec 28, 2023

Found an issue. When the TELEGRAM_ALLOW_CONF_REQUEST is not set and it has false value by default, I cannot send the Telegram user the config.
telegram-req-error

When TELEGRAM_ALLOW_CONF_REQUEST=true, everything is fine.

Good find!

This is because after Bot = &bot, bot variable gets garbage collected. Not sure how to solve it yet, other than making it package level var, which I would like to avoid.

@0xCA
Copy link
Contributor Author

0xCA commented Dec 28, 2023

Okay, it turned out way simpler. I forgot the error check in defer. Should be fixed now.
Also I eliminated another bot rate limit hit possibility.

@ngoduykhanh
Copy link
Owner

Perfect! I am merging the PR. Thank you for addressing all the issues.

@ngoduykhanh ngoduykhanh merged commit 41bf0bc into ngoduykhanh:master Dec 29, 2023
@PulseDiver
Copy link

Where should I add these settings - TELEGRAM_TOKEN, TELEGRAM_ALLOW_CONF_REQUEST and TELEGRAM_FLOOD_WAIT?

@babblo
Copy link

babblo commented Jan 8, 2024

Where should I add these settings - TELEGRAM_TOKEN, TELEGRAM_ALLOW_CONF_REQUEST and TELEGRAM_FLOOD_WAIT?

Those are environment variables

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants