-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
154 lines (129 loc) · 4.6 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
package main
import (
"net/http"
"fmt"
"time"
"flag"
)
type Domain struct {
ID int `json:"id"`
Name string `json:"name"`
Token string `json:"token"`
State string `json:"state"`
IpV4Address string `json:"ipv4Address"`
LastUpdate string `json:"updatedOn"`
Group string `json:"group"`
TTL int `json:"ttl"`
Ipv4 bool `json:"Ipv4"`
Ipv6 bool `json:"ipv6"`
Ipv4WildcardAlias bool `json:"ipv4WildcardAlias"`
}
type UpdateDomainIPRequest struct {
Name string `json:"name"`
Group string `json:"group"`
Ipv4Address string `json:"ipv4Address"`
Ipv6Address string `json:"ipv6Address"`
TTL int `json:"ttl"`
Ipv4 bool `json:"ipv4"`
Ipv6 bool `json:"ipv6"`
Ipv4WildcardAlias bool `json:"ipv4WildcardAlias"`
Ipv6WildcardAlias bool `json:"ipv6WildcardAlias"`
AllowZoneTransfer bool `json:"allowZoneTransfer"`
DnsSec bool `json:"dnssec"`
}
type DnsRequestResponse struct {
StatusCode int `json:"statusCode"`
Domains []Domain `json:"domains"`
}
type ErrorResponse struct {
StatusCode int `json:"statusCode"`
Type string `json:"type"`
Message string `json:"message"`
}
func main() {
var checkDomainName string
var apiKey string
var port int
var checkInterval int
flag.StringVar(&checkDomainName, "domain", "", "check domain name to change ip address if blocked!")
flag.StringVar(&apiKey, "apikey", "", "dynu api key, check : https://www.dynu.com/ControlPanel/APICredentials")
flag.IntVar(&port, "port", 0, "your port to check")
flag.IntVar(&checkInterval, "interval", 30, "check domain ip is blocked time in seconds")
flag.Parse()
if checkDomainName == "" {
fmt.Println(fmt.Sprintf("you should enter -domain , a domain name to check blocked or not , if blocked change ip address"))
return
}
if apiKey == "" {
fmt.Println(fmt.Sprintf("you should enter -apikey , if not have a api key check : https://www.dynu.com/ControlPanel/APICredentials"))
return
}
if port == 0 {
fmt.Println(fmt.Sprintf("you should enter -port , your bot port"))
return
}
client := &http.Client{}
for {
fmt.Println("Checking internet connection...")
connected := checkInternetConnection()
if !connected {
fmt.Println("Check your internet connection, this application can't work without internet connection.")
fmt.Println("------------------------------------------------")
time.Sleep(time.Duration(checkInterval) * time.Second)
continue
}
fmt.Println("Getting registered domain list...")
domainList, err := getDomainList(client, apiKey)
if err != nil {
handleError(GetDomainList, err)
fmt.Println("------------------------------------------------")
time.Sleep(time.Duration(checkInterval) * time.Second)
continue
}
if len(domainList.Domains) == 0 {
fmt.Println("You are not registered a domain in dynu! , cannot continue with empty domain list in dynu panel!")
return
}
var domain *Domain
for _, o := range domainList.Domains {
if o.Name == checkDomainName {
domain = &o
break
}
}
if domain == nil {
fmt.Println(fmt.Sprintf("%s domain name not exist in your dynu panel! exmaple domain name : %s", checkDomainName, "workplace.mywire.org"))
return
}
if domain.IpV4Address == "" {
fmt.Println(fmt.Sprintf("%s have no ipV4Address! please set a ip to domain in dynu panel", checkDomainName))
return
}
fmt.Println(fmt.Sprintf("Checking %s:%d is blocked or not!", domain.IpV4Address, port))
blocked := checkIPAddressIsBlocked(domain.IpV4Address, port)
if blocked {
if checkInternetConnection() {
fmt.Println(fmt.Sprintf("this ip %s:%d is blocked!", domain.IpV4Address, port))
newIP, err := getNewIPAddress()
if err != nil {
handleError(GetNewIPFromFile, err)
return
}
err = updateDomainIP(client, apiKey, domain, newIP)
if err != nil {
handleError(UpdateDomainIP, err)
return
}
fmt.Println(fmt.Sprintf("Update domain ip is successufly , %s domain's new ip is %s", checkDomainName, newIP))
} else {
fmt.Println("Check your internet connection, this application can't work without internet connection.")
fmt.Println("------------------------------------------------")
time.Sleep(time.Duration(checkInterval) * time.Second)
}
} else {
fmt.Println(fmt.Sprintf("Everything is ok! your ip : %s not blocked", domain.IpV4Address))
}
fmt.Println("------------------------------------------------")
time.Sleep(time.Duration(checkInterval) * time.Second)
}
}