]>
Commit | Line | Data |
---|---|---|
af63a615 | 1 | #!/usr/bin/python3 |
3b5ed4e1 MT |
2 | ############################################################################### |
3 | # # | |
4 | # Fireinfo # | |
5 | # Copyright (C) 2010, 2011 IPFire Team (www.ipfire.org) # | |
6 | # # | |
7 | # This program is free software: you can redistribute it and/or modify # | |
8 | # it under the terms of the GNU General Public License as published by # | |
9 | # the Free Software Foundation, either version 3 of the License, or # | |
10 | # (at your option) any later version. # | |
11 | # # | |
12 | # This program is distributed in the hope that it will be useful, # | |
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # | |
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | |
15 | # GNU General Public License for more details. # | |
16 | # # | |
17 | # You should have received a copy of the GNU General Public License # | |
18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. # | |
19 | # # | |
20 | ############################################################################### | |
65891720 MT |
21 | |
22 | import json | |
23 | import logging | |
24 | import logging.handlers | |
3fde7e27 | 25 | import os |
65891720 | 26 | import sys |
3b72291d | 27 | import urllib.request, urllib.parse, urllib.error |
65891720 MT |
28 | |
29 | import fireinfo | |
30 | ||
3fde7e27 | 31 | ENABLED_FILE = "/var/ipfire/main/send_profile" |
a3560310 | 32 | PROXY_SETTINGS = "/var/ipfire/proxy/advanced/settings" |
3fde7e27 | 33 | |
7c181f39 MT |
34 | log_level = logging.INFO |
35 | if "-d" in sys.argv: | |
36 | log_level = logging.DEBUG | |
65891720 MT |
37 | |
38 | # Setup logging | |
39 | log = logging.getLogger() | |
40 | log.setLevel(log_level) | |
41 | log.addHandler(logging.handlers.SysLogHandler("/dev/log")) | |
7c181f39 | 42 | log.addHandler(logging.StreamHandler(sys.stderr)) |
65891720 MT |
43 | for handler in log.handlers: |
44 | handler.setLevel(log_level) | |
45 | ||
83826340 | 46 | PROFILE_URL = "https://fireinfo.ipfire.org/send/%(public_id)s" |
65891720 | 47 | |
a3560310 MT |
48 | def get_upstream_proxy(): |
49 | if not os.path.exists(PROXY_SETTINGS): | |
3f4e98ab | 50 | return {"host" : ""} |
a3560310 MT |
51 | |
52 | proxy_settings = {} | |
53 | with open(PROXY_SETTINGS) as f: | |
54 | for line in f.readlines(): | |
55 | k, v = line.split("=", 1) | |
56 | proxy_settings[k] = v.strip() | |
57 | ||
58 | return { | |
59 | "host" : proxy_settings.get("UPSTREAM_PROXY", ""), | |
60 | "user" : proxy_settings.get("UPSTREAM_USER", ""), | |
61 | "pass" : proxy_settings.get("UPSTREAM_PASSWORD", ""), | |
62 | } | |
63 | ||
65891720 MT |
64 | def send_profile(profile): |
65 | logging.debug("Sending profile:") | |
66 | for line in json.dumps(profile, sort_keys=True, indent=4).splitlines(): | |
67 | logging.debug(line) | |
68 | ||
3b72291d MT |
69 | request = urllib.request.Request(PROFILE_URL % profile, |
70 | data = urllib.parse.urlencode({"profile" : json.dumps(profile)}), | |
a3560310 | 71 | ) |
d78396e3 | 72 | request.add_header("User-Agent", "fireinfo/%s" % fireinfo.__version__) |
a3560310 MT |
73 | |
74 | # Set upstream proxy if we have one. | |
a3560310 | 75 | proxy = get_upstream_proxy() |
74c5e2ad | 76 | |
a3560310 | 77 | if proxy["host"]: |
74c5e2ad PM |
78 | # handling upstream proxies with authentication is more tricky... |
79 | if proxy["user"] and proxy["pass"]: | |
80 | prx_auth_string = "http://%s:%s@%s/" % (proxy["user"], proxy["pass"], proxy["host"]) | |
81 | ||
3b72291d MT |
82 | proxy_handler = urllib.request.ProxyHandler({'http': prx_auth_string, 'https': prx_auth_string}) |
83 | auth = urllib.request.HTTPBasicAuthHandler() | |
84 | opener = urllib.request.build_opener(proxy_handler, auth, urllib.request.HTTPHandler) | |
85 | urllib.request.install_opener(opener) | |
74c5e2ad PM |
86 | else: |
87 | request.set_proxy(proxy["host"], "http") | |
88 | request.set_proxy(proxy["host"], "https") | |
a3560310 | 89 | |
65891720 | 90 | try: |
3b72291d MT |
91 | urllib.request.urlopen(request, timeout=60) |
92 | except (urllib.error.HTTPError, urllib.error.URLError) as e: | |
65891720 MT |
93 | reason = "Unknown reason" |
94 | ||
3b72291d | 95 | if isinstance(e, urllib.error.HTTPError): |
65891720 | 96 | reason = "%s" % e |
3b72291d | 97 | elif isinstance(e, urllib.error.URLError): |
65891720 MT |
98 | reason = e.reason |
99 | ||
100 | logging.error("Profile was not sent propertly: %s" % reason) | |
101 | return | |
102 | ||
103 | logging.debug("Profile was sent successfully.") | |
104 | ||
105 | def main(): | |
65891720 MT |
106 | # Collect system information |
107 | system = fireinfo.System() | |
3fde7e27 | 108 | profile = system.profile() |
67906550 MT |
109 | |
110 | # If --dump is passed -> only dump the output. | |
111 | if "--dump" in sys.argv: | |
e8d315a1 MT |
112 | # Remove the private id when dumping the profile because |
113 | # it contains no information and may confuse people. | |
114 | del profile["private_id"] | |
115 | ||
3b72291d | 116 | print(json.dumps(profile, sort_keys=True, indent=4)) |
67906550 MT |
117 | return 0 |
118 | ||
d20d3659 | 119 | if "--secret-id" in sys.argv: |
3b72291d | 120 | print(system.secret_id) |
d20d3659 MT |
121 | return 0 |
122 | ||
123 | if "--hardware-string" in sys.argv: | |
3b72291d | 124 | print(system._unique_id) |
d20d3659 MT |
125 | return 0 |
126 | ||
bf07eeb1 | 127 | if "--public-id" in sys.argv: |
3b72291d | 128 | print(system.public_id) |
bf07eeb1 MT |
129 | return 0 |
130 | ||
3fde7e27 MT |
131 | if not os.path.exists(ENABLED_FILE): |
132 | del profile["profile"] | |
133 | ||
65891720 | 134 | try: |
3fde7e27 | 135 | send_profile(profile) |
3b72291d | 136 | except urllib.error.URLError: |
65891720 MT |
137 | return 1 |
138 | ||
139 | return 0 | |
140 | ||
141 | sys.exit(main()) |