-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
229 lines (210 loc) · 5.62 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
package server
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
"github.com/jpillora/cloud-torrent/engine"
"github.com/jpillora/cloud-torrent/static"
"github.com/jpillora/go-realtime"
"github.com/jpillora/requestlog"
"github.com/jpillora/scraper/scraper"
"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
//realtime state (sync'd with browser immediately)
rt *realtime.Handler
state struct {
realtime.Object
sync.Mutex
Config engine.Config
SearchProviders scraper.Config
Downloads *fsNode
Torrents map[string]*engine.Torrent
Users map[string]*realtime.User
Stats struct {
Title string
Version string
Runtime string
Uptime time.Time
}
}
}
func (s *Server) Run(version string) error {
tls := s.CertPath != "" || s.KeyPath != "" //poor man's XOR
if tls && (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()
//init maps
s.state.Users = map[string]*realtime.User{}
//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}
if err := s.scraper.LoadConfig(defaultSearchConfig); err != nil {
log.Fatal(err)
}
s.state.SearchProviders = s.scraper.Config //share scraper config
s.scraperh = http.StripPrefix("/search", s.scraper)
s.engine = engine.New()
//realtime
s.rt = realtime.NewHandler()
if err := s.rt.Add("state", &s.state); err != nil {
log.Fatalf("State object not syncable: %s", err)
}
//realtime user events
go func() {
for user := range s.rt.UserEvents() {
if user.Connected {
s.state.Users[user.ID] = user
} else {
delete(s.state.Users, user.ID)
}
s.state.Update()
}
}()
//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()
// log.Printf("torrents #%d files #%d", len(s.state.Torrents), len(s.state.Downloads.Children))
s.state.Unlock()
s.state.Update()
time.Sleep(1 * time.Second)
}
}()
host := s.Host
if host == "" {
host = "0.0.0.0"
}
addr := fmt.Sprintf("%s:%d", host, s.Port)
proto := "http"
if tls {
proto += "s"
}
log.Printf("Listening at %s://%s", proto, addr)
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))
}()
}
h := http.Handler(http.HandlerFunc(s.handle))
if s.Log {
h = requestlog.Wrap(h)
}
if tls {
return http.ListenAndServeTLS(addr, s.CertPath, s.KeyPath, h)
} else {
return http.ListenAndServe(addr, h)
}
}
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.Update()
return nil
}
func (s *Server) handle(w http.ResponseWriter, r *http.Request) {
//handle realtime client connections
if r.URL.Path == "/realtime.js" {
realtime.JS.ServeHTTP(w, r)
return
} else if r.URL.Path == "/realtime" {
s.rt.ServeHTTP(w, r)
return
}
//basic auth
if s.Auth != "" {
u, p, _ := r.BasicAuth()
if s.Auth != u+":"+p {
w.Header().Set("WWW-Authenticate", "Basic")
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Access Denied"))
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)
}