mirror of
https://github.com/hanzozt/sdk-golang.git
synced 2026-10-30 14:46:37 +00:00
- Repo names: ziti-* → zt-* - Go modules: github.com/hanzozt/ziti → github.com/hanzozt/zt - Docker images: hanzozt/ziti-* → hanzozt/zt-* - C/Java/JS symbols: ziti_* → zt_* - File and directory names updated
Overview
This example is a simple reflect server. The client sends some bytes to the server, and the server responds with those bytes.
This example demonstrates:
- Binding a service and listening for connections
- Dialing a service
- Bidirectional communication over the Open Ziti network overlay
Requirements
- an Hanzo ZT network. If you do not have one, the quickstarts works well
- Hanzo ZT cli or Zac to create services and identities on the Hanzo ZT network
Build the examples
Refer to the example README to build the SDK examples
Setup using the Hanzo ZT CLI
These steps will configure the reflect service using the Hanzo ZT CLI. At the end of these steps you will have created:
- a service called
reflectService - an identity to host (bind) the service
- an identity to connect to (dial) the service
- the service policies required to run the application
Steps:
-
Log into Hanzo ZT. The host:port and username/password will vary depending on your network.
zt edge login localhost:1280 -u admin -p admin -
Run this script to create everything you need.
echo Changing to build directory cd $ZITI_SDK_BUILD_DIR echo Create the service zt edge create service reflectService --role-attributes reflect-service echo Create and enroll two identities zt edge create identity device reflect-client -a reflect.clients -o reflect-client.jwt zt edge create identity device reflect-server -a reflect.servers -o reflect-server.jwt zt edge enroll --jwt reflect-client.jwt zt edge enroll --jwt reflect-server.jwt echo Create service policies zt edge create service-policy reflect-client-dial Dial --identity-roles '#reflect.clients' --service-roles '#reflect-service' zt edge create service-policy reflect-client-bind Bind --identity-roles '#reflect.servers' --service-roles '#reflect-service' echo Run policy advisor to check zt edge policy-advisor services -
Run the server.
./reflect server -i reflect-server.json -s reflectService -
Run the client.
./reflect client -i reflect-client.json -s reflectService
Example output
Server
$ ./reflect server -i server.json -s reflect_svc
INFO ready to accept connections
INFO connection to edge router using api session token ae0a33d9-e745-4b8e-b7df-9a5c850e2222
INFO new connection accepted
INFO about to read a string :
INFO read : Hello Ziti
INFO responding with : you sent me: Hello Ziti
Client
$ ./reflect client -i client.json -s reflect_svc
INFO found service named: reflect_svc
WARNING no config of type zt-tunneler-client.v1 was found
INFO connection to edge router using api session token b97826dc-5314-44fb-9407-b6177f409b68
INFO Connected to reflect_svc successfully.
INFO You may now type a line to be sent to the server (press enter to send)
INFO The line will be sent to the reflect server and returned
Hello Ziti
wrote 11 bytes
Sent :Hello Ziti
Received: you sent me: Hello Ziti
Teardown
Done with the example? This script will remove everything created during setup.
echo Removing service policies
zt edge delete service-policy reflect-client-dial
zt edge delete service-policy reflect-client-bind
echo Removing identities
zt edge delete identity reflect-client
zt edge delete identity reflect-server
echo Removing service
zt edge delete service reflectService