I2P Address: [http://git.idk.i2p]

Skip to content
Snippets Groups Projects
I2PTunnelXMLWrapper.java 12.3 KiB
Newer Older
  • Learn to ignore specific revisions
  • aum's avatar
    aum committed
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
    package net.i2p.i2ptunnel;
    
    import org.apache.xmlrpc.*;
    import java.lang.*;
    import java.util.*;
    import java.io.*;
    import java.lang.reflect.*;
    
    
    import net.i2p.*;
    import net.i2p.client.*;
    import net.i2p.client.streaming.*;
    import net.i2p.data.Base64;
    import net.i2p.util.*;
    import net.i2p.util.*;
    import net.i2p.data.*;
    import net.i2p.i2ptunnel.*;
    
    
    /**
     * subclasses I2PTunnel, adding some methods to facilitate
     * the I2PTunnel XML-RPC server. We have to put this class
     * into net.i2p.i2ptunnel, because there are some non-public
     * methods we need to access. For doco on the methods in
     * this class refer to the corresponding doco in I2PTunnelXMLObject
     */
    public class I2PTunnelXMLWrapper extends I2PTunnel
    {
        public Hashtable xmlrpcLookup(String hostname)
        {
            String cmdOut;
            String [] args;
            BufferLogger log = new BufferLogger();
            Hashtable result = new Hashtable();
        
            System.out.println("xmlrpcLookup: hostname='" + hostname + "'");
        
            args = new String[1];
            args[0] = hostname;
        
            //System.out.println("Invoking runLookup");    
        
            runLookup(args, log);
            //System.out.println("Back from runLookup");
        
            cmdOut = log.getBuffer().trim();
            //System.out.println("cmdOut=" + cmdOut);
        
            if (cmdOut.equals("Unknown host"))
            {
                result.put("status", "fail");
            }
            else
            {
                result.put("status", "ok");
                result.put("dest", cmdOut);
            }
        
            return result;
        }
        
        public Hashtable xmlrpcList()
        {
            Hashtable result = new Hashtable();
            BufferLogger log = new BufferLogger();
            Vector jobs = new Vector();
        
            // run the listing
            runList(log);
        
            String raw = log.getBuffer();
            //System.out.println("list: raw='"+raw+"'");
            if (raw.equals(""))
            {
                System.out.println("list: no jobs");
            }
            else
            {
                // got 1 or more job lines, parse them and assemble into jobs array
                String [] rawlines = raw.trim().split("\\n+");
            
                int numLines = Array.getLength(rawlines);
                int i;
            
                System.out.println("list: numLines="+numLines);
            
                for (i = 0; i < numLines; i++)
                {
                    String line = rawlines[i];
            
                    System.out.println("list: line["+i+"]="+line);
            
                    // carve up each job line into constituent fields
                    String [] flds = line.split("\\s+");
                    
                    String jobFld = flds[0];
                    String tcpFld = flds[1];
                    String typeFld = flds[2];
                    String i2pFld = flds[3];
            
                    Integer portInt;
                    int port;
            
                    Hashtable job = new Hashtable();
            
                    // stick in jobnumber, as int
                    Integer jobNumInt = new Integer(jobFld.substring(1, jobFld.length()-1));
                    int jobNum = jobNumInt.intValue();
                    job.put("job", jobNumInt);
            
                    // rest is type-dependent
                    if (typeFld.equals("<-"))
                    {
                        //System.out.println("server");
            
                        // fill out dict for a 'server' result
                        job.put("type", "server");
            
                        // tcp side is in form "hostname/ipaddr:port", carve up
                        String hostsStr;
                        String hostStr;
                        String ipStr;
                        String portStr;
                        
                        String [] tmpFlds = tcpFld.split(":");
            
                        //System.out.println("tmpFlds="+tmpFlds);
            
                        hostsStr = tmpFlds[0];
                        portStr = tmpFlds[1];
            
                        //System.out.println("hostsStr="+hostsStr+" portStr="+portStr);
            
                        portInt = new Integer(portStr);
                        port = portInt.intValue();
            
                        tmpFlds = hostsStr.split("/");
                        hostStr = tmpFlds[0];
                        ipStr = tmpFlds[1];
            
                        //System.out.println("hostStr="+hostStr+" ipStr="+ipStr);
            
                        job.put("host", hostStr);
                        job.put("ip", ipStr);
                        job.put("port", portInt);
                    }
                    else
                    {
                        //System.out.println("client");
            
                        // fill out dict for a 'client' result
                        job.put("type", "client");
            
                        portInt = new Integer(tcpFld);
                        port = portInt.intValue();
            
                        job.put("dest", i2pFld);
                        job.put("port", portInt);
                    }
            
                    jobs.add(job);
                }
            }
        
            result.put("status", "ok");
            //result.put("rawlist", raw);
            //result.put("rawlines", rawlines);
            result.put("jobs", jobs);
        
            return result;
        }
        
        public Hashtable xmlrpcClient(int portNum, String dest)
        {
            Hashtable result = new Hashtable();
            BufferLogger log = new BufferLogger();
        
            String [] args = new String[2];
        
            Integer portNumInt = new Integer(portNum);
            args[0] = portNumInt.toString();
            args[1] = dest;
            runClient(args, log);
        
            String reply = log.getBuffer();
        
            result.put("status", "ok");
            result.put("result", reply);
        
            return result;
        }
        
        public Hashtable xmlrpcServer(String host, int port, String key)
        {
            Hashtable result = new Hashtable();
            BufferLogger log = new BufferLogger();
        
            File keyBinFile;
        
            // create array for cli args
            String [] args = new String[3];
        
            // arg0 is host - easy
            args[0] = host;
        
            // arg1 is port, convert from int to string
            Integer portInt = new Integer(port);
            args[1] = portInt.toString();
        
            // arg2 is key filename
            // gotta convert base64 string to bin, write to
            // a temporary file, and extract the file path
            String keyBin = new String(net.i2p.data.Base64.decode(key));
            try
            {
                keyBinFile = File.createTempFile("xmlrpc_", ".priv");
                keyBinFile.createNewFile();
                FileWriter keyBinFileWriter = new FileWriter(keyBinFile);
                keyBinFileWriter.write(keyBin, 0, keyBin.length());
                keyBinFileWriter.flush();
                keyBinFileWriter.close();
            }
            catch (IOException e)
            {
                result.put("status", "fail");
                result.put("error", "IOException");
                return result;
            }
            args[2] = keyBinFile.getAbsolutePath();
        
            // do the run cmd
            runServer(args, log);
        
            // finished with privkey bin file, delete it
            keyBinFile.delete();
            
            String reply = log.getBuffer().trim();
            
            // Replies with 'Ready!' if ok, something else if not
            if (reply.equals("Ready!"))
            {
                result.put("status", "ok");
            }
            else
            {
                result.put("status", "fail");
                result.put("error", reply);
            }
        
            return result;
        }
        
        //
        // generate a new keypair
        //
        
        public Hashtable xmlrpcGenkeys()
        {
            Hashtable result = new Hashtable();
            BufferLogger log = new BufferLogger();
            String reply = "";
        
            // need a couple of output streams to collect the keys
            ByteArrayOutputStream privStream = new ByteArrayOutputStream();
            ByteArrayOutputStream destStream = new ByteArrayOutputStream();
        
            // generate the keys
            makeKey(privStream, destStream, log);
        
            // extract keys from output streams and convert into base64 strings
            String priv64 = net.i2p.data.Base64.encode(privStream.toByteArray());
            String dest64 = net.i2p.data.Base64.encode(destStream.toByteArray());
        
            // build up result map and bail
            result.put("status", "ok");
            result.put("priv", priv64);
            result.put("dest", dest64);
        
            return result;
        }
        
        // various front-ends for close
        
        public Hashtable xmlrpcClose(int jobnum)
        {
            Hashtable result = new Hashtable();
            BufferLogger log = new BufferLogger();
            String [] args = new String[1];
        
            String jobStr = String.valueOf(jobnum);
            args[0] = jobStr;
        
            runClose(args, log);
        
            String logOut = log.getBuffer();
        
            //System.out.println("close(int): got "+logOut);
        
            result.put("status", "ok");
            return result;
        }
        
        public Hashtable xmlrpcClose(Hashtable criteria)
        {
            Hashtable result = new Hashtable();
            BufferLogger log = new BufferLogger();
            String [] args = new String[1];
        
            // try special case, job==-1 means close all jobs
            if (criteria.containsKey("job"))
            {
                Object j = criteria.get("job");
                //System.out.println("job="+j);
            }
                
        
            if (criteria.containsKey("job") && criteria.get("job").equals("all"))
            {
                // easy - just fire a 'close all'
                //System.out.println("close: job=all");
                return xmlrpcClose("all");
            }
        
            // harder - get joblist and match criteria
            // get a list of running jobs
            Hashtable jobs = xmlrpcList();
            Vector jobsList = (Vector)(jobs.get("jobs"));
            //int numJobs = Array.length(jobsList);
            int numJobs = jobsList.size();
        
            int i;
            for (i = 0; i < numJobs; i++)
            {
                boolean closing = false;
            
                // fetch each job, and test if it matches our criteria
                Hashtable job = (Hashtable)jobsList.get(i);
                //System.out.println("close: existing job: "+job);
        
                if (criteria.containsKey("job") && criteria.get("job").equals(job.get("job")))
                {
                    //System.out.println("close: matched jobnum");
                    closing = true;
                }
                if (criteria.containsKey("type") && criteria.get("type").equals(job.get("type")))
                {
                    //System.out.println("close: matched type");
                    closing = true;
                }
                if (criteria.containsKey("port") && criteria.get("port").equals(job.get("port")))
                {
                    //System.out.println("close: matched port");
                    closing = true;
                }
                if (criteria.containsKey("host") && job.containsKey("host")
                    && (criteria.get("host").equals(job.get("host"))))
                {
                    //System.out.println("close: matched host="+criteria.get("host"));
                    closing = true;
                }
                if (criteria.containsKey("ip") && job.containsKey("ip")
                    && (criteria.get("ip").equals(job.get("ip"))))
                {
                    //System.out.println("close: matched ip="+criteria.get("ip"));
                    closing = true;
                }
        
                if (closing)
                {
                    String jobStr = String.valueOf(job.get("job"));
                    //System.out.println("close: matched criteria - closing job "+jobStr);
                    args[0] = jobStr;
                    runClose(args, log);
                    result.put("status", "ok");
                    return result;
                }
            }
        
            result.put("status", "ok");
            return result;
        }
        
        public Hashtable xmlrpcClose(String job)
        {
            Hashtable result = new Hashtable();
            BufferLogger log = new BufferLogger();
            String [] args = new String[1];
        
            // easy 'close all' case
            if (job.equals("all"))
            {
                args[0] = "all";
                runClose(args, log);
                result.put("status", "ok");
                return result;
            }
        
            // try to convert job to an int
            try
            {
                int jobnum = new Integer(job).intValue();
                return xmlrpcClose(jobnum);
            }
            catch (NumberFormatException e)
            {
                result.put("status", "error");
                result.put("error", "cannot convert job number arg to an int");
                return result;
            }
        }
        
    }