]>
git.ipfire.org Git - oddments/fireinfo.git/blob - src/sendprofile
59ad9f78a8df08be2c62c0dec2878e365fc0e21f
2 ###############################################################################
5 # Copyright (C) 2010, 2011 IPFire Team (www.ipfire.org) #
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. #
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. #
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/>. #
20 ###############################################################################
24 import logging
.handlers
27 import urllib
.request
, urllib
.parse
, urllib
.error
31 ENABLED_FILE
= "/var/ipfire/main/send_profile"
32 PROXY_SETTINGS
= "/var/ipfire/proxy/advanced/settings"
34 log_level
= logging
.INFO
36 log_level
= logging
.DEBUG
39 log
= logging
.getLogger()
40 log
.setLevel(log_level
)
41 log
.addHandler(logging
.handlers
.SysLogHandler("/dev/log"))
42 log
.addHandler(logging
.StreamHandler(sys
.stderr
))
43 for handler
in log
.handlers
:
44 handler
.setLevel(log_level
)
46 PROFILE_URL
= "https://fireinfo.ipfire.org/send/%(public_id)s"
48 def get_upstream_proxy():
49 if not os
.path
.exists(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()
59 "host" : proxy_settings
.get("UPSTREAM_PROXY", ""),
60 "user" : proxy_settings
.get("UPSTREAM_USER", ""),
61 "pass" : proxy_settings
.get("UPSTREAM_PASSWORD", ""),
64 def send_profile(profile
):
65 logging
.debug("Sending profile:")
66 for line
in json
.dumps(profile
, sort_keys
=True, indent
=4).splitlines():
70 data
= urllib
.parse
.urlencode({"profile" : json
.dumps(profile
)})
72 request
= urllib
.request
.Request(PROFILE_URL
% profile
, data
=data
.encode())
73 request
.add_header("User-Agent", "fireinfo/%s" % fireinfo
.__version
__)
75 # Set upstream proxy if we have one.
76 proxy
= get_upstream_proxy()
79 # handling upstream proxies with authentication is more tricky...
80 if proxy
["user"] and proxy
["pass"]:
81 prx_auth_string
= "http://%s:%s@%s/" % (proxy
["user"], proxy
["pass"], proxy
["host"])
83 proxy_handler
= urllib
.request
.ProxyHandler({'http': prx_auth_string
, 'https': prx_auth_string
})
84 auth
= urllib
.request
.HTTPBasicAuthHandler()
85 opener
= urllib
.request
.build_opener(proxy_handler
, auth
, urllib
.request
.HTTPHandler
)
86 urllib
.request
.install_opener(opener
)
88 request
.set_proxy(proxy
["host"], "http")
89 request
.set_proxy(proxy
["host"], "https")
92 urllib
.request
.urlopen(request
, timeout
=60)
93 except (urllib
.error
.HTTPError
, urllib
.error
.URLError
) as e
:
94 reason
= "Unknown reason"
96 if isinstance(e
, urllib
.error
.HTTPError
):
98 elif isinstance(e
, urllib
.error
.URLError
):
101 logging
.error("Profile was not sent propertly: %s" % reason
)
104 logging
.debug("Profile was sent successfully.")
107 # Collect system information
108 system
= fireinfo
.System()
109 profile
= system
.profile()
111 # If --dump is passed -> only dump the output.
112 if "--dump" in sys
.argv
:
113 # Remove the private id when dumping the profile because
114 # it contains no information and may confuse people.
115 del profile
["private_id"]
117 print(json
.dumps(profile
, sort_keys
=True, indent
=4))
120 if "--secret-id" in sys
.argv
:
121 print(system
.secret_id
)
124 if "--hardware-string" in sys
.argv
:
125 print(system
._unique
_id
)
128 if "--public-id" in sys
.argv
:
129 print(system
.public_id
)
132 if not os
.path
.exists(ENABLED_FILE
):
133 del profile
["profile"]
136 send_profile(profile
)
137 except urllib
.error
.URLError
: