forked from scakemyer/quasar
-
Notifications
You must be signed in to change notification settings - Fork 44
/
pastebin.go
204 lines (181 loc) · 4.88 KB
/
pastebin.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
package api
import (
"bytes"
"fmt"
"io"
"net/http"
"net/url"
"os/user"
humanize "github.com/dustin/go-humanize"
"github.com/elgatito/elementum/config"
"github.com/elgatito/elementum/util/ip"
"github.com/elgatito/elementum/xbmc"
"github.com/gin-gonic/gin"
"github.com/goccy/go-json"
)
// PasteProject describes each pastebin project
type PasteProject struct {
Name string
URL string
BaseURL string
IsJSON bool
IsRAW bool
Fields PasteFields
Values PasteFields
}
// PasteFields describes [string]string values for projects
type PasteFields struct {
Title string
Poster string
Syntax string
Expiration string
Content string
}
var pasteProjects = []PasteProject{
{
URL: "https://paste.kodi.tv/documents",
BaseURL: "https://paste.kodi.tv",
Name: "Kodi.tv Hastebin",
IsRAW: true,
},
{
URL: "https://paste.ubuntu.com/",
BaseURL: "https://paste.ubuntu.com",
Name: "Ubuntu Pastebin",
Fields: PasteFields{
Poster: "poster",
Syntax: "syntax",
Expiration: "expiration",
Content: "content",
},
Values: PasteFields{
Syntax: "text",
},
},
{
URL: "https://paste.fedoraproject.org/api/paste/submit",
BaseURL: "https://paste.fedoraproject.org",
Name: "Fedora Pastebin",
IsJSON: true,
Fields: PasteFields{
Title: "title",
Syntax: "language",
Expiration: "expiry_time",
Content: "contents",
},
Values: PasteFields{
Syntax: "text",
},
},
}
// Pastebin uploads /debug/:type to pastebin
func Pastebin(ctx *gin.Context) {
xbmcHost, _ := xbmc.GetXBMCHostWithContext(ctx)
if xbmcHost == nil {
return
}
dialog := xbmcHost.NewDialogProgressBG("Elementum", "LOCALIZE[30457]", "LOCALIZE[30457]")
if dialog != nil {
dialog.Update(0, "Elementum", "LOCALIZE[30457]")
}
pasteURL := ""
defer func() {
if dialog != nil {
dialog.Close()
}
if pasteURL != "" {
xbmcHost.Dialog("Elementum", "LOCALIZE[30454];;"+pasteURL)
}
}()
rurl := fmt.Sprintf("http://%s:%d%s%s", ip.GetLocalHost(), config.Args.LocalPort, "/debug/", ctx.Params.ByName("type"))
log.Infof("Requesting %s before uploading to pastebin", rurl)
resp, err := http.Get(rurl)
if err != nil || resp == nil {
log.Infof("Could not get %s: %#v", rurl, err)
return
}
defer resp.Body.Close()
content, _ := io.ReadAll(resp.Body)
// u, err := user.Current()
// if err != nil {
// u = &user.User{
// Name: "Elementum Uploader",
// Username: "Elementum Uploader",
// }
// }
u := &user.User{
Name: "Elementum Uploader",
Username: "Elementum Uploader",
}
for _, p := range pasteProjects {
log.Infof("Uploading to %#v, %s bytes", p, humanize.Bytes(uint64(len(content))))
values := url.Values{}
if !p.IsRAW {
if p.Fields.Poster != "" {
values.Set(p.Fields.Poster, u.Name)
}
if p.Fields.Syntax != "" {
values.Set(p.Fields.Syntax, p.Values.Syntax)
}
if p.Fields.Expiration != "" {
values.Set(p.Fields.Expiration, p.Values.Expiration)
}
if p.Fields.Title != "" {
values.Set(p.Fields.Title, rurl)
}
values.Set(p.Fields.Content, string(content))
}
var resp *http.Response
var err error
log.Infof("Doing upload to %s", p.URL)
if p.IsRAW {
resp, err = http.Post(p.URL, "application/x-www-form-urlencoded", bytes.NewReader(content))
if err != nil {
log.Errorf("Error creating http request: %s", err)
continue
}
} else if !p.IsJSON {
resp, err = http.PostForm(p.URL, values)
} else {
jsonValue, _ := json.Marshal(values)
resp, err = http.Post(p.URL, "application/json", bytes.NewBuffer(jsonValue))
}
if err != nil {
log.Noticef("Could not upload log file. Error: %#v", err)
continue
} else if resp != nil && resp.StatusCode != 200 {
log.Noticef("Could not upload log file. Status: %#v", resp.StatusCode)
continue
}
defer resp.Body.Close()
if !p.IsJSON && !p.IsRAW && resp != nil && resp.Request != nil && resp.Request.URL != nil {
pasteURL = resp.Request.URL.String()
} else if resp != nil {
content, _ := io.ReadAll(resp.Body)
var respData map[string]*json.RawMessage
if err := json.Unmarshal(content, &respData); err != nil {
log.Warningf("Could not unmarshal response: %s", err)
continue
}
log.Infof("Got response: %#v", respData)
if val, ok := respData["url"]; ok && val != nil {
if err := json.Unmarshal(*val, &pasteURL); err != nil {
log.Warningf("Could not unmarshal response: %s", err)
continue
}
} else if val, ok := respData["key"]; ok && val != nil {
if err := json.Unmarshal(*val, &pasteURL); err != nil {
log.Warningf("Could not unmarshal response: %s", err)
continue
}
pasteURL = p.BaseURL + "/" + pasteURL
}
}
if pasteURL == p.BaseURL || pasteURL == p.BaseURL+"/" {
log.Warningf("Trying next Paste service, because there is no key in url: %s", pasteURL)
continue
}
log.Noticef("Log uploaded to: %s", pasteURL)
return
}
}