static int bz_autotools__stage(void *user_data) { struct bz_env *env = user_data; const char *package_name; struct cork_path *build_dir; struct cork_path *staging_dir; bool verbose; struct cork_env *exec_env; struct cork_exec *exec; rii_check(bz_stage_message(env, "autotools")); rip_check(package_name = bz_env_get_string(env, "name", true)); clog_info("(%s) Stage using autotools", package_name); rip_check(build_dir = bz_env_get_path(env, "build_dir", true)); rip_check(staging_dir = bz_env_get_path(env, "staging_dir", true)); rie_check(verbose = bz_env_get_bool(env, "verbose", true)); /* Create the staging path */ rii_check(bz_create_directory(cork_path_get(staging_dir), 0750)); /* $ make install */ exec = cork_exec_new("make"); cork_exec_add_param(exec, "make"); cork_exec_add_param(exec, "install"); cork_exec_set_cwd(exec, cork_path_get(build_dir)); exec_env = cork_env_clone_current(); cork_env_add(exec_env, "DESTDIR", cork_path_get(staging_dir)); cork_exec_set_env(exec, exec_env); return bz_subprocess_run_exec(verbose, NULL, exec); }
static int start_ss_plugin(const char *plugin, const char *plugin_opts, const char *remote_host, const char *remote_port, const char *local_host, const char *local_port, enum plugin_mode mode) { cork_env_add(env, "SS_REMOTE_HOST", remote_host); cork_env_add(env, "SS_REMOTE_PORT", remote_port); cork_env_add(env, "SS_LOCAL_HOST", local_host); cork_env_add(env, "SS_LOCAL_PORT", local_port); if (plugin_opts != NULL) cork_env_add(env, "SS_PLUGIN_OPTIONS", plugin_opts); #ifdef __ANDROID__ exec = cork_exec_new_with_params("sh", "-c", plugin, NULL); #else exec = cork_exec_new_with_params(plugin, NULL); #endif cork_exec_set_env(exec, env); sub = cork_subprocess_new_exec(exec, NULL, NULL, &exit_code); #ifdef __MINGW32__ cork_subprocess_set_control(sub, sub_control_port); #endif return cork_subprocess_start(sub); }
static void sub_run(int argc, char **argv) { struct cork_env *env; struct cork_exec *exec; struct cork_subprocess_group *group; struct cork_subprocess *sp; if (argc == 0) { cork_command_show_help(&sub, "Missing command"); exit(EXIT_FAILURE); } rp_check_exit(env = cork_env_clone_current()); rp_check_exit(exec = cork_exec_new_with_param_array(argv[0], argv)); cork_exec_set_env(exec, env); if (sub_cwd != NULL) { cork_exec_set_cwd(exec, sub_cwd); } rp_check_exit(group = cork_subprocess_group_new()); rp_check_exit(sp = cork_subprocess_new_exec(exec, NULL, NULL, NULL)); cork_subprocess_group_add(group, sp); ri_check_exit(cork_subprocess_group_start(group)); ri_check_exit(cork_subprocess_group_wait(group)); cork_subprocess_group_free(group); }
/* * For obfsproxy, we use standalone mode for now. * Managed mode needs to use SOCKS5 proxy as forwarder, which is not supported * yet. * * The idea of using standalone mode is quite simple, just assemble the * internal port into obfsproxy parameters. * * Using manually ran scramblesuit as an example: * obfsproxy \ * --data-dir /tmp/ss_libev_plugin_with_suffix \ * scramblesuit \ * --password SOMEMEANINGLESSPASSWORDASEXAMPLE \ * --dest some.server.org:12345 \ * client \ * 127.0.0.1:54321 * * In above case, @plugin = "obfsproxy", * @plugin_opts = "scramblesuit --password SOMEMEANINGLESSPASSWORDASEXAMPLE" * For obfs3, it's even easier, just pass @plugin = "obfsproxy" * @plugin_opts = "obfs3" * * And the rest parameters are all assembled here. * Some old obfsproxy will not be supported as it doesn't even support * "--data-dir" option */ static int start_obfsproxy(const char *plugin, const char *plugin_opts, const char *remote_host, const char *remote_port, const char *local_host, const char *local_port, enum plugin_mode mode) { char *pch; char *opts_dump = NULL; char *buf = NULL; int ret, buf_size = 0; if (plugin_opts != NULL) { opts_dump = strndup(plugin_opts, OBFSPROXY_OPTS_MAX); if (!opts_dump) { ERROR("start_obfsproxy strndup failed"); if (env != NULL) { cork_env_free(env); } return -ENOMEM; } } exec = cork_exec_new(plugin); /* The first parameter will be skipped, so pass @plugin again */ cork_exec_add_param(exec, plugin); cork_exec_add_param(exec, "--data-dir"); buf_size = 20 + strlen(plugin) + strlen(remote_host) + strlen(remote_port) + strlen(local_host) + strlen(local_port); buf = ss_malloc(buf_size); snprintf(buf, buf_size, TEMPDIR "%s_%s:%s_%s:%s", plugin, remote_host, remote_port, local_host, local_port); cork_exec_add_param(exec, buf); /* * Iterate @plugin_opts by space */ if (opts_dump != NULL) { pch = strtok(opts_dump, " "); while (pch) { cork_exec_add_param(exec, pch); pch = strtok(NULL, " "); } } /* The rest options */ if (mode == MODE_CLIENT) { /* Client mode */ cork_exec_add_param(exec, "--dest"); snprintf(buf, buf_size, "%s:%s", remote_host, remote_port); cork_exec_add_param(exec, buf); cork_exec_add_param(exec, "client"); snprintf(buf, buf_size, "%s:%s", local_host, local_port); cork_exec_add_param(exec, buf); } else { /* Server mode */ cork_exec_add_param(exec, "--dest"); snprintf(buf, buf_size, "%s:%s", local_host, local_port); cork_exec_add_param(exec, buf); cork_exec_add_param(exec, "server"); snprintf(buf, buf_size, "%s:%s", remote_host, remote_port); cork_exec_add_param(exec, buf); } cork_exec_set_env(exec, env); sub = cork_subprocess_new_exec(exec, NULL, NULL, &exit_code); #ifdef __MINGW32__ cork_subprocess_set_control(sub, sub_control_port); #endif ret = cork_subprocess_start(sub); ss_free(opts_dump); free(buf); return ret; }
static int bz_autotools__build(void *user_data) { struct bz_env *env = user_data; struct bz_value *ctx = bz_env_as_value(env); const char *package_name; struct cork_path *build_dir; struct cork_path *source_dir; struct cork_path *configure; struct bz_value *configure_args; const char *pkgconfig_path; bool exists; bool verbose; struct cork_exec *exec; struct cork_env *exec_env; struct cork_buffer buf = CORK_BUFFER_INIT(); rii_check(bz_install_dependency_string("autoconf", ctx)); rii_check(bz_install_dependency_string("automake", ctx)); rii_check(bz_build_message(env, "autotools")); rip_check(package_name = bz_env_get_string(env, "name", true)); rip_check(build_dir = bz_env_get_path(env, "build_dir", true)); rip_check(source_dir = bz_env_get_path(env, "source_dir", true)); rip_check(configure = bz_env_get_path(env, "autotools.configure.configure", true)); rie_check(pkgconfig_path = bz_env_get_string(env, "pkgconfig.path", false)); rie_check(verbose = bz_env_get_bool(env, "verbose", true)); /* Create the build path */ rii_check(bz_create_directory(cork_path_get(build_dir), 0750)); clog_info("(%s) Configure using autotools", package_name); /* $ autoreconf -i */ rii_check(bz_file_exists(cork_path_get(configure), &exists)); if (!exists) { exec = cork_exec_new("autoreconf"); cork_exec_add_param(exec, "autoreconf"); cork_exec_add_param(exec, "-i"); cork_exec_set_cwd(exec, cork_path_get(source_dir)); ei_check(bz_subprocess_run_exec(verbose, NULL, exec)); } #define add_dir(buzzy_name, param_name) \ do { \ struct cork_path *value; \ ep_check(value = bz_env_get_path(env, buzzy_name, true)); \ cork_buffer_printf(&buf, "--" param_name "=%s", cork_path_get(value)); \ cork_exec_add_param(exec, buf.buf); \ } while (0) /* $ ./configure ... */ exec = cork_exec_new(cork_path_get(configure)); exec_env = cork_env_clone_current(); cork_exec_set_env(exec, exec_env); cork_exec_add_param(exec, cork_path_get(configure)); add_dir("prefix", "prefix"); add_dir("exec_prefix", "exec-prefix"); add_dir("bin_dir", "bindir"); add_dir("sbin_dir", "sbindir"); add_dir("lib_dir", "libdir"); add_dir("libexec_dir", "libexecdir"); add_dir("share_dir", "datadir"); add_dir("man_dir", "mandir"); /* Add custom configure arguments, if given. */ configure_args = bz_env_get_value(env, "autotools.configure.args"); if (configure_args != NULL) { struct bz_configure_add_arg state = { exec, env }; switch (bz_value_kind(configure_args)) { case BZ_VALUE_SCALAR: ei_check(bz_configure_add_arg(&state, configure_args)); break; case BZ_VALUE_ARRAY: ei_check(bz_array_value_map (configure_args, &state, bz_configure_add_arg)); break; case BZ_VALUE_MAP: bz_bad_config("autotools.configure.args cannot be a map"); goto error; default: cork_unreachable(); } } cork_exec_set_cwd(exec, cork_path_get(build_dir)); if (pkgconfig_path != NULL) { cork_env_add(exec_env, "PKG_CONFIG_PATH", pkgconfig_path); } ei_check(bz_subprocess_run_exec(verbose, NULL, exec)); /* $ make */ clog_info("(%s) Build using autotools", package_name); exec = cork_exec_new("make"); cork_exec_add_param(exec, "make"); cork_exec_set_cwd(exec, cork_path_get(build_dir)); ei_check(bz_subprocess_run_exec(verbose, NULL, exec)); cork_buffer_done(&buf); return 0; error: cork_buffer_done(&buf); return -1; }
int start_plugin(const char *plugin, const char *plugin_opts, const char *remote_host, const char *remote_port, const char *local_host, const char *local_port) { char *new_path = NULL; char *cmd = NULL; if (plugin == NULL) return -1; if (strlen(plugin) == 0) return 0; size_t plugin_len = strlen(plugin); size_t cmd_len = plugin_len + CMD_RESRV_LEN; cmd = ss_malloc(cmd_len); snprintf(cmd, cmd_len, "exec %s", plugin); env = cork_env_clone_current(); const char *path = cork_env_get(env, "PATH"); if (path != NULL) { #ifdef __GLIBC__ char *cwd = get_current_dir_name(); if (cwd) { #else char cwd[PATH_MAX]; if (!getcwd(cwd, PATH_MAX)) { #endif size_t path_len = strlen(path) + strlen(cwd) + 2; new_path = ss_malloc(path_len); snprintf(new_path, path_len, "%s:%s", cwd, path); #ifdef __GLIBC__ free(cwd); #endif } } if (new_path != NULL) cork_env_add(env, "PATH", new_path); cork_env_add(env, "SS_REMOTE_HOST", remote_host); cork_env_add(env, "SS_REMOTE_PORT", remote_port); cork_env_add(env, "SS_LOCAL_HOST", local_host); cork_env_add(env, "SS_LOCAL_PORT", local_port); if (plugin_opts != NULL) cork_env_add(env, "SS_PLUGIN_OPTIONS", plugin_opts); exec = cork_exec_new_with_params("sh", "-c", cmd, NULL); cork_exec_set_env(exec, env); sub = cork_subprocess_new_exec(exec, NULL, NULL, &exit_code); int err = cork_subprocess_start(sub); ss_free(cmd); if (new_path != NULL) ss_free(new_path); return err; } uint16_t get_local_port() { int sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) { return 0; } struct sockaddr_in serv_addr; bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = 0; if (bind(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { return 0; } socklen_t len = sizeof(serv_addr); if (getsockname(sock, (struct sockaddr *)&serv_addr, &len) == -1) { return 0; } if (close (sock) < 0) { return 0; } return ntohs(serv_addr.sin_port); } void stop_plugin() { if (sub != NULL) { cork_subprocess_abort(sub); cork_subprocess_free(sub); } } int is_plugin_running() { if (sub != NULL) { return cork_subprocess_is_finished(sub); } return 0; }
static int bz_pacman__package(void *user_data) { struct bz_env *env = user_data; struct bz_value *ctx = bz_env_as_value(env); struct cork_path *staging_dir; struct cork_path *binary_package_dir; struct cork_path *package_build_dir; struct cork_path *pkgbuild; struct cork_path *package_file; const char *package_name; const char *version; const char *pkgrel; const char *pkgext; const char *architecture; const char *license; bool verbose; struct cork_env *exec_env; struct cork_exec *exec; struct cork_buffer buf = CORK_BUFFER_INIT(); bool staging_exists; rii_check(bz_install_dependency_string("pacman", ctx)); rii_check(bz_package_message(env, "pacman")); rip_check(package_name = bz_env_get_string(env, "name", true)); clog_info("(%s) Package using pacman", package_name); rip_check(staging_dir = bz_env_get_path(env, "staging_dir", true)); rip_check(binary_package_dir = bz_env_get_path(env, "binary_package_dir", true)); rip_check(package_build_dir = bz_env_get_path(env, "package_build_dir", true)); rip_check(pkgbuild = bz_env_get_path(env, "pacman.pkgbuild", true)); rip_check(package_file = bz_env_get_path(env, "pacman.package_file", true)); rip_check(version = bz_env_get_string(env, "pacman.version", true)); rip_check(pkgrel = bz_env_get_string(env, "pacman.pkgrel", true)); rip_check(pkgext = bz_env_get_string(env, "pacman.pkgext", true)); rip_check(architecture = bz_env_get_string(env, "pacman.arch", true)); rip_check(license = bz_env_get_string(env, "license", true)); rie_check(verbose = bz_env_get_bool(env, "verbose", true)); rii_check(bz_file_exists(cork_path_get(staging_dir), &staging_exists)); if (CORK_UNLIKELY(!staging_exists)) { cork_error_set_printf (ENOENT, "Staging directory %s does not exist", cork_path_get(staging_dir)); return -1; } /* NOTE: pacman runs ldconfig automatically, so unlike the other packagers, * we do NOT need to add an ldconfig call to the post-install and * post-remove scripts. */ /* Create the temporary directory and the packaging destination */ rii_check(bz_create_directory(cork_path_get(package_build_dir), 0750)); rii_check(bz_create_directory(cork_path_get(binary_package_dir), 0750)); /* Create a PKGBUILD file for this package */ cork_buffer_append_printf(&buf, "pkgname='%s'\n", package_name); cork_buffer_append_printf(&buf, "pkgver='%s'\n", version); cork_buffer_append_printf(&buf, "pkgrel='%s'\n", pkgrel); cork_buffer_append_printf(&buf, "arch=('%s')\n", architecture); cork_buffer_append_printf(&buf, "license=('%s')\n", license); rii_check(bz_pacman_fill_deps(env, &buf, "depends", "dependencies")); cork_buffer_append_printf(&buf, "package () {\n" " rm -rf \"${pkgdir}\"\n" " cp -a '%s' \"${pkgdir}\"\n" "}\n", cork_path_get(staging_dir) ); /* Add pre- and post-install scripts, if necessary. */ rii_check(bz_pacman_add_install_scripts(env, &buf)); ei_check(bz_create_file(cork_path_get(pkgbuild), &buf, 0640)); cork_buffer_done(&buf); exec_env = cork_env_clone_current(); cork_env_add(exec_env, "PKGDEST", cork_path_get(binary_package_dir)); cork_env_add(exec_env, "PKGEXT", pkgext); exec = cork_exec_new_with_params("makepkg", "-sf", NULL); cork_exec_set_cwd(exec, cork_path_get(package_build_dir)); cork_exec_set_env(exec, exec_env); clog_info("(%s) Create %s using pacman", package_name, cork_path_get(package_file)); return bz_subprocess_run_exec(verbose, NULL, exec); error: cork_buffer_done(&buf); return -1; }