forked from dweymouth/go-jellyfin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
274 lines (234 loc) · 6 KB
/
client.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package jellyfin
import (
"bytes"
"context"
"crypto/md5"
"encoding/json"
"errors"
"fmt"
"io"
"math/rand"
"net"
"net/http"
"net/url"
"os"
"runtime"
"strings"
"time"
)
const (
DefaultTimeOut = 30 * time.Second
)
// Client is the root struct for all Jellyfin API calls
type Client struct {
HTTPClient *http.Client
baseURL *url.URL
ClientName string
ClientVersion string
loggedIn bool
token string
serverID string
username string
userID string
deviceID string // needs to be unique for a user+device combo
}
// NewClient creates a jellyfin Client using the url provided.
func NewClient(urlStr, clientName, clientVersion string, opts ...ClientOptionFunc) (*Client, error) {
// validate the baseurl
if urlStr == "" {
return nil, errors.New("url must be provided")
}
if !strings.HasSuffix(urlStr, "/") {
urlStr += "/"
}
baseURL, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
cli := &Client{
HTTPClient: &http.Client{
Timeout: DefaultTimeOut,
},
baseURL: baseURL,
ClientName: clientName,
ClientVersion: clientVersion,
}
// perform any options provided
for _, option := range opts {
option(cli)
}
return cli, nil
}
// ClientOptionFunc can be used to customize a new jellyfin API client.
type ClientOptionFunc func(*Client)
// Http timeout override.
func WithTimeout(timeout time.Duration) ClientOptionFunc {
return func(c *Client) {
c.HTTPClient.Timeout = timeout
}
}
// Http client override.
func WithHTTPClient(httpClient *http.Client) ClientOptionFunc {
return func(c *Client) {
c.HTTPClient = httpClient
}
}
// BaseURL return a copy of the baseURL.
func (c *Client) BaseURL() *url.URL {
u := *c.baseURL
return &u
}
type loginResponse struct {
User userResponse `json:"User"`
Token string `json:"AccessToken"`
ServerId string `json:"ServerId"`
}
type userResponse struct {
Name string `json:"Name"`
ServerId string `json:"ServerId"`
UserId string `json:"Id"`
}
// Login authenticates a user into the server provided in Client.
// If the login is successful, the access token is stored for future API calls.
func (c *Client) Login(username, password string) error {
body := map[string]string{
"Username": username,
"PW": password,
}
u, err := url.JoinPath(c.BaseURL().String(), "/Users/authenticatebyname")
if err != nil {
return fmt.Errorf("unable to parse url path: %w", err)
}
b := &bytes.Buffer{}
if err := json.NewEncoder(b).Encode(body); err != nil {
return fmt.Errorf("unable to encode body: %w", err)
}
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, u, b)
if err != nil {
return fmt.Errorf("failed to login: %w", err)
}
req.Header.Set("X-Emby-Authorization", c.authHeader())
req.Header.Set("Content-Type", "application/json")
resp, err := c.HTTPClient.Do(req)
if err != nil {
return fmt.Errorf("failed to login: %w", err)
}
defer resp.Body.Close()
switch resp.StatusCode {
case http.StatusOK:
c.loggedIn = true
dto := loginResponse{}
err := json.NewDecoder(resp.Body).Decode(&dto)
if err != nil {
return fmt.Errorf("invalid login response: %w", err)
}
c.token = dto.Token
c.serverID = dto.ServerId
c.username = username
c.userID = dto.User.UserId
c.deviceID = "" // recalculate it next request, should be different per username
case http.StatusBadRequest:
reason, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("login failed: %w", err)
} else {
return fmt.Errorf("login failed: %s", reason)
}
default:
reason, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("login failed: %w", err)
} else {
return fmt.Errorf("login failed: %s", reason)
}
}
return nil
}
type PingResponse struct {
LocalAddress string
ServerName string
Version string
ProductName string
OperatingSystem string
Id string
}
// Ping queries the jellyfin server for a response.
// Return is some basic information about the jellyfin server.
func (c *Client) Ping() (*PingResponse, error) {
body, err := c.get("/System/Info/Public", nil)
if err != nil {
return nil, err
}
defer body.Close()
res := &PingResponse{}
err = json.NewDecoder(body).Decode(res)
if err != nil {
return nil, fmt.Errorf("invalid json response: %v", err)
}
return res, nil
}
// LoggedInUser returns the user associated with this Client.
func (c *Client) LoggedInUser() string {
return c.username
}
func (c *Client) authHeader() string {
auth := fmt.Sprintf("MediaBrowser Client=\"%s\", Device=\"%s\", DeviceId=\"%s\", Version=\"%s\"",
c.ClientName, deviceName(), c.ensureDeviceID(), c.ClientVersion)
return auth
}
func (c *Client) ensureDeviceID() string {
if c.deviceID == "" {
mac, err := macaddress()
if err != nil {
mac = randomKey(16)
}
c.deviceID = fmt.Sprintf("%x", md5.Sum([]byte(mac+c.username)))
}
return c.deviceID
}
func deviceName() string {
hostname, err := os.Hostname()
if err != nil {
switch runtime.GOOS {
case "darwin":
hostname = "mac"
default:
hostname = runtime.GOOS
}
}
return hostname
}
const letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
// randomKey returns a string at the length desired of random mixed-case letters.
func randomKey(length int) string {
data := make([]byte, length)
for i := range data {
data[i] = letters[rand.Intn(len(letters))]
}
return string(data)
}
// adapted from https://gist.github.com/tsilvers/085c5f39430ced605d970094edf167ba
func macaddress() (string, error) {
interfaces, err := net.Interfaces()
if err != nil {
return "", errors.New("failed to get net interfaces")
}
for _, i := range interfaces {
if i.Flags&net.FlagUp != 0 && !bytes.Equal(i.HardwareAddr, nil) {
// Skip locally administered addresses
if i.HardwareAddr[0]&2 == 2 {
continue
}
var mac uint64
for j, b := range i.HardwareAddr {
if j >= 8 {
break
}
mac <<= 8
mac += uint64(b)
}
return fmt.Sprintf("%16.16X", mac), nil
}
}
return "", errors.New("no mac address found")
}