Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable the use of system-wide proxys #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions exporter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3

import xmlrpc.client
import argparse
import logging
Expand All @@ -8,19 +7,43 @@
import json
import re
import sys
import os

from socketserver import ThreadingMixIn
from http.server import HTTPServer
from pprint import pformat
import requests
from prometheus_client import Gauge, Counter, Enum, MetricsHandler, core, Summary, start_http_server

from urllib.parse import urlparse

## Allow homematic_exporter to use the system-wide configured http(s) proxy
class ProxiedTransport(xmlrpc.client.Transport):
def __init__(self):
proxy_http = os.environ.get('http_proxy')
proxy_https = os.environ.get('https_proxy')
proxy = proxy_https if proxy_https else proxy_http
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fallback is mostly intended, but not helpful when only one scheme needs a proxy and the other doesn't. And the main decision should be taken on the originally requested scheme.

proxy_parts = urlparse(proxy)
self.proxy = proxy_parts.hostname
self.proxy_port = proxy_parts.port
self.proxy_headers = {}
super().__init__(self)

def make_connection(self, host):
if not self.proxy or host.startswith('https'):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

even https could be proxied (through the https_proxy)

return super().make_connection(host)
self.realhost = host
import http.client
h = http.client.HTTPConnection(self.proxy, self.proxy_port)
h.set_tunnel(self.realhost, headers=self.proxy_headers)
return h

class HomematicMetricsProcessor(threading.Thread):

METRICS_NAMESPACE = 'homematic'
# Supported Homematic (BidcosRF and IP) device types
DEFAULT_SUPPORTED_TYPES = [
'HmIP-eTRV-2 I9F',
'HmIP-eTRV-B',
'HmIP-eTRV-2',
'HmIP-eTRV-C',
'HmIP-eTRV-C-2',
Expand Down Expand Up @@ -244,7 +267,7 @@ def generate_metrics(self):
logging.debug(pformat(paramset))

def create_proxy(self):
transport = xmlrpc.client.Transport()
transport = ProxiedTransport()
connection = transport.make_connection(self.ccu_host)
connection.timeout = 5
return xmlrpc.client.ServerProxy(self.ccu_url, transport=transport)
Expand Down