-
Notifications
You must be signed in to change notification settings - Fork 3
/
update-ip.py
executable file
·61 lines (48 loc) · 1.61 KB
/
update-ip.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python
import logging
import http.client
import socket
import ssl
import botocore
import boto3
STACK_NAME = "ec2-gaming-sunshine"
def main():
logging.basicConfig(level=logging.INFO)
client = boto3.client("cloudformation")
logging.info(f"Updating stack {STACK_NAME}")
try:
ip = get_ip()
ip_cidr = f"{ip}/32"
logging.info(f"Updating IP whitelisting to: {ip_cidr}")
client.update_stack(
StackName=STACK_NAME,
UsePreviousTemplate=True,
Parameters=[
{"ParameterKey": "MyIp", "ParameterValue": ip_cidr},
{"ParameterKey": "KeyPair", "UsePreviousValue": True},
],
Capabilities=["CAPABILITY_NAMED_IAM"],
)
except botocore.exceptions.ClientError as error:
if error.response["Error"]["Code"] == "ValidationError":
logging.error(error.response["Error"]["Message"])
else:
raise error
def get_ip():
host = "ifconfig.me"
conn = IPv4HTTPSConnection(host)
conn.request("GET", "/ip", headers={"Host": host})
response = conn.getresponse()
ip = response.read().decode("utf-8")
conn.close()
return ip
class IPv4HTTPSConnection(http.client.HTTPSConnection):
def connect(self):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((self.host, self.port))
if self._tunnel_host:
self._tunnel()
context = ssl.create_default_context()
self.sock = context.wrap_socket(self.sock, server_hostname=self.host)
if __name__ == "__main__":
main()