-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
195 lines (162 loc) · 3.93 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
"time"
)
const (
ipv4URL = "https://4.ident.me/"
ipv6URL = "https://6.ident.me/"
ipHistoryPath = "history.json"
checkInterval = 30 * time.Second
maxRetries = 3
retryDelay = 10 * time.Second
)
type IPRecord struct {
Timestamp string `json:"timestamp"`
IPv4 string `json:"ipv4"`
IPv6 string `json:"ipv6"`
}
func getPublicIPs() (string, string, error) {
var ipv4, ipv6 string
var err error
for attempt := 1; attempt <= maxRetries; attempt++ {
ipv4, ipv6, err = fetchIPs()
if err == nil {
return ipv4, ipv6, nil
}
fmt.Printf("Attempt %d: Error fetching IPs: %v\n", attempt, err)
time.Sleep(retryDelay)
}
return "", "", err
}
func fetchIPs() (string, string, error) {
ipv4, err := fetchIP(ipv4URL)
if err != nil {
return "", "", err
}
ipv6, err := fetchIP(ipv6URL)
if err != nil {
fmt.Println("Warning: Could not fetch IPv6 address:", err)
}
return ipv4, ipv6, nil
}
func fetchIP(url string) (string, error) {
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
return strings.TrimSpace(string(body)), nil
}
func readHistory() ([]IPRecord, error) {
file, err := os.Open(ipHistoryPath)
if os.IsNotExist(err) {
return []IPRecord{}, nil
} else if err != nil {
return nil, err
}
defer file.Close()
fileInfo, err := file.Stat()
if err != nil {
return nil, err
}
// Check if the file is empty
if fileInfo.Size() == 0 {
return []IPRecord{}, nil
}
var history []IPRecord
err = json.NewDecoder(file).Decode(&history)
if err != nil {
return nil, err
}
return history, nil
}
func writeHistory(history []IPRecord) error {
file, err := os.Create(ipHistoryPath)
if err != nil {
return err
}
defer file.Close()
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ") // Pretty-print with indentation
err = encoder.Encode(history)
if err != nil {
return err
}
return nil
}
func trackIP(ipChan <-chan IPRecord) {
var lastLoggedIP IPRecord
hasNotifiedUpToDate := false
for ipRecord := range ipChan {
history, err := readHistory()
if err != nil {
fmt.Println("Error reading IP history:", err)
continue
}
if len(history) > 0 {
lastLoggedIP = history[0]
}
// Check if IPs are the same as the last logged one
if ipRecord.IPv4 == lastLoggedIP.IPv4 && ipRecord.IPv6 == lastLoggedIP.IPv6 {
if !hasNotifiedUpToDate {
fmt.Println("🤷 IPs are already up to date")
hasNotifiedUpToDate = true
}
continue
}
// Reset the notification flag when IP changes
hasNotifiedUpToDate = false
ipRecord.Timestamp = time.Now().Format("2006-01-02 15:04:05")
history = append([]IPRecord{ipRecord}, history...)
if err := writeHistory(history); err != nil {
fmt.Println("Error writing IP history:", err)
continue
}
fmt.Printf("📄 New IP logged: {Timestamp: %s, IPv4: %s, IPv6: %s}\n", ipRecord.Timestamp, ipRecord.IPv4, ipRecord.IPv6)
}
}
func serveWeb() {
http.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir("web"))))
http.HandleFunc("/history", func(w http.ResponseWriter, r *http.Request) {
history, err := readHistory()
if err != nil {
http.Error(w, "Error reading IP history", http.StatusInternalServerError)
return
}
jsonData, err := json.Marshal(history)
if err != nil {
http.Error(w, "Error converting history to JSON", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(jsonData)
})
fmt.Println("Starting server on :8080")
http.ListenAndServe(":8080", nil)
}
func main() {
ipChan := make(chan IPRecord)
go func() {
for {
ipv4, ipv6, err := getPublicIPs()
if err != nil {
fmt.Println("Error fetching public IPs:", err)
} else {
ipChan <- IPRecord{IPv4: ipv4, IPv6: ipv6}
}
time.Sleep(checkInterval)
}
}()
go trackIP(ipChan)
serveWeb()
}