Skip to content

Commit 1c08fc7

Browse files
committed
logging. forward_host default
1 parent ef1692e commit 1c08fc7

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Variable | Default | Description
1919
`LOG_COLORIZED` | `0` | Log using colors (`0`=disabled, `1`=enabled).
2020
`LOG_FMT` | `%y%m%d %H:%M:%S` | Log format prefix.
2121
`LISTEN_PORT` | `2055` | Port to listen to for flow packets
22+
`FORWARD_HOST` | `127.0.0.1` | Forward UDP traffic to this host (only applicable if FORWARD_PORTS is set).
23+
`FORWARD_PORTS` | _none_ | Forward UDP traffic to these local ports for other listeners (comma separate for multiple ports).
2224

2325
## Docker build
2426

lib/server.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,26 @@
88
from .state import subscriptions
99

1010
LISTEN_PORT = int(os.getenv('LISTEN_PORT', '2055'))
11-
FORWARD_HOST = os.getenv('FORWARD_HOST', 'host.docker.internal')
11+
FORWARD_HOST = os.getenv('FORWARD_HOST', '127.0.0.1')
1212
FORWARD_PORTS = os.getenv('FORWARD_PORTS')
1313
FORWARD = [
1414
(FORWARD_HOST, pt)
1515
for pt in map(int, FORWARD_PORTS.split(','))
1616
] if FORWARD_PORTS else []
1717
assert all(0 < pt < 65536 and pt != LISTEN_PORT for _, pt in FORWARD)
18+
assert len(FORWARD) == len(set(FORWARD))
1819

1920
COMMON_HEADER_FMT = '>HHL'
2021
COMMON_HEADER_SZ = 8
2122

2223

2324
class ServerProtocol(asyncio.Protocol):
2425

26+
def __init__(self):
27+
super().__init__()
28+
self.log_unsupported_version = 0
29+
self.log_failed_to_forward = set()
30+
2531
def connection_made(self, transport):
2632
self.transport = transport
2733

@@ -36,16 +42,18 @@ def datagram_received(self, data, addr):
3642
) = struct.unpack(COMMON_HEADER_FMT, data[:COMMON_HEADER_SZ])
3743

3844
if version not in (5, 9, 10):
39-
# TODO 10 times
40-
logging.warning('unsupported netflow version')
45+
if self.log_unsupported_version < 10:
46+
self.log_unsupported_version += 1
47+
logging.warning('unsupported netflow version')
4148
return
4249

4350
for dest in FORWARD:
4451
try:
4552
self.transport.sendto(data, dest)
4653
except Exception:
47-
# TODO 1 time / conn
48-
logging.warning('failed to forward package')
54+
if dest not in self.log_failed_to_forward:
55+
self.log_failed_to_forward.add(dest)
56+
logging.warning(f'failed to forward package to {dest}')
4957

5058
# v5 has no templates so could be ignored when no checks are listening
5159
if version == 5 and not subscriptions:

0 commit comments

Comments
 (0)