Skip to content

Commit

Permalink
JSON::XS -> JSON allow backend flexibility && IO::Socket -> IO::Socke…
Browse files Browse the repository at this point in the history
…t::IP to make able use IPv6
  • Loading branch information
zbx-sadman committed Jul 4, 2017
1 parent c8fd724 commit c46e6c6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
16 changes: 12 additions & 4 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
##UniFi Proxy change log

###v1.3.3
### v1.3.4
Enhancements:
- Use JSON module to allow flexibility in which JSON backend is used. Refer to [https://metacpan.org/pod/JSON#CHOOSING-BACKEND](https://metacpan.org/pod/JSON#CHOOSING-BACKEND) for more information;
- IO::Socket::INET changed to IO::Socket::IP to enable IPv6 support. This feature not tested on real IPv6 system, send feedback to me please.

Thanks to [Ross Williams](https://github.com/overhacked) for ideas.


### v1.3.3
Fixed:
- UniFi Controller v3: error with logging in;
- UniFi Controller v3: error in 'still connected' testing on fetching data from controller;
- UniFi Controller v3: mapping _mac_-key to {#NAME} macro (Zabbix's LLD) if _name_-key is empty;
- Debug: print of HTTP response output.
- Debug: print the HTTP response output.

###v1.3.2
### v1.3.2
Fixed:
- MAC detection procedure in 'id' field;
- site list obtaining ('site' object processing does not work).

###v1.3.1
### v1.3.1
Fixed:
- removed "no sites walking" problem when 'sitename' field used with no value;
- fixed code to avoid "push on reference is experimental" warning on perl > v5.20.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## UniFi Proxy
Actual release is v1.3.3.
Actual release is v1.3.4.

See [change log](https://github.com/zbx-sadman/unifi_proxy/blob/master/ChangeLog.md) before update.

Expand Down
34 changes: 17 additions & 17 deletions usr/local/sbin/unifi_proxy.pl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/perl
#
# UniFi Proxy 1.3.3
# UniFi Proxy 1.3.4
#
# (C) Grigory Prigodin 2015-2016
# (C) Grigory Prigodin 2015-2017
# Contact e-mail: zbx.sadman@gmail.com
#
### echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle
Expand All @@ -13,9 +13,9 @@
use strict;
use warnings;
use POSIX ();
use JSON::XS ();
use JSON ();
use LWP ();
use IO::Socket ();
use IO::Socket::IP ();
use IO::Socket::SSL ();
use Data::Dumper ();

Expand All @@ -24,7 +24,7 @@
# CONFIG_FILE_DEFAULT => './unifi_proxy.conf',
TOOL_HOMEPAGE => 'https://github.com/zbx-sadman/unifi_proxy',
TOOL_NAME => 'UniFi Proxy',
TOOL_VERSION => '1.3.3',
TOOL_VERSION => '1.3.4',

# *** Actions ***
ACT_MEDIAN => 'median',
Expand Down Expand Up @@ -106,10 +106,10 @@
for (@ARGV) {
# try to take key from $_
if ( m/^[-](.+)/) {
# key is '--version' ? Set flag && do nothing inside loop
$options->{'version'} = TRUE, next if ($1 eq '-version');
# key is '--version' ? Set flag && jump out from the loop
$options->{'version'} = TRUE, last if ($1 eq '-version');
# key is --help - do the same
$options->{'help'} = TRUE, next if ($1 eq '-help');
$options->{'help'} = TRUE, last if ($1 eq '-help');
# key is just found? Init hash item
$options->{$1} = '';
} else {
Expand All @@ -127,7 +127,7 @@

if ($options->{'help'}) {
print "\n",TOOL_NAME," v", TOOL_VERSION, "\n\nusage: $0 [-C /path/to/config/file] [-D]\n",
"\t-C\tpath to config file\n\t-D\trun in daemon mode\n\nAll other help on ", TOOL_HOMEPAGE, "\n\n";
"\t-C\tpath to config file\n\t-D\trun in daemon mode\n\nMore help on ", TOOL_HOMEPAGE, "\n\n";
exit 0;
}

Expand All @@ -151,14 +151,13 @@
# Read config
readConf();
# Bind to addr:port
my $server = IO::Socket::INET->new(LocalAddr => $globalConfig->{'listenip'},
my $server = IO::Socket::IP->new(LocalAddr => $globalConfig->{'listenip'},
LocalPort => $globalConfig->{'listenport'},
Listen => $globalConfig->{'maxclients'},
Reuse => 1,
Type => IO::Socket::SOCK_STREAM,
Proto => 'tcp',) || die $@;


# Assign subs to handle Signals
$SIG{INT} = $SIG{TERM} = \&handleINTSignal;
$SIG{HUP} = \&handleHUPSignal;
Expand Down Expand Up @@ -233,7 +232,7 @@ sub handleINTSignal {
#
# Make new server with PreFork engine
# - Fork new server process
# - Accept and handle connection from IO::SOCket queue
# - Accept and handle connection from IO::Socket queue
#
#*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/
sub makeServer {
Expand Down Expand Up @@ -274,7 +273,7 @@ sub makeServer {
# LWP::UserAgent object, which must be saved between fetchData() calls
$serverConfig->{'ua'} = undef;
# JSON::XS object
$serverConfig->{'jsonxs'} = JSON::XS->new->utf8;
$serverConfig->{'json_engine'} = JSON->new->utf8;
# -s option used sign
$serverConfig->{'sitename_given'} = FALSE;

Expand Down Expand Up @@ -439,7 +438,7 @@ sub handleConnection {
logMessage(DEBUG_MID, "[.] Make LLD JSON");
# make JSON
delete $selectingResult->{'total'};
$buffer = $gC->{'jsonxs'}->encode($selectingResult);
$buffer = $gC->{'json_engine'}->encode($selectingResult);
} else {
# User want no discovery action
my $totalKeysProcesseed = @{$selectingResult->{'data'}};
Expand Down Expand Up @@ -790,7 +789,7 @@ sub fetchData {
# ...fetch new data from controller...
fetchDataFromController($_[0], $_[2], $objPath, $jsonData, $useShortWay) or logMessage(DEBUG_LOW, "[!] Can't fetch data from controller"), close ($fh), return FALSE;
# unbuffered write it to temp file..
syswrite ($fh, $_[0]->{'jsonxs'}->encode($jsonData));
syswrite ($fh, $_[0]->{'json_engine'}->encode($jsonData));
# Now unlink old cache filedata from cache filename
# All processes, who already read data - do not stop and successfully completed reading
unlink ($cacheFileName);
Expand All @@ -813,7 +812,7 @@ sub fetchData {
# open file
open($fh, "<:mmap", $cacheFileName) or logMessage(DEBUG_LOW, "[!] Can't open '$cacheFileName' ($!)"), return FALSE;
# read data from file
$jsonData=$_[0]->{'jsonxs'}->decode(<$fh>);
$jsonData=$_[0]->{'json_engine'}->decode(<$fh>);
# close cache
close($fh) or logMessage(DEBUG_LOW, "[!] Can't close cache file ($!)"), return FALSE;
}
Expand Down Expand Up @@ -912,7 +911,7 @@ sub fetchDataFromController {
}

logMessage(DEBUG_HIGH, "[>>]\t\t Fetched data:\n\t", $response->decoded_content);
$_[3] = $_[0]->{'jsonxs'}->decode(${$response->content_ref()});
$_[3] = $_[0]->{'json_engine'}->decode(${$response->content_ref()});


# server answer is ok ?
Expand Down Expand Up @@ -1148,6 +1147,7 @@ sub readConf {
die "[!] Version of controller is unknown: '$globalConfig->{'unifiversion'}, stop\n";
}
logMessage(DEBUG_MID, "[.] globalConfig:\n", $globalConfig);
logMessage(DEBUG_MID, "[.] JSON backend: " . JSON::backend());

TRUE;
}

0 comments on commit c46e6c6

Please sign in to comment.