Mercurial > 510Connectbot
comparison src/net/sourceforge/jsocks/SocksDialog.java @ 349:205ee2873330
update jsocks to 2011-03-19
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Fri, 01 Aug 2014 11:23:10 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
348:29076621bab0 | 349:205ee2873330 |
---|---|
1 package net.sourceforge.jsocks; | |
2 import java.awt.*; | |
3 import java.awt.event.*; | |
4 | |
5 /** | |
6 Socks configuration dialog.<br> | |
7 Class which provides GUI means of getting CProxy configuration | |
8 from the user. | |
9 */ | |
10 public class SocksDialog extends Dialog | |
11 implements WindowListener, | |
12 ItemListener, | |
13 ActionListener, | |
14 Runnable{ | |
15 | |
16 //GUI components | |
17 TextField host_text, | |
18 port_text, | |
19 user_text, | |
20 password_text, | |
21 direct_text; | |
22 Button add_button, | |
23 remove_button, | |
24 cancel_button, | |
25 ok_button, | |
26 dismiss_button; | |
27 List direct_list; | |
28 Checkbox socks4radio, | |
29 socks5radio, | |
30 none_check, | |
31 up_check, | |
32 gssapi_check; | |
33 | |
34 Dialog warning_dialog; | |
35 Label warning_label; | |
36 | |
37 String host,user,password; | |
38 int port; | |
39 Thread net_thread = null; | |
40 | |
41 //CheckboxGroups | |
42 CheckboxGroup socks_group = new CheckboxGroup(); | |
43 | |
44 CProxy proxy; | |
45 InetRange ir; | |
46 | |
47 final static int COMMAND_MODE = 0; | |
48 final static int OK_MODE = 1; | |
49 | |
50 int mode; | |
51 | |
52 /**Wether to resolve addresses in separate thread. | |
53 <p> | |
54 Default value is true, however on some JVMs, namely one from | |
55 the Microsoft, it doesn't want to work properly, separate thread | |
56 can't close the dialog opened in GUI thread, and everuthing else | |
57 is then crashes. | |
58 <p> | |
59 When setting this variable to false, SocksDialog will block while | |
60 trying to look up proxy host, and if this takes considerable amount | |
61 of time it might be annoying to user. | |
62 | |
63 */ | |
64 public static boolean useThreads = true; | |
65 | |
66 | |
67 // Constructors | |
68 //////////////////////////////////// | |
69 /** | |
70 Creates SOCKS configuration dialog.<br> | |
71 Uses default initialisation:<br> | |
72 CProxy host: socks-proxy <br> | |
73 CProxy port: 1080 <br> | |
74 Version: 5<br> | |
75 | |
76 */ | |
77 public SocksDialog(Frame parent){ | |
78 this(parent,null); | |
79 } | |
80 /** | |
81 Creates SOCKS configuration dialog and initialises it | |
82 to given proxy. | |
83 */ | |
84 public SocksDialog(Frame parent,CProxy init_proxy){ | |
85 super(parent,"Proxy Configuration",true); | |
86 warning_dialog = new Dialog(parent,"Warning",true); | |
87 | |
88 guiInit(); | |
89 setResizable(false); | |
90 addWindowListener(this); | |
91 Component[] comps = getComponents(); | |
92 for(int i=0;i<comps.length;++i){ | |
93 if(comps[i] instanceof Button) | |
94 ((Button)comps[i]).addActionListener(this); | |
95 else if(comps[i] instanceof TextField) | |
96 ((TextField) comps[i]).addActionListener(this); | |
97 else if(comps[i] instanceof Checkbox){ | |
98 ((Checkbox)comps[i]).addItemListener(this); | |
99 } | |
100 } | |
101 proxy = init_proxy; | |
102 if(proxy != null) doInit(proxy); | |
103 else ir = new InetRange(); | |
104 | |
105 dismiss_button.addActionListener(this); | |
106 warning_dialog.addWindowListener(this); | |
107 } | |
108 | |
109 //Public Methods | |
110 //////////////// | |
111 | |
112 /** | |
113 Displays SOCKS configuartion dialog. | |
114 <p> | |
115 Returns initialised proxy object, or null if user cancels dialog | |
116 by either pressing Cancel or closing the dialog window. | |
117 */ | |
118 public CProxy getProxy(){ | |
119 mode = COMMAND_MODE; | |
120 pack(); | |
121 show(); | |
122 return proxy; | |
123 } | |
124 | |
125 /** | |
126 Initialises dialog to given proxy and displays SOCKS configuartion dialog. | |
127 <p> | |
128 Returns initialised proxy object, or null if user cancels dialog | |
129 by either pressing Cancel or closing the dialog window. | |
130 */ | |
131 public CProxy getProxy(CProxy p){ | |
132 if(p != null){ | |
133 doInit(p); | |
134 } | |
135 mode = COMMAND_MODE; | |
136 pack(); | |
137 show(); | |
138 return proxy; | |
139 } | |
140 | |
141 // WindowListener Interface | |
142 ///////////////////////////////// | |
143 public void windowActivated(java.awt.event.WindowEvent e){ | |
144 } | |
145 public void windowDeactivated(java.awt.event.WindowEvent e){ | |
146 } | |
147 public void windowOpened(java.awt.event.WindowEvent e){ | |
148 } | |
149 public void windowClosing(java.awt.event.WindowEvent e){ | |
150 Window source = e.getWindow(); | |
151 if(source == this){ | |
152 onCancel(); | |
153 }else if(source == warning_dialog){ | |
154 onDismiss(); | |
155 } | |
156 } | |
157 public void windowClosed(java.awt.event.WindowEvent e){ | |
158 } | |
159 public void windowIconified(java.awt.event.WindowEvent e){ | |
160 } | |
161 public void windowDeiconified(java.awt.event.WindowEvent e){ | |
162 } | |
163 | |
164 //ActionListener interface | |
165 /////////////////////////// | |
166 public void actionPerformed(ActionEvent ae){ | |
167 | |
168 Object source = ae.getSource(); | |
169 | |
170 if(source == cancel_button ) | |
171 onCancel(); | |
172 else if(source == add_button || source == direct_text) | |
173 onAdd(); | |
174 else if(source == remove_button) | |
175 onRemove(); | |
176 else if(source == dismiss_button) | |
177 onDismiss(); | |
178 else if(source == ok_button || source instanceof TextField) | |
179 onOK(); | |
180 } | |
181 //ItemListener interface | |
182 //////////////////////// | |
183 public void itemStateChanged(ItemEvent ie){ | |
184 Object source = ie.getSource(); | |
185 //System.out.println("ItemEvent:"+source); | |
186 if(source == socks5radio || source == socks4radio) | |
187 onSocksChange(); | |
188 else if(source == up_check) | |
189 onUPChange(); | |
190 | |
191 } | |
192 //Runnable interface | |
193 //////////////////// | |
194 /** | |
195 Resolves proxy address in other thread, to avoid | |
196 annoying blocking in GUI thread. | |
197 */ | |
198 public void run(){ | |
199 | |
200 if(!initProxy()){ | |
201 //Check if we have been aborted | |
202 if(mode != OK_MODE) return; | |
203 if(net_thread != Thread.currentThread()) return; | |
204 | |
205 mode = COMMAND_MODE; | |
206 warning_label.setText("Look up failed."); | |
207 warning_label.invalidate(); | |
208 return; | |
209 } | |
210 | |
211 //System.out.println("Done!"); | |
212 while(!warning_dialog.isShowing()) | |
213 ; /* do nothing*/; | |
214 | |
215 warning_dialog.dispose(); | |
216 //dispose(); //End Dialog | |
217 } | |
218 | |
219 //Private Methods | |
220 /////////////////// | |
221 private void onOK(){ | |
222 host = host_text.getText().trim(); | |
223 user = user_text.getText(); | |
224 password = password_text.getText(); | |
225 | |
226 if(host.length() == 0){ | |
227 warn("Proxy host is not set!"); | |
228 return; | |
229 } | |
230 if(socks_group.getSelectedCheckbox() == socks4radio){ | |
231 if(user.length() == 0){ | |
232 warn("User name is not set"); | |
233 return; | |
234 } | |
235 | |
236 }else{ | |
237 if(up_check.getState()){ | |
238 if(user.length() == 0){ | |
239 warn("User name is not set."); | |
240 return; | |
241 } | |
242 if(password.length()==0){ | |
243 warn("Password is not set."); | |
244 return; | |
245 } | |
246 }else if(!none_check.getState()){ | |
247 warn("Please select at least one Authentication Method."); | |
248 return; | |
249 } | |
250 } | |
251 | |
252 try{ | |
253 port = Integer.parseInt(port_text.getText()); | |
254 }catch(NumberFormatException nfe){ | |
255 warn("Proxy port is invalid!"); | |
256 return; | |
257 } | |
258 | |
259 mode = OK_MODE; | |
260 | |
261 if(useThreads){ | |
262 net_thread = new Thread(this); | |
263 net_thread.start(); | |
264 | |
265 info("Looking up host: "+host); | |
266 //System.out.println("Info returned."); | |
267 }else if(!initProxy()){ | |
268 warn("Proxy host is invalid."); | |
269 mode = COMMAND_MODE; | |
270 } | |
271 | |
272 if(mode == OK_MODE) dispose(); | |
273 } | |
274 | |
275 private void onCancel(){ | |
276 //System.out.println("Cancel"); | |
277 proxy = null; | |
278 dispose(); | |
279 } | |
280 | |
281 private void onAdd(){ | |
282 String s = direct_text.getText(); | |
283 s.trim(); | |
284 if(s.length() == 0 ) return; | |
285 //Check for Duplicate | |
286 String[] direct_hosts = direct_list.getItems(); | |
287 for(int i=0;i<direct_hosts.length;++i) | |
288 if(direct_hosts[i].equals(s)) return; | |
289 | |
290 direct_list.add(s); | |
291 ir.add(s); | |
292 } | |
293 private void onRemove(){ | |
294 int index = direct_list.getSelectedIndex(); | |
295 if(index < 0) return; | |
296 ir.remove(direct_list.getItem(index)); | |
297 direct_list.remove(index); | |
298 direct_list.select(index); | |
299 } | |
300 | |
301 private void onSocksChange(){ | |
302 Object selected = socks_group.getSelectedCheckbox(); | |
303 if(selected == socks4radio){ | |
304 user_text.setEnabled(true); | |
305 password_text.setEnabled(false); | |
306 none_check.setEnabled(false); | |
307 up_check.setEnabled(false); | |
308 }else{ | |
309 if(up_check.getState()){ | |
310 user_text.setEnabled(true); | |
311 password_text.setEnabled(true); | |
312 }else{ | |
313 user_text.setEnabled(false); | |
314 password_text.setEnabled(false); | |
315 } | |
316 none_check.setEnabled(true); | |
317 up_check.setEnabled(true); | |
318 } | |
319 //System.out.println("onSocksChange:"+selected); | |
320 } | |
321 private void onUPChange(){ | |
322 //System.out.println("onUPChange"); | |
323 if(up_check.getState()){ | |
324 user_text.setEnabled(true); | |
325 password_text.setEnabled(true); | |
326 }else{ | |
327 user_text.setEnabled(false); | |
328 password_text.setEnabled(false); | |
329 } | |
330 } | |
331 private void onDismiss(){ | |
332 warning_dialog.dispose(); | |
333 if(mode == OK_MODE){ | |
334 mode = COMMAND_MODE; | |
335 if(net_thread!=null) net_thread.interrupt(); | |
336 } | |
337 } | |
338 | |
339 private void doInit(CProxy p){ | |
340 if(p.version == 5){ | |
341 socks_group.setSelectedCheckbox(socks5radio); | |
342 onSocksChange(); | |
343 if(((Socks5Proxy)p).getAuthenticationMethod(0)!=null) | |
344 none_check.setState(true); | |
345 UserPasswordAuthentication auth = (UserPasswordAuthentication) | |
346 ((Socks5Proxy)p).getAuthenticationMethod(2); | |
347 if(auth!=null){ | |
348 user_text.setText(auth.getUser()); | |
349 password_text.setText(auth.getPassword()); | |
350 up_check.setState(true); | |
351 onUPChange(); | |
352 } | |
353 }else{ | |
354 socks_group.setSelectedCheckbox(socks4radio); | |
355 onSocksChange(); | |
356 user_text.setText(((Socks4Proxy)p).user); | |
357 } | |
358 ir = (InetRange)(p.directHosts.clone()); | |
359 String[] direct_hosts = ir.getAll(); | |
360 direct_list.removeAll(); | |
361 for(int i=0;i<direct_hosts.length;++i) | |
362 direct_list.add(direct_hosts[i]); | |
363 | |
364 host_text.setText(p.proxyIP.getHostName()); | |
365 port_text.setText(""+p.proxyPort); | |
366 | |
367 } | |
368 | |
369 private boolean initProxy(){ | |
370 try{ | |
371 if(socks_group.getSelectedCheckbox() == socks5radio){ | |
372 proxy = new Socks5Proxy(host,port); | |
373 if(up_check.getState()) | |
374 ((Socks5Proxy)proxy).setAuthenticationMethod(2, | |
375 new UserPasswordAuthentication(user,password)); | |
376 if(!none_check.getState()) | |
377 ((Socks5Proxy)proxy).setAuthenticationMethod(0,null); | |
378 } | |
379 else | |
380 proxy = new Socks4Proxy(host,port,user); | |
381 }catch(java.net.UnknownHostException uhe){ | |
382 return false; | |
383 } | |
384 proxy.directHosts = ir; | |
385 return true; | |
386 } | |
387 private void info(String s){ | |
388 msgBox("Info",s); | |
389 } | |
390 | |
391 private void warn(String s){ | |
392 msgBox("Warning",s); | |
393 } | |
394 | |
395 private void msgBox(String title, String message){ | |
396 warning_label.setText(message); | |
397 warning_label.invalidate(); | |
398 warning_dialog.setTitle(title); | |
399 warning_dialog.pack(); | |
400 warning_dialog.show(); | |
401 } | |
402 | |
403 /*====================================================================== | |
404 Form: | |
405 Table: | |
406 +---+-------+---+---+ | |
407 | | | | | | |
408 +---+-------+---+---+ | |
409 | | | | |
410 +---+-------+-------+ | |
411 | | | | | |
412 +---+-------+-------+ | |
413 | | | | | |
414 +---+-------+-------+ | |
415 | | | | |
416 +-----------+-------+ | |
417 | | | | |
418 | +---+---+ | |
419 | | | | | |
420 +-----------+---+---+ | |
421 | | | | | |
422 +---+---+---+---+---+ | |
423 | | | | | | | |
424 +---+---+---+---+---+ | |
425 */ | |
426 | |
427 void guiInit(){ | |
428 //Some default names used | |
429 Label label; | |
430 Container container; | |
431 Font font; | |
432 | |
433 GridBagConstraints c = new GridBagConstraints(); | |
434 | |
435 font = new Font("Dialog",Font.PLAIN,12); | |
436 | |
437 container = this; | |
438 //container = new Panel(); | |
439 container.setLayout(new GridBagLayout()); | |
440 container.setFont(font); | |
441 container.setBackground(SystemColor.menu); | |
442 c.insets = new Insets(3,3,3,3); | |
443 | |
444 c.gridx=0; c.gridy=0; | |
445 c.gridwidth=1; c.gridheight=1; | |
446 c.anchor=GridBagConstraints.NORTHEAST; | |
447 label = new Label("Host:"); | |
448 container.add(label,c); | |
449 | |
450 c.gridx=1; c.gridy=0; | |
451 c.gridwidth=2; c.gridheight=1; | |
452 c.anchor=GridBagConstraints.NORTHWEST; | |
453 host_text = new TextField("socks-proxy",15); | |
454 container.add(host_text,c); | |
455 | |
456 c.gridx=3; c.gridy=0; | |
457 c.gridwidth=1; c.gridheight=1; | |
458 c.anchor=GridBagConstraints.NORTHEAST; | |
459 label = new Label("Port:"); | |
460 container.add(label,c); | |
461 | |
462 c.gridx=4; c.gridy=0; | |
463 c.gridwidth=1; c.gridheight=1; | |
464 c.anchor=GridBagConstraints.NORTHWEST; | |
465 port_text = new TextField("1080",5); | |
466 container.add(port_text,c); | |
467 | |
468 c.gridx=0; c.gridy=1; | |
469 c.gridwidth=3; c.gridheight=1; | |
470 c.anchor=GridBagConstraints.NORTH; | |
471 socks4radio = new Checkbox("Socks4",socks_group,false); | |
472 //1.0 compatible code | |
473 //socks4radio = new Checkbox("Socks4",false); | |
474 //socks4radio.setCheckboxGroup(socks_group); | |
475 socks4radio.setFont(new Font(font.getName(),Font.BOLD,14)); | |
476 container.add(socks4radio,c); | |
477 | |
478 c.gridx=3; c.gridy=1; | |
479 c.gridwidth=2; c.gridheight=1; | |
480 c.anchor=GridBagConstraints.NORTH; | |
481 socks5radio = new Checkbox("Socks5",socks_group,true); | |
482 //1.0 compatible code | |
483 //socks5radio = new Checkbox("Socks5",true); | |
484 //socks5radio.setCheckboxGroup(socks_group); | |
485 socks5radio.setFont(new Font(font.getName(),Font.BOLD,14)); | |
486 container.add(socks5radio,c); | |
487 | |
488 c.gridx=0; c.gridy=2; | |
489 c.gridwidth=1; c.gridheight=1; | |
490 c.anchor=GridBagConstraints.EAST; | |
491 label = new Label("User Id:"); | |
492 container.add(label,c); | |
493 | |
494 c.gridx=1; c.gridy=2; | |
495 c.gridwidth=2; c.gridheight=1; | |
496 c.anchor=GridBagConstraints.NORTHWEST; | |
497 user_text = new TextField("",15); | |
498 user_text.setEnabled(false); | |
499 container.add(user_text,c); | |
500 | |
501 c.gridx=3; c.gridy=2; | |
502 c.gridwidth=2; c.gridheight=1; | |
503 c.anchor=GridBagConstraints.NORTH; | |
504 label = new Label("Authentication"); | |
505 label.setFont(new Font(font.getName(),Font.BOLD,14)); | |
506 container.add(label,c); | |
507 | |
508 c.gridx=0; c.gridy=3; | |
509 c.gridwidth=1; c.gridheight=1; | |
510 c.anchor=GridBagConstraints.EAST; | |
511 label = new Label("Password:"); | |
512 container.add(label,c); | |
513 | |
514 c.gridx=1; c.gridy=3; | |
515 c.gridwidth=2; c.gridheight=1; | |
516 c.anchor=GridBagConstraints.NORTHWEST; | |
517 password_text = new TextField("",15); | |
518 password_text.setEchoChar('*'); | |
519 password_text.setEnabled(false); | |
520 //password_text.setEchoCharacter('*');//1.0 | |
521 container.add(password_text,c); | |
522 | |
523 c.gridx=3; c.gridy=3; | |
524 c.gridwidth=2; c.gridheight=1; | |
525 c.anchor=GridBagConstraints.NORTHWEST; | |
526 none_check = new Checkbox("None",true); | |
527 container.add(none_check,c); | |
528 | |
529 c.gridx=0; c.gridy=4; | |
530 c.gridwidth=3; c.gridheight=1; | |
531 c.anchor=GridBagConstraints.NORTH; | |
532 label = new Label("Direct Hosts"); | |
533 label.setFont(new Font(font.getName(),Font.BOLD,14)); | |
534 container.add(label,c); | |
535 | |
536 c.gridx=3; c.gridy=4; | |
537 c.gridwidth=2; c.gridheight=1; | |
538 c.anchor=GridBagConstraints.NORTHWEST; | |
539 up_check = new Checkbox("User/Password",false); | |
540 container.add(up_check,c); | |
541 | |
542 c.gridx=0; c.gridy=5; | |
543 c.gridwidth=3; c.gridheight=2; | |
544 c.anchor=GridBagConstraints.NORTHWEST; | |
545 c.fill = GridBagConstraints.BOTH; | |
546 direct_list = new List(3); | |
547 container.add(direct_list,c); | |
548 | |
549 c.gridx=3; c.gridy=5; | |
550 c.gridwidth=2; c.gridheight=1; | |
551 c.anchor=GridBagConstraints.NORTHWEST; | |
552 gssapi_check = new Checkbox("GSSAPI",false); | |
553 gssapi_check.setEnabled(false); | |
554 container.add(gssapi_check,c); | |
555 | |
556 c.gridx=0; c.gridy=7; | |
557 c.gridwidth=3; c.gridheight=1; | |
558 c.anchor=GridBagConstraints.NORTHWEST; | |
559 direct_text = new TextField("",25); | |
560 container.add(direct_text,c); | |
561 | |
562 c.gridx=3; c.gridy=7; | |
563 c.gridwidth=1; c.gridheight=1; | |
564 c.anchor=GridBagConstraints.NORTH; | |
565 add_button = new Button("Add"); | |
566 container.add(add_button,c); | |
567 | |
568 c.gridx=3; c.gridy=6; | |
569 c.gridwidth=1; c.gridheight=1; | |
570 c.anchor=GridBagConstraints.NORTH; | |
571 remove_button = new Button("Remove"); | |
572 container.add(remove_button,c); | |
573 | |
574 c.gridx=1; c.gridy=8; | |
575 c.gridwidth=1; c.gridheight=1; | |
576 c.anchor=GridBagConstraints.NORTH; | |
577 cancel_button = new Button("Cancel"); | |
578 container.add(cancel_button,c); | |
579 | |
580 c.gridx=0; c.gridy=8; | |
581 c.gridwidth=1; c.gridheight=1; | |
582 c.anchor=GridBagConstraints.NORTHWEST; | |
583 ok_button = new Button("OK"); | |
584 container.add(ok_button,c); | |
585 | |
586 //up_check.setEnabled(false); | |
587 | |
588 //Warning Dialog | |
589 dismiss_button = new Button("Dismiss"); | |
590 warning_label = new Label("",Label.CENTER); | |
591 warning_label.setFont(new Font("Dialog",Font.BOLD,15)); | |
592 | |
593 Panel p = new Panel(); | |
594 p.add(dismiss_button); | |
595 warning_dialog.add(p,BorderLayout.SOUTH); | |
596 warning_dialog.add(warning_label,BorderLayout.CENTER); | |
597 warning_dialog.setResizable(false); | |
598 }//end guiInit | |
599 | |
600 /* | |
601 // Main | |
602 //////////////////////////////////// | |
603 public static void main(String[] args) throws Exception{ | |
604 Frame f = new Frame("Test for SocksDialog"); | |
605 f.add("Center", new Label("Fill the Dialog")); | |
606 SocksDialog socksdialog = new SocksDialog(f); | |
607 f.pack(); | |
608 f.show(); | |
609 f.addWindowListener(socksdialog); | |
610 CProxy p = socksdialog.getProxy(); | |
611 System.out.println("Selected: "+p); | |
612 } | |
613 */ | |
614 | |
615 }//end class |