-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
147 lines (118 loc) · 2.88 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"errors"
"fmt"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"os/exec"
"regexp"
"strconv"
)
var streamRE = regexp.MustCompile(`\/stream\..+`)
func openLogFile(fn string) (*os.File, error) {
return os.OpenFile("log.txt", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
}
func main() {
c, err := loadConfig()
if err != nil {
panic(err)
}
if c.LogFile != "" {
f, err := openLogFile(c.LogFile)
if err != nil {
panic(err)
}
defer f.Close()
log.SetOutput(f)
}
address := c.Host + ":" + strconv.Itoa(c.Port)
http.HandleFunc("/", handleRequestAndRedirect(c))
go func() {
fmt.Printf("Running stash proxy on %s\n", address)
err := http.ListenAndServe(address, nil)
if err != nil {
log.Println(err.Error())
}
}()
if c.ChromePath != "" {
err = openChromeAndWait(c)
if err != nil {
log.Println(err.Error())
}
} else {
// just block forever
select {}
}
}
func openChromeAndWait(c *config) error {
// make temporary user-data-dir
dataDir, err := os.MkdirTemp(os.TempDir(), "stash-proxy*")
if err != nil {
return err
}
if dataDir == "" {
return errors.New("could not create temp directory")
}
defer func() {
if err := os.RemoveAll(dataDir); err != nil {
log.Printf("error deleting temporary directory %s: %s", dataDir, err.Error())
}
}()
url := c.Host
if url == "" {
url = "localhost"
}
address := url + ":" + strconv.Itoa(c.Port)
args := []string{
"--incognito",
"--new-window",
`--user-data-dir=` + dataDir,
"http://" + address,
}
p := exec.Command(c.ChromePath, args...)
err = p.Start()
if err != nil {
return err
}
p.Process.Wait()
return nil
}
func serveReverseProxy(target string, res http.ResponseWriter, req *http.Request) {
// parse the url
url, _ := url.Parse(target)
// create the reverse proxy
proxy := httputil.NewSingleHostReverseProxy(url)
// Note that ServeHttp is non blocking and uses a go routine under the hood
proxy.ServeHTTP(res, req)
}
// Given a request send it to the appropriate url
func handleRequestAndRedirect(c *config) func(res http.ResponseWriter, req *http.Request) {
return func(res http.ResponseWriter, req *http.Request) {
if streamRE.MatchString(req.URL.Path) {
localStream(c, res, req)
return
}
serveReverseProxy(c.ServerURL, res, req)
}
}
func localStream(c *config, res http.ResponseWriter, req *http.Request) {
req.URL.Path = streamRE.ReplaceAllString(req.URL.Path, "/stream")
query := req.URL.Query()
if query.Get("apikey") == "" && c.ApiKey != "" {
query.Add("apikey", c.ApiKey)
}
// TODO - handle resolution
// resolution := query.Get("resolution")
// query.Del("resolution")
start := query.Get("start")
query.Del("start")
req.URL.RawQuery = query.Encode()
remoteURL, _ := url.Parse(c.ServerURL)
req.URL.Scheme = remoteURL.Scheme
req.URL.Host = remoteURL.Host
s, _ := stream(c, req.URL.String(), start)
s.Serve(res, req)
}