-
Notifications
You must be signed in to change notification settings - Fork 8
/
targets.go
190 lines (159 loc) · 4.83 KB
/
targets.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
package main
import (
"bytes"
"compress/gzip"
"crypto/tls"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"reflect"
"strings"
"golang.org/x/exp/slices"
)
// nzb file target structure
type Target struct {
name string
getCategories func() (Categories, error)
push func(string, string) error
}
// nzb file targets map
type Targets map[string]Target
// global nzb files targets map
var targets = Targets{
"NZBGET": Target{
name: "NZBGet",
getCategories: nzbget_getCategories,
push: nzbget_push,
},
"SABNZBD": Target{
name: "SABnzbd",
getCategories: sabnzbd_getCategories,
push: sabnzbd_push,
},
"SYNOLOGYDLS": Target{
name: "Synology DownloadStation",
push: synologyds_push,
},
"EXECUTE": Target{
name: "Download folder",
push: execute_push,
},
}
// http request function for the targets
func request(conf interface{}, httpMethod string, path string, headers map[string]string, queryParameters url.Values, body io.Reader, contentType string) ([]byte, error) {
values := reflect.ValueOf(conf)
transportCfg := http.DefaultTransport.(*http.Transport).Clone()
// generate URL
var scheme string
if values.FieldByName("Ssl").Bool() {
scheme = "https://"
if values.FieldByName("SkipCheck").Bool() {
transportCfg.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}
} else {
scheme = "http://"
}
fullUrl := fmt.Sprintf("%s%s", scheme, values.FieldByName("Host").String())
if values.FieldByName("Port").Int() > 0 {
fullUrl += fmt.Sprintf(":%d", values.FieldByName("Port").Int())
}
if strings.Trim(values.FieldByName("Basepath").String(), " /") != "" {
fullUrl += fmt.Sprintf("/%s", strings.Trim(values.FieldByName("Basepath").String(), " /"))
}
if path != "" {
fullUrl += fmt.Sprintf("/%s", strings.Trim(path, " /"))
}
// set up client
client := &http.Client{Transport: transportCfg}
u, err := url.Parse(fullUrl)
if err != nil {
return nil, err
}
// append the query parameters.
q := u.Query()
for k, v := range queryParameters {
q.Set(k, strings.Join(v, ","))
}
// set the query to the encoded parameters
u.RawQuery = q.Encode()
// regardless of GET or POST, we can safely add the body
req, err := http.NewRequest(httpMethod, u.String(), body)
if err != nil {
return nil, err
}
// for each header passed, add the header value to the request
for k, v := range headers {
req.Header.Set(k, v)
}
// if content type is provided, add to header
if contentType != "" {
req.Header.Set("Content-Type", contentType)
}
// if basic auth username and password are set add auth header
if values.FieldByName("BasicauthUsername").String() != "" && values.FieldByName("BasicauthPassword").String() != "" {
req.SetBasicAuth(values.FieldByName("BasicauthUsername").String(), values.FieldByName("BasicauthPassword").String())
}
// finally, do the request
res, err := client.Do(req)
if err != nil {
return nil, err
}
if res == nil {
return nil, fmt.Errorf("calling %s returned empty response", u.String())
}
defer res.Body.Close()
// read the response data
responseData, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("error calling %s:\nstatus: %s\nresponseData: %s", u.String(), res.Status, responseData)
}
return responseData, nil
}
func createMultipartBody(nzb string, filename string, compression string, compressionTypes []string) (*bytes.Buffer, string, error) {
availableCompressions := map[string]func(*bytes.Buffer) io.WriteCloser{
"zip": func(buffer *bytes.Buffer) io.WriteCloser {
return gzip.NewWriter(buffer)
},
}
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("nzbfile", filename)
if err != nil {
return nil, "", err
}
if _, ok := availableCompressions[compression]; ok && slices.Contains(compressionTypes, compression) {
before := len(nzb)
fmt.Println()
Log.Info("Compression ...")
Log.Info("Uncompressed: %12s", prettyByteSize(before))
compressedBuffer := &bytes.Buffer{}
compressionWriter := availableCompressions[compression](compressedBuffer)
if _, err := io.Copy(compressionWriter, strings.NewReader(nzb)); err != nil {
compressionWriter.Close()
return nil, "", err
}
if err := compressionWriter.Close(); err != nil {
return nil, "", err
}
after := compressedBuffer.Len()
Log.Info("Compressed: %14s", prettyByteSize(after))
Log.Info("Compression: %12s", fmt.Sprintf("-%.2f%s", 100-(float64(after)/float64(before)*100), " %"))
if _, err := io.Copy(part, compressedBuffer); err != nil {
return nil, "", err
}
} else {
if _, err := io.Copy(part, strings.NewReader(nzb)); err != nil {
return nil, "", err
}
}
contentType := writer.FormDataContentType()
if err := writer.Close(); err != nil {
return nil, "", err
}
return body, contentType, nil
}