remote-i3wm-go/main.go
2023-08-24 20:33:21 +02:00

300 lines
6.1 KiB
Go

package main
import (
"crypto/subtle"
"encoding/json"
"errors"
"fmt"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"golang.org/x/net/websocket"
"net/http"
"os/exec"
"strconv"
"strings"
)
type Message struct {
Type string `json:type`
}
type SimpleMessageData struct {
Value string `json:type`
}
type PointerMessageData struct {
X string `json:x`
Y string `json:y`
Click string `json:click`
}
type MessageResponse struct {
Type string `json:"type"`
Value string `json:"value"`
}
func getSimpleMessageValue(msg string) string {
data := SimpleMessageData{}
json.Unmarshal([]byte(msg), &data)
return data.Value
}
func sendMessageResponse(ws *websocket.Conn, r MessageResponse) {
value, _ := json.Marshal(r)
websocket.Message.Send(ws, string(value))
}
func ws(c echo.Context) error {
var actions = Actions{
Functions: make(map[string]func(ws *websocket.Conn, msg string) error),
}
actions.add("pointer", func(ws *websocket.Conn, msg string) error {
data := PointerMessageData{}
json.Unmarshal([]byte(msg), &data)
if data.Click != "" {
keys := make(map[string]string)
keys["left"] = "1"
keys["middle"] = "2"
keys["right"] = "3"
key, exists := keys[data.Click]
if !exists {
return errors.New("Invalid value")
}
cmd := exec.Command("xdotool", "click", key)
return cmd.Run()
}
location := exec.Command("xdotool", "getmouselocation")
output, _ := location.Output()
position := string(output)
currentX := 0.0
currentY := 0.0
for key, value := range strings.Split(position, " ") {
if key == 0 {
currentX, _ = strconv.ParseFloat(strings.Replace(value, "x:", "", 1), 32)
} else if key == 1 {
currentY, _ = strconv.ParseFloat(strings.Replace(value, "y:", "", 1), 32)
}
}
fmt.Printf("%+v\n", currentX)
fmt.Printf("%+v\n", currentY)
newX, _ := strconv.ParseFloat(data.X, 32)
newY, _ := strconv.ParseFloat(data.Y, 32)
x := currentX + newX*2.5
y := currentY + newY*2.5
cmd := exec.Command("xdotool", "mousemove", fmt.Sprintf("%.0f", x), fmt.Sprintf("%.0f", y))
return cmd.Run()
})
actions.add("scroll", func(ws *websocket.Conn, msg string) error {
value := getSimpleMessageValue(msg)
key := ""
if value == "down" {
key = "5"
} else if value == "up" {
key = "4"
} else {
return errors.New("Invalid value")
}
for i := 0; i < 2; i++ {
cmd := exec.Command("xdotool", "click", key)
cmd.Run()
}
return nil
})
actions.add("workspace", func(ws *websocket.Conn, msg string) error {
value := getSimpleMessageValue(msg)
if value == "" {
return errors.New("Invalid value")
}
cmd := exec.Command("i3-msg", fmt.Sprintf("workspace \"%s\"", value))
return cmd.Run()
})
actions.add("volume", func(ws *websocket.Conn, msg string) error {
value := getSimpleMessageValue(msg)
if value == "" {
return errors.New("Invalid value")
}
if value == "up" {
cmd := exec.Command("amixer", "set", "Master", "2%+")
sendMessageResponse(ws, MessageResponse{
Type: "response",
Value: "Volume up",
})
return cmd.Run()
}
if value == "down" {
cmd := exec.Command("amixer", "set", "Master", "2%-")
sendMessageResponse(ws, MessageResponse{
Type: "response",
Value: "Volume down",
})
return cmd.Run()
}
cmd := exec.Command("amixer", "set", "Master", fmt.Sprintf("%s%%", value))
sendMessageResponse(ws, MessageResponse{
Type: "response",
Value: fmt.Sprintf("Volume set to %s%%", value),
})
return cmd.Run()
})
actions.add("media", func(ws *websocket.Conn, msg string) error {
return nil
})
actions.add("keys", func(ws *websocket.Conn, msg string) error {
value := strings.TrimSpace(getSimpleMessageValue(msg))
if value == "" {
return errors.New("Invalid value")
}
keys := []string{}
for _, key := range strings.Split(value, ",") {
if key == "win" {
key = "super"
} else if key == "ctrl" {
key = "Control_L"
} else if key == "alt" {
key = "Alt_L"
}
if key != "" {
keys = append(keys, key)
}
}
if len(keys) == 0 {
return errors.New("Invalid value")
}
cmd := exec.Command("xdotool", "key", strings.Join(keys, "+"))
return cmd.Run()
})
actions.add("key", func(ws *websocket.Conn, msg string) error {
value := strings.TrimSpace(getSimpleMessageValue(msg))
keys := make(map[string]string)
keys["up"] = "Up"
keys["down"] = "Down"
keys["left"] = "Left"
keys["right"] = "Right"
keys["tab"] = "Tab"
keys["backspace"] = "BackSpace"
keys["enter"] = "Return"
keys["space"] = "space"
keys["escape"] = "Escape"
key, exists := keys[value]
if !exists {
return errors.New("Invalid value")
}
cmd := exec.Command("xdotool", "key", key)
return cmd.Run()
})
actions.add("text", func(ws *websocket.Conn, msg string) error {
value := strings.TrimSpace(getSimpleMessageValue(msg))
if value == "" {
return errors.New("Invalid value")
}
cmd := exec.Command("xdotool", "type", value)
return cmd.Run()
})
actions.add("screenshot", func(ws *websocket.Conn, msg string) error {
return nil
})
actions.add("messages", func(ws *websocket.Conn, msg string) error {
return nil
})
websocket.Handler(func(ws *websocket.Conn) {
defer ws.Close()
for {
// Write
// err := websocket.Message.Send(ws, "Hello, Client!")
// if err != nil {
// c.Logger().Error(err)
// }
msg := ""
websocket.Message.Receive(ws, &msg)
message := Message{}
json.Unmarshal([]byte(msg), &message)
if message.Type != "" && actions.has(message.Type) {
fmt.Printf("%+v\n", msg)
err := actions.exec(message.Type, ws, msg)
fmt.Printf("%+v\n", err)
}
}
}).ServeHTTP(c.Response(), c.Request())
return nil
}
func main() {
e := echo.New()
e.HideBanner = true
e.Use(middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) {
if subtle.ConstantTimeCompare([]byte(username), []byte("admin")) == 1 &&
subtle.ConstantTimeCompare([]byte(password), []byte("admin")) == 1 {
return true, nil
}
return false, nil
}))
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
e.GET("/ws", ws)
e.Logger.Fatal(e.Start(":4000"))
}