KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSchOverJHttpTunnel


1 /* -*-mode:java; c-basic-offset:2; -*- */
2 /*
3 Copyright (c) 2004 ymnk, JCraft,Inc. All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8   1. Redistributions of source code must retain the above copyright notice,
9      this list of conditions and the following disclaimer.
10
11   2. Redistributions in binary form must reproduce the above copyright
12      notice, this list of conditions and the following disclaimer in
13      the documentation and/or other materials provided with the distribution.
14
15   3. The names of the authors may not be used to endorse or promote products
16      derived from this software without specific prior written permission.
17
18 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
19 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
21 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
22 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */

29
30 import com.jcraft.jsch.*;
31 import com.jcraft.jhttptunnel.*;
32
33 import java.awt.*;
34 import java.awt.event.*;
35 import javax.swing.*;
36
37 import java.io.*;
38 import java.net.*;
39
40 public class JSchOverJHttpTunnel{
41   public static void main(String JavaDoc[] arg){
42
43     try{
44       JSch jsch=new JSch();
45
46       if(arg.length==0){
47     System.err.println("Enter username@hostname[:port]");
48     System.exit(1);
49       }
50       String JavaDoc host=arg[0];
51       String JavaDoc user=host.substring(0, host.indexOf('@'));
52       host=host.substring(host.indexOf('@')+1);
53       int port=8888;
54       if(host.indexOf(':')!=-1){
55     try{
56       port=Integer.parseInt(host.substring(host.lastIndexOf(':') + 1));
57       host=host.substring(0, host.lastIndexOf(':'));
58     }
59     catch (Exception JavaDoc e) {
60       System.err.println(e);
61       System.exit(1);
62     }
63       }
64
65       Session session=jsch.getSession(user, host, port);
66       //session.setPassword("your password");
67

68       UserInfo ui=new MyUserInfo();
69       session.setUserInfo(ui);
70
71       session.setSocketFactory(new SocketFactory() {
72       InputStream in=null;
73       OutputStream out=null;
74       JHttpTunnelClient jhtc=null;
75       public Socket createSocket(String JavaDoc host, int port) throws IOException, UnknownHostException {
76         jhtc=new JHttpTunnelClient(host, port);
77         //jhtc.setProxy(proxy_host, proxy_port);
78

79         jhtc.setInBound(new InBoundSocket());
80         jhtc.setOutBound(new OutBoundSocket());
81
82         jhtc.connect();
83         return new Socket(); // dummy
84
}
85       public InputStream getInputStream(Socket socket) throws IOException {
86         if(in==null)
87           in=jhtc.getInputStream();
88         return in;
89       }
90       public OutputStream getOutputStream(Socket socket) throws IOException {
91         if(out==null)
92           out=jhtc.getOutputStream();
93         return out;
94       }
95     });
96
97       //java.util.Hashtable config=new java.util.Hashtable();
98
//config.put("StrictHostKeyChecking", "no");
99
//session.setConfig(config);
100

101       session.connect();
102
103       Channel channel=session.openChannel("shell");
104
105       channel.setInputStream(System.in);
106       channel.setOutputStream(System.out);
107
108       channel.connect();
109     }
110     catch(Exception JavaDoc e){
111       System.out.println(e);
112     }
113   }
114
115   public static class MyUserInfo implements UserInfo{
116     public String JavaDoc getPassword(){ return passwd; }
117     public boolean promptYesNo(String JavaDoc str){
118       Object JavaDoc[] options={ "yes", "no" };
119       int foo=JOptionPane.showOptionDialog(null,
120              str,
121              "Warning",
122              JOptionPane.DEFAULT_OPTION,
123              JOptionPane.WARNING_MESSAGE,
124              null, options, options[0]);
125        return foo==0;
126     }
127   
128     String JavaDoc passwd;
129     JTextField passwordField=(JTextField)new JPasswordField(20);
130
131     public String JavaDoc getPassphrase(){ return null; }
132     public boolean promptPassphrase(String JavaDoc message){ return true; }
133     public boolean promptPassword(String JavaDoc message){
134       Object JavaDoc[] ob={passwordField};
135       int result=
136       JOptionPane.showConfirmDialog(null, ob, message,
137                     JOptionPane.OK_CANCEL_OPTION);
138       if(result==JOptionPane.OK_OPTION){
139     passwd=passwordField.getText();
140     return true;
141       }
142       else{ return false; }
143     }
144     public void showMessage(String JavaDoc message){
145       JOptionPane.showMessageDialog(null, message);
146     }
147   }
148
149 }
150
Popular Tags