-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
98 lines (82 loc) · 1.95 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
)
type wrIntercept struct {
http.ResponseWriter
status int
}
func (wr *wrIntercept) WriteHeader(statusCode int) {
wr.status = statusCode
wr.ResponseWriter.WriteHeader(statusCode)
}
func (wr *wrIntercept) Status() int {
return wr.status
}
func main() {
conf := flag.String("config", "/etc/xtream/xtream.conf", "stream data config path")
flag.Parse()
var err error
defer func() {
if err != nil {
os.Exit(1)
}
}()
sd, err = loadStreamData(*conf)
if err != nil {
log.Printf("failed reading config from: %s", *conf)
return
}
// kiss routing
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
wi := &wrIntercept{w, http.StatusOK}
switch r.URL.Path {
case "/":
root(wi, r)
case "/authz":
handleAuthz(wi, r)
default:
http.Error(wi, http.StatusText(http.StatusNotFound), http.StatusNotFound)
}
log.Printf("handled request %s - %s (%d - %s)", r.Method, r.URL.Path, wi.Status(), http.StatusText(wi.Status()))
})
server := http.Server{
Addr: "localhost:8000",
Handler: handler,
}
err = server.ListenAndServe()
if err != nil {
log.Printf("server error: %s", err)
return
}
}
func root(w http.ResponseWriter, r *http.Request) {
}
func handleAuthz(w http.ResponseWriter, r *http.Request) {
app := r.URL.Query().Get("app")
if app == "" {
log.Println("missing app name")
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
sk := r.URL.Query().Get("name")
if sk == "" {
log.Println("missing stream key")
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
appData, ok := sd.App(sk)
if !ok {
log.Println("invalid sk provided")
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
if app != appData.Name {
http.Redirect(w, r, fmt.Sprintf("rtmp://xtream/%s", appData.Name), http.StatusFound)
return
}
}