-
Notifications
You must be signed in to change notification settings - Fork 4
/
ipfloater.py
114 lines (102 loc) · 5.46 KB
/
ipfloater.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
#! /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 sys
from cpyutils.parameters import CmdLineParser, Flag, Parameter, Argument, Operation
import cpyutils.eventloop as eventloop
import cpyutils.db as db
import cpyutils.log
import cpyutils.xmlrpcutils as xmlrpcutils
import logging
import version
_LOGGER = cpyutils.log.Log("IPFLOATER")
def main_function():
logging.basicConfig(filename=None,level=logging.DEBUG)
eventloop.create_eventloop(True)
class IPFloaterCmdLine(CmdLineParser):
def preops(self, result, error):
SERVER=result.values['--server-ip'][0]
PORT=result.values['--server-port'][0]
self._XMLRPC_SERVER = xmlrpcutils.ServerProxy("http://%s:%d" % (SERVER, PORT))
def ippool(self, parse_result, error):
try:
_, ips = self._XMLRPC_SERVER.get_public_ips()
return True, "IP Pool:\n%s\n%s" % ("-"*40, ", ".join(ips))
except:
return False, "Could not contact the server"
def getip(self, parse_result, error):
result, ep = self._XMLRPC_SERVER.create_public_redirection("", -1, parse_result.values['private ip'][0], 0)
if result:
return True, "Public IP obtained: %s" % str(ep)
else:
return False, "Could not obtain a redirection (server responded: %s)" % ep
def redirect(self, parse_result, error):
result, ep = self._XMLRPC_SERVER.create_public_redirection(parse_result.values['public ip'][0], 0, parse_result.values['private ip'][0], 0)
if result:
return True, "Public IP obtained: %s" % str(ep)
else:
return False, "Could not obtain a redirection (server responded: %s)" % ep
def releaseip(self, parse_result, error):
ip = parse_result.values['public ip'][0]
result, ep = self._XMLRPC_SERVER.clean_public_ip(ip)
if result:
return True, "Released the redirection from IP %s" % (ip)
else:
return False, "Could not release the redirection from IP %s (server responded: %s)" % (ip, ep)
def status(self, result, error):
try:
return True, "Table of redirections:\n%s\n%s" % ("-"*40, self._XMLRPC_SERVER.get_redirections())
except:
return False, "Could not contact the server"
def version(self, result, error):
try:
server_version = self._XMLRPC_SERVER.get_version()
return True, "Client version: %s\nServer version: %s" % (version.get(), server_version)
except:
return True, "Client version: %s\nCould not contact server" % version.get()
def arp(self, parse_result, error):
mac = parse_result.values['mac'][0]
result, ip = self._XMLRPC_SERVER.arp(mac)
if result:
return True, "%s" % (ip)
else:
return False, "Failed to get the ip address for %s (server responded: %s)" % (mac, ip)
ap = IPFloaterCmdLine("ipfloater", "This the client for ipfloaterd, which is a server that deals with iptables to enable floating IPs in private networks", [
Parameter("--server-ip", "-i", "The ip adress in which ipfloater listens", 1, False, ["127.0.0.1"]),
Parameter("--server-port", "-p", "The ip port in which ipfloater listens", 1, False, [7000]),
Operation("getip", desc = "Requests a floating IP for a private IP", arguments = [
Argument("private ip", "private ip address to which is requested the floating ip", mandatory = True, count = 1),
]),
Operation("redirect", desc = "Redirects a floating IP to a private IP", arguments = [
Argument("public ip", "floating ip address", mandatory = True, count = 1),
Argument("private ip", "private ip address to which is requested the floating ip", mandatory = True, count = 1),
]),
Operation("releaseip", desc = "Releases a floating IP", arguments = [
Argument("public ip", "public ip address (the floating ip)", mandatory = True, count = 1),
]),
Operation("status", desc = "Gets the status of the redirections"),
Operation("version", desc = "Gets the version of the client and the server"),
Operation("ippool", desc = "Gets the public ip addresses in the pool"),
Operation("arp", desc = "Requests the IP for a MAC address", arguments = [
Argument("mac", "the mac address for which is requested the ip", mandatory = True, count = 1),
]),
])
ap.self_service(True)
if __name__ == '__main__':
main_function()