forked from jpillora/cloud-torrent
/
server.go
187 lines (174 loc) · 4.55 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
package server
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"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"
)
//Server is the "State" portion of the diagram
type Server struct {
//config
Port int `help:"Listening port"`
Host string `help:"Listening interface (default all)"`
Auth string `help:"Optional basic auth (in form user:password)"`
ConfigPath string `help:"Configuration file path"`
Log bool `help:"Enable request logging"`
//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
}
}
func (s *Server) init() error {
//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,
EnableSeeding: false,
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 err := json.Unmarshal(b, &c); err != nil {
return fmt.Errorf("Malformed configuration: %s", err)
}
}
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(2 * time.Second)
}
}()
//ready
return nil
}
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) Run() error {
if err := s.init(); err != nil {
return err
}
// TODO if Open {
// cross platform open - https://github.com/skratchdot/open-golang
// }
h := http.Handler(http.HandlerFunc(s.handle))
if s.Log {
h = requestlog.Wrap(h)
}
log.Printf("Listening on %d...", s.Port)
return http.ListenAndServe(s.Host+":"+strconv.Itoa(s.Port), h)
}
func (s *Server) handle(w http.ResponseWriter, r *http.Request) {
//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
}
}
//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
}
//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)
}