-
Notifications
You must be signed in to change notification settings - Fork 0
/
Captchan.java
548 lines (487 loc) · 15.5 KB
/
Captchan.java
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
public class Captchan {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception e) {
System.err.println("Unable to load native look and feel");
}
new Captchan();
}
});
}
private String MAX_FILE_SIZE;
private String topicURL;
private String postURL;
private String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; "
+ "rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 GTB7.1";
private final Deque<String> reChallenges = new LinkedList<String>();
private final Deque<String> reResponses = new LinkedList<String>();
private File files[];
private int workingOn;
// matches ${n} and ${n+x}, x is captured
private final Pattern pattern = Pattern.compile("\\$\\{n(?:\\+(\\d*))?\\}");
private final Object lock = new Object();
private volatile boolean isBroken;
private volatile boolean isPaused;
private boolean isStarted;
private boolean isTextSent;
private final CaptchanWindow captchan = new CaptchanWindow();
private final ReCaptchaWindow reCaptcha = new ReCaptchaWindow(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
stopEnteringCaptchas();
}
});
private final WindowAdapter actConfirmStop = new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
int returnValue = JOptionPane.showConfirmDialog(captchan.frame,"Stop?",
"Confirmation",JOptionPane.YES_NO_OPTION,JOptionPane.INFORMATION_MESSAGE);
if(returnValue == JOptionPane.YES_OPTION) {
isBroken = true;
isPaused = false;
synchronized(lock) {
lock.notifyAll();
}
}
}
};
private Captchan() {
setupComponents();
loadSettings();
addListeners();
useFastCloseListener();
initialLog();
captchan.frame.setVisible(true);
}
private void setupComponents() {
captchan.setup();
reCaptcha.setup();
}
private void addListeners() {
captchan.btnSelect.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
java.awt.FileDialog fd = new java.awt.FileDialog(captchan.frame,
"Select multiple files",java.awt.FileDialog.LOAD);
fd.setIconImage(captchan.icon);
fd.setMultipleMode(true);
fd.setFilenameFilter(new java.io.FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
// Open only images
if(dir.isDirectory()) {
return true;
}
name = name.toLowerCase();
if(name.endsWith(".jpg") || name.endsWith(".jpeg") || name.endsWith(".gif")
|| name.endsWith(".png") || name.endsWith(".bmp")) {
return true;
}
return false;
// throw new UnsupportedOperationException("Not supported yet.");
}
});
fd.setVisible(true);
File temp[] = fd.getFiles();
if(temp.length > 0) {
files = temp;
captchan.btnSelect.setText(Integer.toString(files.length) + " Selected...");
}
}
// @Override
// public void actionPerformed(ActionEvent e) {
// final java.awt.Frame f = new java.awt.Frame();
// f.setIconImage(icon);
// final javax.swing.JFileChooser filechooser = new javax.swing.JFileChooser();
// filechooser.setMultiSelectionEnabled(true);
// javax.swing.filechooser.FileFilter filter = new javax.swing.filechooser.FileFilter() {
//
// @Override
// public boolean accept(File pathname) {
// if(pathname.isDirectory()) {
// return true;
// }
//
// String ext = pathname.getName().toLowerCase();
// return ext.endsWith("jpg") || ext.endsWith("jpeg") || ext.endsWith("gif")
// || ext.endsWith("png") || ext.endsWith("bmp");
// }
//
// @Override
// public String getDescription() {
// return "Images";
// }
// };
// filechooser.setFileFilter(filter);
// int returnval = filechooser.showOpenDialog(f);
// if(returnval == javax.swing.JFileChooser.APPROVE_OPTION) {
// files = filechooser.getSelectedFiles();
// captchan.btnSelect.setText(Integer.toString(files.length) + " Selected...");
// }
// }
});
captchan.btnAction.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(isStarted) {
pauseResume();
} else {
start();
}
}
private void start() {
boolean isContent = (files != null) && (files.length > 0);
boolean isBoardChosen = (!captchan.tfBoard.getText().equals(""));
if(isContent && isBoardChosen) {
new Thread(new Runnable() {
@Override
public void run() {
work();
}
}).start();
}
}
private void pauseResume() {
synchronized(lock) {
isPaused = !isPaused;
captchan.btnAction.setText(isPaused?"Resume":"Pause");
lock.notifyAll();
}
}
});
reCaptcha.reBreak.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
stopEnteringCaptchas();
}
});
reCaptcha.reInput.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
boolean isNewChallengeRequested = (reCaptcha.reInput.getText().equals(""));
if(isNewChallengeRequested) {
reCaptcha.newChallenge();
} else {
reChallenges.addFirst(reCaptcha.reChal);
reResponses.addFirst(reCaptcha.reInput.getText());
reCaptcha.reInput.setText("");
reCaptcha.newChallenge();
boolean isAnotherChallenge = (reChallenges.size() + workingOn < files.length);
if(isAnotherChallenge) {
solveCaptchas();
} else {
resetTitles();
}
}
}
});
}
private void stopEnteringCaptchas() {
reCaptcha.reInput.requestFocus();
isBroken = true;
resetTitles();
}
private void initialLog() {
log("* Thread empty = \n new thread");
log("* ${n} in comment = \n # being uploaded");
log("* Queue Msg sends \n the text below in \n the next post once");
log("* Checkbox keeps \n button pressed");
}
private void loadSettings() {
try {
Scanner scan = new Scanner(new File("captchan.ini"));
System.out.println("Loading settings");
String[] settings = {"board=","name=","email=","subject=","comment=","interval="};
DescriptionField[] boxes = {captchan.tfBoard,captchan.tfName,captchan.tfEmail,
captchan.tfSubject,captchan.tfComment,captchan.tfSeconds};
while(scan.hasNextLine()) {
String input = scan.nextLine();
for(int i = 0;i < settings.length;i++) {
boolean isLineContentSetting = (input.toLowerCase().startsWith(settings[i]));
boolean isLineUserAgent = (input.toLowerCase().startsWith("user-ugent="));
if(isLineContentSetting) {
boxes[i].setText(input.substring(settings[i].length()));
break;
} else if(isLineUserAgent) {
userAgent = input.substring("user-agent".length());
break;
}
}
}
scan.close();
} catch(FileNotFoundException e) {
// Don't load default settings
}
}
private void log(String str) {
captchan.log(str);
}
private void work() {
firstTasks();
while(workingOn < files.length) {
allowPause();
if(isBroken) {
// If the user broke the process, exit
updateFiles(workingOn);
break;
}
String result;
try {
log("Uploading " + (workingOn + 1));
log(":" + files[workingOn].getName());
result = upload();
} catch(UploadFailException e) {
log("Upload failed");
continue;
} catch(BadCaptchaException e) {
log("Mistyped captcha");
reCaptcha.newChallenge();
solveCaptchas();
continue;
} catch(FileTooLargeException e) {
log("File too large; skipping");
log(": File size is " + files[workingOn].length());
workingOn++;
continue;
} catch(MaliciousFileException e) {
log("Malicious file");
continue;
} catch(FloodException e) {
log("Flood detected");
int interval = Integer.parseInt(captchan.tfSeconds.getText());
interval += interval / 2;
captchan.tfSeconds.setText(Integer.toString(interval));
sleep();
continue;
} catch(MaxLimitException e) {
log("Max Limit");
isBroken = true;
continue;
} catch(Error404Exception e) {
log("404");
isBroken = true;
continue;
} catch(UploadException e) {
log("Error");
System.err.println("Unknown upload error");
isBroken = true;
continue;
} catch(IOException e) {
log("Error posting");
isBroken = true;
continue;
}
boolean isFirstFile = (workingOn == 0);
boolean isThreadSpecified = (!captchan.tfThread.getText().equals(""));
if(isFirstFile && !isThreadSpecified)
getResourceThread(result);
clearCommentArea();
log("Success");
boolean isLastFile = (workingOn == files.length - 1);
if(isLastFile) {
log("Complete");
break; // don't bother sleeping for the last file
}
sleep();
workingOn++; // If successful, work on the next file
}
done();
}
private void firstTasks() {
log("-----");
log("Start");
initVariables();
log("Max file size: " + MAX_FILE_SIZE);
}
private void initVariables() {
isStarted = true;
isPaused = false;
isBroken = false;
isTextSent = false;
workingOn = 0;
captchan.btnAction.setText("Pause");
captchan.setComponentsEnabled(false);
useConfirmStopListener();
String board = captchan.tfBoard.getText();
String thread = captchan.tfThread.getText();
topicURL = "http://boards.4chan.org/" + board;
postURL = "http://sys.4chan.org/" + board + "/post";
boolean isThreadSpecified = (!thread.equals(""));
if(isThreadSpecified)
topicURL += "/res/" + thread;
boolean isIntervalSpecified = (!captchan.tfSeconds.getText().equals(""));
if(!isIntervalSpecified)
captchan.tfSeconds.setText("60");
MAX_FILE_SIZE = ChanBoard.getMaxFileSize(topicURL);
solveCaptchas();
// After solving captchas, allow pausing
captchan.btnAction.setEnabled(true);
}
private void solveCaptchas() {
boolean isAnotherChallenge = (reChallenges.size() + workingOn < files.length);
if(isAnotherChallenge) {
captchan.frame.setTitle("*Captchan");
boolean isFirstCaptcha = (workingOn == 0);
if(isFirstCaptcha) // If initializing captchas, show progress
reCaptcha.reFrame.setTitle("Enter Captcha (" + (1 + reChallenges.size()) + "/"
+ files.length + ")");
reCaptcha.reFrame.setVisible(true);
}
}
private void allowPause() {
synchronized(lock) {
while(isPaused) {
try {
lock.wait();
} catch(InterruptedException e) {
// nothing
}
}
}
}
private String upload() throws UploadException,IOException {
Uploader uploader = new Uploader();
uploader.setMaxFileSize(MAX_FILE_SIZE);
uploader.setName(captchan.tfName.getText());
uploader.setEmail(captchan.tfEmail.getText());
uploader.setSubject(captchan.tfSubject.getText());
uploader.setComment(getComment());
uploader.setChallenge(reChallenges.removeFirst());
uploader.setResponse(reResponses.removeFirst());
uploader.setFile(files[workingOn]);
uploader.setPostURL(postURL);
uploader.setTopicURL(topicURL);
uploader.setThread(captchan.tfThread.getText());
uploader.setUserAgent(userAgent);
return uploader.upload();
}
private String getComment() {
// combine both comment boxes into one string
String comment = captchan.tfComment.getText();
if(captchan.btnSend.isToggledOn()) {
comment += "\r\n" + captchan.taMsg.getText();
isTextSent = true;
}
comment = numberReplace(comment,workingOn);
return comment;
}
private void clearCommentArea() {
if(captchan.btnSend.isToggledOn() && !captchan.btnSend.isChecked() && isTextSent) {
// if one-time message
captchan.btnSend.setToggledOn(false);
captchan.taMsg.setText("");
}
isTextSent = false;
}
private void updateFiles(final int i) {
// Remove completed files
boolean isEveryFileUploaded = (i == files.length - 1);
if(isEveryFileUploaded) {
files = null;
captchan.btnSelect.setText("Select files...");
} else {
File temp[] = new File[files.length - i];
System.arraycopy(files,i,temp,0,temp.length);
files = temp;
captchan.btnSelect.setText(Integer.toString(files.length) + " Selected...");
}
}
private void done() {
if(isBroken) {
log("Broken");
} else {
captchan.btnSelect.setText("Select Files...");
files = null;
log("Done");
}
useFastCloseListener();
isStarted = false;
captchan.btnAction.setText("Start");
captchan.setComponentsEnabled(true);
}
private void resetTitles() {
captchan.frame.setTitle("Captchan");
reCaptcha.reFrame.setVisible(false);
reCaptcha.reFrame.setTitle("Enter Captcha");
}
private void getResourceThread(String page) {
// Get the newly created thread's ID
int left = page.indexOf("<!-- thread:0,no:") + "<!-- thread:0,no:".length();
boolean isThreadAvailable = (left != -1);
if(isThreadAvailable) {
int right = page.indexOf(" ",left);
captchan.tfThread.setText(page.substring(left,right));
topicURL += "/res/" + captchan.tfThread.getText();
} else {
log("Problem with initial post");
System.err.println(page);
isBroken = true;
}
}
private String numberReplace(String comment, int i) {
Matcher match = pattern.matcher(comment);
StringBuffer sb = new StringBuffer();
while(match.find()) {
int offset = 0;
try {
offset = Integer.parseInt(match.group(1));
} catch(NumberFormatException e) {
// Do nothing
}
match.appendReplacement(sb,Integer.toString(1 + i + offset));
}
return match.appendTail(sb).toString(); // works even if no replacement is made
}
private void sleep() {
long timeToSleep;
try {
timeToSleep = Long.parseLong(captchan.tfSeconds.getText());
} catch(NumberFormatException e) {
captchan.tfSeconds.setText("90");
timeToSleep = 90L;
}
timeToSleep *= 1000L;
long timeStartedAt = System.currentTimeMillis();
synchronized(lock) {
while(!isBroken) {
try {
lock.wait(timeToSleep);
break;
} catch(InterruptedException e) {
long timeInterruptedAt = System.currentTimeMillis();
long timePassed = timeInterruptedAt - timeStartedAt;
timeToSleep -= timePassed;
}
}
}
}
private void useConfirmStopListener() {
captchan.frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
captchan.frame.addWindowListener(actConfirmStop);
}
private void useFastCloseListener() {
captchan.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
captchan.frame.removeWindowListener(actConfirmStop);
}
}