-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
112 lines (93 loc) · 2.74 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
package main
import (
"bufio"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"strings"
"github.com/gorilla/mux"
)
// Path to dnsmasq leases file
const leasesFilePath = "/var/lib/misc/dnsmasq.leases"
// Function to read the MAC address for a given hostname from dnsmasq leases file
func getMacAddressFromHostname(hostname string) (string, error) {
file, err := os.Open(leasesFilePath)
if err != nil {
return "", fmt.Errorf("failed to open leases file: %v", err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
fields := strings.Fields(line)
if len(fields) >= 4 && fields[3] == hostname {
return fields[1], nil
}
}
if err := scanner.Err(); err != nil {
return "", fmt.Errorf("error reading leases file: %v", err)
}
return "", fmt.Errorf("hostname not found")
}
// Function to read the IP address for a given hostname from dnsmasq leases file
func getIPAddressFromHostname(hostname string) (string, error) {
file, err := os.Open(leasesFilePath)
if err != nil {
return "", fmt.Errorf("failed to open leases file: %v", err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
fields := strings.Fields(line)
if len(fields) >= 4 && fields[3] == hostname {
return fields[2], nil
}
}
if err := scanner.Err(); err != nil {
return "", fmt.Errorf("error reading leases file: %v", err)
}
return "", fmt.Errorf("hostname not found")
}
// Function to handle WOL request
func wolHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
hostname := vars["hostname"]
macAddress, err := getMacAddressFromHostname(hostname)
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
// Execute the wol command with the MAC address
cmd := exec.Command("wakeonlan", "-i", "192.168.0.255", macAddress)
err = cmd.Run()
if err != nil {
http.Error(w, fmt.Sprintf("Failed to execute wol command: %v", err), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Sent WOL packet to MAC address %s", macAddress)
}
// Function to handle IP resolution request
func resolveIPHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
hostname := vars["hostname"]
ipAddress, err := getIPAddressFromHostname(hostname)
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "%s", ipAddress)
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/wol/{hostname}", wolHandler).Methods("GET")
r.HandleFunc("/res/{hostname}", resolveIPHandler).Methods("GET")
fmt.Println("Starting server on :5001")
if err := http.ListenAndServe(":5001", r); err != nil {
log.Fatalf("Failed to start server: %v", err)
}
}