Last active
December 23, 2022 03:29
-
-
Save ssippe/cf636d1009357b7b545efa09dc64144d to your computer and use it in GitHub Desktop.
awspunch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.CodeDom; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Sockets; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Amazon; | |
using Amazon.EC2; | |
using Amazon.EC2.Model; | |
namespace awspunch | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var currentIp = GetIpAddress(); | |
var oldIp = GetIpAddressFromFile(); | |
Console.WriteLine($"oldip={oldIp} newip={currentIp}"); | |
var securityGroup = GetSecurityGroup(); | |
RemoveIpHole(oldIp, securityGroup); | |
CreateIpHole(currentIp, securityGroup); | |
SaveIpToFile(currentIp); | |
Console.ReadLine(); | |
} | |
private static void RemoveIpHole(string ip, SecurityGroup securityGroup) | |
{ | |
using (var ec2Client = new AmazonEC2Client(RegionEndpoint.APSoutheast2)) | |
{ | |
var resp = ec2Client.RevokeSecurityGroupIngress(new RevokeSecurityGroupIngressRequest | |
{ | |
GroupId = securityGroup.GroupId, | |
IpPermissions = | |
new List<IpPermission> | |
{ | |
new IpPermission | |
{ | |
IpProtocol = "-1", | |
IpRanges = new List<string> {$"{ip}/32"}, | |
} | |
} | |
}); | |
Console.WriteLine("RemoveIpHole " + resp.HttpStatusCode); | |
} | |
} | |
static IpPermission GetIpPermission(string ip) | |
{ | |
return new IpPermission | |
{ | |
FromPort = -1, | |
ToPort = -1, | |
IpProtocol = "-1", | |
IpRanges = new List<string> { $"{ip}/32" }, | |
}; | |
} | |
private static void CreateIpHole(string ip, SecurityGroup securityGroup) | |
{ | |
using (var ec2Client = new AmazonEC2Client(RegionEndpoint.APSoutheast2)) | |
{ | |
var resp = ec2Client.AuthorizeSecurityGroupIngress(new AuthorizeSecurityGroupIngressRequest | |
{ | |
GroupId = securityGroup.GroupId, | |
GroupName = securityGroup.GroupName, | |
IpPermissions = | |
new List<IpPermission> | |
{ | |
GetIpPermission(ip) } | |
}); | |
Console.WriteLine("CreateIpHole " + resp.HttpStatusCode); | |
} | |
} | |
private static string GetIpAddressFromFile() | |
{ | |
return File.ReadAllText("ip.txt").Trim(); | |
} | |
private static void SaveIpToFile(string ip) | |
{ | |
File.WriteAllText("ip.txt", ip.Trim()); | |
} | |
static private string GetIpAddress() | |
{ | |
return new WebClient().DownloadString("http://icanhazip.com/s").Trim(); | |
} | |
static SecurityGroup GetSecurityGroup() | |
{ | |
using (var ec2Client = new AmazonEC2Client(RegionEndpoint.APSoutheast2)) | |
{ | |
var request = new DescribeSecurityGroupsRequest(); | |
var response = ec2Client.DescribeSecurityGroups(request); | |
return response.SecurityGroups.SingleOrDefault(f => f.GroupName == "dev-hole"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment