TCP Connection over a secure ssh connection

北城余情 提交于 2019-12-12 04:12:47

问题


I am trying to use JSCH to connect to a remote server and then from that server open a telnet like session over a tcp/ip port. Say connect to server A, and once connected issue a tcp connection to server B over another port. In my webserver logs I see a GET / logged but not GET /foo as I would expect. ANything I m missing here? (I do not need to use Port forwarding since the remote port is accessible to the system I am connected to)

package com.tekmor;

import com.jcraft.jsch.*;

import java.io.BufferedReader;
.
.

public class Siranga {

     public static void main(String[] args){
        Siranga t=new Siranga();
           try{
               t.go();
           } catch(Exception ex){
               ex.printStackTrace();
        }
    }
    public void go() throws Exception{
        String host="hostXXX.com";
        String user="USER";
        String password="PASS";
        int port=22;

        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");      

        String remoteHost="hostYYY.com";
        int remotePort=80;


       try { 
        JSch jsch=new JSch();
        Session session=jsch.getSession(user, host, port);
        session.setPassword(password);
        session.setConfig(config);
        session.connect();
        Channel channel=session.openChannel("direct-tcpip");  


        ((ChannelDirectTCPIP)channel).setHost(remoteHost);
        ((ChannelDirectTCPIP)channel).setPort(remotePort);

        String cmd = "GET /foo";

        InputStream in = channel.getInputStream();
        OutputStream out = channel.getOutputStream();

        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();



        channel.disconnect();
        session.disconnect();

        System.out.println("foo");

       }
       catch (Exception e){
           e.printStackTrace();

       }

    }
}

回答1:


Read your HTTP specification again. The request header should end with an empty line. So assuming you have no more header lines, you should at least have to line breaks at the end. (Line break here means a CRLF combination.)

Also, the request line should contain the HTTP version identifier after the URL.

So try this change to your program:

String command = "GET /foo HTTP/1.0\r\n\r\n";

As a hint: Instead of manually piping data from your ByteArrayInputStream to the channel's output stream, you could use the setInputStream method. Also, don't forget to read the result from the channel's input stream.



来源:https://stackoverflow.com/questions/15210811/tcp-connection-over-a-secure-ssh-connection

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!