From 230c55ca28e2f0740bca40def575f9741d889b58 Mon Sep 17 00:00:00 2001 From: eugene Date: Tue, 21 Apr 2020 11:52:33 -0400 Subject: [PATCH] hook in ziti_callbacks --- wintun-testing/build.sh | 5 +- wintun-testing/cziti/ctun.go | 81 ++++++++++-------------------- wintun-testing/cziti/sdk.c | 26 +++------- wintun-testing/cziti/sdk.go | 8 +-- wintun-testing/cziti/sdk.h | 3 -- wintun-testing/ziti-wintun/main.go | 2 +- 6 files changed, 41 insertions(+), 84 deletions(-) diff --git a/wintun-testing/build.sh b/wintun-testing/build.sh index d157a574..7271604c 100644 --- a/wintun-testing/build.sh +++ b/wintun-testing/build.sh @@ -21,7 +21,10 @@ if [[ ! -f ./build/CMakeCache.txt ]]; then fi "${CMAKE}" --build ./build --target install +cp ./install/lib/ziti.dll . +cp ./install/lib/libuv.dll . + export CGO_CFLAGS="-DNOGDI -I $(cygpath -a -m ./install/include)" export CGO_LDFLAGS="-L $(cygpath -a -m ./install/lib)" -go build ./ziti-wintun +go build -a ./ziti-wintun diff --git a/wintun-testing/cziti/ctun.go b/wintun-testing/cziti/ctun.go index 1bfd40d7..cb742470 100644 --- a/wintun-testing/cziti/ctun.go +++ b/wintun-testing/cziti/ctun.go @@ -2,10 +2,12 @@ package cziti // /* -#cgo LDFLAGS: -l ziti_tunneler -l lwipcore -l lwipwin32arch -l ziti_tunneler +#cgo LDFLAGS: -l ziti_tunneler -l lwipcore -l lwipwin32arch -l ziti_tunneler -l ziti_tunneler_cbs -#include "nf/netif_driver.h" +#include #include +#include + #include extern int netifClose(netif_handle dev); @@ -13,20 +15,15 @@ extern ssize_t netifRead(netif_handle dev, void *buf, size_t buf_len); extern ssize_t netifWrite(netif_handle dev, void *buf, size_t len); extern int netifSetup(netif_handle dev, uv_loop_t *loop, packet_cb packetCb, void* ctx); -extern tunneler_sdk_options* new_tun_opts(); typedef struct netif_handle_s { const char* id; packet_cb on_packet_cb; void *packet_cb_ctx; } netif_handle_t; -extern void* zitiDial(char *service_name, void *ziti_dial_ctx, tunneler_io_context tnlr_io_ctx); -extern void zitiClose(void *ziti_dial_ctx); -extern int zitiWrite(void *ziti_io_ctx, void *data, int len); extern void readAsync(struct uv_async_s *a); extern void call_on_packet(void *packet, ssize_t len, packet_cb cb, void *ctx); -extern uv_async_t* new_async(); extern void dnsHandler(tunneler_io_context tio, void *ctx, addr_t src, uint16_t sport, void *data, ssize_t len); */ @@ -37,14 +34,13 @@ import ( "github.com/miekg/dns" "golang.zx2c4.com/wireguard/tun" "net" - "strings" "unsafe" ) const dnsIP = "169.254.1.253" type Tunnel interface { - AddIntercept(service string, hostname string, port int) + AddIntercept(service string, hostname string, port int, ctx unsafe.Pointer) } type tunnel struct{ @@ -64,7 +60,7 @@ type tunnel struct{ var devMap = make(map[string]*tunnel) func HookupTun(dev tun.Device) (Tunnel, error) { - fmt.Println("in HookupTun") + fmt.Println("in HookupTun ") defer fmt.Println("exiting HookupTun") name, err := dev.Name() if err != nil { @@ -78,16 +74,16 @@ func HookupTun(dev tun.Device) (Tunnel, error) { t := &tunnel{ dev: dev, driver: drv, - writeQ: make(chan []byte), + writeQ: make(chan []byte, 64), readQ: make(chan []byte, 64), } devMap[name] = t - opts := C.new_tun_opts() + opts := (*C.tunneler_sdk_options)(C.calloc(1, C.sizeof_tunneler_sdk_options)) opts.netif_driver = drv - opts.ziti_dial = C.ziti_dial_cb(C.zitiDial) - opts.ziti_close = C.ziti_close_cb(C.zitiClose) - opts.ziti_write = C.ziti_write_cb(C.zitiWrite) + opts.ziti_dial = C.ziti_dial_cb(C.ziti_sdk_c_dial) + opts.ziti_close = C.ziti_close_cb(C.ziti_sdk_c_close) + opts.ziti_write = C.ziti_write_cb(C.ziti_sdk_c_write) t.tunCtx = C.NF_tunneler_init(opts, _impl.libuvCtx.l) @@ -113,10 +109,9 @@ func makeDriver(name string) C.netif_driver { func netifWrite(h C.netif_handle, buf unsafe.Pointer, length C.size_t) C.ssize_t { t, found := devMap[C.GoString(h.id)] if !found { - fmt.Errorf("should not be here\n") + panic("should not be here\n") return -1 } - fmt.Println("netifWrite len=", length); b := C.GoBytes(buf, C.int(length)) @@ -139,7 +134,7 @@ func netifSetup(h C.netif_handle, l *C.uv_loop_t, packetCb C.packet_cb, ctx unsa return -1 } - t.read = C.new_async() + t.read = (*C.uv_async_t)(C.calloc(1, C.sizeof_uv_async_t)) C.uv_async_init(l, t.read, C.uv_async_cb(C.readAsync)) t.read.data = unsafe.Pointer(h) fmt.Printf("in netifSetup netif[%s] handle[%p]\n", C.GoString(h.id), h) @@ -167,6 +162,10 @@ func (t *tunnel)runReadLoop() { if err != nil { panic(err) } + + if len(t.readQ) == cap(t.readQ) { + fmt.Println("read loop is about to block") + } t.readQ <- buf[:nr] C.uv_async_send((*C.uv_async_t)(unsafe.Pointer(t.read))) } @@ -174,8 +173,6 @@ func (t *tunnel)runReadLoop() { //export readAsync func readAsync(a *C.uv_async_t) { - i := 0 - dev := (*C.netif_handle_t)(a.data) id := C.GoString(dev.id) @@ -185,12 +182,12 @@ func readAsync(a *C.uv_async_t) { panic(errors.New("where is my tunnel?")) } - for len(t.readQ) > 0 { + np := len(t.readQ) + for i := np; i > 0; i-- { b := <- t.readQ buf := C.CBytes(b) C.call_on_packet(buf, C.ssize_t(len(b)), t.onPacket, t.onPacketCtx) C.free(buf) - i++ } } @@ -218,8 +215,11 @@ func (t *tunnel) runWriteLoop() { } -func (t *tunnel) AddIntercept(service string, host string, port int) { - res := C.NF_tunneler_intercept_v1(t.tunCtx, unsafe.Pointer(nil), C.CString(service), C.CString(host), C.int(port)) +func (t *tunnel) AddIntercept(service string, host string, port int, ctx unsafe.Pointer) { + zitiCtx := (*C.ziti_context)(C.malloc(C.sizeof_ziti_context)) + zitiCtx.nf_ctx = ctx + res := C.NF_tunneler_intercept_v1(t.tunCtx, unsafe.Pointer(zitiCtx), + C.CString(service), C.CString(host), C.int(port)) fmt.Println("intercept added", res) } @@ -260,6 +260,7 @@ func dnsHandler(tio C.tunneler_io_context, ctx unsafe.Pointer, src C.addr_t, spo if err == nil { replyC := C.CBytes(reply) rc := C.NF_udp_send(tio, src, sport, replyC, C.ssize_t(len(reply))) + C.free(replyC) if rc != 0 { fmt.Println("NF_udp_reply rc=", rc) } @@ -268,34 +269,4 @@ func dnsHandler(tio C.tunneler_io_context, ctx unsafe.Pointer, src C.addr_t, spo } } -/*******************************************************************/ -//export zitiDial -func zitiDial(service *C.char, ziti_dial_ctx unsafe.Pointer, tio C.tunneler_io_context) unsafe.Pointer { - fmt.Println("received dial request for ", C.GoString(service)) - defer fmt.Println("dial finished") - msg := []byte("welcome to Canadian translator\n") - - msgC := C.CBytes(msg) - C.NF_tunneler_write(tio, msgC, C.int(len(msg))) - // C.free(msgC) - - return unsafe.Pointer(tio) -} - -//export zitiClose -func zitiClose(ziti_dial_ctx unsafe.Pointer) { - fmt.Println("in zitiClose") -} - -//export zitiWrite -func zitiWrite(zio unsafe.Pointer, data unsafe.Pointer, length C.int) C.int { - msg := strings.TrimSpace(string(C.GoBytes(data, length))) - - reply := []byte(fmt.Sprint(msg, ", eh?\n")) - - replyC := C.CBytes(reply) - C.NF_tunneler_write(C.tunneler_io_context(zio), replyC, C.int(len(reply))) - //C.free(replyC) - - return C.ZITI_CONNECTED -} \ No newline at end of file +/*******************************************************************/ \ No newline at end of file diff --git a/wintun-testing/cziti/sdk.c b/wintun-testing/cziti/sdk.c index 19666874..9af9d420 100644 --- a/wintun-testing/cziti/sdk.c +++ b/wintun-testing/cziti/sdk.c @@ -3,57 +3,43 @@ #include #include -libuv_ctx* new_libuv_ctx() { - return calloc(1, sizeof(libuv_ctx)); -} void libuv_stopper(uv_async_t *a) { uv_stop(a->loop); } + void libuv_init(libuv_ctx *lctx) { lctx->l = uv_default_loop(); uv_async_init(lctx->l, &lctx->stopper, libuv_stopper); } + void libuv_runner(void *arg) { init_debug(); - printf("starting event loop\n"); + ZITI_LOG(INFO, "starting event loop"); fflush(stdout); uv_loop_t *l = arg; uv_run(l, UV_RUN_DEFAULT); - printf("event finished finished\n"); - + ZITI_LOG(INFO, "event finished finished\n"); } void libuv_run(libuv_ctx *lctx) { uv_thread_create(&lctx->t, libuv_runner, lctx->l); } + void libuv_stop(libuv_ctx *lctx) { uv_async_send(&lctx->stopper); int err = uv_thread_join(&lctx->t); if (err) { - fprintf(stderr, "%d: %s\n", err, uv_strerror(err)); + ZITI_LOG(ERROR, "failed to join uv_loop thread: %d[%s]", err, uv_strerror(err)); } } -extern void initCB(nf_context nf, int status, void *ctx); -extern void _init_cb(nf_context nf, int status, void *ctx) { -initCB(nf, status, ctx); -} - static const char* _all[] = { "all", NULL }; const char** all_configs = _all; -extern tunneler_sdk_options* new_tun_opts() { - return calloc(1, sizeof(tunneler_sdk_options)); -} - extern void call_on_packet(const char *packet, ssize_t len, packet_cb cb, void *ctx) { cb(packet, len, ctx); } - -extern uv_async_t* new_async() { -return calloc(1, sizeof(uv_async_t)); -} \ No newline at end of file diff --git a/wintun-testing/cziti/sdk.go b/wintun-testing/cziti/sdk.go index 37266f09..2e047dc6 100644 --- a/wintun-testing/cziti/sdk.go +++ b/wintun-testing/cziti/sdk.go @@ -21,7 +21,7 @@ type sdk struct { var _impl sdk func init() { - _impl.libuvCtx = C.new_libuv_ctx() + _impl.libuvCtx = (*C.libuv_ctx)(C.calloc(1, C.sizeof_libuv_ctx)) C.libuv_init(_impl.libuvCtx) } @@ -69,13 +69,10 @@ var tunCfgName = C.CString("ziti-tunneler-client.v1") func serviceCB(nf C.nf_context, service *C.ziti_service, status C.int, data unsafe.Pointer) { ctx := (*CZitiCtx)(data) - fmt.Printf("%+v\n", ctx) if ctx.Services == nil { m := make(map[string]Service) ctx.Services = &m } - fmt.Printf("%+v\n", ctx) - name := C.GoString(service.name) if status == C.ZITI_SERVICE_UNAVAILABLE { @@ -106,6 +103,9 @@ func serviceCB(nf C.nf_context, service *C.ziti_service, status C.int, data unsa fmt.Println(err) } else { fmt.Printf("service[%s] is mapped to <%s:%d>\n", name, ip.String(), port) + for _, t := range devMap { + t.AddIntercept(name, ip.String(), port, unsafe.Pointer(ctx.nf)) + } } } } diff --git a/wintun-testing/cziti/sdk.h b/wintun-testing/cziti/sdk.h index 8e9f4ca2..2ad8dc26 100644 --- a/wintun-testing/cziti/sdk.h +++ b/wintun-testing/cziti/sdk.h @@ -17,13 +17,10 @@ typedef struct libuv_ctx_s { uv_async_t stopper; } libuv_ctx; -libuv_ctx* new_libuv_ctx(); void libuv_stopper(uv_async_t *a); void libuv_init(libuv_ctx *lctx); void libuv_runner(void *arg); void libuv_run(libuv_ctx *lctx); void libuv_stop(libuv_ctx *lctx); -extern void _init_cb(nf_context nf, int status, void *ctx); - extern const char** all_configs; \ No newline at end of file diff --git a/wintun-testing/ziti-wintun/main.go b/wintun-testing/ziti-wintun/main.go index 7077b2b0..5c4b9a7e 100644 --- a/wintun-testing/ziti-wintun/main.go +++ b/wintun-testing/ziti-wintun/main.go @@ -29,7 +29,7 @@ func main() { interfaceName := os.Args[1] fmt.Println("creating TUN device") - tunDevice, err := tun.CreateTUN(interfaceName, 0) + tunDevice, err := tun.CreateTUN(interfaceName, 64 * 1024) if err == nil { realInterfaceName, err2 := tunDevice.Name() if err2 == nil {