-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
144 lines (117 loc) · 3.68 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
package main
import (
"fmt"
"log"
"net"
"net/http"
"github.com/skip2/go-qrcode"
"os/exec"
"strconv"
)
func moveMouse(w http.ResponseWriter, r *http.Request) {
// Parse x and y from query parameters
xStr := r.URL.Query().Get("x")
yStr := r.URL.Query().Get("y")
log.Printf("New Mouse Move")
x, errX := strconv.Atoi(xStr)
y, errY := strconv.Atoi(yStr)
if errX != nil || errY != nil {
http.Error(w, "Invalid x or y parameters", http.StatusBadRequest)
log.Printf("Failed")
return
}
// Construct the nircmd command to move the mouse
cmd := exec.Command("./helper/nircmd.exe", "setcursor", fmt.Sprintf("%d", x), fmt.Sprintf("%d", y))
// Run the command
if err := cmd.Run(); err != nil {
http.Error(w, "Failed to move mouse", http.StatusInternalServerError)
return
}
// Respond with success
w.WriteHeader(http.StatusOK)
w.Write([]byte(fmt.Sprintf("Moved mouse to: (%d, %d)", x, y)))
}
func handler(w http.ResponseWriter, r *http.Request) {
// Log new connection
ip := r.RemoteAddr
log.Printf("New connection | IP Address: %s\n", ip)
// Serve the HTML file
http.ServeFile(w, r, "static/index.html")
}
func executeCommand(w http.ResponseWriter, r *http.Request) {
ip := r.RemoteAddr
command := r.URL.Query().Get("command")
if command == "" {
http.Error(w, "Command not specified", http.StatusBadRequest)
return
}
// log the command
log.Printf("From: %s | Command: %s\n", ip, command)
// Using the helper function to execute the command
executeCommandHelper(command, w)
}
func getLocalIP() (string, error) {
// Get a list of all network interfaces
addrs, err := net.InterfaceAddrs()
if err != nil {
return "", err
}
// Loop through the addresses and find the first valid IPv4 address
for _, addr := range addrs {
// Check if the address is IP address
ipNet, ok := addr.(*net.IPNet)
if ok && !ipNet.IP.IsLoopback() && ipNet.IP.To4() != nil {
ip := ipNet.IP.String()
// Check if the IP is in a private network range
if isPrivateIP(ip) {
return ip, nil
}
}
}
return "", fmt.Errorf("unable to find local IP address")
}
// To check if an IP address is in the private range
func isPrivateIP(ip string) bool {
privateBlocks := []string{"192.168.", "10.", "172.16.", "172.17.", "172.18.", "172.19.", "172.20.", "172.21.", "172.22.", "172.23.", "172.24.", "172.25.", "172.26.", "172.27.", "172.28.", "172.29.", "172.30.", "172.31."}
for _, block := range privateBlocks {
if len(ip) >= len(block) && ip[:len(block)] == block {
return true
}
}
return false
}
// To print QR code in the terminal
func printQRCode(url string) {
qr, err := qrcode.New(url, qrcode.Medium)
if err != nil {
log.Fatalf("Failed to generate QR code: %v", err)
}
// Convert QR code to a string and print it
qrStr := qr.ToSmallString(false)
fmt.Println(qrStr)
}
func main() {
ip, err := getLocalIP()
if err != nil {
log.Fatalf("Failed to get local IP address: %v", err)
}
// Serve static files from the "static" directory
fs := http.FileServer(http.Dir("static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
// Set up the route handlers
http.HandleFunc("/", handler)
http.HandleFunc("/execute", executeCommand)
http.HandleFunc("/move", moveMouse)
// Construct the URL
url := fmt.Sprintf("http://%s:3001", ip)
// Print QR code in the terminal
printQRCode(url)
// Log connection info
fmt.Println("Scan the QR Code or use the URL to access it from your phone or other devices on the same network.")
fmt.Printf("URL: %s\n", url)
fmt.Println("Listening to your command")
// Start the server
if err := http.ListenAndServe("0.0.0.0:3001", nil); err != nil {
log.Fatal(err)
}
}