Beispiel #1
0
jboolean Java_net_pagekite_lib_PageKiteAPI_init(
  JNIEnv* env, jclass unused_class
, jstring japp_id
, jint jmax_kites
, jint jmax_frontends
, jint jmax_conns
, jstring jdyndns_url
, jint jflags
, jint jverbosity
){
  if (pagekite_manager_global != NULL) return JNI_FALSE;

  const jbyte* app_id = NULL;
  if (japp_id != NULL) app_id = (*env)->GetStringUTFChars(env, japp_id, NULL);
  int max_kites = jmax_kites;
  int max_frontends = jmax_frontends;
  int max_conns = jmax_conns;
  const jbyte* dyndns_url = NULL;
  if (jdyndns_url != NULL) dyndns_url = (*env)->GetStringUTFChars(env, jdyndns_url, NULL);
  int flags = jflags;
  int verbosity = jverbosity;

  pagekite_manager_global = pagekite_init(app_id, max_kites, max_frontends, max_conns, dyndns_url, flags, verbosity);

  if (japp_id != NULL) (*env)->ReleaseStringUTFChars(env, japp_id, app_id);
  if (jdyndns_url != NULL) (*env)->ReleaseStringUTFChars(env, jdyndns_url, dyndns_url);
  return (pagekite_manager_global != NULL);
}
Beispiel #2
0
int main(int argc, char **argv) {
  pagekite_mgr m;
  unsigned int bail_on_errors = 0;
  unsigned int conn_eviction_idle_s = 0;
  char* proto;
  char* kitename;
  char* secret;
  char* lua_settings[MAX_PLUGIN_ARGS+1];
  int lua_settingc = 0;
  int lua_no_defaults = 0;
  int gotargs = 0;
  int verbosity = 0;
  int use_ipv4 = 1;
#ifdef HAVE_IPV6
  int use_ipv6 = 1;
#endif
  int use_current = 1;
  int use_ssl = 1;
  int use_fake_ping = 0;
  int use_watchdog = 0;
  int max_conns = 25;
  int spare_frontends = 0;
  char* fe_hostname = NULL;
  char* ddns_url = PAGEKITE_NET_DDNS;
  int ac;
  int pport;
  int lport;

  /* FIXME: Is this too lame? */
  srand(time(0) ^ getpid());

  while (-1 != (ac = getopt(argc, argv, "46c:B:CE:F:Io:Ln:qRSvWZ"))) {
    switch (ac) {
      case '4':
        use_ipv4 = 0;
        break;
      case '6':
#ifdef HAVE_IPV6
        use_ipv6 = 0;
#endif
        break;
      case 'C':
        use_current = 0;
        break;
      case 'v':
        verbosity++;
        break;
      case 'q':
        verbosity--;
        break;
      case 'I':
        use_ssl = 0;
        break;
      case 'R':
        use_fake_ping = 1;
        break;
      case 'S':
        ddns_url = NULL;
        break;
      case 'W':
        use_watchdog = 1;
        break;
      case 'F':
        gotargs++;
        assert(fe_hostname == NULL);
        fe_hostname = strdup(optarg);
        break;
      case 'B':
        gotargs++;
        if (1 == sscanf(optarg, "%u", &bail_on_errors)) break;
        usage(EXIT_ERR_USAGE);
      case 'c':
        gotargs++;
        if (1 == sscanf(optarg, "%d", &max_conns)) break;
        usage(EXIT_ERR_USAGE);
      case 'E':
        gotargs++;
        if (1 == sscanf(optarg, "%u", &conn_eviction_idle_s)) break;
        usage(EXIT_ERR_USAGE);
      case 'o':
        if (lua_settingc >= MAX_PLUGIN_ARGS) usage(EXIT_ERR_USAGE);
        gotargs++;
        lua_settings[lua_settingc++] = strdup(optarg);
        break;
      case 'L':
        lua_no_defaults = 1;
        break;
      case 'n':
        gotargs++;
        if (1 == sscanf(optarg, "%d", &spare_frontends)) break;
        usage(EXIT_ERR_USAGE);
      default:
        usage(EXIT_ERR_USAGE);
    }
    gotargs++;
  }
  lua_settings[lua_settingc] = NULL;

  if ((argc-1-gotargs) < 5 || ((argc-1-gotargs) % 5) != 0) {
    usage(EXIT_ERR_USAGE);
  }

#ifndef _MSC_VER
  signal(SIGUSR1, &raise_log_level);
#endif

  int flags = 0;
  if (use_ssl) flags |= PK_WITH_SSL;
  if (use_ipv4) flags |= PK_WITH_IPV4;
#ifdef HAVE_IPV6
  if (use_ipv6) flags |= PK_WITH_IPV6;
#endif

  if (NULL == (m = pagekite_init("pagekitec",
                                 1 + (argc-1-gotargs)/5, /* Kites */
                                 PAGEKITE_NET_FE_MAX,
                                 max_conns,
                                 ddns_url,
                                 flags,
                                 verbosity)))
  {
    pagekite_perror(m, argv[0]);
    safe_exit(EXIT_ERR_MANAGER_INIT);
  }

  /* Set all the parameters */
  pagekite_want_spare_frontends(m, spare_frontends);
  pagekite_enable_watchdog(m, use_watchdog);
  pagekite_enable_fake_ping(m, use_fake_ping);
  pagekite_set_bail_on_errors(m, bail_on_errors);
  pagekite_set_conn_eviction_idle_s(m, conn_eviction_idle_s);
  pagekite_enable_lua_plugins(m, !lua_no_defaults, lua_settings);

  for (ac = gotargs; ac+5 < argc; ac += 5) {
    if ((1 != sscanf(argv[ac+1], "%d", &lport)) ||
        (1 != sscanf(argv[ac+4], "%d", &pport))) {
      usage(EXIT_ERR_USAGE);
    }
    proto = argv[ac+2];
    kitename = argv[ac+3];
    secret = argv[ac+5];
    if ((0 > pagekite_add_kite(m, proto, kitename, 0, secret,
                               "localhost", lport)) ||
        (use_current && (0 > (pagekite_add_frontend(m, kitename, pport)))))
    {
      pagekite_perror(m, argv[0]);
      safe_exit(EXIT_ERR_ADD_KITE);
    }
  }

  /* The API could do this stuff on INIT, but since we allow for manually
     specifying a front-end hostname, we do things by hand. */
  if (fe_hostname) {
    if (0 > pagekite_add_frontend(m, fe_hostname, 443)) {
      pagekite_perror(m, argv[0]);
      safe_exit(EXIT_ERR_FRONTENDS);
    }
  }
  else if (ddns_url != NULL) {
    if (0 > pagekite_add_service_frontends(m, flags)) {
      pagekite_perror(m, argv[0]);
      safe_exit(EXIT_ERR_FRONTENDS);
    }
  }

  if (0 > pagekite_start(m)) {
    pagekite_perror(m, argv[0]);
    safe_exit(EXIT_ERR_START_THREAD);
  }

  pagekite_wait(m);
  pagekite_free(m);

  return 0;
}