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

Allow multiple admins' Telegram IDs #4

Merged
merged 1 commit into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion app/bytes_uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type BytesUploader struct {
func NewBytesUploader(name string, b []byte) *BytesUploader {
return &BytesUploader{
name: name,
b: b,
b: b,
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ func formatContainer(c types.Container) string {
strings.Join(c.Names, ":"),
c.Status,
)
}
}
37 changes: 23 additions & 14 deletions app/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand All @@ -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
}

Expand All @@ -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,
})
}

Expand Down Expand Up @@ -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),
})
}

Expand All @@ -149,4 +158,4 @@ func (h *Handler) commandHelp(chatID int) {
/remove remove everything`

h.sendMessage(chatID, str)
}
}
14 changes: 7 additions & 7 deletions app/logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const (
prefix = "ovpn_"

data = "data"
tcp = "tcp"
udp = "udp"
tcp = "tcp"
udp = "udp"
)

type Logic struct {
Expand Down Expand Up @@ -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})
Expand All @@ -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
}
Expand Down Expand Up @@ -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"})
Expand Down Expand Up @@ -180,4 +180,4 @@ func (l *Logic) execute3(w io.Writer, args []string, stdin io.Reader) error {

err := cmd.Run()
return err
}
}
2 changes: 1 addition & 1 deletion app/parse_addr.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ func parseAddr(addr string) (proto string, host string, port string, ok bool) {
}

return match[1], match[2], match[3], true
}
}
8 changes: 4 additions & 4 deletions app/parse_addr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -26,4 +26,4 @@ func TestParseAddr(t *testing.T) {
assert.Equal(t, c.port, port)
assert.Equal(t, true, ok)
}
}
}
2 changes: 1 addition & 1 deletion app/to_str.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ import "strconv"

func str(id int) string {
return strconv.Itoa(id)
}
}
4 changes: 2 additions & 2 deletions conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand All @@ -17,4 +17,4 @@ func ParseEnv() (*Struct, error) {
}

return &cfg, nil
}
}
10 changes: 5 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}