entry.cpp 3.9 KB
Newer Older
W
init  
wangyunlai.wyl 已提交
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
/**
 * Copyright (c) 2021 OceanBase
 * OceanBase Migration Service LogProxy is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */

#include <functional>
#include "common/log.h"
#include "common/option.h"
#include "common/config.h"
#include "common/ob_aes256.h"
#include "arranger/arranger.h"

using namespace oceanbase::logproxy;

int main(int argc, char** argv)
{
  Config& conf = Config::instance();
  conf.process_name_address.set((uint64_t)argv[0]);

  OmsOptions options;
  options.add(OmsOption('h', "help", false, "help", [&](const std::string&) {
    options.usage();
    exit(0);
  }));
F
Fankux 已提交
32 33 34 35
  options.add(OmsOption('v', "version", false, "program version", [&](const std::string&) {
    printf("version: " __OMS_VERSION__ "\n");
    exit(0);
  }));
W
init  
wangyunlai.wyl 已提交
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
  options.add(OmsOption('f', "file", true, "configuration json file", [&](const std::string& optarg) {
    if (conf.load(optarg) != OMS_OK) {
      OMS_INFO << "failed to load config: " << optarg;
      exit(-1);
    }
  }));
  options.add(OmsOption('P', "listen_port", true, "listen port", [&](const std::string& optarg) {
    conf.service_port.set(::strtol(optarg.c_str(), nullptr, 10));
  }));
  options.add(OmsOption('D', "debug", false, "debug mode not send message", [&](const std::string& optarg) {
    conf.debug.set(true);
    OMS_INFO << "enable DEBUG mode";
  }));
  options.add(OmsOption('V', "verbose", false, "print plentiful log", [&](const std::string& optarg) {
    conf.verbose.set(true);
    OMS_INFO << "enable VERBOSE log";
  }));
  options.add(OmsOption('x', "encrypt", true, "encrypt text", [&](const std::string& optarg) {
    AES aes;
    char* encrypted = nullptr;
    int encrypted_len = 0;
    int ret = aes.encrypt(optarg.c_str(), optarg.size(), &encrypted, encrypted_len);
    if (ret != OMS_OK) {
      printf("Failed to encrypt\n");
      exit(-1);
    }
    std::string hex;
    dumphex(encrypted, encrypted_len, hex);
    printf("%s\n", hex.c_str());
    free(encrypted);
    exit(0);
  }));
  options.add(OmsOption('y', "decrypt", true, "decrypt text", [&](const std::string& optarg) {
    AES aes;
    std::string binary;
    hex2bin(optarg.c_str(), optarg.size(), binary);
    if (binary.empty()) {
      printf("Failed to decrypt, invalid hex input\n");
      exit(-1);
    }

    char* plain = nullptr;
    int plain_len = 0;
    int ret = aes.decrypt(binary.c_str(), binary.size(), &plain, plain_len);
    if (ret != OMS_OK) {
      printf("Failed to decrypt\n");
      exit(-1);
    }
    printf("%s\n", std::string(plain, plain_len).c_str());
    free(plain);
    exit(0);
  }));

  std::string file = "./conf/conf.json";

  struct option* long_options = options.long_pattern();
  std::string pattern = options.pattern();

  // read file config first
  std::map<int, std::string> opts;
  for (int opt = 0; (opt = getopt_long(argc, argv, pattern.c_str(), long_options, nullptr)) != -1;) {
    if (opt == 'f') {
      options.get('f')->func(std::string(optarg));
    } else {
      opts.emplace(opt, optarg == nullptr ? "" : optarg);
    }
  }

  // command line options overwrite configuration options
  for (auto& opt_entry : opts) {
    OmsOption* option = options.get(opt_entry.first);
    if (option == nullptr) {
      options.usage();
      return 0;
    } else {
      option->func(option->is_arg ? opt_entry.second : "");
    }
  }

  init_log(argv[0]);

  if (Arranger::instance().init() != OMS_OK) {
    ::exit(-1);
  }

  int ret = Arranger::instance().run_foreground();
  OMS_WARN << "!!!LogProxy Exit, ret: " << ret;
  return 0;
}