-
Notifications
You must be signed in to change notification settings - Fork 4
/
ipfloaterd.py
241 lines (192 loc) · 8.94 KB
/
ipfloaterd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#! /usr/bin/env python
# coding: utf-8
#
# Floating IP Addresses manager (IPFloater)
# Copyright (C) 2015 - GRyCAP - Universitat Politecnica de Valencia
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import config
import sys
import version
from cpyutils.parameters import CmdLineParser, Flag, Parameter, Argument
import cpyutils.eventloop as eventloop
import cpyutils.db as db
import cpyutils.xmlrpcutils as xmlrpcutils
import cpyutils.log
import cpyutils.config
import endpoint
import iptables
import arptable
import os
from cpyutils.iputils import check_ip
import signal
'''
In the awful case that a uuid already exists in the iptables table, how should the endpoint proceed? The
OVERWRITE_RULES flag states wether to overwrite the rule or raise an error.
- perhaps a new id should be generated? (this could happen although statiscally has a low probability)
- this flag in inherited from when sequential numbers were used for ids and all started at 0 at boot
'''
OVERWRITE_RULES=True
_LOGGER = cpyutils.log.Log("IPFLOATER")
def get_endpoint_manager():
global _ENDPOINT_MANAGER
return _ENDPOINT_MANAGER
def get_arp_table():
global _ARP_TABLE
return _ARP_TABLE
_ENDPOINT_MANAGER = None
_ARP_TABLE = None
def create_public_redirection(ip_pub, port_pub, ip_priv, port_priv):
'''
This method requests a whole specific public IP to be redirected to a private IP
'''
if ip_pub == "": ip_pub = None
if port_pub < 0: port_pub = None
if ip_priv == "": ip_priv = None
if port_priv < 0: port_priv = None
if _ENDPOINT_MANAGER is None:
return False, "Endpoint Manager not found"
ep_list, info = _ENDPOINT_MANAGER.request_endpoint(ip_pub, port_pub, ip_priv, port_priv)
if len(ep_list) == 0:
return False, "Could not obtain a redirection for %s:%d (%s)" % (ip_priv, port_priv, info)
if len(ep_list) > 0:
ep = ep_list[0]
result, msg = _ENDPOINT_MANAGER.apply_endpoint(ep)
if not result:
return False, "Could not apply the redirection %s (%s)" % (ep, msg)
return True, str(ep)
else:
return False, "Could not find any free IP"
def unregister_redirection_to(dst_ip, dst_port):
if _ENDPOINT_MANAGER is None:
return False, "Endpoint Manager not found"
result = _ENDPOINT_MANAGER.terminate_redirection_to(dst_ip, dst_port)
if not result:
return False, "Could not delete the redirection to %s:%d. Does it exist?" % (dst_ip, dst_port)
return True, "Redirection %s unregistered" % ep
def unregister_redirection_from(public_ip, public_port):
if _ENDPOINT_MANAGER is None:
return False, "Endpoint Manager not found"
result = _ENDPOINT_MANAGER.terminate_redirection_from(public_ip, public_port)
if not result:
return False, "Could not delete the redirection from %s:%d. Does it exist?" % (public_ip, public_port)
return True, "Redirection %s unregistered" % ep
def unregister_redirection(public_ip, public_port, private_ip, private_port):
if _ENDPOINT_MANAGER is None:
return False, "Endpoint Manager not found"
ep = Endpoint(public_ip, public_port, private_ip, private_port)
result = _ENDPOINT_MANAGER.terminate_endpoint(ep)
if not result:
return False, "Could not delete the redirection %s. Does it exist?" % (str(ep))
return True, "Redirection %s unregistered" % ep
def clean_private_ip(private_ip):
if _ENDPOINT_MANAGER is None:
return False, "Endpoint Manager not found"
if not _ENDPOINT_MANAGER.clean_private_ip(private_ip):
return False, "Could not clean the redirections to %s. Do they exist?" % (private_ip)
return True, "Redirections to %s unregistered" % private_ip
def clean_public_ip(public_ip):
if _ENDPOINT_MANAGER is None:
return False, "Endpoint Manager not found"
if not _ENDPOINT_MANAGER.clean_public_ip(public_ip):
return False, "Could not clean the redirections from %s. Do they exist?" % (public_ip)
return True, "Redirections from %s unregistered" % public_ip
def arp(mac):
global _ARP_TABLE
if _ARP_TABLE is None:
return False, "ARP table not found"
ip = _ARP_TABLE.get_ip(mac)
if ip is None:
return False, "Could not get the IP address for %s" % (mac)
return True, ip
def get_public_ips():
if _ENDPOINT_MANAGER is None:
return False, "Endpoint Manager not found"
return True, _ENDPOINT_MANAGER.get_public_ips()
def get_version():
return version.get()
def get_redirections():
return str(_ENDPOINT_MANAGER)
def handler_sigint(signal, frame):
_LOGGER.info("removing iptables rules")
iptables.cleanup_rules()
# iptables.find_endpointchains_and_remove()
sys.exit(0)
def main_loop():
global _ENDPOINT_MANAGER, _ARP_TABLE
eventloop.create_eventloop(True)
ap = CmdLineParser("ipfloater", "This is a server that deals with iptables to enable floating IPs in private networks", [
Flag("--block-public", "-b", value = True, default = config.config.BLOCK_PUBLIC_IPS),
Parameter("--db", "-d", "The path for the persistence file", 1, False, [config.config.DB]),
Parameter("--listen-ip", "-i", "The ip adress in which ipfloater will listen for xmlrpc requests", 1, False, [ config.config.LISTEN_IP ]),
Parameter("--listen-port", "-p", "The ip port in which ipfloater will listen for xmlrpc requests", 1, False, [ config.config.LISTEN_PORT ]),
Parameter("--rest-ip", "-s", "The ip adress in which ipfloater will listen for restful requests", 1, False, [ config.config.REST_IP ]),
Parameter("--rest-port", "-t", "The ip port in which ipfloater will listen for restful requests", 1, False, [ config.config.REST_PORT ]),
Parameter("--arp-table", "-a", "The file that contains a set of whitespace separated pairs MAC IP that will be used to resolve arp requests. The IPs will also be added to the IP pool.", 1, False, [ config.config.IP_POOL_FILE ]),
])
# Will try to exit removing the iptables rules
signal.signal(signal.SIGINT, handler_sigint)
signal.signal(signal.SIGTERM, handler_sigint)
parsed, result, info = ap.parse(sys.argv[1:])
if not parsed:
if (result is None):
print "Error:", info
sys.exit(-1)
else:
print info
sys.exit(0)
SERVER=result.values['--listen-ip'][0]
PORT=result.values['--listen-port'][0]
_ENDPOINT_MANAGER = endpoint.EndpointManager(result.values['--db'][0])
_ARP_TABLE = arptable.ARPTable()
arp_filename = result.values['--arp-table'][0]
if arp_filename != "":
arp_filename = os.path.expanduser(os.path.expandvars(arp_filename))
if _ARP_TABLE.read_from_file(arp_filename) is not None:
for ip in _ARP_TABLE.get_ips():
_ENDPOINT_MANAGER.add_public_ip(ip)
for ip in _ARP_TABLE.get_ips_without_mac():
_ENDPOINT_MANAGER.add_public_ip(ip)
# TODO: persist in database
for ip in config.config.IP_POOL:
_ENDPOINT_MANAGER.add_public_ip(ip)
for ipmask in config.config.PRIVATE_IP_RANGES:
_ENDPOINT_MANAGER.add_private_range(ipmask)
if not xmlrpcutils.create_xmlrpc_server_in_thread(SERVER, PORT, [arp, create_public_redirection, unregister_redirection, unregister_redirection_from, unregister_redirection_to, clean_private_ip, clean_public_ip, get_version, get_redirections, get_public_ips]):
_LOGGER.error("could not setup the service")
raise Exception("could not setup the service")
#if REMOVE_RULES_AT_BOOT:
# iptables.find_endpointchains_and_remove()
iptables.cleanup_rules()
iptables.setup_basic_rules()
if result.values['--block-public']:
for ip in _ENDPOINT_MANAGER.get_public_ips():
iptables.block_ip(ip)
_ENDPOINT_MANAGER.get_data_from_db()
_LOGGER.info("server running in %s:%d" % (SERVER, PORT))
RESTIP=result.values['--rest-ip'][0]
RESTPORT=result.values['--rest-port'][0]
try:
RESTPORT = int(RESTPORT)
except:
RESTPORT = 0
if (RESTIP is not None) and (RESTIP != "") and (RESTPORT > 0):
import restserver
import cpyutils.restutils
cpyutils.restutils.run_in_thread(RESTIP, RESTPORT)
_LOGGER.info("REST server running in %s:%d" % (RESTIP, RESTPORT))
eventloop.get_eventloop().loop()
if __name__ == '__main__':
main_loop()