From 91e3ca5da9c850d177b75f9fb3d57449fc1f710c Mon Sep 17 00:00:00 2001 From: Igor Baliuk Date: Fri, 14 Aug 2020 00:36:26 +0300 Subject: [PATCH] Allow multiple admins' Telegram IDs --- README.md | 6 ++++++ app/bytes_uploader.go | 2 +- app/format.go | 2 +- app/handler.go | 37 +++++++++++++++++++++++-------------- app/logic.go | 14 +++++++------- app/parse_addr.go | 2 +- app/parse_addr_test.go | 8 ++++---- app/to_str.go | 2 +- conf/config.go | 4 ++-- main.go | 10 +++++----- 10 files changed, 51 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 5d21ac7..3d67317 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,12 @@ docker run -d \ --env BOT_TOKEN=1231231231:AAAAAAAAABBBBCCCCCCCCCCCCCC \ arthurwow/ovpnbot ``` +or +```bash +docker run -d --name ovpn-tg-bot --volume /var/run/docker.sock:/var/run/docker.sock:ro --env ADMIN_TELEGRAM_ID=123456789 --env BOT_TOKEN=1231231231:AAAAAAAAABBBBCCCCCCCCCCCCCC arthurwow/ovpnbot +``` + +**ADMIN_TELEGRAM_ID** is a comma-separated list of the bot admins' Telegram IDs ### Proxy diff --git a/app/bytes_uploader.go b/app/bytes_uploader.go index 8f50ee8..d7b5039 100644 --- a/app/bytes_uploader.go +++ b/app/bytes_uploader.go @@ -13,7 +13,7 @@ type BytesUploader struct { func NewBytesUploader(name string, b []byte) *BytesUploader { return &BytesUploader{ name: name, - b: b, + b: b, } } diff --git a/app/format.go b/app/format.go index 19b968e..c0ae149 100644 --- a/app/format.go +++ b/app/format.go @@ -14,4 +14,4 @@ func formatContainer(c types.Container) string { strings.Join(c.Names, ":"), c.Status, ) -} \ No newline at end of file +} diff --git a/app/handler.go b/app/handler.go index eb4d11e..1f6aa2a 100644 --- a/app/handler.go +++ b/app/handler.go @@ -2,23 +2,23 @@ package app import ( "fmt" - "github.com/petuhovskiy/telegram" "strings" - "github.com/rwlist/ovpn-bot/conf" + "github.com/petuhovskiy/telegram" ) type Handler struct { - bot *telegram.Bot + bot *telegram.Bot logic *Logic - cfg *conf.Struct + + adminIDs []int } -func NewHandler(bot *telegram.Bot, logic *Logic, cfg *conf.Struct) *Handler { +func NewHandler(bot *telegram.Bot, logic *Logic, adminIDs []int) *Handler { return &Handler{ - bot: bot, - logic: logic, - cfg: cfg, + bot: bot, + logic: logic, + adminIDs: adminIDs, } } @@ -28,7 +28,16 @@ func (h *Handler) Handle(upd telegram.Update) { } msg := upd.Message - if msg.From.ID != h.cfg.AdminID { + + isAdmin := false + for _, adminID := range h.adminIDs { + if msg.From.ID == adminID { + isAdmin = true + break + } + } + + if !isAdmin { return } @@ -37,8 +46,8 @@ func (h *Handler) Handle(upd telegram.Update) { func (h *Handler) sendMessage(chatID int, text string) { _, _ = h.bot.SendMessage(&telegram.SendMessageRequest{ - ChatID: str(chatID), - Text: text, + ChatID: str(chatID), + Text: text, }) } @@ -124,8 +133,8 @@ func (h *Handler) commandGenerate(chatID int, profileName string) { } _, _ = h.bot.SendDocument(&telegram.SendDocumentRequest{ - ChatID: str(chatID), - Document: NewBytesUploader(profileName + ".ovpn", res), + ChatID: str(chatID), + Document: NewBytesUploader(profileName+".ovpn", res), }) } @@ -149,4 +158,4 @@ func (h *Handler) commandHelp(chatID int) { /remove remove everything` h.sendMessage(chatID, str) -} \ No newline at end of file +} diff --git a/app/logic.go b/app/logic.go index 470f2ea..e66aa7a 100644 --- a/app/logic.go +++ b/app/logic.go @@ -15,8 +15,8 @@ const ( prefix = "ovpn_" data = "data" - tcp = "tcp" - udp = "udp" + tcp = "tcp" + udp = "udp" ) type Logic struct { @@ -53,7 +53,7 @@ func (l *Logic) CommandInit(w io.Writer, addr string) error { return err } - dataMount := dataVolume+":/etc/openvpn" + dataMount := dataVolume + ":/etc/openvpn" // docker run -v $OVPN_DATA:/etc/openvpn --rm kylemanna/openvpn ovpn_genconfig -u $(PROTO)://$(HOST):$(PORT) err = l.execute(w, []string{"docker", "run", "-v", dataMount, "--rm", "kylemanna/openvpn", "ovpn_genconfig", "-u", addr}) @@ -72,13 +72,13 @@ func (l *Logic) CommandInit(w io.Writer, addr string) error { tcpContainer := prefix + tcp // docker run -v $OVPN_DATA:/etc/openvpn -d --restart=always --name $(NAME)_udp -p $(PORT):1194/udp --cap-add=NET_ADMIN kylemanna/openvpn ovpn_run --proto udp - err = l.execute(w, []string{"docker", "run", "-v", dataMount, "-d", "--restart=always", "--name", udpContainer, "-p", port+":1194/udp", "--cap-add=NET_ADMIN", "kylemanna/openvpn", "ovpn_run", "--proto", "udp"}) + err = l.execute(w, []string{"docker", "run", "-v", dataMount, "-d", "--restart=always", "--name", udpContainer, "-p", port + ":1194/udp", "--cap-add=NET_ADMIN", "kylemanna/openvpn", "ovpn_run", "--proto", "udp"}) if err != nil { return err } // docker run -v $OVPN_DATA:/etc/openvpn -d --restart=always --name $(NAME)_tcp -p $(PORT):1194/tcp --cap-add=NET_ADMIN kylemanna/openvpn ovpn_run --proto tcp - err = l.execute(w, []string{"docker", "run", "-v", dataMount, "-d", "--restart=always", "--name", tcpContainer, "-p", port+":1194/tcp", "--cap-add=NET_ADMIN", "kylemanna/openvpn", "ovpn_run", "--proto", "tcp"}) + err = l.execute(w, []string{"docker", "run", "-v", dataMount, "-d", "--restart=always", "--name", tcpContainer, "-p", port + ":1194/tcp", "--cap-add=NET_ADMIN", "kylemanna/openvpn", "ovpn_run", "--proto", "tcp"}) if err != nil { return err } @@ -143,7 +143,7 @@ func (l *Logic) CommandStatus() (string, error) { func (l *Logic) CommandGenerate(w *botWriter, profileName string) ([]byte, error) { dataVolume := prefix + data - dataMount := dataVolume+":/etc/openvpn" + dataMount := dataVolume + ":/etc/openvpn" // docker run -v ovpn_data:/etc/openvpn --rm -i kylemanna/openvpn easyrsa build-client-full client_name nopass err := l.execute(w, []string{"docker", "run", "-v", dataMount, "--rm", "-i", "kylemanna/openvpn", "easyrsa", "build-client-full", profileName, "nopass"}) @@ -180,4 +180,4 @@ func (l *Logic) execute3(w io.Writer, args []string, stdin io.Reader) error { err := cmd.Run() return err -} \ No newline at end of file +} diff --git a/app/parse_addr.go b/app/parse_addr.go index ba3c7dc..fe3104f 100644 --- a/app/parse_addr.go +++ b/app/parse_addr.go @@ -11,4 +11,4 @@ func parseAddr(addr string) (proto string, host string, port string, ok bool) { } return match[1], match[2], match[3], true -} \ No newline at end of file +} diff --git a/app/parse_addr_test.go b/app/parse_addr_test.go index 6d93835..84e92b9 100644 --- a/app/parse_addr_test.go +++ b/app/parse_addr_test.go @@ -8,10 +8,10 @@ import ( func TestParseAddr(t *testing.T) { type test struct { - in string + in string proto string - host string - port string + host string + port string } cases := []test{ @@ -26,4 +26,4 @@ func TestParseAddr(t *testing.T) { assert.Equal(t, c.port, port) assert.Equal(t, true, ok) } -} \ No newline at end of file +} diff --git a/app/to_str.go b/app/to_str.go index 9212848..833948c 100644 --- a/app/to_str.go +++ b/app/to_str.go @@ -4,4 +4,4 @@ import "strconv" func str(id int) string { return strconv.Itoa(id) -} \ No newline at end of file +} diff --git a/conf/config.go b/conf/config.go index 13559a6..dd710e4 100644 --- a/conf/config.go +++ b/conf/config.go @@ -5,7 +5,7 @@ import ( ) type Struct struct { - AdminID int `env:"ADMIN_TELEGRAM_ID"` + AdminIDs []int `env:"ADMIN_TELEGRAM_ID" envSeparator:","` // Comma-separated list of the bot admins' Telegram IDs BotToken string `env:"BOT_TOKEN"` } @@ -17,4 +17,4 @@ func ParseEnv() (*Struct, error) { } return &cfg, nil -} \ No newline at end of file +} diff --git a/main.go b/main.go index c58cbb8..1ad295f 100644 --- a/main.go +++ b/main.go @@ -36,18 +36,18 @@ func main() { }) ch, err := updates.StartPolling(bot, telegram.GetUpdatesRequest{ - Offset: 0, - Limit: 50, - Timeout: 10, + Offset: 0, + Limit: 50, + Timeout: 10, }) if err != nil { log.Fatal(err) } l := app.NewLogic(dockerClient) - h := app.NewHandler(bot, l, cfg) + h := app.NewHandler(bot, l, cfg.AdminIDs) for upd := range ch { h.Handle(upd) } -} \ No newline at end of file +}