コード例 #1
0
        public async Task <NetworkControllerStatus> GetNetworkControllerStatusAsync(CancellationToken cs)
        {
            try
            {
                string output = await CommandExecutor.Execute(
                    LinuxTrafficControllerHelper.CommandName,
                    LinuxTrafficControllerHelper.GetShowRules(this.networkInterfaceName),
                    cs);

                // parse output to see if online or offline
                if (output.Contains("qdisc noqueue"))
                {
                    Log.LogDebug("No rule is set");
                    return(NetworkControllerStatus.Disabled);
                }
                else
                {
                    Log.LogDebug($"Found rules {output}");
                    return(NetworkControllerStatus.Enabled);
                }
            }
            catch (Exception e)
            {
                Log.LogError(e, "Failed to get network status");
                return(NetworkControllerStatus.Unknown);
            }
        }
コード例 #2
0
        async Task <bool> AddDropRule(CancellationToken cs)
        {
            try
            {
                IPAddress[] iothubAddresses = await Dns.GetHostAddressesAsync(this.iotHubHostname);

                if (iothubAddresses.Length == 0)
                {
                    throw new CommandExecutionException("No IP found for iothub hostname");
                }

                foreach (var item in iothubAddresses)
                {
                    Log.LogInformation($"Found iotHub IP {item}");
                }

                // Adding rules to filter packages by iotHub IP
                // Details about how the rules work https://wiki.archlinux.org/index.php/Advanced_traffic_control
                Log.LogInformation($"Executing: {LinuxTrafficControllerHelper.GetRootRule(this.networkInterfaceName)}");

                await CommandExecutor.Execute(
                    LinuxTrafficControllerHelper.CommandName,
                    LinuxTrafficControllerHelper.GetRootRule(this.networkInterfaceName),
                    cs);

                Log.LogInformation($"Executing: {LinuxTrafficControllerHelper.GetNetworkEmulatorAddRule(this.networkInterfaceName, this.profileRuleSettings)}");

                await CommandExecutor.Execute(
                    LinuxTrafficControllerHelper.CommandName,
                    LinuxTrafficControllerHelper.GetNetworkEmulatorAddRule(this.networkInterfaceName, this.profileRuleSettings),
                    cs);

                Log.LogInformation($"Executing: {LinuxTrafficControllerHelper.GetIpFilter(this.networkInterfaceName, iothubAddresses)}");

                await CommandExecutor.Execute(
                    LinuxTrafficControllerHelper.CommandName,
                    LinuxTrafficControllerHelper.GetIpFilter(this.networkInterfaceName, iothubAddresses),
                    cs);

                return(true);
            }
            catch (Exception e)
            {
                Log.LogError(e, "Failed to set drop rule");
                return(false);
            }
        }
コード例 #3
0
        async Task <bool> RemoveDropRule(CancellationToken cs)
        {
            try
            {
                // Delete the root rules cleans the chilldren rules
                await CommandExecutor.Execute(
                    LinuxTrafficControllerHelper.CommandName,
                    LinuxTrafficControllerHelper.GetRemoveAllArguments(this.networkInterfaceName),
                    cs);

                return(true);
            }
            catch (Exception e)
            {
                Log.LogError(e, "Failed to set accept rule");
                return(false);
            }
        }