-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
137 lines (111 loc) · 3.12 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
package main
import (
"context"
"embed"
"encoding/json"
"errors"
"io/fs"
"log"
"log/slog"
"net/http"
"os"
"os/signal"
"path"
"strings"
"syscall"
"time"
)
//go:embed all:webui/dist
var website embed.FS
// set by the build script
var Version string
var shutdown func(restart bool)
func run() {
cfg := getConfig()
srv := &http.Server{Addr: cfg.ServerAddr}
shutdown = func(restart bool) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
srv.Shutdown(ctx)
cancel()
slog.Info("relay server stopped")
if restart {
run()
}
}
go func(srv *http.Server) {
err := srv.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
slog.Error(
"relay server closed unexpectly",
slog.String("error", err.Error()),
)
os.Exit(1)
}
}(srv)
slog.Info(
"relay server started",
slog.String("httpAddress", cfg.ServerAddr),
slog.String("multicastInterface", cfg.McastIface),
)
}
func main() {
// if running as a service, disable log timestamp as the service manager
// will add it
if os.Getppid() == 1 {
log.SetFlags(0)
}
slog.Info("MyIPTV", slog.String("version", Version))
loadConfig()
initDDNS()
// the website
dist, _ := fs.Sub(website, "webui/dist")
http.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
upath := r.URL.Path
if !strings.HasPrefix(upath, "/") {
upath = "/" + upath
r.URL.Path = upath
}
upath = path.Clean(upath)
if f, err := http.FS(dist).Open(upath); err == nil {
f.Close()
} else if errors.Is(err, fs.ErrNotExist) {
upath = "/"
}
http.ServeFileFS(w, r, dist, upath)
})
// a simple file server for the 'file' directory as an add-on feature
http.Handle("GET /file/", http.FileServer(http.Dir("")))
// APIs to manage the relay server
http.HandleFunc("POST /api/restart", apiRestart)
http.HandleFunc("GET /api/interfaces-and-ips", apiListInterfacesAndIPs)
http.HandleFunc("GET /api/config", apiGetConfig)
http.HandleFunc("PUT /api/config", apiUpdateConfig)
http.HandleFunc("GET /api/channel-groups", apiListChannelGroups)
http.HandleFunc("PUT /api/channel-groups", apiUpdateChannelGroups)
http.HandleFunc("GET /api/epg/{channel}", apiGetEPG)
http.HandleFunc("POST /api/epg", apiUpdateEPG)
http.HandleFunc("GET /api/relays", apiListRelays)
http.HandleFunc("DELETE /api/relays/{addr}", apiCloseRelayConnection)
http.HandleFunc("DELETE /api/relays/{addr}/{client}", apiCloseRelayClient)
// for IPTV clients
http.HandleFunc("GET /iptv/relay/{addr}", iptvRelay)
http.HandleFunc("GET /iptv/channels", iptvListChannels)
http.HandleFunc("GET /iptv/epg", iptvGetEPG)
// run and wait `Ctrl-C` or `Term` to exit
run()
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt, syscall.SIGTERM)
<-signals
shutdown(false)
}
// apiRestart restarts the relay server
func apiRestart(w http.ResponseWriter, r *http.Request) {
shutdown(true)
}
// apiListInterfacesAndIPs lists all network interfaces and their IPs
func apiListInterfacesAndIPs(w http.ResponseWriter, r *http.Request) {
_ = r
m := GetInterfacesAndIPs()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(m)
}