예제 #1
0
  private void startOpenVPNThreadArgs(String[] argv, Map<String, String> env) {
    LinkedList<String> argvlist = new LinkedList<String>();

    Collections.addAll(argvlist, argv);

    ProcessBuilder pb = new ProcessBuilder(argvlist);
    // Hack O rama

    String lbpath = genLibraryPath(argv, pb);

    pb.environment().put("LD_LIBRARY_PATH", lbpath);

    // Add extra variables
    for (Entry<String, String> e : env.entrySet()) {
      pb.environment().put(e.getKey(), e.getValue());
    }
    pb.redirectErrorStream(true);
    try {
      mProcess = pb.start();

      // openvpn被配置为从stdin接受配置文件
      // 写入配置文件
      Writer writer = new OutputStreamWriter(mProcess.getOutputStream(), "UTF-8");
      VpnProfile startProfile = VPNLaunchHelper.getStartProfile();
      String configString = startProfile.getConfigFile(VPNLaunchHelper.getStartContext(), false);
      writer.write(configString);
      writer.close();

      InputStream in = mProcess.getInputStream();
      BufferedReader br = new BufferedReader(new InputStreamReader(in));

      while (true) {
        String logline = br.readLine();
        if (logline == null) return;

        if (logline.startsWith(DUMP_PATH_STRING))
          mDumpPath = logline.substring(DUMP_PATH_STRING.length());

        if (logline.startsWith(BROKEN_PIE_SUPPORT) || logline.contains(BROKEN_PIE_SUPPORT2))
          mBrokenPie = true;

        // 1380308330.240114 18000002 Send to HTTP proxy: 'X-Online-Host: bla.blabla.com'

        Pattern p = Pattern.compile("(\\d+).(\\d+) ([0-9a-f])+ (.*)");
        Matcher m = p.matcher(logline);
        if (m.matches()) {
          int flags = Integer.parseInt(m.group(3), 16);
          String msg = m.group(4);
          int logLevel = flags & 0x0F;

          VpnStatus.LogLevel logStatus = VpnStatus.LogLevel.INFO;

          if ((flags & M_FATAL) != 0) logStatus = VpnStatus.LogLevel.ERROR;
          else if ((flags & M_NONFATAL) != 0) logStatus = VpnStatus.LogLevel.WARNING;
          else if ((flags & M_WARN) != 0) logStatus = VpnStatus.LogLevel.WARNING;
          else if ((flags & M_DEBUG) != 0) logStatus = VpnStatus.LogLevel.VERBOSE;

          if (msg.startsWith("MANAGEMENT: CMD")) logLevel = Math.max(4, logLevel);

          VpnStatus.logMessageOpenVPN(logStatus, logLevel, msg);
        } else {
          VpnStatus.logInfo("P:" + logline);
        }
      }

    } catch (IOException e) {
      VpnStatus.logException("Error reading from output of OpenVPN process", e);
      stopProcess();
    }
  }