Skip to content

Commit

Permalink
Merge pull request #778 from openziti/cli11-instead-of-subcommand
Browse files Browse the repository at this point in the history
CLI11 instead of subcommand
  • Loading branch information
ekoby authored Nov 27, 2024
2 parents 6911408 + b07e378 commit 3aca1d3
Show file tree
Hide file tree
Showing 6 changed files with 254 additions and 266 deletions.
2 changes: 1 addition & 1 deletion deps/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ if (NOT TARGET tlsuv)
else ()
FetchContent_Declare(tlsuv
GIT_REPOSITORY https://github.com/openziti/tlsuv.git
GIT_TAG v0.32.8
GIT_TAG v0.32.9
)
FetchContent_MakeAvailable(tlsuv)
endif (tlsuv_DIR)
Expand Down
10 changes: 9 additions & 1 deletion library/ziti.c
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,7 @@ int ziti_get_transfer_rates(ziti_context ztx, double *up, double *down) {
static void free_ztx(uv_handle_t *h) {
ziti_context ztx = h->data;

model_map_clear(&ztx->ext_signers, (_free_f)free_ziti_jwt_signer_ptr);
model_map_clear(&ztx->ctrl_details, (_free_f) free_ziti_controller_detail_ptr);
ziti_auth_query_free(ztx->auth_queries);
ziti_posture_checks_free(ztx->posture_checks);
Expand All @@ -649,10 +650,14 @@ static void free_ztx(uv_handle_t *h) {
FREE(ztx->session_token);

ziti_ctrl_close(ztx_get_controller(ztx));
ztx->tlsCtx->free_ctx(ztx->tlsCtx);
if (ztx->tlsCtx) ztx->tlsCtx->free_ctx(ztx->tlsCtx);
if (ztx->id_creds.cert) {
ztx->id_creds.cert->free(ztx->id_creds.cert);
}
if (ztx->id_creds.key) {
ztx->id_creds.key->free(ztx->id_creds.key);
}

free_ziti_config(&ztx->config);

ziti_event_t ev = {0};
Expand Down Expand Up @@ -682,6 +687,9 @@ static void shutdown_and_free(ziti_context ztx) {
CLOSE_AND_NULL(ztx->prepper);
CLOSE_AND_NULL(ztx->refresh_timer);

ztx->tlsCtx->free_ctx(ztx->tlsCtx);
ztx->tlsCtx = NULL;

uv_close((uv_handle_t *) &ztx->w_async, free_ztx);
}

Expand Down
25 changes: 7 additions & 18 deletions programs/ziti-prox-c/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@

add_executable(ziti-prox-c proxy.c)
add_executable(ziti-prox-c proxy.c
main.cpp
proxy.h)

FetchContent_Declare(subcommand
GIT_REPOSITORY https://github.com/openziti/subcommands.c.git
GIT_TAG main
)
FetchContent_GetProperties(subcommand)
if (NOT subcommand_POPULATED)
FetchContent_Populate(subcommand)
endif ()
add_library(subcommand INTERFACE)
target_include_directories(subcommand INTERFACE ${subcommand_SOURCE_DIR})


if(WIN32)
target_include_directories(ziti-prox-c PRIVATE win32/include)
target_sources(ziti-prox-c PRIVATE win32/src/getopt.c)
endif()

target_link_libraries(ziti-prox-c PUBLIC ziti subcommand)
target_link_libraries(ziti-prox-c PUBLIC ziti CLI11::CLI11)
if (MSVC)
target_link_libraries(ziti-prox-c PUBLIC shlwapi)
endif (MSVC)
target_include_directories(ziti-prox-c PRIVATE ${ziti-sdk_SOURCE_DIR}/inc_internal)

set(ZITI_ARCHIVE_NAME "ziti-prox-c-${PROJECT_VERSION}-${CPACK_SYSTEM_NAME}-${CMAKE_SYSTEM_PROCESSOR}.${archive_sfx}")
Expand Down
90 changes: 90 additions & 0 deletions programs/ziti-prox-c/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (c) 2024. NetFoundry Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
//
// You may obtain a copy of the License at
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


#include <utils.h>
#include <CLI/CLI.hpp>

#if _WIN32
#include <windows.h>
#include <shlwapi.h>
#define basename(p) PathFindFileName(p)
#else
#include <libgen.h>
#endif


#include "proxy.h"

class Run: public CLI::App {
public:

Run(): App("run proxy", "run"),
debug(2) {
add_option("--debug,-d", debug, "log level")->envname("ZITI_LOG");
add_option("--identity,-i", identity, "identity config")->required();
add_option("listener", intercepts, "<name:port>");
add_option("--bind,-b", bindings, "bind service <name:host:port>");
add_option("--bind-udp,-B", udp_bindings, "bind udp service <name:host:port>");
add_option("--proxy,-p", proxy, "proxy url");

final_callback([this] {
this->execute();
});
}


private:
int debug;
std::string identity;
std::vector<std::string> intercepts;
std::vector<std::string> bindings;
std::vector<std::string> udp_bindings;
std::string proxy;

void execute() const {
run_opts opts{};
opts.identity = this->identity.c_str();
opts.debug = this->debug;
for (auto &intercept: this->intercepts) {
model_list_append(&opts.intercepts, intercept.c_str());
}
for (auto &binding: this->bindings) {
model_list_append(&opts.bindings, binding.c_str());
}
for (auto &udp: this->udp_bindings) {
model_list_append(&opts.udp_bindings, udp.c_str());
}
if (!proxy.empty()) opts.proxy = this->proxy.c_str();
int rc = run_proxy(&opts);
::exit(rc);
}
};

int main(int argc, char *argv[]) {
const char *name = basename(argv[0]);
CLI::App app{name};

bool verbose = false;
auto ver = app.add_subcommand("version", "print version information");
ver->add_flag("--verbose,-v", verbose, "verbose output");
ver->final_callback([&verbose] {
std::cout << ziti_get_build_version(verbose) << std::endl;
});

app.add_subcommand(std::make_shared<Run>());
app.require_subcommand(1);
CLI11_PARSE(app, argc, argv);
return 0;
}
Loading

0 comments on commit 3aca1d3

Please sign in to comment.