diff --git a/.gitignore b/.gitignore index 831af645c..a15627933 100755 --- a/.gitignore +++ b/.gitignore @@ -1,19 +1,56 @@ -# Main binaries created in *nix builds -/zerotier-one -/zerotier-idtool -/zerotier-cli -/zerotier-selftest -/zerotier -/nltest - -# OS-created garbage files from various platforms +build/ +/version.h .DS_Store +.Trashes +*.swp +._* +*~ +*~.nib .Apple* Thumbs.db @eaDir -._* +DerivedData/ +*.pbxuser +*.mode1v3 +*.mode2v3 +*.perspectivev3 +!default.pbxuser +!default.mode1v3 +!default.mode2v3 +!default.perspectivev3 +*.xccheckout +xcuserdata/ +.vscode +__pycache__ +attic/world/*.c25519 +attic/world/mkworld +*.log +*.opensdf +*.user +*.cache +*.obj +*.tlog +*.pid +*.pkg +*.o +*.o-* +*.core +*.deb +*.rpm +*.autosave +*.tmp +.depend +node_modules +debian/files +debian/zerotier-one +debian/zerotier-one*.debhelper +debian/*.log +debian/zerotier-one.substvars +root/identity.* +root/config.* +/ext/installfiles/windows/chocolatey/zerotier-one/*.nupkg +/go/zerotier -# Windows build droppings /windows/ZeroTierOne.sdf /windows/ZeroTierOne.v11.suo /windows/x64 @@ -32,7 +69,7 @@ Thumbs.db /ext/installfiles/windows/ZeroTier One-SetupFiles /ext/installfiles/windows/Prerequisites /ext/installfiles/windows/*-cache -/ZeroTier One.msi +/*.msi /windows/.vs *.vcxproj.backup /windows/TapDriver6/Win7Debug @@ -43,50 +80,6 @@ enc_temp_folder /windows/copyutil/bin /windows/copyutil/obj -# *nix/Mac build droppings -/build-* -/ZeroTierOneInstaller-* -/examples/docker/zerotier-one -/examples/docker/test-*.env -/world/mkworld -/world/*.c25519 -zt1-src.tar.gz -/MacEthernetTapAgent - -# Miscellaneous temporaries, build files, etc. -*.log -*.opensdf -*.user -*.cache -*.obj -*.tlog -*.pid -*.pkg -*.o -/*.a -*.dylib -*.so -*.so.* -*.o-* -*.core -*.deb -*.rpm -*.autosave -*.tmp -.depend -node_modules -zt1_update_* -debian/files -debian/zerotier-one -debian/zerotier-one*.debhelper -debian/*.log -debian/zerotier-one.substvars -root-watcher/config.json -root/identity.* -root/zerotier-root -root/config.* - -# Java/Android/JNI build droppings java/obj/ java/libs/ java/bin/ @@ -98,29 +91,3 @@ java/build_win32/ windows/WinUI/obj/ windows/WinUI/bin/ windows/ZeroTierOne/Debug/ -/ext/installfiles/windows/chocolatey/zerotier-one/*.nupkg - -# Miscellaneous mac/Xcode droppings -.DS_Store -.Trashes -*.swp -*~.nib -DerivedData/ -build/ -*.pbxuser -*.mode1v3 -*.mode2v3 -*.perspectivev3 -!default.pbxuser -!default.mode1v3 -!default.mode2v3 -!default.perspectivev3 -*.xccheckout -xcuserdata/ -ext/librethinkdbxx/build -.vscode -__pycache__ -*~ -attic/world/*.c25519 -attic/world/mkworld -/version.h diff --git a/go/Makefile b/go/Makefile new file mode 100644 index 000000000..5afd85a9f --- /dev/null +++ b/go/Makefile @@ -0,0 +1,2 @@ +all: + go build -o zerotier cmd/zerotier/zerotier.go diff --git a/go/cmd/zerotier/cli/service.go b/go/cmd/zerotier/cli/service.go index ca6a8e373..898232f71 100644 --- a/go/cmd/zerotier/cli/service.go +++ b/go/cmd/zerotier/cli/service.go @@ -37,9 +37,7 @@ func Service(basePath, authToken string, args []string) { osSignalChannel := make(chan os.Signal, 2) signal.Notify(osSignalChannel, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGINT, syscall.SIGBUS) signal.Ignore(syscall.SIGUSR1, syscall.SIGUSR2) - go func() { - <-osSignalChannel - node.Close() - os.Exit(0) - }() + <-osSignalChannel + node.Close() + os.Exit(0) } diff --git a/go/cmd/zerotier/zerotier.go b/go/cmd/zerotier/zerotier.go index 12bd185ac..30c7d4caf 100644 --- a/go/cmd/zerotier/zerotier.go +++ b/go/cmd/zerotier/zerotier.go @@ -54,6 +54,13 @@ func readAuthToken(basePath string) string { return "" } +func authTokenRequired(authToken string) { + if len(authToken) == 0 { + fmt.Println("FATAL: unable to read API authorization token from service path or user home ('sudo' may be needed)") + os.Exit(1) + } +} + func main() { globalOpts := flag.NewFlagSet("global", flag.ContinueOnError) hflag := globalOpts.Bool("h", false, "") // support -h to be canonical with other Unix utilities @@ -81,16 +88,13 @@ func main() { if len(*pflag) > 0 { basePath = *pflag } + var authToken string if len(*tflag) > 0 { authToken = *tflag } else { authToken = readAuthToken(basePath) } - if len(authToken) == 0 { - fmt.Println("FATAL: unable to read API authorization token from service path or user home ('sudo' may be needed)") - os.Exit(1) - } authToken = strings.TrimSpace(authToken) switch args[0] { @@ -103,24 +107,34 @@ func main() { case "service": cli.Service(basePath, authToken, cmdArgs) case "status": + authTokenRequired(authToken) cli.Status(basePath, authToken, cmdArgs, *jflag) case "peers": + authTokenRequired(authToken) cli.Peers(basePath, authToken, cmdArgs) case "roots": + authTokenRequired(authToken) cli.Roots(basePath, authToken, cmdArgs) case "addroot": + authTokenRequired(authToken) cli.AddRoot(basePath, authToken, cmdArgs) case "removeroot": + authTokenRequired(authToken) cli.RemoveRoot(basePath, authToken, cmdArgs) case "networks": + authTokenRequired(authToken) cli.Networks(basePath, authToken, cmdArgs) case "join": + authTokenRequired(authToken) cli.Join(basePath, authToken, cmdArgs) case "leave": + authTokenRequired(authToken) cli.Leave(basePath, authToken, cmdArgs) case "show": + authTokenRequired(authToken) cli.Show(basePath, authToken, cmdArgs) case "set": + authTokenRequired(authToken) cli.Set(basePath, authToken, cmdArgs) } diff --git a/go/native/GoGlue.cpp b/go/native/GoGlue.cpp index 19db5129e..016d24afc 100644 --- a/go/native/GoGlue.cpp +++ b/go/native/GoGlue.cpp @@ -21,6 +21,7 @@ #include "../../node/Address.hpp" #include "../../osdep/OSUtils.hpp" #include "../../osdep/EthernetTap.hpp" +#include "../../osdep/ManagedRoute.hpp" #include #include @@ -102,7 +103,7 @@ struct ZT_GoNode_Impl }; static const std::string defaultHomePath(OSUtils::platformDefaultHomePath()); -extern "C" const char *ZT_PLATFORM_DEFAULT_HOMEPATH = defaultHomePath.c_str(); +const char *ZT_PLATFORM_DEFAULT_HOMEPATH = defaultHomePath.c_str(); /****************************************************************************/ diff --git a/go/pkg/zerotier/localconfig.go b/go/pkg/zerotier/localconfig.go index 5a6c90334..a4fc24f62 100644 --- a/go/pkg/zerotier/localconfig.go +++ b/go/pkg/zerotier/localconfig.go @@ -108,7 +108,7 @@ func (lc *LocalConfig) Read(p string, saveDefaultsIfNotExist bool) error { data, err := ioutil.ReadFile(p) if err != nil { - if err != os.ErrNotExist { + if !os.IsNotExist(err) { return err } if saveDefaultsIfNotExist { diff --git a/go/pkg/zerotier/network.go b/go/pkg/zerotier/network.go index d0ebc2449..c6c60e735 100644 --- a/go/pkg/zerotier/network.go +++ b/go/pkg/zerotier/network.go @@ -328,7 +328,7 @@ func (n *Network) updateConfig(nc *NetworkConfig, ls *NetworkLocalSettings) { k := r.key() wantManagedRoutes[k] = true if _, have := haveManagedRoutes[k]; !have { - n.node.log.Printf("%.16x adding managed route %s", n.ID, r.Target.String()) + n.node.log.Printf("%.16x adding managed route %s", n.ID, r.String()) n.tap.AddRoute(&r) } } @@ -336,7 +336,7 @@ func (n *Network) updateConfig(nc *NetworkConfig, ls *NetworkLocalSettings) { } for k, r := range haveManagedRoutes { if _, want := wantManagedRoutes[k]; !want { - n.node.log.Printf("%.16x removing managed route %s", n.ID, r.Target.String()) + n.node.log.Printf("%.16x removing managed route %s", n.ID, r.String()) n.tap.RemoveRoute(r) } } diff --git a/go/pkg/zerotier/node.go b/go/pkg/zerotier/node.go index a6cdbea9f..802f503a2 100644 --- a/go/pkg/zerotier/node.go +++ b/go/pkg/zerotier/node.go @@ -17,8 +17,8 @@ package zerotier // and generally contains all the other CGO stuff. //#cgo CFLAGS: -O3 -//#cgo LDFLAGS: ${SRCDIR}/../../../build/node/libzt_core.a ${SRCDIR}/../../../build/osdep/libzt_osdep.a ${SRCDIR}/../../../build/go/native/libzt_go_native.a -lc++ -lpthread -//#define ZT_CGO 1 +//#cgo darwin LDFLAGS: ${SRCDIR}/../../../build/go/native/libzt_go_native.a ${SRCDIR}/../../../build/node/libzt_core.a ${SRCDIR}/../../../build/osdep/libzt_osdep.a -lc++ -lpthread +//#cgo linux android LDFLAGS: ${SRCDIR}/../../../build/go/native/libzt_go_native.a ${SRCDIR}/../../../build/node/libzt_core.a ${SRCDIR}/../../../build/osdep/libzt_osdep.a -lstdc++ -lpthread -lm //#include "../../native/GoGlue.h" import "C" @@ -68,9 +68,6 @@ const ( // CoreVersionBuild is the build version of the ZeroTier core CoreVersionBuild int = C.ZEROTIER_ONE_VERSION_BUILD - // PlatformDefaultHomePath is the default location of ZeroTier's working path on this system - PlatformDefaultHomePath string = C.GoString(C.ZT_PLATFORM_DEFAULT_HOMEPATH) - // AFInet is the address family for IPv4 AFInet = C.AF_INET @@ -81,7 +78,10 @@ const ( ) var ( - nodesByUserPtr map[uintptr]*Node + // PlatformDefaultHomePath is the default location of ZeroTier's working path on this system + PlatformDefaultHomePath string = C.GoString(C.ZT_PLATFORM_DEFAULT_HOMEPATH) + + nodesByUserPtr = make(map[uintptr]*Node) nodesByUserPtrLock sync.RWMutex ) @@ -862,7 +862,7 @@ func goVirtualNetworkConfigFunc(gn, tapP unsafe.Pointer, nwid C.uint64_t, op C.i return } node.networksLock.RLock() - network := node.networks[uint64(nwid)] + network := node.networks[NetworkID(nwid)] node.networksLock.RUnlock() if network != nil { ncc := (*C.ZT_VirtualNetworkConfig)(conf) @@ -1124,7 +1124,7 @@ func handleTapMulticastGroupChange(gn unsafe.Pointer, nwid, mac C.uint64_t, adi return } node.networksLock.RLock() - network := node.networks[uint64(nwid)] + network := node.networks[NetworkID(nwid)] node.networksLock.RUnlock() if network != nil { tap, _ := network.tap.(*nativeTap) diff --git a/go/pkg/zerotier/route.go b/go/pkg/zerotier/route.go index 1b5afa65b..72feb8c0b 100644 --- a/go/pkg/zerotier/route.go +++ b/go/pkg/zerotier/route.go @@ -33,6 +33,14 @@ type Route struct { Metric uint16 } +// String returns a string representation of this route +func (r *Route) String() string { + if len(r.Via) == 0 { + return r.Target.String() + "->LAN" + } + return r.Target.String() + "->" + r.Via.String() +} + // key generates a key suitable for a map[] from this route func (r *Route) key() (k [6]uint64) { copy(((*[16]byte)(unsafe.Pointer(&k[0])))[:], r.Target.IP)