-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathconnection.go
125 lines (103 loc) · 3 KB
/
connection.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
package airplay
import (
"crypto/md5"
"errors"
"fmt"
"io"
"net/http"
"regexp"
)
const (
digestAuthUsername = "AirPlay"
digestAuthRealm = "AirPlay"
)
type connection struct {
device Device
passwordHash string
}
func newConnection(device Device) *connection {
return &connection{device: device}
}
func (c *connection) setPassword(password string) {
c.passwordHash = fmt.Sprintf("%x", md5.Sum([]byte(digestAuthUsername+":"+digestAuthRealm+":"+password)))
}
func (c *connection) get(path string) (*http.Response, error) {
return c.getWithHeader(path, http.Header{})
}
func (c *connection) getWithHeader(path string, header http.Header) (*http.Response, error) {
return c.request("GET", path, nil, header)
}
func (c *connection) post(path string, body io.ReadSeeker) (*http.Response, error) {
return c.postWithHeader(path, body, http.Header{})
}
func (c *connection) postWithHeader(path string, body io.ReadSeeker, header http.Header) (*http.Response, error) {
return c.request("POST", path, body, header)
}
func (c *connection) request(method, path string, body io.ReadSeeker, header http.Header) (*http.Response, error) {
response, err := c.do(method, path, body, header)
if err != nil {
return nil, err
}
if response.StatusCode == http.StatusUnauthorized {
if c.passwordHash == "" {
msg := fmt.Sprintf(
"airplay: [ERR] Device %s:%d is required password",
c.device.Addr,
c.device.Port,
)
return nil, errors.New(msg)
}
token := c.authorizationHeader(response, method, path, header)
// body is closed first c.do().
body.Seek(0, 0)
header.Add("Authorization", token)
response, err = c.do(method, path, body, header)
if err != nil {
return nil, err
}
if response.StatusCode == http.StatusUnauthorized {
msg := fmt.Sprintf(
"airplay: [ERR] Wrong password to %s:%d",
c.device.Addr,
c.device.Port,
)
return nil, errors.New(msg)
}
}
return response, nil
}
func (c *connection) do(method, path string, body io.ReadSeeker, header http.Header) (*http.Response, error) {
req, err := http.NewRequest(method, c.endpoint()+path, body)
if err != nil {
return nil, err
}
req.Header = header
client := &http.Client{}
response, err := client.Do(req)
if err != nil {
return nil, err
}
return response, nil
}
func (c *connection) endpoint() string {
return fmt.Sprintf("http://%s:%d/", c.device.Addr, c.device.Port)
}
func (c *connection) authorizationHeader(response *http.Response, method, path string, header http.Header) string {
pattern := regexp.MustCompile("^Digest .*nonce=\"([^\"]+)\"")
results := pattern.FindStringSubmatch(response.Header.Get("Www-Authenticate"))
if results == nil {
return ""
}
nonce := results[1]
a1 := c.passwordHash
a2 := fmt.Sprintf("%x", md5.Sum([]byte(method+":/"+path)))
resp := fmt.Sprintf("%x", md5.Sum([]byte(a1+":"+nonce+":"+a2)))
return fmt.Sprintf(
"Digest username=\"%s\", realm=\"%s\", uri=\"/%s\", nonce=\"%s\", response=\"%s\"",
digestAuthUsername,
digestAuthRealm,
path,
nonce,
resp,
)
}