-
Notifications
You must be signed in to change notification settings - Fork 88
/
main.go
117 lines (96 loc) · 2.93 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
package main
import (
"fmt"
"net/http"
"os"
"runtime"
"strconv"
"strings"
"time"
"github.com/op/go-logging"
"github.com/steeve/pulsar/api"
"github.com/steeve/pulsar/bittorrent"
"github.com/steeve/pulsar/config"
"github.com/steeve/pulsar/util"
"github.com/steeve/pulsar/xbmc"
)
var log = logging.MustGetLogger("main")
const (
PulsarLogo = `
.__
______ __ __| | ___________ _______
\____ \| | \ | / ___/\__ \\_ __ \
| |_> > | / |__\___ \ / __ \| | \/
| __/|____/|____/____ >(____ /__|
|__| \/ \/
`
)
func ensureSingleInstance() {
http.Head(fmt.Sprintf("http://localhost:%d/shutdown", config.ListenPort))
}
func makeBTConfiguration(conf *config.Configuration) *bittorrent.BTConfiguration {
btConfig := &bittorrent.BTConfiguration{
LowerListenPort: conf.BTListenPortMin,
UpperListenPort: conf.BTListenPortMax,
DownloadPath: conf.DownloadPath,
MaxUploadRate: conf.UploadRateLimit,
MaxDownloadRate: conf.DownloadRateLimit,
}
if conf.SocksEnabled == true {
btConfig.Proxy = &bittorrent.ProxySettings{
Type: bittorrent.ProxyTypeSocks5Password,
Hostname: conf.SocksHost,
Port: conf.SocksPort,
Username: conf.SocksLogin,
Password: conf.SocksPassword,
}
}
return btConfig
}
func main() {
// Make sure we are properly multithreaded.
runtime.GOMAXPROCS(runtime.NumCPU())
logging.SetFormatter(logging.MustStringFormatter("%{time:2006-01-02 15:04:05} %{level:.4s} %{module:-15s} %{message}"))
logging.SetBackend(logging.NewLogBackend(os.Stdout, "", 0))
for _, line := range strings.Split(PulsarLogo, "\n") {
log.Info(line)
}
log.Info("Version: %s Git: %s Go: %s", util.Version, util.GitCommit, runtime.Version())
conf := config.Reload()
ensureSingleInstance()
Migrate()
xbmc.CloseAllDialogs()
log.Info("Addon: %s v%s", conf.Info.Id, conf.Info.Version)
btService := bittorrent.NewBTService(*makeBTConfiguration(conf))
var shutdown = func() {
log.Info("Shutting down...")
btService.Close()
log.Info("Bye bye")
os.Exit(0)
}
var watchParentProcess = func() {
for {
// did the parent die? shutdown!
if os.Getppid() == 1 {
log.Warning("Parent shut down. Me too.")
go shutdown()
break
}
time.Sleep(1 * time.Second)
}
}
go watchParentProcess()
http.Handle("/", api.Routes(btService))
http.Handle("/files/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
handler := http.StripPrefix("/files/", http.FileServer(bittorrent.NewTorrentFS(btService, config.Get().DownloadPath)))
handler.ServeHTTP(w, r)
}))
http.Handle("/reload", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
btService.Reconfigure(*makeBTConfiguration(config.Reload()))
}))
http.Handle("/shutdown", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
shutdown()
}))
xbmc.Notify("Pulsar", "Pulsar daemon has started", config.AddonIcon())
http.ListenAndServe(":"+strconv.Itoa(config.ListenPort), nil)
}