From 04834553affcabe72da525c254fdfd38e9f3d468 Mon Sep 17 00:00:00 2001
From: rudolfkoenig <>
Date: Tue, 5 Sep 2017 22:07:18 +0000
Subject: [PATCH] HttpUtils.pm: proxy global attribute (Forum #60471)
git-svn-id: https://svn.fhem.de/fhem/trunk@15017 2b470e98-0d58-463d-a4d8-8e2adae1ed80
---
fhem/CHANGED | 1 +
fhem/FHEM/HttpUtils.pm | 34 +++++++++++++++++++++++++++++-
fhem/docs/commandref_frame.html | 11 +++++++++-
fhem/docs/commandref_frame_DE.html | 18 ++++++++++++----
fhem/fhem.pl | 11 ++--------
5 files changed, 60 insertions(+), 15 deletions(-)
diff --git a/fhem/CHANGED b/fhem/CHANGED
index cbd413f14..826c6a3c0 100644
--- a/fhem/CHANGED
+++ b/fhem/CHANGED
@@ -1,5 +1,6 @@
# Add changes at the top of the list. Keep it in ASCII, and 80-char wide.
# Do not insert empty lines here, update check depends on it.
+ - feature: proxy global attribute for HttpUtils (Forum #60471)
- bugfix: 93_DbLog: V2.22.5, several fixes Forum:#75894
- feature: 74_GardenaSmartDevice: check character for create devicenames
- bugfix: FHEM2FHEM: addStateEvent attribute (Forum #76229)
diff --git a/fhem/FHEM/HttpUtils.pm b/fhem/FHEM/HttpUtils.pm
index 7790238a8..e92a6cb63 100644
--- a/fhem/FHEM/HttpUtils.pm
+++ b/fhem/FHEM/HttpUtils.pm
@@ -78,6 +78,8 @@ HttpUtils_Close($)
delete($hash->{hu_filecount});
delete($hash->{hu_blocking});
delete($hash->{hu_portSfx});
+ delete($hash->{hu_proxy});
+ delete($hash->{hu_port});
delete($hash->{directReadFn});
delete($hash->{directWriteFn});
}
@@ -291,10 +293,22 @@ HttpUtils_Connect($)
$port = ($hash->{protocol} eq "https" ? 443: 80);
}
$hash->{hu_portSfx} = ($port =~ m/^(80|443)$/ ? "" : ":$port");
+ $hash->{hu_port} = $port;
$hash->{path} = '/' unless defined($hash->{path});
$hash->{addr} = "$hash->{protocol}://$host:$port";
$hash->{auth} = urlDecode("$user:$pwd") if($authstring);
+ my $proxy = AttrVal("global", "proxy", undef);
+ if($proxy) {
+ my $pe = AttrVal("global", "proxyExclude", undef);
+ if(!$pe || $host !~ m/$pe/) {
+ my @hp = split(":", $proxy);
+ $host = $hp[0];
+ $port = $hp[1] if($hp[1]);
+ $hash->{hu_proxy} = 1;
+ }
+ }
+
return HttpUtils_Connect2($hash) if($hash->{conn} && $hash->{keepalive});
if($hash->{callback}) { # Nonblocking staff
@@ -389,6 +403,20 @@ HttpUtils_Connect2($)
return $errstr;
} else {
$hash->{conn}->blocking(1);
+
+ if($hash->{hu_proxy}) { # can block!
+ my $hdr = "CONNECT $hash->{host}:$hash->{hu_port} HTTP/1.0\r\n".
+ "User-Agent: fhem\r\n".
+ "\r\n";
+ syswrite $hash->{conn}, $hdr;
+ my $buf;
+ my $len = sysread($hash->{conn},$buf,65536);
+ if(!defined($len) || $len <= 0 || $buf !~ m/HTTP.*200/) {
+ HttpUtils_Close($hash);
+ return "Proxy denied CONNECT";
+ }
+ }
+
my $sslVersion = AttrVal("global", "sslVersion", "SSLv23:!SSLv3:!SSLv2");
$sslVersion = AttrVal($hash->{NAME}, "sslVersion", $sslVersion)
if($hash->{NAME});
@@ -453,7 +481,11 @@ HttpUtils_Connect2($)
$method = ($data ? "POST" : "GET") if( !$method );
my $httpVersion = $hash->{httpversion} ? $hash->{httpversion} : "1.0";
- my $hdr = "$method $hash->{path} HTTP/$httpVersion\r\n";
+
+ my $path = $hash->{path};
+ $path = "$hash->{protocol}://$hash->{host}$hash->{hu_portSfx}$path"
+ if($hash->{hu_proxy});
+ my $hdr = "$method $path HTTP/$httpVersion\r\n";
$hdr .= "Host: $hash->{host}$hash->{hu_portSfx}\r\n";
$hdr .= "User-Agent: fhem\r\n"
if(!$hash->{header} || $hash->{header} !~ "User-Agent:");
diff --git a/fhem/docs/commandref_frame.html b/fhem/docs/commandref_frame.html
index 27cd1afa4..6888a79f8 100644
--- a/fhem/docs/commandref_frame.html
+++ b/fhem/docs/commandref_frame.html
@@ -1507,7 +1507,7 @@ The following local attributes are used by a wider range of devices:
Windows.
-
+