frontend: limit telegram commands by bot name

This commit is contained in:
Lan Tian 2021-06-19 16:23:43 +08:00
parent bda06ddd5e
commit 7c7814cc7b
No known key found for this signature in database
GPG Key ID: 3D2E9DC81E5791C7
3 changed files with 60 additions and 9 deletions

View File

@ -21,6 +21,7 @@ type settingType struct {
netSpecificMode string netSpecificMode string
titleBrand string titleBrand string
navBarBrand string navBarBrand string
telegramBotName string
} }
var setting settingType var setting settingType
@ -34,6 +35,7 @@ func main() {
dnsInterface: "asn.cymru.com", dnsInterface: "asn.cymru.com",
titleBrand: "Bird-lg Go", titleBrand: "Bird-lg Go",
navBarBrand: "Bird-lg Go", navBarBrand: "Bird-lg Go",
telegramBotName: "",
} }
if env := os.Getenv("BIRDLG_SERVERS"); env != "" { if env := os.Getenv("BIRDLG_SERVERS"); env != "" {
@ -67,6 +69,9 @@ func main() {
if env := os.Getenv("BIRDLG_NAVBAR_BRAND"); env != "" { if env := os.Getenv("BIRDLG_NAVBAR_BRAND"); env != "" {
settingDefault.navBarBrand = env settingDefault.navBarBrand = env
} }
if env := os.Getenv("BIRDLG_TELEGRAM_BOT_NAME"); env != "" {
settingDefault.telegramBotName = env
}
serversPtr := flag.String("servers", strings.Join(settingDefault.servers, ","), "server name prefixes, separated by comma") serversPtr := flag.String("servers", strings.Join(settingDefault.servers, ","), "server name prefixes, separated by comma")
domainPtr := flag.String("domain", settingDefault.domain, "server name domain suffixes") domainPtr := flag.String("domain", settingDefault.domain, "server name domain suffixes")
@ -77,6 +82,7 @@ func main() {
netSpecificModePtr := flag.String("net-specific-mode", settingDefault.netSpecificMode, "network specific operation mode, [(none)|dn42]") netSpecificModePtr := flag.String("net-specific-mode", settingDefault.netSpecificMode, "network specific operation mode, [(none)|dn42]")
titleBrandPtr := flag.String("title-brand", settingDefault.titleBrand, "prefix of page titles in browser tabs") titleBrandPtr := flag.String("title-brand", settingDefault.titleBrand, "prefix of page titles in browser tabs")
navBarBrandPtr := flag.String("navbar-brand", settingDefault.navBarBrand, "brand to show in the navigation bar") navBarBrandPtr := flag.String("navbar-brand", settingDefault.navBarBrand, "brand to show in the navigation bar")
telegramBotNamePtr := flag.String("telegram-bot-name", settingDefault.navBarBrand, "telegram bot name (used to filter @bot commands)")
flag.Parse() flag.Parse()
if *serversPtr == "" { if *serversPtr == "" {
@ -106,6 +112,7 @@ func main() {
strings.ToLower(*netSpecificModePtr), strings.ToLower(*netSpecificModePtr),
*titleBrandPtr, *titleBrandPtr,
*navBarBrandPtr, *navBarBrandPtr,
*telegramBotNamePtr,
} }
ImportTemplates() ImportTemplates()

View File

@ -31,7 +31,8 @@ type tgWebhookResponse struct {
func telegramIsCommand(message string, command string) bool { func telegramIsCommand(message string, command string) bool {
b := false b := false
b = b || strings.HasPrefix(message, "/"+command+"@") b = b || strings.HasPrefix(message, "/"+command+"@"+setting.telegramBotName+" ")
b = b || message == "/"+command+"@"+setting.telegramBotName
b = b || strings.HasPrefix(message, "/"+command+" ") b = b || strings.HasPrefix(message, "/"+command+" ")
b = b || message == "/"+command b = b || message == "/"+command
return b return b

View File

@ -0,0 +1,43 @@
package main
import (
"testing"
)
func doTestTelegramIsCommand(t *testing.T, message string, command string, expected bool) {
if telegramIsCommand(message, command) != expected {
t.Errorf("telegramIsCommand(\"%s\", \"%s\") unexpected result", message, command)
}
}
func TestTelegramIsCommand(t *testing.T) {
setting.telegramBotName = "test_bot"
// Recognize command
doTestTelegramIsCommand(t, "/trace", "trace", true)
doTestTelegramIsCommand(t, "/trace", "trace1234", false)
doTestTelegramIsCommand(t, "/trace", "tra", false)
doTestTelegramIsCommand(t, "/trace", "abcdefg", false)
// Recognize command with parameters
doTestTelegramIsCommand(t, "/trace google.com", "trace", true)
doTestTelegramIsCommand(t, "/trace google.com", "trace1234", false)
doTestTelegramIsCommand(t, "/trace google.com", "tra", false)
doTestTelegramIsCommand(t, "/trace google.com", "abcdefg", false)
// Recognize command with bot name
doTestTelegramIsCommand(t, "/trace@test_bot", "trace", true)
doTestTelegramIsCommand(t, "/trace@test_bot", "trace1234", false)
doTestTelegramIsCommand(t, "/trace@test_bot", "tra", false)
doTestTelegramIsCommand(t, "/trace@test_bot", "abcdefg", false)
doTestTelegramIsCommand(t, "/trace@test_bot_123", "trace", false)
doTestTelegramIsCommand(t, "/trace@test_", "trace", false)
// Recognize command with bot name and parameters
doTestTelegramIsCommand(t, "/trace@test_bot google.com", "trace", true)
doTestTelegramIsCommand(t, "/trace@test_bot google.com", "trace1234", false)
doTestTelegramIsCommand(t, "/trace@test_bot google.com", "tra", false)
doTestTelegramIsCommand(t, "/trace@test_bot google.com", "abcdefg", false)
doTestTelegramIsCommand(t, "/trace@test_bot_123 google.com", "trace", false)
doTestTelegramIsCommand(t, "/trace@test google.com", "trace", false)
}