-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
140 lines (120 loc) · 3.43 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"regexp"
"strings"
log "github.com/sirupsen/logrus"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
var (
ServiceURL string
ServiceUserName string
ServicePassword string
TelegramToken string
)
type ErrorMessage struct {
URL []string `json:"url"`
}
func main() {
ServiceURL = os.Getenv("SERVICE_URL")
ServiceUserName = os.Getenv("SERVICE_USERNAME")
ServicePassword = os.Getenv("SERVICE_PASSWORD")
TelegramToken = os.Getenv("TELEGRAM_TOKEN")
if ServiceURL == "" || ServiceUserName == "" || ServicePassword == "" || TelegramToken == "" {
log.Fatal("missing data from environment")
}
bot, err := tgbotapi.NewBotAPI(TelegramToken)
if err != nil {
log.Fatal(err)
}
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 5
updates := bot.GetUpdatesChan(u)
for update := range updates {
if update.Message.IsCommand() {
continue
}
if update.Message != nil {
log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
servers := findServers(update.Message.Text)
if len(servers) == 0 {
servers = findServers(update.Message.Caption)
}
if len(servers) > 0 {
reply(update, bot, fmt.Sprintf("I found %d servers in this message.", len(servers)))
replyText := make([]string, len(servers))
for i, server := range servers {
err := addServer(server)
if err != nil {
replyText[i] = fmt.Sprintf("Error: %v\n", err)
} else {
replyText[i] = fmt.Sprintf("Added server %s\n", server)
}
log.Info(replyText[i])
}
for _, replyStr := range replyText {
reply(update, bot, replyStr)
}
} else {
if update.Message.Chat.Type == "private" {
reply(update, bot, fmt.Sprintf("I could not find any servers in this message"))
}
}
}
}
}
func reply(update tgbotapi.Update, bot *tgbotapi.BotAPI, reply string) {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, reply)
msg.ReplyToMessageID = update.Message.MessageID
_, err := bot.Send(msg)
if err != nil {
log.Printf("error sending reply %v", err)
}
}
func findServers(input string) []string {
servers := []string{}
r, err := regexp.Compile("(\n+\\s*|\\s+)(ss://[A-Za-z0-9]+=*@.+:\\d+|ss://[A-Za-z0-9]+)")
if err != nil {
log.Printf("error building RE %v", err)
return nil
}
for _, address := range r.FindAllString(fmt.Sprintf("\n%s", input), -1) {
servers = append(servers, strings.TrimSpace(address))
}
return servers
}
func addServer(server string) error {
log.Infof("Adding server %s", server)
client := &http.Client{}
var data = strings.NewReader(fmt.Sprintf("url=%s", server))
req, err := http.NewRequest("POST", fmt.Sprintf("%s/api/proxies/", ServiceURL), data)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("User-Agent", "shadowmere-helper-bot")
req.SetBasicAuth(ServiceUserName, ServicePassword)
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != http.StatusCreated {
errors := ErrorMessage{}
err := json.Unmarshal(body, &errors)
if err != nil {
return fmt.Errorf("request failed with code %d.\nError message is not in the expected format", resp.StatusCode)
}
return fmt.Errorf("could not add key %s because %s", server, errors.URL)
}
return nil
}