-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.go
54 lines (47 loc) · 1.37 KB
/
server.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
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"github.com/gorilla/mux"
)
var keyLocation = "key.txt"
func indexHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "<a href=\"https://github.com/Thor77/nginx-rtmp-keyauth\">nginx-rtmp-keyauth</a>")
}
func authHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost || r.FormValue("call") != "publish" {
http.Error(w, "", http.StatusNotImplemented)
return
}
keyfile, err := ioutil.ReadFile(keyLocation)
if err != nil {
http.Error(w, "", http.StatusInternalServerError)
log.Print(err)
return
}
clientIdentifier := fmt.Sprintf("from %s for %s/%s", r.FormValue("addr"), r.FormValue("app"), r.FormValue("name"))
key := strings.TrimRight(strings.TrimRight(string(keyfile), "\n"), "\r")
if givenKey := r.FormValue("key"); givenKey != key {
log.Printf("Failed authentication attempt %s", clientIdentifier)
http.Error(w, "", http.StatusForbidden)
} else {
log.Printf("Successfull authentication %s", clientIdentifier)
}
}
func main() {
if len(os.Args) >= 2 {
keyLocation = os.Args[1]
log.Printf("Set key location to %s", keyLocation)
}
r := mux.NewRouter()
r.HandleFunc("/", indexHandler)
r.HandleFunc("/auth", authHandler)
http.Handle("/", r)
address := ":8080"
log.Printf("Listening on %s", address)
log.Fatal(http.ListenAndServe(address, nil))
}