예제 #1
0
        /// <summary>
        /// Returns the current networks you're connected to.
        /// </summary>
        /// <returns>List of ZeroTierNetwork objects</returns>
        public List<ZeroTierNetwork> GetNetworks()
        {
            var request = WebRequest.Create(url + "/network" + "?auth=" + authtoken) as HttpWebRequest;
            if (request == null)
            {
                throw new LibZeroTierException("ZeroTier Request Response Empty");
            }

            request.Method = "GET";
            request.ContentType = "application/json";
            request.Timeout = 10000;

            try
            {
                var httpResponse = (HttpWebResponse)request.GetResponse();

                if (httpResponse.StatusCode == HttpStatusCode.OK)
                {
                    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                    {
                        var responseText = streamReader.ReadToEnd();

                        List<ZeroTierNetwork> networkList = null;
                        try
                        {
                            networkList = JsonConvert.DeserializeObject<List<ZeroTierNetwork>>(responseText);
                            foreach (ZeroTierNetwork n in networkList)
                            {
                                // all networks received via JSON are connected by definition
                                n.IsConnected = true;
                            }
                        }
                        catch (JsonReaderException e)
                        {
                            Console.WriteLine(e.ToString());
                        }

                        return networkList;
                    }
                }
                else if (httpResponse.StatusCode == HttpStatusCode.Unauthorized)
                {
                    APIHandler.initHandler(true);
                }
            }
            catch (System.Net.Sockets.SocketException)
            {
                throw new LibZeroTierException("ZeroTier Request Response Empty");
            }
            catch (System.Net.WebException e)
            {
                HttpWebResponse res = (HttpWebResponse)e.Response;
                if (res != null && res.StatusCode == HttpStatusCode.Unauthorized)
                {
                    APIHandler.initHandler(true);
                }
                else
                {
                    throw new LibZeroTierException("ZeroTier Request Response Empty");
                }
            }

            return null;
        }
        public async Task StartServerAsync(string Network = null)
        {
            // get zero teir process(es) and kill em
            Process[] Zero = Process.GetProcessesByName("ZeroTier One");
            foreach (var item in Zero)
            {
                item.Kill(); item.WaitForExit();
            }
            NetworkInfoLog("[ZeroTier] [API] Stopped all ZeroTier processes");
            NetworkInfoLog("[ZeroTier] [API] Locating...");
            // Delete all network history
            DeleteAllNonConnectedNetworks();
            // start zero teir
            Process proccess = new Process();

            proccess.StartInfo.FileName       = ZeroTierEXEPath;
            proccess.StartInfo.WindowStyle    = ProcessWindowStyle.Hidden;
            proccess.StartInfo.CreateNoWindow = true;
            proccess.Start();
            // log sum info
            NetworkInfoLog("[ZeroTier] [API] Initialized!");
            NetworkInfoLog("[ZeroTier] [API] Loading API...");
            // instantiate the API manager if it has not been done already
            if (ZeroTierHandler == null)
            {
                ZeroTierHandler = new APIHandler();
                ZeroTierHandler.AddEventHandler(ZeroTierHandler_NetworkChangeEvent);
                NetworkInfoLog("[ZeroTier] [API] API Created!");
            }
            else
            {
                NetworkInfoLog("[ZeroTier] [API] API Loaded!");
            }
            // if there is a network provided, dont create one
            if (Network == null)
            {
                Network = await CreateNewNetwork();
            }
            NetworkInfoLog("[ZeroTier] [API-NET] P2P Network Created!");
            // leave any joined networks
            foreach (var LocalNet in ZeroTierHandler.GetNetworks())
            {
                ZeroTierHandler.LeaveNetwork(LocalNet.NetworkId);
            }
            // make sure the network is added, and joined properly
            bool Connected = false;

            while (!Connected)
            {
                foreach (var LocalNet in ZeroTierHandler.GetNetworks())
                {
                    if (LocalNet.NetworkId == Network)
                    {
                        if (LocalNet.IsConnected == true && LocalNet.NetworkStatus.Equals("OK"))
                        {
                            Connected = true;
                        }
                    }
                }
                if (!Connected)
                {
                    ZeroTierHandler.JoinNetwork(Network);
                }
            }
            Network_Settings.Network_Id = Network;
            NetworkInfoLog("[ZeroTier] [Network] Joined Network: " + Network_Settings.Network_Id);
            NetworkInfoLog("[ZeroTier] [Network] P2P Connection Established!");
        }
예제 #3
0
        /// <summary>
        /// Gets the status response from the ZeroTier service.
        /// </summary>
        /// <returns></returns>
        public ZeroTierStatus GetStatus()
        {
            var request = WebRequest.Create(url + "/status" + "?auth=" + authtoken) as HttpWebRequest;
            request.Headers.Add("X-ZT1-Auth",authtoken);
            if (request != null)
            {
                request.Method = "GET";
                request.ContentType = "application/json";
            }

            try
            {
                var httpResponse = (HttpWebResponse)request.GetResponse();
                if (httpResponse.StatusCode == HttpStatusCode.OK)
                {
                    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                    {
                        var responseText = streamReader.ReadToEnd();

                        ZeroTierStatus status = null;
                        try
                        {
                            status = JsonConvert.DeserializeObject<ZeroTierStatus>(responseText);

                            if (ZeroTierAddress != status.Address)
                            {
                                ZeroTierAddress = status.Address;
                            }
                        }
                        catch (JsonReaderException e)
                        {
                            Console.WriteLine(e.ToString());
                        }

                        return status;
                    }
                }
                else if (httpResponse.StatusCode == HttpStatusCode.Unauthorized)
                {
                    APIHandler.initHandler(true);
                    return null;
                }

            }
            catch (System.Net.Sockets.SocketException ex)
            {
                throw new LibZeroTierException("ZeroTier Exception:", ex);
            }
            catch (System.Net.WebException e)
            {
                HttpWebResponse res = (HttpWebResponse)e.Response;
                if (res != null && res.StatusCode == HttpStatusCode.Unauthorized)
                {
                    APIHandler.initHandler(true);
                    return null;
                }
                else
                {
                    throw new LibZeroTierException("ZeroTier Exception:", e);
                }
            }

            return null;
        }