-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlow.java
More file actions
530 lines (478 loc) · 13.9 KB
/
Flow.java
File metadata and controls
530 lines (478 loc) · 13.9 KB
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
package login;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.*;
public class Flow extends JPanel implements ActionListener,FocusListener,ItemListener
{
String acc,pass,temp1,index;
int id=0;
int clicknum=0;
int closePane=0;
DataBaseconnection dbc=new DataBaseconnection();
ResultSet res,res1,res2,res3,res4,res5,res6,res7;
ResultSetMetaData rsmd;
JPanel npanel,mpanel,spanel;
JButton button,button1,exit,trip,setDefault;
JLabel la1,la2,la3,la4,la5,la6;
JComboBox<String> user;
JTextField t4;
JPasswordField pw;
URL url;
JLabel l1,l2,l3,l4,l5,l6,l7;
Random random=new Random();
List list=new ArrayList();
List addList=new ArrayList();
int[] string=new int[4];
ScheduledExecutorService ser1= Executors.newScheduledThreadPool(1);//自动切换账号
ScheduledExecutorService ser3= Executors.newScheduledThreadPool(1);//AutoLogout
ScheduledExecutorService ser4= Executors.newScheduledThreadPool(2);//流量监控
ScheduledExecutorService ser5= Executors.newScheduledThreadPool(1);//AutoLogin
public Flow()
{
npanel=new JPanel();
spanel=new JPanel();
mpanel=new JPanel();
button=new JButton("Summit");
button1=new JButton("Logout");
trip=new JButton("自动切换账号");
exit=new JButton("Exit");
setDefault=new JButton("设置为默认账号");
la1=new JLabel("账号: ",JLabel.RIGHT);
la2=new JLabel("密码: ",JLabel.RIGHT);
la3=new JLabel("将当前账号设置为默认登陆账号,以便下次自动登陆轻松上网",JLabel.CENTER);
la3.setFont(new Font("宋体",Font.BOLD,14));
la4= new JLabel("",JLabel.CENTER);la4.setFont(new Font("宋体",Font.BOLD,20));
la5= new JLabel("",JLabel.CENTER);la5.setFont(new Font("宋体",Font.BOLD,14));
la6= new JLabel("",JLabel.CENTER);la6.setFont(new Font("宋体",Font.BOLD,20));
//获取输入历史
user=new JComboBox<String>();
user.setEditable(true);
res5=dbc.executeQuery("select * from lazer");
user.addItem("");
try {
while (res5.next())
{
user.addItem(res5.getString(1));
}
} catch (SQLException e) {e.printStackTrace();}
user.addFocusListener(this);
user.addItemListener(this);
pw=new JPasswordField(10);
pw.addFocusListener(this);
//
l5=new JLabel("",JLabel.CENTER);
l5.setBackground(Color.cyan);
l6=new JLabel("",JLabel.CENTER);
l7=new JLabel("800",JLabel.CENTER);
t4=new JTextField(10);
//设置默认的流量额度
t4.setText("800");
l1=new JLabel("Username:",JLabel.RIGHT);
l2=new JLabel(" Used(M) :",JLabel.RIGHT);
l3=new JLabel(" Total(M) :",JLabel.RIGHT);
l4=new JLabel("设置额定流量(M) :",JLabel.RIGHT);
init();
}
public void init()
{
npanel.add(la1);npanel.add(user);npanel.add(l1);npanel.add(l5);
npanel.add(la2);npanel.add(pw);npanel.add(l2);npanel.add(l6);
npanel.add(button);npanel.add(button1);npanel.add(l3);
npanel.add(l7);npanel.add(exit);npanel.add(trip);npanel.add(l4);
npanel.add(t4);
npanel.setLayout(new GridLayout(4,4));
mpanel.add(la4);mpanel.add(la5);
mpanel.setLayout(new GridLayout(2,1));
spanel.add(la3);spanel.add(setDefault);spanel.add(la6);
add(mpanel,BorderLayout.NORTH);add(npanel,BorderLayout.CENTER);add(spanel,BorderLayout.SOUTH);
setLayout(new GridLayout(3,1));
setVisible(true);
setBounds(800,0,500,500);
setBackground(Color.cyan);
button.addActionListener(this);
button1.addActionListener(this);
exit.addActionListener(this);
trip.addActionListener(this);
setDefault.addActionListener(this);
/*
* 判断是否有默认的登陆账号,并且字段名不能为某些特殊字符,如default等
*/
res4=dbc.executeQuery("select * from stuacc where defaultacc='yes'");
try
{
while (res4.next())
{
String username=res4.getString(1);
String password=res4.getString(2);
new Login(username,password);
}
ser4.scheduleAtFixedRate(new Update(),0,1000,TimeUnit.MILLISECONDS);//动态显示
}
catch (SQLException e)
{
e.printStackTrace();
}
}
//实现Tab切换文本框
public void focusGained(FocusEvent e) {}
public void focusLost(FocusEvent e) {}
//选择账号并显示密码
public void itemStateChanged(ItemEvent e)
{
if (e.getStateChange()==ItemEvent.SELECTED)
{
res6=dbc.executeQuery("select * from lazer where account='"+user.getSelectedItem()+"'");
try
{
if (res6.next())
{
pw.setText(res6.getString(2));
}
else{pw.setText("");};
} catch (SQLException e1) {e1.printStackTrace();}
}
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand()=="Summit")
{
new Login((String) user.getSelectedItem(),pw.getText());
//判断用户名与密码是否正确
try {
URL url =new URL("http://192.168.31.4:8080/");
HttpURLConnection hurl = (HttpURLConnection) url.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(hurl.getInputStream()));
String temp;
StringBuffer sb= new StringBuffer();
while ((temp=br.readLine())!=null)
{
sb.append(temp);
}
String temp1 = sb.toString();
if (temp1.contains("Password"))
{
JOptionPane.showMessageDialog(null,"Invalid username&&||password","用户名或密码错误",JOptionPane.ERROR_MESSAGE);
}
//确保用户名和密码输入正确
else
{//保留输入历史的同时,保存至数据库
new Lazerinput((String) user.getSelectedItem(),pw.getText());
res1=dbc.executeQuery("select * from stuacc where account='"+(String) user.getSelectedItem()+"'");
try
{
//Check whether there exists acounnt
if (!res1.next())
{
res=dbc.executeQuery("select * from stuacc");
try
{
while (res.next())
{
//遍历到最后
id=Integer.parseInt(res.getString(3));
}
dbc.executeUpdate("insert into stuacc values('"+(String) user.getSelectedItem()+"',"
+ "'"+pw.getText()+"','"+(id+1)+"','')");
}
catch (SQLException e1)
{e1.printStackTrace();}
}
}
catch (HeadlessException e2)
{e2.printStackTrace();}
catch (SQLException e2)
{e2.printStackTrace();}
}
}
catch (HeadlessException e1) {
e1.printStackTrace();
}
catch (MalformedURLException e1) {
e1.printStackTrace();
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
else if(e.getSource()==trip)//自动切换账号按钮
{
/*
* 初始化对即将要自动切换账号的集合
*/
list.clear();
res3=dbc.executeQuery("select * from stuacc where account!=''");
try
{
while (res3.next())
{
list.add(res3.getString(3));
}
}
catch (SQLException e1)
{
e1.printStackTrace();
}
trip.setEnabled(false);
ser1.scheduleAtFixedRate(new TaskSelection(),0,10000,TimeUnit.MILLISECONDS);
}
else if (e.getActionCommand()=="Logout")
{
if (list.size()!=list.size())
{
res=dbc.executeQuery("select * from stuacc where account='"+l5.getText()+"'");
try
{
while(res.next())
{
int media=(Integer.parseInt(res.getString(3))-1);
addList.add(media);
addList.addAll(list);
list.clear();
list.addAll(addList);
}
}
catch (NumberFormatException e1)
{
e1.printStackTrace();
}
catch (SQLException e1)
{
e1.printStackTrace();
}
}
new Logout();
//添加logout之前本机所产生的流量
la4.setText("You hava logged out");
la5.setText("");
l5.setText("");l6.setText("");l7.setText("");//清空
}
else if (e.getSource()==setDefault)
{
SetDefaultAcc sDA=new SetDefaultAcc(l5.getText());
Thread t = new Thread(sDA);
t.start();
}
else if(e.getActionCommand()=="Exit")
{
System.exit(0);
}
}
//自动切换Logout部分类
class AutoLogout extends TimerTask
{
public void run()
{
//Logout
new Logout();
res1=dbc.executeQuery("select * from stuacc where account='"+l5.getText()+"'");
try
{
while (res1.next())
{
list.remove(res1.getString(3));
}
}
catch (SQLException e2)
{
e2.printStackTrace();
}
}
}
//自动切换Login部分
class AutoLogin extends TimerTask
{
public void run()
{
index=(String) list.get(0);
res=dbc.executeQuery("select * from StuAcc where id='"+index+"'");//Pay attention to the blank(空格)
try
{
while (res.next())
{
acc=res.getString(1);
pass=res.getString(2);
System.out.println(acc);
}
new Login(acc,pass);
}
catch (Exception e)
{System.out.println("W");}
//get the original code source
try {
URL url =new URL("http://192.168.31.4:8080/");
HttpURLConnection hurl = (HttpURLConnection) url.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(hurl.getInputStream()));
String temp = null;
StringBuffer sb= new StringBuffer();
while ((temp=br.readLine())!=null)
{
sb.append(temp);
}
temp1 = sb.toString();
}
catch(Exception e){e.printStackTrace();}
while (temp1.contains("Password"))
{
//判断用户名与密码是否正确
list.remove(index);
index=(String) list.get(0);
res=dbc.executeQuery("select * from StuAcc where id='"+index+"'");//Pay attention to the blank(空格)
try
{
while (res.next())
{
acc=res.getString(1);
pass=res.getString(2);
System.out.println(acc);
}
new Login(acc,pass);
//Flush the web page
Thread.sleep(3000);
}catch (Exception ex){ex.printStackTrace();}
try {
URL url =new URL("http://192.168.31.4:8080/");
HttpURLConnection hurl = (HttpURLConnection) url.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(hurl.getInputStream()));
String temp;
StringBuffer sb= new StringBuffer();
while ((temp=br.readLine())!=null)
{
sb.append(temp);
}
temp1 = sb.toString();
}
catch(Exception e){e.printStackTrace();}
}
}
}
//实时监控流量类
class Update extends TimerTask
{
public void run()
{
try {
DecimalFormat df = new DecimalFormat("#0.00");
URL url = new URL("http://192.168.31.4:8080/");
//打开URL
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//得到输入流,即获得了网页的内容
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection
.getInputStream()));
String line;
StringBuffer sb=new StringBuffer();
while((line = reader.readLine()) != null)
{
sb.append(line);//asp页面打印的内容,注意是整个页面内容,包括HTML标签
}
String acc=sb.substring(2896,2922);//Used Bytes
String stacc=sb.substring(2550,2580);//Account
String total = sb.substring(3250,3280);//Total Flow
int numm=stacc.indexOf(">");
int num=stacc.indexOf("<");
String accou=stacc.substring(numm+1,num);
int num1 = sb.indexOf("Used bytes");
String account= sb.substring(num1,num1+10);
String regEx="[^0-9]";
Pattern p = Pattern.compile(regEx);
Matcher m1 = p.matcher(acc);
Matcher m2 = p.matcher(total);
String mrp1=m1.replaceAll("").trim();
String mrp2=m2.replaceAll("").trim();
double usedflow1=Double.parseDouble(mrp1);
double usedflow2=Double.parseDouble(mrp2);
String mused=df.format(usedflow1/(1024*1024));
String mused1=df.format(usedflow2/(1024*1024));
l5.setText(accou);l6.setText(mused);l7.setText(mused1);
//用户联网状态
if (account.equals("Used bytes"))
{
la4.setText(accou+"------Connected------");
}
else
{
la4.setText("You have logged out");
la5.setText("");
}
//判断流量是否超过指定额度流量
if (Double.parseDouble(l6.getText())>=Double.parseDouble(t4.getText()))
{
la5.setText("Red Warning :"+accou+"的账号已经超过额定流量,您还可使用 :"+df.format(Double.parseDouble(mused1)-Double.parseDouble(mused))+"M");
}
else {la5.setText(accou+"账号流量在指定范围内,请放心使用");}
//显示默认登陆账号
res=dbc.executeQuery("select * from stuacc where defaultacc='yes'");
if (res.next())
{
la6.setText("当前的默认的自动登陆账号为 : "+res.getString(1));
}
else {la6.setText("");}
}
catch (Exception e)
{}
}
}
//定时器
class TaskSelection extends TimerTask
{
public void run()
{
try
{
/*
* 防止多次弹窗
*/
if (list.size()==0&&closePane==0)
{
try
{
la4.setText("");la5.setText("流量群已全爆完,等着挨揍吧,少年");
l5.setText("");l6.setText("");l7.setText("");
closePane+=1;
trip.setEnabled(false);
ser1.shutdownNow();
}
catch (Exception e)
{
e.printStackTrace();
}
}
if ((Double.parseDouble(l6.getText())>=Double.parseDouble(t4.getText()))&&list.size()!=0)
{
ser5.schedule(new AutoLogout(),0,TimeUnit.MILLISECONDS);
Thread.sleep(5000);
ser3.schedule(new AutoLogin(),0,TimeUnit.MILLISECONDS);//缓冲Logout
}
}
catch (Exception e) { }
}
}
}