-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
180 lines (153 loc) · 4.44 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
)
const (
APIKey = "o1zrmHAF"
)
// OptimizationIP represents an individual IP entry in the API response
type OptimizationIP struct {
Colo string `json:"colo"`
IP string `json:"ip"`
Latency int `json:"latency"`
Line string `json:"line"`
Loss int `json:"loss"`
Node string `json:"node"`
Speed int `json:"speed"`
Time string `json:"time"`
}
// OptimizationIPResponse represents the full API response structure
type OptimizationIPResponse struct {
Code int `json:"code"`
Total int `json:"total"`
Info map[string][]OptimizationIP `json:"info"`
}
// getOptimizationIP sends a POST request to fetch the optimization IPs
func getOptimizationIP(ipType string) (*OptimizationIPResponse, error) {
url := "https://api.hostmonit.com/get_optimization_ip"
data := map[string]string{
"key": APIKey,
"type": ipType,
}
jsonData, err := json.Marshal(data)
if err != nil {
return nil, fmt.Errorf("failed to marshal data: %v", err)
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return nil, fmt.Errorf("failed to create request: %v", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to send request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %v", err)
}
var response OptimizationIPResponse
if err := json.Unmarshal(body, &response); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %v", err)
}
return &response, nil
}
func sendMessageToTelegram(TelegramBotToken, chatID, message, imageURL string) error {
url := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", TelegramBotToken)
data := map[string]interface{}{
"chat_id": chatID,
"text": message,
"parse_mode": "MarkdownV2", // Enable MarkdownV2 formatting
"photo": imageURL, // Add the image URL
}
jsonData, err := json.Marshal(data)
if err != nil {
return fmt.Errorf("failed to marshal Telegram message data: %v", err)
}
resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonData))
if err != nil {
return fmt.Errorf("failed to send message to Telegram: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("unexpected status code: %d, response: %s", resp.StatusCode, body)
}
fmt.Print(resp)
return nil
}
func main() {
imageURL := "images.png"
TelegramBotToken, ok := os.LookupEnv("BOT_TOKEN")
if !ok {
fmt.Printf("BOT_TOKEN not set\n")
}
TelegramChatID, ok := os.LookupEnv("CHAT_ID")
if !ok {
fmt.Printf("CHAT_ID not set\n")
}
ipv4Response, err := getOptimizationIP("v4")
if err != nil {
fmt.Printf("Error getting IPv4: %v\n", err)
return
}
ipv4 := []string{}
if ipv4Response.Code == 200 && ipv4Response.Total > 0 {
for _, region := range ipv4Response.Info {
for _, ip := range region {
ipv4 = append(ipv4, ip.IP)
}
}
}
ipv6Response, err := getOptimizationIP("v6")
if err != nil {
fmt.Printf("Error getting IPv6: %v\n", err)
return
}
ipv6 := []string{}
if ipv6Response.Code == 200 && ipv6Response.Total > 0 {
for _, region := range ipv6Response.Info {
for _, ip := range region {
ipv6 = append(ipv6, ip.IP)
}
}
}
if len(ipv4) > 0 || len(ipv6) > 0 {
message := "CloudFlare Optimized IPs:\n\n"
if len(ipv4) > 0 {
message += "IPv4:\n" + formatIPs(ipv4[:min(len(ipv4), 25)]) + "\n\n"
}
if len(ipv6) > 0 {
message += "IPv6:\n" + formatIPs(ipv6[:min(len(ipv6), 25)]) + "\n\n"
}
message += "@cloudflare2tg"
if err := sendMessageToTelegram(TelegramBotToken, TelegramChatID, message, imageURL); err != nil {
fmt.Printf("Error sending message to Telegram: %v\n", err)
}
}
}
func formatIPs(ips []string) string {
formattedIPs := make([]string, len(ips))
for i, ip := range ips {
formattedIPs[i] = "`" + ip + "`"
}
return strings.Join(formattedIPs, "\n")
}
func min(a, b int) int {
if a < b {
return a
}
return b
}