-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathip_notify.py
More file actions
executable file
·287 lines (245 loc) · 8.87 KB
/
ip_notify.py
File metadata and controls
executable file
·287 lines (245 loc) · 8.87 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/usr/bin/env python3
# Copyright 2023-2026, jack-mil and contributors
# SPDX-License-Identifier: MIT
import argparse
import json
import logging
import os
import urllib.request
from argparse import Namespace
from datetime import datetime, timezone
from logging.handlers import RotatingFileHandler
from pathlib import Path
from urllib.error import HTTPError, URLError
IP_PROVIDERS = [
"https://am.i.mullvad.net/ip", # See: https://mullvad.net/en/check
"https://ip.me/", # See: https://ip.me/about
]
def get_args() -> Namespace:
args = argparse.ArgumentParser()
args.add_argument(
"--service",
type=str,
help="Service type to send webhook to; discord or msteams (default: discord)",
)
args.add_argument(
"--webhook",
type=str,
help="URL of webhook endpoint",
)
args.add_argument(
"-o",
"--cache-file",
type=str,
help="File to store IP",
)
args.add_argument(
"--test",
action="store_true",
help="Always send the webhook data",
)
return args.parse_args()
def get_config() -> Namespace:
"""Get the configuration values from arguments, falling back to env vars if present"""
args = get_args()
config = Namespace()
config.test = args.test
config.service = args.service or os.getenv("WEBHOOK_SERVICE") or "discord"
config.webhook = args.webhook or os.getenv("WEBHOOK_URL")
config.embed_color = os.getenv("EMBED_COLOR", "1bb106")
config.author_url = os.getenv(
"AUTHOR_URL", "https://codeberg.org/jack-mil/ip-notify"
)
config.icon_url = os.getenv("ICON_URL")
ip_cache_path = args.cache_file or os.getenv("IP_CACHE")
# Create the default directory if no env var
if ip_cache_path is None:
cache_home = os.getenv("XDG_CACHE_HOME") or Path(
os.getenv("HOME") or Path.home(), ".cache/"
)
ip_cache_path = Path(cache_home, "ip_notify_cache")
config.ip_cache = Path(ip_cache_path)
return config
logger = logging.getLogger()
def setup_logging():
# debug to rotating file, errors to stderr
formatter = logging.Formatter("[%(asctime)s] (%(name)s) %(levelname)s: %(message)s")
logger.setLevel(logging.DEBUG)
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.INFO)
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)
log_file = os.getenv("LOG_FILE")
if log_file is not None:
file_handler = RotatingFileHandler(
filename=log_file, maxBytes=5 * 1e3, backupCount=1
)
file_handler.setFormatter(formatter)
file_handler.setLevel(logging.INFO)
logger.addHandler(file_handler)
def send_notification(new_ip: str, old_ip: str, config):
"""Send the webhook POST request directly"""
match config.service:
case "discord":
func = discord_data
case "msteams":
func = teams_data
case err:
logging.error("Unsupported webhook service %s", err)
return
payload = func(config, new_ip, old_ip)
body = json.dumps(payload).encode("utf-8")
request = urllib.request.Request(
config.webhook,
method="POST",
data=body,
headers={"Content-Type": "application/json", "User-Agent": "ip-notify/1.0"},
)
try:
with urllib.request.urlopen(request, timeout=5) as res:
if res.status == 200:
logging.info("Sent %s notification", config.service)
else:
logging.info(
"Sent %s notification (response %s)", config.service, res.status
)
except TimeoutError:
logging.error("%s webhook request timed out after 5s", config.service)
except HTTPError as exc:
error_body = exc.read().decode().strip()
logging.error(
"Failed to send %s notification: HTTP %s - { %s } ",
config.service,
exc.code,
error_body,
)
except URLError as exc:
error_body = exc.read().decode().strip()
logging.error(
"Send %s notification failed due to POST error: %s",
config.service,
exc,
)
def discord_data(config, new_ip: str, old_ip: str) -> dict:
"""Construct payload data for Discord webhook"""
color_decimal = int(config.embed_color.lstrip("#"), 16)
payload = {
"username": "IP Notify",
"avatar_url": config.author_url,
"embeds": [
{
"title": "Test Message" if config.test else "Address Changed",
"color": color_decimal,
"author": {
"name": "IP Notify",
"url": config.author_url,
"icon_url": config.icon_url,
},
"fields": [
{
"name": "New :green_circle:",
"value": f"**{new_ip}**",
"inline": False,
},
{
"name": "Old :red_circle:",
"value": f"~~{old_ip}~~",
"inline": False,
},
],
"footer": {
"text": "Occurred",
},
# ISO-8601 timestamp
"timestamp": datetime.now(timezone.utc).isoformat(),
}
],
}
return payload
def teams_data(config, old_ip: str, new_ip: str) -> dict:
"""Construct webhook data for MS Teams webhook"""
payload = {
"type": "message",
"attachments": [
{
"contentType": "application/vnd.microsoft.card.adaptive",
"contentUrl": "null",
"content": {
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.2",
"type": "AdaptiveCard",
"body": [
{
"type": "TextBlock",
"size": "Medium",
"weight": "Bolder",
"text": "IP Address Changed",
},
{
"type": "FactSet",
"facts": [
{"title": "Old:", "value": old_ip},
{"title": "New:", "value": new_ip},
],
},
],
},
}
],
}
return payload
def get_current_ip(providers: list[str]) -> str | None:
"""Uses Mullvad or Proton VPN trace service to lookup current public IP"""
for provider in providers:
with urllib.request.urlopen(provider, timeout=1.5) as res:
if res.status == 200:
# both providers return a single line with the ip address
info = res.read().decode().split("\n")
current_ip = info[0]
logging.debug(f"Public ip [{current_ip}] grabbed from [{provider}]")
return current_ip
logging.warning(f"Provider error: [{provider}] Response: {res.status}")
logging.error("FATAL: All providers unavailable. IP lookup failed")
def get_last_ip(ip_file: str) -> str | None:
"""Read old ip from a file"""
if not ip_file.exists():
logging.info("No existing ip record found")
return None
old_ip = ip_file.read_text().split("\n")[0]
return old_ip
def save_current_ip(ip: str, ip_file: str):
"""Saves the current public IP to a file"""
try:
ip_file.write_text(ip + "\n")
except OSError as e:
logger.error(f"Could not save current IP: {e}")
def main() -> int:
"""Run once to check current IP against old IP and notify if changed"""
config = get_config()
setup_logging()
cache_path = config.ip_cache
if config.webhook is None:
logging.error("Must configure a webhook endpoint using args or env vars")
return 1
logging.info("Watching for IP changes...")
new_ip = get_current_ip(IP_PROVIDERS)
old_ip = get_last_ip(cache_path)
if new_ip is None:
logging.error("Could not determine public IP. Task failed")
return 1
elif old_ip is None or config.test:
if config.test:
logging.info("Sending initial test message")
else:
logging.info(f"First time detected. IP is [{new_ip}]")
send_notification(new_ip, old_ip, config)
save_current_ip(new_ip, cache_path)
elif new_ip != old_ip:
logging.info(f"Public ip changed from {old_ip} to {new_ip}")
send_notification(new_ip, old_ip, config)
save_current_ip(new_ip, cache_path)
else:
logging.info("No change in public IP")
return 0
if __name__ == "__main__":
raise SystemExit(main())