예제 #1
0
  /**
   * 执行shell命令
   *
   * @param cmd
   * @return
   */
  public static String execute(String cmd, long timeout) {
    try {
      String[] cmds = null;
      if (cmd.startsWith("sh -c ")) {
        cmd = cmd.substring(6).trim();
        cmds = new String[] {"sh", "-c", cmd};
      }

      String line = "";
      Process process = null;
      if (cmds == null) {
        process = Runtime.getRuntime().exec(cmd);
      } else {
        process = Runtime.getRuntime().exec(cmds);
      }

      try {
        // 等待进程退出
        if (timeout <= 0) {
          process.waitFor();
        } else {
          synchronized (process) {
            process.wait(timeout);
          }
        }

        StringBuilder builder = new StringBuilder(4096);
        BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
        while ((line = input.readLine()) != null) {
          builder.append(line).append("\r\n");
        }
        input.close();

        String result = builder.toString();
        if (result == null || result.length() <= 0) {
          result = "success";
        }
        return result;
      } catch (Exception e) {
        HawkException.catchException(e);
      } finally {
        process.destroy();
      }
    } catch (Exception e) {
      HawkException.catchException(e);
    }
    return "failed";
  }
예제 #2
0
  /**
   * 实际解析协议模板
   *
   * @param protocol
   * @param template
   * @return
   */
  @SuppressWarnings("unchecked")
  public <T extends GeneratedMessage> T parseProtocol(HawkProtocol protocol, T template) {
    if (protocol != null && template != null) {
      try {
        T pbProtocol = template;
        Parser<T> parser = (Parser<T>) parsers.get(template.getClass().getName());
        if (parser == null) {
          parser = (Parser<T>) template.getClass().getField("PARSER").get(template);
          parsers.put(template.getClass().getName(), parser);
        }

        if (protocol.getSize() > 0) {
          pbProtocol =
              parser.parseFrom(protocol.getOctets().getBuffer().array(), 0, protocol.getSize());
        }

        logProtocolBuilder(protocol, pbProtocol);
        return pbProtocol;
      } catch (Exception e) {
        HawkException.catchException(e);
        // 抛出运行时异常
        throw new RuntimeException("protocol parse exception: " + protocol.getType());
      }
    }
    return null;
  }
 /**
  * 格式:
  * game=%s&platform=%s&server=%s&puid=%s&device=%s&playerid=%d&playerlevel=%d&changetype=%d&changeaction=%s&goldtype=%d&gold=%d&time=%s
  */
 @Override
 public void handle(HttpExchange httpExchange) throws IOException {
   try {
     Map<String, String> params = CollectorHttpServer.parseHttpParam(httpExchange);
     Collector.checkToken(params.get("token"));
     doReport(params);
   } catch (Exception e) {
     HawkException.catchException(e);
   } finally {
     CollectorHttpServer.response(httpExchange, null);
   }
 }
  @Override
  @SuppressWarnings("unused")
  public void handle(HttpExchange httpExchange) throws IOException {
    try {
      String contentBody = HawkOSOperator.readRequestBody(httpExchange).replace("\r", "");

      int pos = contentBody.indexOf("\n");
      if (pos > 0) {
        int index = 0;
        String header = contentBody.substring(0, pos).trim();
        String items[] = header.split("\\^");
        if (items.length >= 7) {
          String md5 = "";
          int stackPos = contentBody.indexOf("stack traceback");
          if (stackPos > 0) {
            md5 = HawkMd5.makeMD5(contentBody.substring(stackPos));
          } else {
            md5 = HawkMd5.makeMD5(contentBody);
          }
          if (logMd5Map.containsKey(md5)) {
            return;
          }
          logMd5Map.put(md5, md5);
          HawkLog.logPrintln("\r\nUserLog: " + contentBody);

          String game = items[index++];
          String logtype = items[index++];
          String platform = items[index++];
          String serverid = items[index++];
          String puid = items[index++];
          String phoneinfo = items[index++];
          String sysinfo = items[index++];
          if (game.length() > 0
              && logtype.length() > 0
              && platform.length() > 0
              && serverid.length() > 0
              && puid.length() > 0) {
            // 创建日志目录
            String dir = HawkOSOperator.getWorkPath() + "userlog/" + game + "/" + logtype + "/";
            HawkOSOperator.createDir(dir);

            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd.HHmmss");
            String tm = sdf.format(HawkTime.getDate());
            String fileName = String.format("%s%s.%s.%s.%s", dir, platform, serverid, puid, tm);
            HawkOSOperator.saveAsFile(contentBody, fileName);
          }
        }
      }
    } catch (Exception e) {
      HawkException.catchException(e);
    }
  }
예제 #5
0
 @Override
 public void handle(HttpExchange httpExchange) throws IOException {
   try {
     String method = httpExchange.getRequestMethod();
     if ("GET".equals(method)) {
       doGet(httpExchange);
     } else if ("POST".equals(method)) {
       doPost(httpExchange);
     }
   } catch (Exception e) {
     HawkException.catchException(e);
   } finally {
     httpExchange.close();
   }
 }
예제 #6
0
  /**
   * 实际解析协议模板
   *
   * @param protocol
   * @return
   */
  public HawkPacket parsePacket(HawkProtocol protocol) {
    if (protocol != null) {
      try {
        HawkPacket packet = createPacket(protocol.getType());
        if (packet != null) {
          packet.unmarshal(protocol.getOctets());
          return packet;
        }
        // 协议存根不存在
        throw new RuntimeException("packet stub illegal: " + protocol.getType());

      } catch (Exception e) {
        HawkException.catchException(e);
        // 抛出运行时异常
        throw new RuntimeException("packet parse exception: " + protocol.getType());
      }
    }
    return null;
  }
예제 #7
0
  /**
   * 实际解析协议模板
   *
   * @param protocol
   * @param template
   * @return
   */
  @SuppressWarnings("unchecked")
  public <T extends GeneratedMessage> T parseFromJson(HawkProtocol protocol, T template) {
    if (protocol != null && template != null) {
      try {
        T pbProtocol = template;
        if (protocol.getSize() > 0) {
          String pbJson =
              new String(protocol.getOctets().getBuffer().array(), 0, protocol.getSize());
          GeneratedMessage.Builder<?> builder = (Builder<?>) template.newBuilderForType();
          JsonFormat.merge(pbJson, builder);
          pbProtocol = (T) builder.build();
        }

        logProtocolBuilder(protocol, pbProtocol);
        return pbProtocol;
      } catch (Exception e) {
        HawkException.catchException(e);
        // 抛出运行时异常
        throw new RuntimeException("protocol parse exception: " + protocol.getType());
      }
    }
    return null;
  }