frontend: limit telegram commands by bot name
This commit is contained in:
parent
bda06ddd5e
commit
7c7814cc7b
@ -21,19 +21,21 @@ type settingType struct {
|
||||
netSpecificMode string
|
||||
titleBrand string
|
||||
navBarBrand string
|
||||
telegramBotName string
|
||||
}
|
||||
|
||||
var setting settingType
|
||||
|
||||
func main() {
|
||||
var settingDefault = settingType{
|
||||
servers: []string{""},
|
||||
proxyPort: 8000,
|
||||
whoisServer: "whois.verisign-grs.com",
|
||||
listen: ":5000",
|
||||
dnsInterface: "asn.cymru.com",
|
||||
titleBrand: "Bird-lg Go",
|
||||
navBarBrand: "Bird-lg Go",
|
||||
servers: []string{""},
|
||||
proxyPort: 8000,
|
||||
whoisServer: "whois.verisign-grs.com",
|
||||
listen: ":5000",
|
||||
dnsInterface: "asn.cymru.com",
|
||||
titleBrand: "Bird-lg Go",
|
||||
navBarBrand: "Bird-lg Go",
|
||||
telegramBotName: "",
|
||||
}
|
||||
|
||||
if env := os.Getenv("BIRDLG_SERVERS"); env != "" {
|
||||
@ -67,6 +69,9 @@ func main() {
|
||||
if env := os.Getenv("BIRDLG_NAVBAR_BRAND"); 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")
|
||||
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]")
|
||||
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")
|
||||
telegramBotNamePtr := flag.String("telegram-bot-name", settingDefault.navBarBrand, "telegram bot name (used to filter @bot commands)")
|
||||
flag.Parse()
|
||||
|
||||
if *serversPtr == "" {
|
||||
@ -91,7 +97,7 @@ func main() {
|
||||
pos := strings.Index(server, "<")
|
||||
if pos != -1 {
|
||||
serversDisplay[i] = server[0:pos]
|
||||
servers[i] = server[pos+1:len(server)-1]
|
||||
servers[i] = server[pos+1 : len(server)-1]
|
||||
}
|
||||
}
|
||||
|
||||
@ -106,6 +112,7 @@ func main() {
|
||||
strings.ToLower(*netSpecificModePtr),
|
||||
*titleBrandPtr,
|
||||
*navBarBrandPtr,
|
||||
*telegramBotNamePtr,
|
||||
}
|
||||
|
||||
ImportTemplates()
|
||||
|
@ -31,7 +31,8 @@ type tgWebhookResponse struct {
|
||||
|
||||
func telegramIsCommand(message string, command string) bool {
|
||||
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 || message == "/"+command
|
||||
return b
|
||||
|
43
frontend/telegram_bot_test.go
Normal file
43
frontend/telegram_bot_test.go
Normal 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)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user