Add support for the "Open" action

This commit is contained in:
Tyler 2019-05-21 19:51:04 -04:00
parent 7dec9e6fd2
commit 67fb2e867b
3 changed files with 100 additions and 42 deletions

9
.gitignore vendored
View File

@ -1,4 +1,11 @@
# Server generated files # Server generated files
/server.crt /server.crt
/server.key /server.key
/token.txt /token.txt
# Binaries
*.exe
# IDEA files
*.iml
.idea/

91
handlers.go Normal file
View File

@ -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
}

42
main.go
View File

@ -8,9 +8,7 @@ import (
"math/rand" "math/rand"
"meow.tf/streamdeck-remote-server/icon" "meow.tf/streamdeck-remote-server/icon"
"net/http" "net/http"
"net/url"
"os" "os"
"strings"
"time" "time"
) )
@ -99,45 +97,7 @@ func listenServer() {
mux := http.NewServeMux() mux := http.NewServeMux()
mux.HandleFunc("/url/open", authenticationHandler(requirePost(urlOpenHandler))) mux.HandleFunc("/url/open", authenticationHandler(requirePost(urlOpenHandler)))
mux.HandleFunc("/cmd/open", authenticationHandler(requirePost(openHandler)))
http.ListenAndServeTLS(":4443", "server.crt", "server.key", mux) 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)
}