summaryrefslogtreecommitdiff
path: root/src/main.cc
blob: f4f6a846a551beefded719ec9e419c1816c142ff (plain)
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
32
33
34
35
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// -*- mode: c++; c-basic-offset: 2; -*-

#include "common.hh"

#include <cstring>
#include <memory>
#include <signal.h>
#include <unistd.h>

#include "args.hh"
#include "config.hh"
#include "io.hh"
#include "logger.hh"
#include "looper.hh"
#include "proxy.hh"
#include "resolver.hh"

namespace {

Proxy* g_proxy;

void proxy_quit(int UNUSED(sig)) {
  g_proxy->quit();
}

void proxy_reload(int UNUSED(sig)) {
  g_proxy->reload();
}

void setup_signals(Proxy* proxy) {
  g_proxy = proxy;
  struct sigaction action;
  memset(&action, 0, sizeof(action));
  action.sa_handler = proxy_quit;
  action.sa_flags = SA_RESTART;
  sigaction(SIGINT, &action, nullptr);
  sigaction(SIGQUIT, &action, nullptr);
  action.sa_handler = proxy_reload;
  sigaction(SIGHUP, &action, nullptr);
}

std::string get_cwd() {
  std::string ret;
  char buf[128];
  auto ptr = getcwd(buf, 128);
  if (ptr) {
    ret.assign(ptr, strlen(ptr));
    return ret;
  }
  if (errno != ERANGE) return ret;
  ret.resize(1024);
  while (true) {
    ptr = getcwd(&ret[0], ret.size());
    if (ptr) {
      ret.resize(strlen(ret.data()));
      break;
    }
    if (errno != ERANGE) {
      ret.resize(0);
      break;
    }
    ret.resize(ret.size() * 2);
  }
  return ret;
}

}  // namespace

int main(int argc, char** argv) {
  std::unique_ptr<Args> args(Args::create());
  args->add('C', "config", "FILE", "load config from FILE instead of default");
  args->add('F', "foreground", "do not fork into background and log to stdout");
  args->add('m', "monitor", "HOST:PORT", "accept monitors on HOST:PORT");
  args->add('h', "help", "display this text and exit.");
  args->add('V', "version", "display version and exit.");
  if (!args->run(argc, argv)) {
    std::cerr << "Try `tp --help` for usage." << std::endl;
    return EXIT_FAILURE;
  }
  if (args->is_set('h')) {
    std::cout << "Usage: `tp [OPTIONS...] [BIND][:PORT]`\n"
              << "Transparent proxy.\n"
              << '\n';
    args->print_help();
    return EXIT_SUCCESS;
  }
  if (args->is_set('V')) {
    std::cout << "TransparentProxy version " VERSION
              << " written by Joel Klinghed <the_jk@yahoo.com>" << std::endl;
    return EXIT_SUCCESS;
  }
  std::unique_ptr<Config> config(Config::create());
  switch (args->arguments().size()) {
  case 0:
    break;
  case 1: {
    auto arg = args->arguments().front();
    auto colon = arg.find(':');
    if (colon == std::string::npos) {
      config->set("proxy_bind", arg);
    } else {
      if (colon > 0) {
        config->set("proxy_bind", arg.substr(0, colon));
      }
      config->set("proxy_port", arg.substr(colon + 1));
    }
    break;
  }
  default:
    std::cerr << "Too many arguments.\n"
              << "Try `tp --help` for usage." << std::endl;
    return EXIT_FAILURE;
  }
  if (args->is_set('F')) {
    config->set("foreground", "true");
  }
  auto monitor = args->arg('m', nullptr);
  if (monitor) {
    auto str = std::string(monitor);
    auto colon = str.find(':');
    if (colon == 0 || colon == std::string::npos) {
      std::cerr << "Invalid argument to monitor, expected HOST:PORT not: "
                << str << std::endl;
      return EXIT_FAILURE;
    }
    config->set("monitor", "true");
    config->set("monitor_bind", str.substr(0, colon));
    config->set("monitor_port", str.substr(colon + 1));
  }
  auto configfile = args->arg('C', nullptr);
  if (configfile) {
    config->load_file(configfile);
  } else {
    config->load_name("tp");
  }
  if (!config->good()) {
    std::cerr << "Error loading config\n"
              << config->last_error() << std::endl;
    return EXIT_FAILURE;
  }
  std::unique_ptr<Logger> logger(Logger::create_stderr());
  std::unique_ptr<Logger> file_logger;
  auto logfile = args->arg('l', nullptr);
  bool logfile_from_argument;
  if (!logfile) {
    logfile = config->get("logfile", nullptr);
    logfile_from_argument = false;
  } else {
    logfile_from_argument = true;
  }
  if (logfile) {
    if (logfile[0] != '/') {
      logger->out(Logger::ERR, "Logfile need to be an absolute path, not: %s",
                  logfile);
      return EXIT_FAILURE;
    }
    file_logger.reset(Logger::create_file(logfile));
    if (!file_logger) {
      logger->out(Logger::ERR, "Unable to open %s for logging: %s",
                  logfile, strerror(errno));
      return EXIT_FAILURE;
    }
  }
  io::auto_fd accept_socket(Proxy::setup_accept_socket(config.get(),
                                                       logger.get()));
  if (!accept_socket) return EXIT_FAILURE;
  io::auto_fd monitor_socket;
  if (config->get("monitor", false)) {
    monitor_socket.reset(Proxy::setup_monitor_socket(config.get(),
                                                     logger.get()));
  }
  auto foreground = config->get("foreground", false);
  auto cwd = get_cwd();
  std::unique_ptr<Looper> looper(Looper::create());
  std::unique_ptr<Resolver> resolver(Resolver::create(looper.get()));
  std::unique_ptr<Proxy> proxy(
      Proxy::create(config.get(), cwd, configfile,
                    foreground ? "bogus" :
                        (logfile_from_argument ? logfile : nullptr),
                    foreground ? logger.get() : file_logger.get(),
                    accept_socket.release(),
                    monitor_socket.release(),
                    looper.get(), resolver.get()));
  if (!foreground) {
    if (daemon(0, 0)) {
      logger->out(Logger::ERR, "Failed to fork: %s", strerror(errno));
      return EXIT_FAILURE;
    }
  }
  setup_signals(proxy.get());
  return proxy->run() ? EXIT_SUCCESS : EXIT_FAILURE;
}