-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
255 lines (220 loc) · 7.12 KB
/
main.go
File metadata and controls
255 lines (220 loc) · 7.12 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package main
import (
"context"
"crypto/tls"
"flag"
"fmt"
"net"
"net/http"
"os"
"strings"
"time"
)
// Cloudflare IP ranges (IPv4 & IPv6)
var cloudflareIPRanges = []string{
// IPv4
"173.245.48.0/20", "103.21.244.0/22", "103.22.200.0/22", "103.31.4.0/22",
"141.101.64.0/18", "108.162.192.0/18", "190.93.240.0/20", "188.114.96.0/20",
"197.234.240.0/22", "198.41.128.0/17", "162.158.0.0/15", "104.16.0.0/13",
"104.24.0.0/14", "172.64.0.0/13", "131.0.72.0/22",
// IPv6
"2400:cb00::/32", "2606:4700::/32", "2803:f800::/32", "2405:b500::/32",
"2405:8100::/32", "2a06:98c0::/29", "2c0f:f248::/32",
}
func isCloudflareIP(ipStr string) bool {
ip := net.ParseIP(ipStr)
if ip == nil {
return false
}
for _, cidr := range cloudflareIPRanges {
_, subnet, _ := net.ParseCIDR(cidr)
if subnet.Contains(ip) {
return true
}
}
return false
}
func main() {
flag.Usage = func() {
fmt.Printf("🚀 Domain Checker CLI - Hubungi kami di opendesa.id jika ada kendala\n\n")
fmt.Printf("PENGGUNAAN:\n")
fmt.Printf(" checkdomain -domain <domain> [opsi lainnya]\n\n")
fmt.Printf("OPSI:\n")
fmt.Print(" -domain string\n")
fmt.Print(" Nama Domain yang ingin dicek (contoh: google.com atau web.desa.id)\n")
fmt.Print(" -origin string\n")
fmt.Print(" Alamat IP server asli (Origin IP) untuk verifikasi pointing dan SSL asli\n")
fmt.Printf("\nCONTOH:\n")
fmt.Printf(" checkdomain -domain google.com\n")
fmt.Printf(" checkdomain -domain opendesa.id -origin 103.154.142.172\n\n")
fmt.Printf("FITUR:\n")
fmt.Printf(" ✅ Cek status dan masa berlaku SSL (HTTPs)\n")
fmt.Printf(" 🌐 Cek IP Pointing (A/AAAA Records) dengan label Cloudflare\n")
fmt.Printf(" ☁️ Deteksi status Proxy Cloudflare (Orange Cloud)\n")
fmt.Printf(" 🎯 Verifikasi Origin IP (memastikan pointing Cloudflare benar ke server Anda)\n")
}
domain := flag.String("domain", "", "Domain to check")
expectedOrigin := flag.String("origin", "", "Expected origin IP")
flag.Parse()
if *domain == "" {
flag.Usage()
os.Exit(0)
}
fmt.Printf("\n🔍 Checking domain: %s\n", *domain)
fmt.Println(strings.Repeat("-", 40))
// 1. SSL Check
checkSSL(*domain)
// 2. DNS Pointing Check
ips := checkDNS(*domain)
// 3. Cloudflare Check
isCF := checkCloudflare(*domain, ips)
// 4. Origin Verification
if *expectedOrigin != "" {
verifyOrigin(*domain, *expectedOrigin, isCF)
} else if isCF {
fmt.Println("💡 Penjelasan: Domain ini di-proxy oleh Cloudflare.")
fmt.Println(" Untuk memastikan pointing benar, jalankan dengan -origin <IP_SERVER_ANDA>")
}
fmt.Println(strings.Repeat("-", 40))
}
func checkSSL(domain string) {
fmt.Print("🔒 SSL Status: ")
// Use port 443 for SSL check
conn, err := tls.DialWithDialer(&net.Dialer{Timeout: 5 * time.Second}, "tcp", domain+":443", &tls.Config{InsecureSkipVerify: true})
if err != nil {
fmt.Printf("❌ Failed to connect: %v\n", err)
return
}
defer conn.Close()
if len(conn.ConnectionState().PeerCertificates) == 0 {
fmt.Println("❌ No certificates found")
return
}
cert := conn.ConnectionState().PeerCertificates[0]
expiry := cert.NotAfter
now := time.Now()
fmt.Printf("✅ Active (Expires on %s)\n", expiry.Format("2006-01-02"))
fmt.Printf(" 📝 Subject: %s\n", cert.Subject.CommonName)
fmt.Printf(" 🏢 Issuer: %s\n", cert.Issuer.Organization[0])
if now.After(expiry) {
fmt.Printf(" 🔴 STATUS: EXPIRED (since %s)\n", expiry.Format("2006-01-02"))
} else {
daysRemaining := int(expiry.Sub(now).Hours() / 24)
fmt.Printf(" ⏳ Sisa: %d hari\n", daysRemaining)
}
}
func checkDNS(domain string) []string {
fmt.Print("🌐 DNS Pointing: \n")
ips, err := net.LookupIP(domain)
if err != nil {
fmt.Printf(" ❌ Failed to lookup IP: %v\n", err)
return nil
}
var ipStrings []string
for _, ip := range ips {
ipStr := ip.String()
label := "(Direct/Server IP)"
if isCloudflareIP(ipStr) {
label = "(Cloudflare IP)"
}
fmt.Printf(" - %-40s %s\n", ipStr, label)
ipStrings = append(ipStrings, ipStr)
}
return ipStrings
}
func checkCloudflare(domain string, ips []string) bool {
fmt.Print("☁️ Cloudflare: ")
// Method 1: Check IP ranges
isProxiedByIP := false
for _, ip := range ips {
if isCloudflareIP(ip) {
isProxiedByIP = true
break
}
}
// Method 2: Check Headers
isProxiedByHeader := false
client := &http.Client{Timeout: 5 * time.Second}
resp, err := client.Get("http://" + domain)
if err == nil {
defer resp.Body.Close()
if resp.Header.Get("CF-RAY") != "" || strings.ToLower(resp.Header.Get("Server")) == "cloudflare" {
isProxiedByHeader = true
}
}
if isProxiedByIP || isProxiedByHeader {
fmt.Println("🟠 Proxied (Orange Cloud Active)")
return true
}
fmt.Println("⚪ Not Proxied")
return false
}
func verifyOrigin(domain, originIP string, isProxied bool) {
fmt.Printf("🎯 Origin Verification [%s]: \n", originIP)
// 1. Check HTTP Connection
fmt.Print(" 🌐 HTTP Status: ")
client := &http.Client{
Timeout: 10 * time.Second,
Transport: &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
return (&net.Dialer{}).DialContext(ctx, network, originIP+":80")
},
},
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
req, _ := http.NewRequest("GET", "http://"+domain, nil)
resp, err := client.Do(req)
if err != nil {
fmt.Printf("❌ HTTP failed: %v\n", err)
} else {
defer resp.Body.Close()
if resp.StatusCode >= 200 && resp.StatusCode < 500 {
fmt.Printf("✅ HTTP OK (Origin is replying)\n")
} else {
fmt.Printf("⚠️ HTTP Status %d\n", resp.StatusCode)
}
}
// 2. Check Origin SSL (on port 443)
fmt.Print(" 🔒 Origin SSL: ")
conf := &tls.Config{
ServerName: domain,
InsecureSkipVerify: true,
}
conn, err := tls.DialWithDialer(&net.Dialer{Timeout: 5 * time.Second}, "tcp", originIP+":443", conf)
if err != nil {
fmt.Printf("❌ Failed to connect: %v\n", err)
return
}
defer conn.Close()
cert := conn.ConnectionState().PeerCertificates[0]
expiry := cert.NotAfter
// Check if domain matches certificate subject or SANs
dnsMatch := false
if cert.Subject.CommonName == domain || strings.Contains(cert.Subject.CommonName, "*."+strings.SplitN(domain, ".", 2)[1]) {
dnsMatch = true
}
for _, names := range cert.DNSNames {
if names == domain || (strings.HasPrefix(names, "*.") && strings.HasSuffix(domain, names[2:])) {
dnsMatch = true
break
}
}
if dnsMatch {
fmt.Printf("✅ Active (Expires on %s)\n", expiry.Format("2006-01-02"))
} else {
fmt.Printf("⚠️ MISMATCH (Expires on %s)\n", expiry.Format("2006-01-02"))
fmt.Printf(" ⚠️ Warning: Domain tidak cocok dengan sertifikat di IP ini!\n")
}
fmt.Printf(" 📝 Subject: %s\n", cert.Subject.CommonName)
if len(cert.Issuer.Organization) > 0 {
fmt.Printf(" 🏢 Issuer: %s\n", cert.Issuer.Organization[0])
} else {
fmt.Printf(" 🏢 Issuer: %s\n", cert.Issuer.CommonName)
}
if isProxied {
fmt.Println(" 💡 Info: SSL yang anda lihat di atas adalah SSL asli di server Anda (Origin).")
fmt.Println(" Sedangkan SSL di hasil awal adalah SSL milik Cloudflare (Edge).")
}
}