2016-06-03 19:02:02 +02:00
|
|
|
from Interfaces import *
|
|
|
|
import ConfigParser
|
|
|
|
import jsonpickle
|
|
|
|
from vendor.configobj import ConfigObj
|
|
|
|
import struct
|
|
|
|
import array
|
|
|
|
import os.path
|
|
|
|
import os
|
|
|
|
|
2018-03-19 18:11:50 +01:00
|
|
|
import FPE
|
|
|
|
|
2016-06-03 19:02:02 +02:00
|
|
|
class FlexPE:
|
2018-03-19 18:11:50 +01:00
|
|
|
MTU = 500
|
2016-06-03 19:02:02 +02:00
|
|
|
router = None
|
|
|
|
config = None
|
|
|
|
destinations = []
|
|
|
|
interfaces = []
|
2018-03-19 20:51:26 +01:00
|
|
|
configdir = os.path.expanduser("~")+"/.flexpe"
|
|
|
|
configpath = ""
|
|
|
|
storagepath = ""
|
|
|
|
cachepath = ""
|
|
|
|
|
|
|
|
# TODO: Move this to Transport
|
|
|
|
packetlist = []
|
|
|
|
|
|
|
|
def __init__(self,configdir=None):
|
|
|
|
if configdir != None:
|
|
|
|
FlexPE.configdir = configdir
|
|
|
|
|
|
|
|
FlexPE.configpath = FlexPE.configdir+"/config"
|
|
|
|
FlexPE.storagepath = FlexPE.configdir+"/storage"
|
|
|
|
FlexPE.cachepath = FlexPE.configdir+"/storage/cache"
|
|
|
|
|
|
|
|
if not os.path.isdir(FlexPE.storagepath):
|
|
|
|
os.makedirs(FlexPE.storagepath)
|
|
|
|
|
|
|
|
if not os.path.isdir(FlexPE.cachepath):
|
|
|
|
os.makedirs(FlexPE.cachepath)
|
2018-03-16 11:40:37 +01:00
|
|
|
|
|
|
|
if os.path.isfile(self.configpath):
|
|
|
|
self.config = ConfigObj(self.configpath)
|
2018-03-19 16:39:08 +01:00
|
|
|
FPE.log("Configuration loaded from "+self.configpath)
|
2016-06-03 19:02:02 +02:00
|
|
|
else:
|
2018-03-19 16:39:08 +01:00
|
|
|
FPE.log("Could not load config file, creating default configuration...")
|
2016-06-03 19:02:02 +02:00
|
|
|
self.createDefaultConfig()
|
|
|
|
|
|
|
|
self.applyConfig()
|
2018-03-19 20:51:26 +01:00
|
|
|
FPE.Identity.loadKnownDestinations()
|
2016-06-03 19:02:02 +02:00
|
|
|
FlexPE.router = self
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def addDestination(destination):
|
|
|
|
destination.MTU = FlexPE.MTU
|
|
|
|
FlexPE.destinations.append(destination)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def outbound(raw):
|
|
|
|
for interface in FlexPE.interfaces:
|
|
|
|
if interface.OUT:
|
2018-03-19 16:39:08 +01:00
|
|
|
FPE.log("Transmitting via: "+str(interface), FPE.LOG_DEBUG)
|
2016-06-03 19:02:02 +02:00
|
|
|
interface.processOutgoing(raw)
|
|
|
|
|
|
|
|
def applyConfig(self):
|
2018-03-19 16:39:08 +01:00
|
|
|
for option in self.config["logging"]:
|
|
|
|
value = self.config["logging"][option]
|
|
|
|
if option == "loglevel":
|
|
|
|
FPE.loglevel = int(value)
|
|
|
|
|
2016-06-03 19:02:02 +02:00
|
|
|
for name in self.config["interfaces"]:
|
|
|
|
c = self.config["interfaces"][name]
|
|
|
|
try:
|
|
|
|
if c["type"] == "UdpInterface":
|
|
|
|
interface = UdpInterface.UdpInterface(
|
2018-03-19 20:51:26 +01:00
|
|
|
FPE.Transport,
|
2016-06-03 19:02:02 +02:00
|
|
|
c["listen_ip"],
|
|
|
|
int(c["listen_port"]),
|
|
|
|
c["forward_ip"],
|
|
|
|
int(c["forward_port"])
|
|
|
|
)
|
|
|
|
|
|
|
|
if c["use_as_outgoing"].lower() == "true":
|
|
|
|
interface.OUT = True
|
|
|
|
|
|
|
|
interface.name = name
|
|
|
|
FlexPE.interfaces.append(interface)
|
|
|
|
|
|
|
|
if c["type"] == "SerialInterface":
|
|
|
|
interface = SerialInterface.SerialInterface(
|
2018-03-19 20:51:26 +01:00
|
|
|
FPE.Transport,
|
2016-06-03 19:02:02 +02:00
|
|
|
c["port"],
|
|
|
|
int(c["speed"]),
|
|
|
|
int(c["databits"]),
|
|
|
|
c["parity"],
|
|
|
|
int(c["stopbits"])
|
|
|
|
)
|
|
|
|
|
|
|
|
if c["use_as_outgoing"].lower() == "true":
|
|
|
|
interface.OUT = True
|
|
|
|
|
|
|
|
interface.name = name
|
|
|
|
FlexPE.interfaces.append(interface)
|
|
|
|
|
|
|
|
except Exception as e:
|
2018-03-19 16:39:08 +01:00
|
|
|
FPE.log("The interface \""+name+"\" could not be created. Check your configuration file for errors!", FPE.LOG_ERROR)
|
|
|
|
FPE.log("The contained exception was: "+str(e), FPE.LOG_ERROR)
|
2016-06-03 19:02:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def createDefaultConfig(self):
|
|
|
|
self.config = ConfigObj()
|
|
|
|
self.config.filename = FlexPE.configpath
|
|
|
|
self.config["interfaces"] = {}
|
|
|
|
self.config["interfaces"]["Default UDP Interface"] = {}
|
|
|
|
self.config["interfaces"]["Default UDP Interface"]["type"] = "UdpInterface"
|
|
|
|
self.config["interfaces"]["Default UDP Interface"]["listen_ip"] = "0.0.0.0"
|
|
|
|
self.config["interfaces"]["Default UDP Interface"]["listen_port"] = 7777
|
|
|
|
self.config["interfaces"]["Default UDP Interface"]["forward_ip"] = "255.255.255.255"
|
|
|
|
self.config["interfaces"]["Default UDP Interface"]["forward_port"] = 7777
|
|
|
|
self.config["interfaces"]["Default UDP Interface"]["use_as_outgoing"] = "true"
|
|
|
|
if not os.path.isdir(FlexPE.configdir):
|
|
|
|
os.makedirs(FlexPE.configdir)
|
|
|
|
self.config.write()
|
|
|
|
self.applyConfig()
|