diff --git a/.gitignore b/.gitignore index 3bb3d94..935599c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,11 @@ # Server generated files /server.crt /server.key -/token.txt \ No newline at end of file +/token.txt + +# Binaries +*.exe + +# IDEA files +*.iml +.idea/ \ No newline at end of file diff --git a/handlers.go b/handlers.go new file mode 100644 index 0000000..86eb5a3 --- /dev/null +++ b/handlers.go @@ -0,0 +1,91 @@ +package main + +import ( + "log" + "net/http" + "net/url" + "os" + "os/exec" + "strings" +) + +func authenticationHandler(next http.HandlerFunc) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + // Check authorization header + auth := strings.SplitN(r.Header.Get("Authorization"), " ", 2) + + if len(auth) != 2 || auth[0] != "Bearer" || auth[1] != token { + log.Println("Authentication failed, got:", auth[1]) + http.Error(w, "authorization failed", http.StatusUnauthorized) + return + } + + next(w, r) + } +} + +func urlOpenHandler(w http.ResponseWriter, r *http.Request) { + r.ParseForm() + + urlStr := r.Form.Get("url") + + if urlStr == "" { + w.WriteHeader(http.StatusBadRequest) + log.Println("empty url") + return + } + + u, err := url.Parse(urlStr) + + if err != nil || u.Scheme == "" || u.Host == "" { + w.WriteHeader(http.StatusBadRequest) + log.Println("invalid url scheme/host or err:", err) + return + } + + log.Println("Opening URL", urlStr) + + openUrl(urlStr) +} + +func openHandler(w http.ResponseWriter, r *http.Request) { + r.ParseForm() + + pathStr := r.Form.Get("path") + + if pathStr == "" { + w.WriteHeader(http.StatusBadRequest) + return + } + + s, err := os.Stat(pathStr) + + if os.IsNotExist(err) || s.IsDir() { + w.WriteHeader(http.StatusBadRequest) + return + } + + p, err := Start(pathStr) + + if err != nil { + w.WriteHeader(http.StatusBadRequest) + return + } + + p.Release() +} + +func Start(args ...string) (p *os.Process, err error) { + if args[0], err = exec.LookPath(args[0]); err == nil { + var procAttr os.ProcAttr + procAttr.Files = []*os.File{os.Stdin, os.Stdout, os.Stderr} + + p, err := os.StartProcess(args[0], args, &procAttr) + + if err == nil { + return p, nil + } + } + + return nil, err +} diff --git a/main.go b/main.go index 4596eb7..f1201fe 100644 --- a/main.go +++ b/main.go @@ -8,9 +8,7 @@ import ( "math/rand" "meow.tf/streamdeck-remote-server/icon" "net/http" - "net/url" "os" - "strings" "time" ) @@ -99,45 +97,7 @@ func listenServer() { mux := http.NewServeMux() mux.HandleFunc("/url/open", authenticationHandler(requirePost(urlOpenHandler))) + mux.HandleFunc("/cmd/open", authenticationHandler(requirePost(openHandler))) http.ListenAndServeTLS(":4443", "server.crt", "server.key", mux) } - -func authenticationHandler(next http.HandlerFunc) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - // Check authorization header - auth := strings.SplitN(r.Header.Get("Authorization"), " ", 2) - - if len(auth) != 2 || auth[0] != "Bearer" || auth[1] != token { - log.Println("Authentication failed, got:", auth[1]) - http.Error(w, "authorization failed", http.StatusUnauthorized) - return - } - - next(w, r) - } -} - -func urlOpenHandler(w http.ResponseWriter, r *http.Request) { - r.ParseForm() - - urlStr := r.Form.Get("url") - - if urlStr == "" { - w.WriteHeader(http.StatusBadRequest) - log.Println("empty url") - return - } - - u, err := url.Parse(urlStr) - - if err != nil || u.Scheme == "" || u.Host == "" { - w.WriteHeader(http.StatusBadRequest) - log.Println("invalid url scheme/host or err:", err) - return - } - - log.Println("Opening URL", urlStr) - - openUrl(urlStr) -} \ No newline at end of file