mirror of
https://github.com/ngoduykhanh/wireguard-ui.git
synced 2025-06-07 00:46:58 +03:00
SendRequestedConfigsToTelegram func, simplified dependencies, fixes
This commit is contained in:
parent
659df606f6
commit
4ed37f997a
3 changed files with 76 additions and 56 deletions
6
main.go
6
main.go
|
@ -264,10 +264,8 @@ func main() {
|
||||||
app.GET(util.BasePath+"/static/*", echo.WrapHandler(http.StripPrefix(util.BasePath+"/static/", assetHandler)))
|
app.GET(util.BasePath+"/static/*", echo.WrapHandler(http.StripPrefix(util.BasePath+"/static/", assetHandler)))
|
||||||
|
|
||||||
initDeps := telegram.TgBotInitDependencies{
|
initDeps := telegram.TgBotInitDependencies{
|
||||||
DB: db,
|
DB: db,
|
||||||
BuildClientConfig: util.BuildClientConfig,
|
SendRequestedConfigsToTelegram: util.SendRequestedConfigsToTelegram,
|
||||||
TgUseridToClientID: util.TgUseridToClientID,
|
|
||||||
TgUseridToClientIDMutex: &util.TgUseridToClientIDMutex,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
initTelegram(initDeps)
|
initTelegram(initDeps)
|
||||||
|
|
|
@ -2,24 +2,19 @@ package telegram
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/NicoNex/echotron/v3"
|
"github.com/NicoNex/echotron/v3"
|
||||||
"github.com/labstack/gommon/log"
|
"github.com/labstack/gommon/log"
|
||||||
"github.com/ngoduykhanh/wireguard-ui/model"
|
|
||||||
"github.com/ngoduykhanh/wireguard-ui/store"
|
"github.com/ngoduykhanh/wireguard-ui/store"
|
||||||
"github.com/skip2/go-qrcode"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type BuildClientConfig func(client model.Client, server model.Server, setting model.GlobalSetting) string
|
type SendRequestedConfigsToTelegram func(db store.IStore, userid int64) []string
|
||||||
|
|
||||||
type TgBotInitDependencies struct {
|
type TgBotInitDependencies struct {
|
||||||
DB store.IStore
|
DB store.IStore
|
||||||
BuildClientConfig BuildClientConfig
|
SendRequestedConfigsToTelegram SendRequestedConfigsToTelegram
|
||||||
TgUseridToClientID map[int64]([]string)
|
|
||||||
TgUseridToClientIDMutex *sync.RWMutex
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -32,12 +27,6 @@ var (
|
||||||
TgBotMutex sync.RWMutex
|
TgBotMutex sync.RWMutex
|
||||||
|
|
||||||
floodWait = make(map[int64]int64, 0)
|
floodWait = make(map[int64]int64, 0)
|
||||||
|
|
||||||
qrCodeSettings = model.QRCodeSettings{
|
|
||||||
Enabled: true,
|
|
||||||
IncludeDNS: true,
|
|
||||||
IncludeMTU: true,
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func Start(initDeps TgBotInitDependencies) (err error) {
|
func Start(initDeps TgBotInitDependencies) (err error) {
|
||||||
|
@ -98,44 +87,19 @@ func Start(initDeps TgBotInitDependencies) (err error) {
|
||||||
}
|
}
|
||||||
floodWait[userid] = time.Now().Unix()
|
floodWait[userid] = time.Now().Unix()
|
||||||
|
|
||||||
initDeps.TgUseridToClientIDMutex.RLock()
|
failed := initDeps.SendRequestedConfigsToTelegram(initDeps.DB, userid)
|
||||||
if clids, found := initDeps.TgUseridToClientID[userid]; found && len(clids) > 0 {
|
if len(failed) > 0 {
|
||||||
initDeps.TgUseridToClientIDMutex.RUnlock()
|
messageText := "Failed to send configs:\n"
|
||||||
|
for _, f := range failed {
|
||||||
for _, clid := range clids {
|
messageText += f + "\n"
|
||||||
func(clid string) {
|
|
||||||
clientData, err := initDeps.DB.GetClientByID(clid, qrCodeSettings)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// build config
|
|
||||||
server, _ := initDeps.DB.GetServer()
|
|
||||||
globalSettings, _ := initDeps.DB.GetGlobalSettings()
|
|
||||||
config := initDeps.BuildClientConfig(*clientData.Client, server, globalSettings)
|
|
||||||
configData := []byte(config)
|
|
||||||
var qrData []byte
|
|
||||||
|
|
||||||
if clientData.Client.PrivateKey != "" {
|
|
||||||
qrData, err = qrcode.Encode(config, qrcode.Medium, 512)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
userid, err := strconv.ParseInt(clientData.Client.TgUserid, 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
SendConfig(userid, clientData.Client.Name, configData, qrData, true)
|
|
||||||
}(clid)
|
|
||||||
time.Sleep(2 * time.Second)
|
|
||||||
}
|
}
|
||||||
} else {
|
bot.SendMessage(
|
||||||
initDeps.TgUseridToClientIDMutex.RUnlock()
|
messageText,
|
||||||
|
userid,
|
||||||
|
&echotron.MessageOptions{
|
||||||
|
ReplyToMessageID: update.Message.ID,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
|
@ -174,7 +138,7 @@ func SendConfig(userid int64, clientName string, confData, qrData []byte, ignore
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateFloodWait() {
|
func updateFloodWait() {
|
||||||
thresholdTS := time.Now().Unix() - 60000*int64(TelegramFloodWait)
|
thresholdTS := time.Now().Unix() - 60*int64(TelegramFloodWait)
|
||||||
for userid, ts := range floodWait {
|
for userid, ts := range floodWait {
|
||||||
if ts < thresholdTS {
|
if ts < thresholdTS {
|
||||||
delete(floodWait, userid)
|
delete(floodWait, userid)
|
||||||
|
|
60
util/util.go
60
util/util.go
|
@ -20,6 +20,7 @@ import (
|
||||||
|
|
||||||
"github.com/ngoduykhanh/wireguard-ui/store"
|
"github.com/ngoduykhanh/wireguard-ui/store"
|
||||||
"github.com/ngoduykhanh/wireguard-ui/telegram"
|
"github.com/ngoduykhanh/wireguard-ui/telegram"
|
||||||
|
"github.com/skip2/go-qrcode"
|
||||||
"golang.org/x/mod/sumdb/dirhash"
|
"golang.org/x/mod/sumdb/dirhash"
|
||||||
|
|
||||||
externalip "github.com/glendc/go-external-ip"
|
externalip "github.com/glendc/go-external-ip"
|
||||||
|
@ -28,8 +29,14 @@ import (
|
||||||
"github.com/sdomino/scribble"
|
"github.com/sdomino/scribble"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var qrCodeSettings = model.QRCodeSettings{
|
||||||
|
Enabled: true,
|
||||||
|
IncludeDNS: true,
|
||||||
|
IncludeMTU: true,
|
||||||
|
}
|
||||||
|
|
||||||
// BuildClientConfig to create wireguard client config string
|
// BuildClientConfig to create wireguard client config string
|
||||||
var BuildClientConfig telegram.BuildClientConfig = func(client model.Client, server model.Server, setting model.GlobalSetting) string {
|
func BuildClientConfig(client model.Client, server model.Server, setting model.GlobalSetting) string {
|
||||||
// Interface section
|
// Interface section
|
||||||
clientAddress := fmt.Sprintf("Address = %s\n", strings.Join(client.AllocatedIPs, ","))
|
clientAddress := fmt.Sprintf("Address = %s\n", strings.Join(client.AllocatedIPs, ","))
|
||||||
clientPrivateKey := fmt.Sprintf("PrivateKey = %s\n", client.PrivateKey)
|
clientPrivateKey := fmt.Sprintf("PrivateKey = %s\n", client.PrivateKey)
|
||||||
|
@ -579,6 +586,57 @@ func WriteWireGuardServerConfig(tmplDir fs.FS, serverConfig model.Server, client
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SendRequestedConfigsToTelegram to send client all their configs. Returns failed configs list.
|
||||||
|
func SendRequestedConfigsToTelegram(db store.IStore, userid int64) []string {
|
||||||
|
failedList := make([]string, 0)
|
||||||
|
TgUseridToClientIDMutex.RLock()
|
||||||
|
if clids, found := TgUseridToClientID[userid]; found && len(clids) > 0 {
|
||||||
|
TgUseridToClientIDMutex.RUnlock()
|
||||||
|
|
||||||
|
for _, clid := range clids {
|
||||||
|
clientData, err := db.GetClientByID(clid, qrCodeSettings)
|
||||||
|
if err != nil {
|
||||||
|
// return fmt.Errorf("unable to get client")
|
||||||
|
failedList = append(failedList, clid)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// build config
|
||||||
|
server, _ := db.GetServer()
|
||||||
|
globalSettings, _ := db.GetGlobalSettings()
|
||||||
|
config := BuildClientConfig(*clientData.Client, server, globalSettings)
|
||||||
|
configData := []byte(config)
|
||||||
|
var qrData []byte
|
||||||
|
|
||||||
|
if clientData.Client.PrivateKey != "" {
|
||||||
|
qrData, err = qrcode.Encode(config, qrcode.Medium, 512)
|
||||||
|
if err != nil {
|
||||||
|
// return fmt.Errorf("unable to encode qr")
|
||||||
|
failedList = append(failedList, clientData.Client.Name)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
userid, err := strconv.ParseInt(clientData.Client.TgUserid, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
// return fmt.Errorf("tg usrid is unreadable")
|
||||||
|
failedList = append(failedList, clientData.Client.Name)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
err = telegram.SendConfig(userid, clientData.Client.Name, configData, qrData, true)
|
||||||
|
if err != nil {
|
||||||
|
failedList = append(failedList, clientData.Client.Name)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
time.Sleep(2 * time.Second)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
TgUseridToClientIDMutex.RUnlock()
|
||||||
|
}
|
||||||
|
return failedList
|
||||||
|
}
|
||||||
|
|
||||||
func LookupEnvOrString(key string, defaultVal string) string {
|
func LookupEnvOrString(key string, defaultVal string) string {
|
||||||
if val, ok := os.LookupEnv(key); ok {
|
if val, ok := os.LookupEnv(key); ok {
|
||||||
return val
|
return val
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue