1 2 29 30 package com.jcraft.jsch; 31 32 import java.net.*; 33 34 class ChannelX11 extends Channel{ 35 36 static private final int LOCAL_WINDOW_SIZE_MAX=0x20000; 37 static private final int LOCAL_MAXIMUM_PACKET_SIZE=0x4000; 38 39 static private final int TIMEOUT=10*1000; 40 41 private static String host="127.0.0.1"; 42 private static int port=6000; 43 44 private boolean init=true; 45 46 static byte[] cookie=null; 47 private static byte[] cookie_hex=null; 48 49 private static java.util.Hashtable faked_cookie_pool=new java.util.Hashtable (); 50 private static java.util.Hashtable faked_cookie_hex_pool=new java.util.Hashtable (); 51 52 private static byte[] table={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 53 0x61,0x62,0x63,0x64,0x65,0x66}; 54 55 private Socket socket = null; 56 57 static int revtable(byte foo){ 58 for(int i=0; i<table.length; i++){ 59 if(table[i]==foo)return i; 60 } 61 return 0; 62 } 63 static void setCookie(String foo){ 64 cookie_hex=foo.getBytes(); 65 cookie=new byte[16]; 66 for(int i=0; i<16; i++){ 67 cookie[i]=(byte)(((revtable(cookie_hex[i*2])<<4)&0xf0) | 68 ((revtable(cookie_hex[i*2+1]))&0xf)); 69 } 70 } 71 static void setHost(String foo){ host=foo; } 72 static void setPort(int foo){ port=foo; } 73 static byte[] getFakedCookie(Session session){ 74 synchronized(faked_cookie_hex_pool){ 75 byte[] foo=(byte[])faked_cookie_hex_pool.get(session); 76 if(foo==null){ 77 Random random=Session.random; 78 foo=new byte[16]; 79 synchronized(random){ 80 random.fill(foo, 0, 16); 81 } 82 89 faked_cookie_pool.put(session, foo); 90 byte[] bar=new byte[32]; 91 for(int i=0; i<16; i++){ 92 bar[2*i]=table[(foo[i]>>>4)&0xf]; 93 bar[2*i+1]=table[(foo[i])&0xf]; 94 } 95 faked_cookie_hex_pool.put(session, bar); 96 foo=bar; 97 } 98 return foo; 99 } 100 } 101 102 ChannelX11(){ 103 super(); 104 105 setLocalWindowSizeMax(LOCAL_WINDOW_SIZE_MAX); 106 setLocalWindowSize(LOCAL_WINDOW_SIZE_MAX); 107 setLocalPacketSize(LOCAL_MAXIMUM_PACKET_SIZE); 108 109 type="x11".getBytes(); 110 111 connected=true; 112 124 } 125 126 public void run(){ 127 128 try{ 129 socket=Util.createSocket(host, port, TIMEOUT); 130 socket.setTcpNoDelay(true); 131 io=new IO(); 132 io.setInputStream(socket.getInputStream()); 133 io.setOutputStream(socket.getOutputStream()); 134 sendOpenConfirmation(); 135 } 136 catch(Exception e){ 137 sendOpenFailure(SSH_OPEN_ADMINISTRATIVELY_PROHIBITED); 138 close=true; 139 disconnect(); 140 return; 141 } 142 143 thread=Thread.currentThread(); 144 Buffer buf=new Buffer(rmpsize); 145 Packet packet=new Packet(buf); 146 int i=0; 147 try{ 148 while(thread!=null && 149 io!=null && 150 io.in!=null){ 151 i=io.in.read(buf.buffer, 152 14, 153 buf.buffer.length-14 154 -32 -20 ); 156 if(i<=0){ 157 eof(); 158 break; 159 } 160 if(close)break; 161 packet.reset(); 162 buf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA); 163 buf.putInt(recipient); 164 buf.putInt(i); 165 buf.skip(i); 166 session.write(packet, this, i); 167 } 168 } 169 catch(Exception e){ 170 } 172 disconnect(); 173 } 174 175 private byte[] cache=new byte[0]; 176 private byte[] addCache(byte[] foo, int s, int l){ 177 byte[] bar=new byte[cache.length+l]; 178 System.arraycopy(foo, s, bar, cache.length, l); 179 if(cache.length>0) 180 System.arraycopy(cache, 0, bar, 0, cache.length); 181 cache=bar; 182 return cache; 183 } 184 185 void write(byte[] foo, int s, int l) throws java.io.IOException { 186 188 if(init){ 189 190 foo=addCache(foo, s, l); 191 s=0; 192 l=foo.length; 193 194 if(l<9) 195 return; 196 197 int plen=(foo[s+6]&0xff)*256+(foo[s+7]&0xff); 198 int dlen=(foo[s+8]&0xff)*256+(foo[s+9]&0xff); 199 200 if((foo[s]&0xff)==0x42){ 201 } 202 else if((foo[s]&0xff)==0x6c){ 203 plen=((plen>>>8)&0xff)|((plen<<8)&0xff00); 204 dlen=((dlen>>>8)&0xff)|((dlen<<8)&0xff00); 205 } 206 else{ 207 } 209 210 if(l<12+plen+((-plen)&3)+dlen) 211 return; 212 213 byte[] bar=new byte[dlen]; 214 System.arraycopy(foo, s+12+plen+((-plen)&3), bar, 0, dlen); 215 byte[] faked_cookie=null; 216 217 synchronized(faked_cookie_pool){ 218 faked_cookie=(byte[])faked_cookie_pool.get(session); 219 } 220 221 233 234 if(equals(bar, faked_cookie)){ 235 if(cookie!=null) 236 System.arraycopy(cookie, 0, foo, s+12+plen+((-plen)&3), dlen); 237 } 238 else{ 239 thread=null; 241 eof(); 242 io.close(); 243 disconnect(); 244 } 245 init=false; 246 io.put(foo, s, l); 247 cache=null; 248 return; 249 } 250 io.put(foo, s, l); 251 } 252 253 private static boolean equals(byte[] foo, byte[] bar){ 254 if(foo.length!=bar.length)return false; 255 for(int i=0; i<foo.length; i++){ 256 if(foo[i]!=bar[i])return false; 257 } 258 return true; 259 } 260 } 261 | Popular Tags |