forked from pierre-emmanuelJ/iptv-proxy
/
server.go
189 lines (157 loc) · 4.72 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
/*
* Iptv-Proxy is a project to proxyfie an m3u file and to proxyfie an Xtream iptv service (client API).
* Copyright (C) 2020 Pierre-Emmanuel Jacquier
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package server
import (
"bytes"
"fmt"
"log"
"net/url"
"os"
"path"
"path/filepath"
"strings"
"github.com/gin-contrib/cors"
"github.com/jamesnetherton/m3u"
"github.com/ALiP61/iptv-proxy/pkg/config"
uuid "github.com/satori/go.uuid"
"github.com/gin-gonic/gin"
)
var defaultProxyfiedM3UPath = filepath.Join(os.TempDir(), uuid.NewV4().String()+".iptv-proxy.m3u")
var endpointAntiColision = strings.Split(uuid.NewV4().String(), "-")[0]
// Config represent the server configuration
type Config struct {
*config.ProxyConfig
// M3U service part
playlist *m3u.Playlist
// this variable is set only for m3u proxy endpoints
track *m3u.Track
// path to the proxyfied m3u file
proxyfiedM3UPath string
endpointAntiColision string
}
// NewServer initialize a new server configuration
func NewServer(config *config.ProxyConfig) (*Config, error) {
var p m3u.Playlist
if config.RemoteURL.String() != "" {
var err error
p, err = m3u.Parse(config.RemoteURL.String())
if err != nil {
return nil, err
}
}
if trimmedCustomId := strings.Trim(config.CustomId, "/"); trimmedCustomId != "" {
endpointAntiColision = trimmedCustomId
}
return &Config{
config,
&p,
nil,
defaultProxyfiedM3UPath,
endpointAntiColision,
}, nil
}
// Serve the iptv-proxy api
func (c *Config) Serve() error {
if err := c.playlistInitialization(); err != nil {
return err
}
router := gin.Default()
router.Use(cors.Default())
group := router.Group("/")
c.routes(group)
return router.Run(fmt.Sprintf(":%d", c.HostConfig.Port))
}
func (c *Config) playlistInitialization() error {
if len(c.playlist.Tracks) == 0 {
return nil
}
f, err := os.Create(c.proxyfiedM3UPath)
if err != nil {
return err
}
defer f.Close()
return c.marshallInto(f, false)
}
// MarshallInto a *bufio.Writer a Playlist.
func (c *Config) marshallInto(into *os.File, xtream bool) error {
filteredTrack := make([]m3u.Track, 0, len(c.playlist.Tracks))
ret := 0
into.WriteString("#EXTM3U\n") // nolint: errcheck
for i, track := range c.playlist.Tracks {
var buffer bytes.Buffer
buffer.WriteString("#EXTINF:") // nolint: errcheck
buffer.WriteString(fmt.Sprintf("%d ", track.Length)) // nolint: errcheck
for i := range track.Tags {
if i == len(track.Tags)-1 {
buffer.WriteString(fmt.Sprintf("%s=%q", track.Tags[i].Name, track.Tags[i].Value)) // nolint: errcheck
continue
}
buffer.WriteString(fmt.Sprintf("%s=%q ", track.Tags[i].Name, track.Tags[i].Value)) // nolint: errcheck
}
uri, err := c.replaceURL(track.URI, i-ret, xtream)
if err != nil {
ret++
log.Printf("ERROR: track: %s: %s", track.Name, err)
continue
}
into.WriteString(fmt.Sprintf("%s, %s\n%s\n", buffer.String(), track.Name, uri)) // nolint: errcheck
filteredTrack = append(filteredTrack, track)
}
c.playlist.Tracks = filteredTrack
return into.Sync()
}
// ReplaceURL replace original playlist url by proxy url
func (c *Config) replaceURL(uri string, trackIndex int, xtream bool) (string, error) {
oriURL, err := url.Parse(uri)
if err != nil {
return "", err
}
protocol := "http"
if c.HTTPS {
protocol = "https"
}
customEnd := strings.Trim(c.CustomEndpoint, "/")
if customEnd != "" {
customEnd = fmt.Sprintf("/%s", customEnd)
}
uriPath := oriURL.EscapedPath()
if xtream {
uriPath = strings.ReplaceAll(uriPath, c.XtreamUser.PathEscape(), c.User.PathEscape())
uriPath = strings.ReplaceAll(uriPath, c.XtreamPassword.PathEscape(), c.Password.PathEscape())
} else {
uriPath = path.Join("/", c.endpointAntiColision, c.User.PathEscape(), c.Password.PathEscape(), fmt.Sprintf("%d", trackIndex), path.Base(uriPath))
}
basicAuth := oriURL.User.String()
if basicAuth != "" {
basicAuth += "@"
}
newURI := fmt.Sprintf(
"%s://%s%s:%d%s%s",
protocol,
basicAuth,
c.HostConfig.Hostname,
c.AdvertisedPort,
customEnd,
uriPath,
)
newURL, err := url.Parse(newURI)
if err != nil {
return "", err
}
return newURL.String(), nil
}