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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
package main
import (
"context"
"flag"
"fmt"
"net"
"net/http"
"os"
"strconv"
"time"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/rs/zerolog/log"
pb "github.com/terminaldweller/grpc/telebot/v1"
"golang.org/x/net/proxy"
"google.golang.org/grpc"
)
var botChannelID = flag.Int64(
"botchannelid",
146328407, //nolint: mnd,gomnd
"determines the channel id the telgram bot should send messages to")
const (
telegramBotTokenEnvVar = "TELEGRAM_BOT_TOKEN" //nolint: gosec
httpClientTimeout = 5
)
type server struct {
pb.UnimplementedNotificationServiceServer
}
func GetProxiedClient() (*http.Client, error) {
var isProxied bool
proxyURL := os.Getenv("ALL_PROXY")
if proxyURL == "" {
proxyURL = os.Getenv("HTTPS_PROXY")
}
if proxyURL == "" {
isProxied = false
}
var dialerProxy proxy.Dialer
var dialer net.Dialer
var err error
if isProxied {
dialerProxy, err = proxy.SOCKS5("tcp", proxyURL, nil, proxy.Direct)
if err != nil {
return nil, fmt.Errorf("[GetProxiedClient] : %w", err)
}
} else {
dialer = net.Dialer{
Timeout: 5 * time.Second, //nolint: mnd,gomnd
}
if err != nil {
return nil, fmt.Errorf("[GetProxiedClient] : %w", err)
}
}
dialContext := func(ctx context.Context, network, address string) (net.Conn, error) {
if isProxied {
netConn, err := dialerProxy.Dial(network, address)
if err == nil {
return netConn, nil
}
return netConn, fmt.Errorf("[dialContext] : %w", err)
} else {
netConn, err := dialer.Dial(network, address)
if err == nil {
return netConn, nil
}
return netConn, fmt.Errorf("[dialContext] : %w", err)
}
}
transport := &http.Transport{
DialContext: dialContext,
DisableKeepAlives: true,
}
client := &http.Client{
Transport: transport,
Timeout: httpClientTimeout * time.Second,
CheckRedirect: nil,
Jar: nil,
}
return client, nil
}
func getTGBot() *tgbotapi.BotAPI {
token := os.Getenv(telegramBotTokenEnvVar)
// client, err := GetProxiedClient()
// if err != nil {
// log.Fatal().Err(err)
// }
// bot, err := tgbotapi.NewBotAPIWithClient(token[1:len(token)-1], client)
bot, err := tgbotapi.NewBotAPI(token[1 : len(token)-1])
if err != nil {
log.Fatal().Err(err).Send()
}
return bot
}
func sendMessage(bot *tgbotapi.BotAPI, msgText string, channelID int64) error {
msg := tgbotapi.NewMessage(channelID, msgText)
_, err := bot.Send(msg)
return err
}
func (s *server) Notify(
ctx context.Context,
NotificationRequest *pb.NotificationRequest,
) (*pb.NotificationResponse, error) {
var err error
tgbotapi := getTGBot()
if NotificationRequest.ChannelId == 0 {
err = sendMessage(tgbotapi, NotificationRequest.NotificationText, *botChannelID)
} else {
err = sendMessage(tgbotapi, NotificationRequest.NotificationText, NotificationRequest.ChannelId)
}
if err != nil {
return &pb.NotificationResponse{Error: err.Error(), IsOK: false}, err
}
return &pb.NotificationResponse{Error: "", IsOK: true}, nil
}
func startServer(port uint16) {
listener, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", port))
if err != nil {
log.Fatal().Err(err).Send()
}
var opts []grpc.ServerOption
grpcServer := grpc.NewServer(opts...)
pb.RegisterNotificationServiceServer(grpcServer, &server{})
if err := grpcServer.Serve(listener); err != nil {
log.Fatal().Err(err).Send()
}
}
func main() {
flagPort := flag.String("port", "8000", "determines the port the service runs on")
flag.Parse()
port, _ := strconv.Atoi(*flagPort)
startServer(uint16(port))
}
|