Exemple #1
0
  public int ParseUDP(BufferedReader bufferedReader, int count, Logger runtimeLogger) {

    try {
      while (true) {
        String line = bufferedReader.readLine();
        count++;

        if ("[/udp]".equalsIgnoreCase(line)) {
          break;
        }

        if (line.startsWith("#") | "".equals(line)) {
          continue;
        }

        String[] words = line.split(":");
        if (words.length == 3) {

          Pattern pattern;
          Matcher matcher;

          String regexName = "\\S+";
          pattern = Pattern.compile(regexName);
          matcher = pattern.matcher(words[0]);

          if (!matcher.find()) {
            runtimeLogger.error(
                String.format(
                    "UDP config error occurs at line %d: invalid UDP name %s", count, words[0]));
            System.exit(-1);
          }

          String regexLocalPort = "\\d{1,5}";
          pattern = Pattern.compile(regexLocalPort);
          matcher = pattern.matcher(words[1]);

          if (!matcher.find()) {
            runtimeLogger.error(
                String.format(
                    "UDP config error occurs at line %d: invalid local port %s", count, words[1]));
            System.exit(-1);
          }

          int localPort = Integer.valueOf(words[1]);
          if (localPort <= 0 | localPort > 65535) {
            runtimeLogger.error(
                String.format(
                    "UDP config error occurs at line %d: local port should between 1 - 65536",
                    count));
            System.exit(-1);
          }

          String regexRemotePort = "\\d{1,5}";
          pattern = Pattern.compile(regexRemotePort);
          matcher = pattern.matcher(words[2]);

          if (!matcher.find()) {
            runtimeLogger.error(
                String.format(
                    "UDP config error occurs at line %d: invalid remote port format", count));
            System.exit(-1);
          }

          int remotePort = Integer.valueOf(words[1]);
          if (remotePort <= 0 | remotePort > 65535) {
            runtimeLogger.error(
                String.format(
                    "UDP config error occurs at line %d: remote port should between 1 - 65536",
                    count));
            System.exit(-1);
          }

          PublicTunnelConfiguration publicTunnelConfiguration = new PublicTunnelConfiguration();
          publicTunnelConfiguration.setProtocol("udp");
          publicTunnelConfiguration.setName(words[0]);
          publicTunnelConfiguration.setLocalPort(Integer.valueOf(words[1]));
          publicTunnelConfiguration.setRemotePort(Integer.valueOf(words[2]));
          publicTunnelConfigurations.put(
              publicTunnelConfiguration.getName(), publicTunnelConfiguration);

        } else {
          runtimeLogger.error(String.format("UDP %s format is wrong at %d", line, count));
          System.exit(-1);
        }
      }
      return count;

    } catch (IOException e) {
      runtimeLogger.error(e.getMessage(), e);
      return 0;
    }
  }