mirror of
https://github.com/skidoodle/iphistory.git
synced 2025-02-15 08:29:16 +01:00
chore: Refactor function and variable names for clarity
This commit is contained in:
parent
9e153ebed3
commit
23d176dcc7
1 changed files with 16 additions and 16 deletions
32
main.go
32
main.go
|
@ -12,14 +12,14 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
url = "https://one.one.one.one/cdn-cgi/trace"
|
traceURL = "https://one.one.one.one/cdn-cgi/trace"
|
||||||
filePath = "ip_history.txt"
|
ipHistoryPath = "ip_history.txt"
|
||||||
checkInterval = 30 * time.Second
|
checkInterval = 30 * time.Second
|
||||||
maxRetries = 3
|
maxRetries = 3
|
||||||
retryDelay = 10 * time.Second
|
retryDelay = 10 * time.Second
|
||||||
)
|
)
|
||||||
|
|
||||||
func FetchPublicIP() (string, error) {
|
func getPublicIP() (string, error) {
|
||||||
var ip string
|
var ip string
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
@ -35,7 +35,7 @@ func FetchPublicIP() (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func fetchIP() (string, error) {
|
func fetchIP() (string, error) {
|
||||||
resp, err := http.Get(url)
|
resp, err := http.Get(traceURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
@ -54,8 +54,8 @@ func fetchIP() (string, error) {
|
||||||
return "", fmt.Errorf("IP address not found")
|
return "", fmt.Errorf("IP address not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadIPHistory() ([]string, error) {
|
func readHistory() ([]string, error) {
|
||||||
file, err := os.Open(filePath)
|
file, err := os.Open(ipHistoryPath)
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
return []string{}, nil
|
return []string{}, nil
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
|
@ -71,8 +71,8 @@ func ReadIPHistory() ([]string, error) {
|
||||||
return history, scanner.Err()
|
return history, scanner.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
func WriteIPHistory(history []string) error {
|
func writeHistory(history []string) error {
|
||||||
file, err := os.Create(filePath)
|
file, err := os.Create(ipHistoryPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -88,10 +88,10 @@ func WriteIPHistory(history []string) error {
|
||||||
return writer.Flush()
|
return writer.Flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TrackIP(ipChan <-chan string) {
|
func trackIP(ipChan <-chan string) {
|
||||||
var lastLoggedIP string
|
var lastLoggedIP string
|
||||||
for ip := range ipChan {
|
for ip := range ipChan {
|
||||||
history, err := ReadIPHistory()
|
history, err := readHistory()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error reading IP history:", err)
|
fmt.Println("Error reading IP history:", err)
|
||||||
continue
|
continue
|
||||||
|
@ -100,7 +100,7 @@ func TrackIP(ipChan <-chan string) {
|
||||||
entry := fmt.Sprintf("%s - %s", time.Now().Format("2006-01-02 15:04:05 MST"), ip)
|
entry := fmt.Sprintf("%s - %s", time.Now().Format("2006-01-02 15:04:05 MST"), ip)
|
||||||
if len(history) == 0 || !strings.Contains(history[0], ip) {
|
if len(history) == 0 || !strings.Contains(history[0], ip) {
|
||||||
history = append([]string{entry}, history...)
|
history = append([]string{entry}, history...)
|
||||||
if err := WriteIPHistory(history); err != nil {
|
if err := writeHistory(history); err != nil {
|
||||||
fmt.Println("Error writing IP history:", err)
|
fmt.Println("Error writing IP history:", err)
|
||||||
}
|
}
|
||||||
fmt.Printf("📄 New IP logged: %s\n", entry)
|
fmt.Printf("📄 New IP logged: %s\n", entry)
|
||||||
|
@ -114,13 +114,13 @@ func TrackIP(ipChan <-chan string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ServeWeb() {
|
func serveWeb() {
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
http.ServeFile(w, r, "index.html")
|
http.ServeFile(w, r, "index.html")
|
||||||
})
|
})
|
||||||
|
|
||||||
http.HandleFunc("/history", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/history", func(w http.ResponseWriter, r *http.Request) {
|
||||||
history, err := ReadIPHistory()
|
history, err := readHistory()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Error reading IP history", http.StatusInternalServerError)
|
http.Error(w, "Error reading IP history", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
@ -145,7 +145,7 @@ func main() {
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
ip, err := FetchPublicIP()
|
ip, err := getPublicIP()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error fetching public IP:", err)
|
fmt.Println("Error fetching public IP:", err)
|
||||||
} else {
|
} else {
|
||||||
|
@ -155,6 +155,6 @@ func main() {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
go TrackIP(ipChan)
|
go trackIP(ipChan)
|
||||||
ServeWeb()
|
serveWeb()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue