简体   繁体   中英

http request over ssh tunnel

I have two server. I use first server installed tor application on it for hide my ip adress. It will be named as SSH Tunnel Server henceforth. Second server is to send http request on ssh tunnelling. And it will be named as Client Sever. I wrote the codes below, for the purpose of above. But I can't read SSH Tunnel Server response. Where Am I doing wrong ?

            String user = "root";
            String password = "password";
            String host = "198.199."11.111";
            int port=22;

            JSch jsch = new JSch();
            Session session = jsch.getSession(user, host, port);
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");



            session.connect();

            Channel channel=session.openChannel("direct-tcpip");
            ((ChannelDirectTCPIP)channel).setHost("whatismyipaddress.com");
            ((ChannelDirectTCPIP)channel).setPort(80);

            String cmd = "GET / HTTP/1.1" ;

            InputStream in = channel.getInputStream();
            OutputStream out = channel.getOutputStream();
            channel.setInputStream(System.in);
            channel.setOutputStream(System.out);
            channel.connect(10000);

            byte[] bytes = cmd.getBytes();          
            InputStream is = new ByteArrayInputStream(cmd.getBytes("UTF-8"));

            int numRead;

            while ( (numRead = is.read(bytes) ) >= 0) {

                  out.write(bytes, 0, numRead);
                  System.out.println(numRead);
            }

            out.flush();

            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                for (String line; (line = reader.readLine()) != null;){
                    System.out.println(line);
                }
            } catch (java.io.IOException exc) {
                System.out.println(exc.toString());
            }
            channel.disconnect();
            session.disconnect();
            System.out.println();

You're not outputting any line terminators following the GET command. Try changing the GET string to this:

String cmd = "GET / HTTP/1.1\r\n\r\n";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM