Esempio n. 1
0
        private PortChangeResult ClosePort(Configs.Rule rule)
        {
            var client = new AmazonEC2Client();

            RevokeSecurityGroupIngressResponse res = null;

            try
            {
                var ipRule = GetIpPermissionRule(rule, false);
                res = client.RevokeSecurityGroupIngress(new RevokeSecurityGroupIngressRequest()
                {
                    IpPermissions = new List <IpPermission>()
                    {
                        ipRule
                    },
                    GroupId = rule.SecurityGroupId
                });
            }
            catch (AmazonEC2Exception ex)
            {
                if (ex.Message.Contains("The specified rule does not exist"))
                {
                    return(new PortChangeResult
                    {
                        Message = $"Success: Connection to port {rule.Port} (already) CLOSED",
                        Color = Color.DarkGreen
                    });
                }
                return(new PortChangeResult
                {
                    Message = $"Error (Port {rule.Port}): " + ex.Message,
                    Color = Color.DarkRed
                });
            }

            if (res.HttpStatusCode == HttpStatusCode.OK)
            {
                return(new PortChangeResult
                {
                    Message = $"Success: Connection to port {rule.Port} CLOSED",
                    Color = Color.DarkGreen
                });
            }

            return(new PortChangeResult
            {
                Message = $"Error: couldn't close port {rule.Port}. Code: " + res.HttpStatusCode,
                Color = Color.DarkRed
            });
        }
Esempio n. 2
0
        private PortChangeResult OpenPort(Configs.Rule rule)
        {
            var client = new AmazonEC2Client();

            AuthorizeSecurityGroupIngressResponse res = null;

            try
            {
                res = client.AuthorizeSecurityGroupIngress(new AuthorizeSecurityGroupIngressRequest()
                {
                    IpPermissions = new List <IpPermission>()
                    {
                        GetIpPermissionRule(rule)
                    },
                    GroupId = rule.SecurityGroupId
                });
            }
            catch (AmazonEC2Exception ex)
            {
                if (ex.Message.Contains("the specified rule") && ex.Message.Contains("already exists"))
                {
                    return(new PortChangeResult
                    {
                        Message = $"Success: Connection to port {rule.Port} is (already) OPEN",
                        Color = Color.DarkGreen
                    });
                }
                return(new PortChangeResult
                {
                    Message = $"Error (Port {rule.Port}): " + ex.Message,
                    Color = Color.DarkRed
                });
            }

            if (res.HttpStatusCode == HttpStatusCode.OK)
            {
                return(new PortChangeResult
                {
                    Message = $"Success: Connection to port {rule.Port} is OPEN",
                    Color = Color.DarkGreen
                });
            }

            return(new PortChangeResult
            {
                Message = $"Error: couldn't open port {rule.Port}. Code: " + res.HttpStatusCode,
                Color = Color.DarkRed
            });
        }
Esempio n. 3
0
 private IpPermission GetIpPermissionRule(Configs.Rule rule, bool setDescription = true)
 {
     return(new IpPermission()
     {
         FromPort = rule.Port,
         ToPort = rule.Port,
         IpProtocol = "tcp",
         Ipv4Ranges = new List <IpRange>
         {
             new IpRange()
             {
                 CidrIp = _configs.PublicIp + "/32",
                 Description = setDescription ? _configs.DeviceName : null
             }
         }
     });
 }