/
server.go
248 lines (241 loc) · 6.32 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package server
import (
"compress/gzip"
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
"github.com/NYTimes/gziphandler"
"github.com/jpillora/cloud-torrent/engine"
"github.com/jpillora/cloud-torrent/static"
"github.com/jpillora/cookieauth"
"github.com/jpillora/requestlog"
"github.com/jpillora/scraper/scraper"
"github.com/jpillora/velox"
"github.com/skratchdot/open-golang/open"
)
//Server is the "State" portion of the diagram
type Server struct {
//config
Title string `help:"Title of this instance" env:"TITLE"`
Port int `help:"Listening port" env:"PORT"`
Host string `help:"Listening interface (default all)"`
Auth string `help:"Optional basic auth in form 'user:password'" env:"AUTH"`
ConfigPath string `help:"Configuration file path"`
KeyPath string `help:"TLS Key file path"`
CertPath string `help:"TLS Certicate file path" short:"r"`
Log bool `help:"Enable request logging"`
Open bool `help:"Open now with your default browser"`
//http handlers
files, static http.Handler
scraper *scraper.Handler
scraperh http.Handler
//torrent engine
engine *engine.Engine
state struct {
velox.State
sync.Mutex
Config engine.Config
SearchProviders scraper.Config
Downloads *fsNode
Torrents map[string]*engine.Torrent
Users map[string]string
Stats struct {
Title string
Version string
Runtime string
Uptime time.Time
System stats
}
}
}
// Run the server
func (s *Server) Run(version string) error {
isTLS := s.CertPath != "" || s.KeyPath != "" //poor man's XOR
if isTLS && (s.CertPath == "" || s.KeyPath == "") {
return fmt.Errorf("You must provide both key and cert paths")
}
s.state.Stats.Title = s.Title
s.state.Stats.Version = version
s.state.Stats.Runtime = strings.TrimPrefix(runtime.Version(), "go")
s.state.Stats.Uptime = time.Now()
s.state.Stats.System.pusher = velox.Pusher(&s.state)
//init maps
s.state.Users = map[string]string{}
//will use a the local embed/ dir if it exists, otherwise will use the hardcoded embedded binaries
s.files = http.HandlerFunc(s.serveFiles)
s.static = ctstatic.FileSystemHandler()
s.scraper = &scraper.Handler{
Log: false, Debug: false,
Headers: map[string]string{
//we're a trusty browser :)
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36",
},
}
if err := s.scraper.LoadConfig(defaultSearchConfig); err != nil {
log.Fatal(err)
}
//scraper
s.state.SearchProviders = s.scraper.Config //share scraper config
go s.fetchSearchConfigLoop()
s.scraperh = http.StripPrefix("/search", s.scraper)
//torrent engine
s.engine = engine.New()
//configure engine
c := engine.Config{
DownloadDirectory: "./downloads",
EnableUpload: true,
AutoStart: true,
}
if _, err := os.Stat(s.ConfigPath); err == nil {
if b, err := ioutil.ReadFile(s.ConfigPath); err != nil {
return fmt.Errorf("Read configuration error: %s", err)
} else if len(b) == 0 {
//ignore empty file
} else if err := json.Unmarshal(b, &c); err != nil {
return fmt.Errorf("Malformed configuration: %s", err)
}
}
if c.IncomingPort <= 0 || c.IncomingPort >= 65535 {
c.IncomingPort = 50007
}
if err := s.reconfigure(c); err != nil {
return fmt.Errorf("initial configure failed: %s", err)
}
//poll torrents and files
go func() {
for {
s.state.Lock()
s.state.Torrents = s.engine.GetTorrents()
s.state.Downloads = s.listFiles()
s.state.Unlock()
s.state.Push()
time.Sleep(1 * time.Second)
}
}()
//start collecting stats
go func() {
for {
c := s.engine.Config()
s.state.Stats.System.loadStats(c.DownloadDirectory)
time.Sleep(5 * time.Second)
}
}()
host := s.Host
if host == "" {
host = "0.0.0.0"
}
addr := fmt.Sprintf("%s:%d", host, s.Port)
proto := "http"
if isTLS {
proto += "s"
}
if s.Open {
openhost := host
if openhost == "0.0.0.0" {
openhost = "localhost"
}
go func() {
time.Sleep(1 * time.Second)
open.Run(fmt.Sprintf("%s://%s:%d", proto, openhost, s.Port))
}()
}
//define handler chain, from last to first
h := http.Handler(http.HandlerFunc(s.handle))
//gzip
compression := gzip.DefaultCompression
minSize := 0 //IMPORTANT
gzipWrap, _ := gziphandler.NewGzipLevelAndMinSize(compression, minSize)
h = gzipWrap(h)
//auth
if s.Auth != "" {
user := s.Auth
pass := ""
if s := strings.SplitN(s.Auth, ":", 2); len(s) == 2 {
user = s[0]
pass = s[1]
}
h = cookieauth.New().SetUserPass(user, pass).Wrap(h)
log.Printf("Enabled HTTP authentication")
}
if s.Log {
h = requestlog.Wrap(h)
}
log.Printf("Listening at %s://%s", proto, addr)
//serve!
server := http.Server{
//disable http2 due to velox bug
TLSNextProto: map[string]func(*http.Server, *tls.Conn, http.Handler){},
//address
Addr: addr,
//handler stack
Handler: h,
}
if isTLS {
return server.ListenAndServeTLS(s.CertPath, s.KeyPath)
}
return server.ListenAndServe()
}
func (s *Server) reconfigure(c engine.Config) error {
dldir, err := filepath.Abs(c.DownloadDirectory)
if err != nil {
return fmt.Errorf("Invalid path")
}
c.DownloadDirectory = dldir
if err := s.engine.Configure(c); err != nil {
return err
}
b, _ := json.MarshalIndent(&c, "", " ")
ioutil.WriteFile(s.ConfigPath, b, 0755)
s.state.Config = c
s.state.Push()
return nil
}
func (s *Server) handle(w http.ResponseWriter, r *http.Request) {
//handle realtime client library
if r.URL.Path == "/js/velox.js" {
velox.JS.ServeHTTP(w, r)
return
}
//handle realtime client connections
if r.URL.Path == "/sync" {
conn, err := velox.Sync(&s.state, w, r)
if err != nil {
log.Printf("sync failed: %s", err)
return
}
s.state.Users[conn.ID()] = r.RemoteAddr
s.state.Push()
conn.Wait()
delete(s.state.Users, conn.ID())
s.state.Push()
return
}
//search
if strings.HasPrefix(r.URL.Path, "/search") {
s.scraperh.ServeHTTP(w, r)
return
}
//api call
if strings.HasPrefix(r.URL.Path, "/api/") {
//only pass request in, expect error out
if err := s.api(r); err == nil {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
} else {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
}
return
}
//no match, assume static file
s.files.ServeHTTP(w, r)
}