📄 session.cs
字号:
_isConnected=false;
//e.printStackTrace();
if(e is RuntimeException) throw (RuntimeException)e;
if(e is JSchException) throw (JSchException)e;
throw new JSchException("Session.connect: "+e);
}
}
private KeyExchange receive_kexinit(Buffer buf)
{
int j=buf.getInt();
if(j!=buf.getLength())
{ // packet was compressed and
buf.getByte(); // j is the size of deflated packet.
I_S=new byte[buf.index-5];
}
else
{
I_S=new byte[j-1-buf.getByte()];
}
Fireball.Ssh.java.System.arraycopy(buf.buffer, buf.s, I_S, 0, I_S.Length);
/*
try{
byte[] tmp=new byte[I_S.Length];
Fireball.Ssh.java.System.arraycopy(I_S, 0, tmp, 0, I_S.Length);
Buffer tmpb=new Buffer(tmp);
System.Console.WriteLine("I_S: len="+I_S.Length);
tmpb.setOffSet(17);
System.Console.WriteLine("kex: "+new String(tmpb.getString()));
System.Console.WriteLine("server_host_key: "+new String(tmpb.getString()));
System.Console.WriteLine("cipher.c2s: "+new String(tmpb.getString()));
System.Console.WriteLine("cipher.s2c: "+new String(tmpb.getString()));
System.Console.WriteLine("mac.c2s: "+new String(tmpb.getString()));
System.Console.WriteLine("mac.s2c: "+new String(tmpb.getString()));
System.Console.WriteLine("compression.c2s: "+new String(tmpb.getString()));
System.Console.WriteLine("compression.s2c: "+new String(tmpb.getString()));
System.Console.WriteLine("lang.c2s: "+new String(tmpb.getString()));
System.Console.WriteLine("lang.s2c: "+new String(tmpb.getString()));
System.Console.WriteLine("?: "+(tmpb.getByte()&0xff));
System.Console.WriteLine("??: "+tmpb.getInt());
}
catch(Exception e){
System.Console.WriteLine(e);
}
*/
send_kexinit();
String[] guess=KeyExchange.guess(I_S, I_C);
if(guess==null)
{
throw new JSchException("Algorithm negotiation fail");
}
if(!isAuthed &&
(guess[KeyExchange.PROPOSAL_ENC_ALGS_CTOS].equals("none") ||
(guess[KeyExchange.PROPOSAL_ENC_ALGS_STOC].equals("none"))))
{
throw new JSchException("NONE Cipher should not be chosen before authentification is successed.");
}
KeyExchange kex=null;
try
{
Class c=Class.forName(getConfig(guess[KeyExchange.PROPOSAL_KEX_ALGS]));
kex=(KeyExchange)(c.newInstance());
}
catch(Exception e){ System.Console.Error.WriteLine("kex: "+e); }
kex._guess=guess;
kex.init(this, V_S, V_C, I_S, I_C);
return kex;
}
private bool in_kex=false;
public void rekey()
{
send_kexinit();
}
private void send_kexinit()
{
if(in_kex) return;
in_kex=true;
// byte SSH_MSG_KEXINIT(20)
// byte[16] cookie (random bytes)
// String kex_algorithms
// String server_host_key_algorithms
// String encryption_algorithms_client_to_server
// String encryption_algorithms_server_to_client
// String mac_algorithms_client_to_server
// String mac_algorithms_server_to_client
// String compression_algorithms_client_to_server
// String compression_algorithms_server_to_client
// String languages_client_to_server
// String languages_server_to_client
packet.reset();
buf.putByte((byte) SSH_MSG_KEXINIT);
lock(random)
{
random.fill(buf.buffer, buf.index, 16); buf.skip(16);
}
buf.putString(getConfig("kex").getBytes());
buf.putString(getConfig("server_host_key").getBytes());
buf.putString(getConfig("cipher.c2s").getBytes());
buf.putString(getConfig("cipher.s2c").getBytes());
buf.putString(getConfig("mac.c2s").getBytes());
buf.putString(getConfig("mac.s2c").getBytes());
buf.putString(getConfig("compression.c2s").getBytes());
buf.putString(getConfig("compression.s2c").getBytes());
buf.putString(getConfig("lang.c2s").getBytes());
buf.putString(getConfig("lang.s2c").getBytes());
buf.putByte((byte)0);
buf.putInt(0);
buf.setOffSet(5);
I_C=new byte[buf.getLength()];
buf.getByte(I_C);
write(packet);
}
private void send_newkeys()
{
// send SSH_MSG_NEWKEYS(21)
packet.reset();
buf.putByte((byte)SSH_MSG_NEWKEYS);
write(packet);
}
private void checkHost(String host, KeyExchange kex)
{
String shkc=getConfig("StrictHostKeyChecking");
//System.Console.WriteLine("shkc: "+shkc);
byte[] K_S=kex.getHostKey();
String key_type=kex.getKeyType();
String key_fprint=kex.getFingerPrint();
hostkey=new HostKey(host, K_S);
HostKeyRepository hkr=jsch.getHostKeyRepository();
int i=0;
lock(hkr)
{
i=hkr.check(host, K_S);
}
bool insert=false;
if((shkc.equals("ask") || shkc.equals("yes")) &&
i==HostKeyRepository.CHANGED)
{
String file=null;
lock(hkr)
{
file=hkr.getKnownHostsRepositoryID();
}
if(file==null){file="known_hosts";}
String message=
"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"+
"@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @\n"+
"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"+
"IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY! \n"+
"Someone could be eavesdropping on you right now (man-in-the-middle attack)!\n"+
"It is also possible that the "+key_type+" host key has just been changed.\n"+
"The fingerprint for the "+key_type+" key sent by the remote host is\n"+
key_fprint+".\n"+
"Please contact your system administrator.\n"+
"Add correct host key in "+file+" to get rid of this message.";
bool b=false;
if(userinfo!=null)
{
//userinfo.showMessage(message);
b=userinfo.promptYesNo(message+
"\nDo you want to delete the old key and insert the new key?");
}
//throw new JSchException("HostKey has been changed: "+host);
if(!b)
{
throw new JSchException("HostKey has been changed: "+host);
}
else
{
lock(hkr)
{
hkr.remove(host, null);
insert=true;
}
}
}
// bool insert=false;
if((shkc.equals("ask") || shkc.equals("yes")) &&
(i!=HostKeyRepository.OK) && !insert)
{
if(shkc.equals("yes"))
{
throw new JSchException("reject HostKey: "+host);
}
//System.Console.WriteLine("finger-print: "+key_fprint);
if(userinfo!=null)
{
bool foo=userinfo.promptYesNo(
"The authenticity of host '"+host+"' can't be established.\n"+
key_type+" key fingerprint is "+key_fprint+".\n"+
"Are you sure you want to continue connecting?"
);
if(!foo)
{
throw new JSchException("reject HostKey: "+host);
}
insert=true;
}
else
{
if(i==HostKeyRepository.NOT_INCLUDED)
throw new JSchException("UnknownHostKey: "+host);
else throw new JSchException("HostKey has been changed: "+host);
}
}
if(shkc.equals("no") &&
HostKeyRepository.NOT_INCLUDED==i)
{
insert=true;
}
if(insert)
{
lock(hkr)
{
hkr.add(host, K_S, userinfo);
}
}
}
//public void start(){ (new Thread(this)).start(); }
public Channel openChannel(String type)
{
if(!_isConnected)
{
throw new JSchException("session is down");
}
try
{
Channel channel=Channel.getChannel(type);
addChannel(channel);
channel.init();
return channel;
}
catch(Exception e)
{
System.Console.WriteLine(e);
}
return null;
}
// encode will bin invoked in write with synchronization.
public void encode(Packet packet)
{
//System.Console.WriteLine("encode: "+packet.buffer.buffer[5]);
//System.Console.WriteLine(" "+packet.buffer.index);
//if(packet.buffer.buffer[5]==96){
//Thread.dumpStack();
//}
if(deflater!=null)
{
packet.buffer.index=deflater.compress(packet.buffer.buffer,
5, packet.buffer.index);
}
if(c2scipher!=null)
{
packet.padding(c2scipher.getIVSize());
int pad=packet.buffer.buffer[4];
lock(random)
{
random.fill(packet.buffer.buffer, packet.buffer.index-pad, pad);
}
}
else
{
packet.padding(8);
}
byte[] mac=null;
if(c2smac!=null)
{
c2smac.update(seqo);
c2smac.update(packet.buffer.buffer, 0, packet.buffer.index);
mac=c2smac.doFinal();
}
if(c2scipher!=null)
{
byte[] buf=packet.buffer.buffer;
c2scipher.update(buf, 0, packet.buffer.index, buf, 0);
}
if(mac!=null)
{
packet.buffer.putByte(mac);
}
}
int[] uncompress_len=new int[1];
private int cipher_size=8;
public Buffer read(Buffer buf)
{
int j=0;
while(true)
{
buf.reset();
io.getByte(buf.buffer, buf.index, cipher_size); buf.index+=cipher_size;
if(s2ccipher!=null)
{
s2ccipher.update(buf.buffer, 0, cipher_size, buf.buffer, 0);
}
// j=((buf.buffer[0]<<24)&0xff000000)|
// ((buf.buffer[1]<<16)&0x00ff0000)|
// ((buf.buffer[2]<< 8)&0x0000ff00)|
// ((buf.buffer[3] )&0x000000ff);
j=Util.ToInt32( buf.buffer, 0 );
j=j-4-cipher_size+8;
if(j>0)
{
io.getByte(buf.buffer, buf.index, j); buf.index+=(j);
if(s2ccipher!=null)
{
s2ccipher.update(buf.buffer, cipher_size, j, buf.buffer, cipher_size);
}
}
/*
io.getByte(buf.buffer, buf.index, 8); buf.index+=8;
if(s2ccipher!=null){
s2ccipher.update(buf.buffer, 0, 8, buf.buffer, 0);
}
j=((buf.buffer[0]<<24)&0xff000000)|
((buf.buffer[1]<<16)&0x00ff0000)|
((buf.buffer[2]<< 8)&0x0000ff00)|
((buf.buffer[3] )&0x000000ff);
io.getByte(buf.buffer, buf.index, j-4); buf.index+=(j-4);
if(s2ccipher!=null){
s2ccipher.update(buf.buffer, 8, j-4, buf.buffer, 8);
}
*/
if(s2cmac!=null)
{
s2cmac.update(seqi);
s2cmac.update(buf.buffer, 0, buf.index);
byte[] result=s2cmac.doFinal();
io.getByte(mac_buf, 0, mac_buf.Length);
if(!Arrays.equals(result, mac_buf))
{
System.Console.WriteLine("mac error");
throw new IOException("MAC Error");
}
}
seqi++;
if(inflater!=null)
{
//inflater.uncompress(buf);
int pad=buf.buffer[4];
uncompress_len[0]=buf.index-5-pad;
byte[] foo=inflater.uncompress(buf.buffer, 5, uncompress_len);
if(foo!=null)
{
buf.buffer=foo;
buf.index=5+uncompress_len[0];
}
else
{
System.Console.Error.WriteLine("fail in inflater");
break;
}
}
int type=buf.buffer[5]&0xff;
//System.Console.WriteLine("read: "+type);
if(type==SSH_MSG_DISCONNECT)
{
buf.rewind();
buf.getInt();buf.getShort();
int reason_code=buf.getInt();
byte[] description=buf.getString();
byte[] language_tag=buf.getString();
/*
System.Console.Error.WriteLine("SSH_MSG_DISCONNECT:"+
" "+reason_code+
" "+new String(description)+
" "+new String(language_tag));
*/
throw new JSchException("SSH_MSG_DISCONNECT:"+
" "+reason_code+
" "+new String(description)+
" "+new String(language_tag));
//break;
}
else if(type==SSH_MSG_IGNORE)
{
}
else if(type==SSH_MSG_DEBUG)
{
buf.rewind();
buf.getInt();buf.getShort();
/*
byte always_display=(byte)buf.getByte();
byte[] message=buf.getString();
byte[] language_tag=buf.getString();
System.Console.Error.WriteLine("SSH_MSG_DEBUG:"+
" "+new String(message)+
" "+new String(language_tag));
*/
}
else if(type==SSH_MSG_CHANNEL_WINDOW_ADJUST)
{
buf.rewind();
buf.getInt();buf.getShort();
Channel c=Channel.getChannel(buf.getInt(), this);
if(c==null)
{
}
else
{
c.addRemoteWindowSize(buf.getInt());
}
}
else
{
break;
}
}
buf.rewind();
return buf;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -