-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintporn.go
354 lines (283 loc) · 9.11 KB
/
intporn.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package forumposter
import (
"bytes"
"encoding/json"
"fmt"
"mime/multipart"
"regexp"
"strings"
"github.com/PuerkitoBio/goquery"
log "github.com/sirupsen/logrus"
)
//IntPornInfoSite provides the info to make https request for post
type IntPornInfoSite struct {
URL string
User string
Password string
F string // Forum number, for new post
T string // Thread Number, for reply
CSRF string // csrf for POST
AttachmentHash string
AttachmentHashCombined string
LastDate string
LastKnowDate string
}
type intPornReponse struct {
Status string `json:"status"`
HTML struct {
Content string `json:"content"`
CSS []string `json:"css"`
Js []string `json:"js"`
} `json:"html"`
LastDate int `json:"lastDate"`
Visitor struct {
ConversationsUnread string `json:"conversations_unread"`
AlertsUnread string `json:"alerts_unread"`
TotalUnread string `json:"total_unread"`
} `json:"visitor"`
Message string `json:"message"`
Redirect string `json:"redirect"`
}
//IntPornLogin function to make login. Return Error
func (c *Collector) IntPornLogin(i IntPornInfoSite, url string) error {
postLogin := &Request{
Body: nil,
URL: url,
Method: "POST",
Writer: nil,
}
resp, err := c.fetch(postLogin)
if err != nil {
return err
}
log.Traceln("[Forum-Poster] Response:", string(resp))
// Check if Login
if err := i.checkLogin(string(resp)); err != nil {
return err
}
return nil
}
// Check class .p-navgroup-linkText and looks for the username
func (i *IntPornInfoSite) checkLogin(p string) error {
// Load the HTML document
log.Debugf("Looking for username %s into page", i.User)
doc, err := goquery.NewDocumentFromReader(strings.NewReader(string(p)))
if err != nil {
log.Fatal(err)
}
// Extract
// * username
found := false
doc.Find(".p-navgroup-linkText").Each(func(n int, s *goquery.Selection) {
if s.Text() == i.User {
log.Debugf("Login Check Successfull")
found = true
}
})
if !found {
return ErrLoginFailed
}
return nil
}
func (i *IntPornInfoSite) getCSRF(p string) error {
// Load the HTML document
log.Debugln("Extracting CSRF")
doc, err := goquery.NewDocumentFromReader(strings.NewReader(string(p)))
if err != nil {
return fmt.Errorf("Can't decode for extract CSRF")
}
// Extract
// * csrf
var ok bool
i.CSRF, ok = doc.Find("input[name='_xfToken']").Attr("value")
if !ok {
return fmt.Errorf("Can't find form_token")
}
return nil
}
// @p: page's code
// @reply: new or reply
func (i *IntPornInfoSite) getValuePost(p string, action string) error {
// Extract:
// attachment_hash
// attachment_hash_combined
// last_date
// last_known_date
// Load the HTML document
log.Debugln("Extracting Value for Post")
doc, err := goquery.NewDocumentFromReader(strings.NewReader(string(p)))
if err != nil {
return fmt.Errorf("Can't decode for extract value for post")
}
var ok bool
i.AttachmentHash, ok = doc.Find("input[name='attachment_hash']").Attr("value")
if !ok {
return fmt.Errorf("Can't find attachment_hash")
}
i.AttachmentHashCombined, ok = doc.Find("input[name='attachment_hash_combined']").Attr("value")
if !ok {
return fmt.Errorf("Can't find attachment_hash_combined")
}
if action == "reply" {
i.LastDate, ok = doc.Find("input[name='last_date']").Attr("value")
if !ok {
return fmt.Errorf("Can't find last_date")
}
i.LastKnowDate, ok = doc.Find("input[name='last_known_date']").Attr("value")
if !ok {
return fmt.Errorf("Can't find last_known_date")
}
}
return nil
}
// IntPorn function to post to IntoPorn.com
func (c *Collector) IntPorn(i IntPornInfoSite, p Payload, a string) (string, error) {
var url string
//var action string
// Read Homepgae to extract CSRF
homePage := &Request{
URL: fmt.Sprintf("%s/", i.URL),
Method: "GET",
}
hp, err := c.fetch(homePage)
if err != nil {
return "", err
}
log.Traceln("[Forum-Poster]IntPorn - HomePage Response", string(hp))
// Get CSRF
if err := i.getCSRF(string(hp)); err != nil {
return "", fmt.Errorf("[Forum-Poster]IntPorn - Can not get CSRF: %s", err)
}
log.WithFields(log.Fields{
"csrf": i.CSRF,
"URL": fmt.Sprintf("%s/", i.URL),
"Version": c.Version,
}).Debug("[Forum-Poster] - Extract Values")
// Check if login is still available
if err := i.checkLogin(string(hp)); err != nil {
log.Debugf("[Forum-Poster]IntPorn - Make new login")
url = fmt.Sprintf("%s/login/login?login=%s&password=%s&remember=1&_xfRedirect=%s&_xfToken=%s", i.URL, i.User, i.Password, i.URL, i.CSRF)
if err := c.IntPornLogin(i, url); err != nil {
return "", fmt.Errorf("[Forum-Poster]IntPorn Login - %s", err)
}
}
// Set post NEW or REPLY
switch a {
case "new":
log.Infoln("* Post new thread to", i.URL)
//https://www.intporn.org/forums/test/post-thread
url = fmt.Sprintf("%s/forums/%s/post-thread", i.URL, i.F)
case "reply":
log.Infoln("* Reply thread to", i.T)
url = fmt.Sprintf("%s/threads/%s", i.URL, i.T)
default:
return "", fmt.Errorf("[Forum-Poster]IntPorn - Choice are: new or reply. Set the right one")
}
// For post new/reply, forum needs exact URL like
// https://www.intporn.org/threads/test.1234567/
// where to make request, so make first request only with T
// number, and save response URL
readRealThread := &Request{
Body: nil,
URL: url,
Method: "GET",
Writer: nil,
}
log.WithFields(log.Fields{
"readRealThread": readRealThread,
"URL": url,
}).Debug("[Forum-Poster]VBulletin - Extract Values")
body, err := c.fetch(readRealThread)
if err != nil {
return "", fmt.Errorf("[Forum-Poster]IntPorn Read Page for Get Data - %s", err)
}
log.Traceln("[Forum-Poster]IntPorn - Real Thread Response", string(body))
// Extract value for post data
if err := i.getValuePost(string(body), a); err != nil {
return "", fmt.Errorf("[Forum-Poster]IntPorn Extract Data - %s", err)
}
// Get CSRF
if err := i.getCSRF(string(body)); err != nil {
return "", fmt.Errorf("[Forum-Poster]IntPorn - Can not get CSRF: %s", err)
}
// Get last part of URL
xfRequestUris := strings.Split(c.FinalURL, "threads")
xfRequestURI := fmt.Sprintf("/threads%s", xfRequestUris[len(xfRequestUris)-1])
// Make URL to Post
// Set post NEW or REPLY
// Post Reply Thread
postload := &bytes.Buffer{}
writerLoad := multipart.NewWriter(postload)
_ = writerLoad.WriteField("attachment_hash", i.AttachmentHash)
_ = writerLoad.WriteField("attachment_hash_combined", i.AttachmentHashCombined)
_ = writerLoad.WriteField("message_html", p.Message)
_ = writerLoad.WriteField("_xfToken", i.CSRF)
_ = writerLoad.WriteField("_xfWithData", "1")
_ = writerLoad.WriteField("_xfToken", i.CSRF)
_ = writerLoad.WriteField("_xfResponseType", "json")
switch a {
case "new":
_ = writerLoad.WriteField("title", p.Title)
_ = writerLoad.WriteField("tags", p.Tags)
_ = writerLoad.WriteField("watch_thread", "1")
_ = writerLoad.WriteField("watch_thread_email", "1")
_ = writerLoad.WriteField("_xfSet[watch_thread]", "1")
_ = writerLoad.WriteField("poll[question]", "")
_ = writerLoad.WriteField("poll[new_responses][]", "")
_ = writerLoad.WriteField("poll[max_votes_type]", "single")
_ = writerLoad.WriteField("poll[change_vote]", "1")
_ = writerLoad.WriteField("_xfRequestUri", fmt.Sprintf("/forums/%s/post-thread", i.F))
log.Infoln("* Post new thread to", i.URL)
case "reply":
_ = writerLoad.WriteField("_xfRequestUri", xfRequestURI) // /threads/testing.1874299/
_ = writerLoad.WriteField("last_date", i.LastDate)
_ = writerLoad.WriteField("last_known_date", i.LastKnowDate)
url = fmt.Sprintf("%sadd-reply", c.FinalURL)
default:
return "", fmt.Errorf("[Forum-Poster]IntPorn - Choice are: new or reply. Set the right one")
}
log.WithFields(log.Fields{
"attachment_hash": i.AttachmentHash,
"attachment_hash_combined": i.AttachmentHashCombined,
"last_date": i.LastDate,
"last_known_date": i.LastKnowDate,
"Thread": i.T,
"URL": url,
"URL Redirected": c.FinalURL,
"CSRF": i.CSRF,
"xfRequestUri": xfRequestURI,
}).Debug("[Forum-Poster]IntPorn - Extract Values")
err = writerLoad.Close()
if err != nil {
fmt.Println(err)
return "", fmt.Errorf("[Forum-Poster]IntPorn - Post Thread %v", err)
}
log.Debugln("Posting to FORUM ID", i.F)
postThread := &Request{
Body: postload,
URL: url,
Method: "POST",
Writer: writerLoad,
}
resp, err := c.fetch(postThread)
if err != nil {
return "", err
}
log.Debugln(string(resp))
log.Traceln("[Forum-Poster]IntPorn - Response:", string(resp))
// Unmarshal Response
rp := intPornReponse{}
if err := json.Unmarshal(resp, &rp); err != nil {
return "", fmt.Errorf("[Forum-Poster]IntPorn - Unmarshal Response %v", err)
}
if rp.Status != "ok" {
return "", fmt.Errorf("[Forum-Poster]IntPorn - Not Posted")
}
if a == "new" {
return rp.Redirect, nil
}
// Get ID of post
var re = regexp.MustCompile(`(?m)post-(\d+)`)
id := re.FindString(rp.HTML.Content)
return fmt.Sprintf("%s%s%s", i.URL, xfRequestURI, id), nil
}