Beispiel #1
0
  public void LoadConfiguration(String[] args, Option option, Logger runtimeLogger) {

    Options options = new Options();
    options.addOption("config", true, "config path");

    CommandLineParser parser = new DefaultParser();
    CommandLine cmd = null;
    try {
      cmd = parser.parse(options, args);
    } catch (ParseException e) {
      runtimeLogger.error(e.getMessage(), e);
    }

    if (cmd == null) {
      runtimeLogger.error("command is null");
      return;
    }

    if (cmd.hasOption("config")) {

      FileReader fileReader = null;
      BufferedReader bufferedReader = null;
      try {
        fileReader = new FileReader(cmd.getOptionValue("config"));
        bufferedReader = new BufferedReader(fileReader);

        Stack<String> items = new Stack<>();
        int count = 0;
        while (true) {

          String line = bufferedReader.readLine();
          if (line == null) {
            break;
          }
          count++;

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

          switch (line) {
            case "[auth]":
              items.push("[auth]");
              count = ParseAuth(bufferedReader, count, runtimeLogger);
              if (count == 0) {
                runtimeLogger.error("Error occurs when parsing [auth]");
                System.exit(-1);
              }
              items.pop();
              break;

            case "[server]":
              items.push("[server]");
              count = ParseServer(bufferedReader, count, runtimeLogger);
              if (count == 0) {
                runtimeLogger.error("Error occurs when parsing [server]");
                System.exit(-1);
              }
              items.pop();
              break;

            case "[tcp]":
              items.push("[tcp]");
              count = ParseTcp(bufferedReader, count, runtimeLogger);
              if (count == 0) {
                runtimeLogger.error("Error occurs when parsing [tcp]");
                System.exit(-1);
              }
              items.pop();
              break;

            case "[http]":
              items.push("[http]");
              count = ParseHttp(bufferedReader, count, runtimeLogger);
              if (count == 0) {
                runtimeLogger.error("Error occurs when parsing [http]");
                System.exit(-1);
              }
              items.pop();
              break;

            case "[udp]":
              items.push("[udp]");
              count = ParseUDP(bufferedReader, count, runtimeLogger);
              if (count == 0) {
                runtimeLogger.error("Error occurs when parsing [udp]");
                System.exit(-1);
              }
              items.pop();
              break;

            default:
              runtimeLogger.error(String.format("Unknown syntax at %s[%d]", line, count));
              System.exit(-1);
          }
        }
        if (!items.empty()) {
          runtimeLogger.error(String.format("Mismatch bracket %s", items.pop()));
        }

        runtimeLogger.info(String.format("Read %d config lines", count));

      } catch (IOException e) {

        runtimeLogger.error(String.format("Can not open file %s", cmd.getOptionValue("config")));
      } finally {

        if (fileReader != null) {
          try {
            fileReader.close();
          } catch (IOException e) {
            runtimeLogger.error("Error occurs when closing the config file");
          }
        }

        if (bufferedReader != null) {
          try {
            bufferedReader.close();
          } catch (IOException e) {
            runtimeLogger.error("Error occurs when closing the config file");
          }
        }
      }

    } else {
      PublicTunnelConfiguration publicTunnelConfiguration = new PublicTunnelConfiguration();
      publicTunnelConfiguration.setName("default");

      publicTunnelConfigurations.put(
          publicTunnelConfiguration.getName(), publicTunnelConfiguration);
    }

    switch (args[0]) {
      case "list":
        for (Map.Entry<String, PublicTunnelConfiguration> entry :
            publicTunnelConfigurations.entrySet()) {
          String publicTunnelName = entry.getKey();
          PublicTunnelConfiguration publicTunnelConfiguration = entry.getValue();
          String description;
          switch (publicTunnelConfiguration.getProtocol()) {
            case "tcp":
              description =
                  String.format(
                      "%s:%d:%d:%s",
                      publicTunnelName,
                      publicTunnelConfiguration.getLocalPort(),
                      publicTunnelConfiguration.getRemotePort(),
                      publicTunnelConfiguration.getProtocol());
              break;

            case "http":
              description =
                  String.format(
                      "%s:%s:%d:%d:%s",
                      publicTunnelName,
                      publicTunnelConfiguration.getSubDomain(),
                      publicTunnelConfiguration.getLocalPort(),
                      publicTunnelConfiguration.getRemotePort(),
                      publicTunnelConfiguration.getProtocol());
              break;

            case "udp":
              description =
                  String.format(
                      "%s:%d:%d:%s",
                      publicTunnelName,
                      publicTunnelConfiguration.getLocalPort(),
                      publicTunnelConfiguration.getRemotePort(),
                      publicTunnelConfiguration.getProtocol());
              break;

            default:
              description = String.format("%s:unknown tunnel", publicTunnelName);
          }

          System.out.println(description);
        }
        System.exit(0);

      case "version":
        System.out.println(String.format("Natman version:%f\n", option.getVersion()));
        System.exit(0);

      case "help":
        System.out.println(prompt);
        System.exit(0);

      case "":
        System.out.println(prompt);
        System.exit(0);

      default:
    }
  }
Beispiel #2
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;
    }
  }