# Path: toughwlan/models.py
# def get_metadata(db_engine):
# class SystemSession(DeclarativeBase):
# class SystemCache(DeclarativeBase):
# class TrwOperator(DeclarativeBase):
# class TrwOperatorRule(DeclarativeBase):
# class TrwParam(DeclarativeBase):
# class TrwIsp(DeclarativeBase):
# class TrwIspService(DeclarativeBase):
# class TrwBas(DeclarativeBase):
# class TrwDomain(DeclarativeBase):
# class TrwDomainAttr(DeclarativeBase):
# class TrwSsid(DeclarativeBase):
# class TrwRadius(DeclarativeBase):
# class TrwTemplate(DeclarativeBase):
# class TrwOperateLog(DeclarativeBase):
# class TrwOSTypes(DeclarativeBase):
# 
# Path: toughwlan/manage/settings.py
# def redis_conf(config):
#     eredis_url = os.environ.get("REDIS_URL")
#     eredis_port = os.environ.get("REDIS_PORT")
#     eredis_pwd = os.environ.get("REDIS_PWD")
#     eredis_db = os.environ.get("REDIS_DB")
# 
#     is_update = any([eredis_url,eredis_port,eredis_pwd,eredis_db])
# 
#     if eredis_url:
#         config['redis']['host'] = eredis_url
#     if eredis_port:
#         config['redis']['port'] = int(eredis_port)
#     if eredis_pwd:
#         config['redis']['passwd'] = eredis_pwd 
#     if eredis_db:
#         config['redis']['db'] = int(eredis_db)
#     if is_update:
#         config.save()
# 
#     return config['redis']


import sys
import time
import os
from twisted.python import log
from twisted.internet import task
from twisted.internet import protocol
from twisted.internet import reactor
from txportal.packet import cmcc, huawei
from toughlib import utils, logger
from toughlib import redis_cache
from toughwlan import models
from toughwlan.manage.settings import redis_conf
            self.transport.write(str(resp), (host, port))
        except:
            pass
            
    
    def datagramReceived(self, datagram, (host, port)):
        try:
            nas = self.get_nas(host)
            ac_addr = nas['ip_addr']
            ac_port = int(nas['ac_port'])
            secret = utils.safestr(nas['bas_secret'])
            _vendor= utils.safestr(nas['portal_vendor'])
            if _vendor not in ('cmccv1','cmccv2','huaweiv1','huaweiv2'):
                self.render_error(msg=u"AC server portal_vendor {0} not support ".format(_vendor))
                return

            vendor = client.PortalClient.vendors.get(_vendor)
            req = vendor.proto(
                secret=secret,
                packet=datagram,
                source=(host, port)
            )

            logger.info("Received portal packet from %s:%s: %s"%(host,port,utils.safestr(req)))
            if req.type in self.actions:
                self.actions[req.type](req, vendor, secret, (host, port))
            else:
                logger.error('Not support packet from ac host ' + host)

        except Exception as err:

# Path: toughwlan/models.py
# def get_metadata(db_engine):
# class SystemSession(DeclarativeBase):
# class SystemCache(DeclarativeBase):
# class TrwOperator(DeclarativeBase):
# class TrwOperatorRule(DeclarativeBase):
# class TrwParam(DeclarativeBase):
# class TrwIsp(DeclarativeBase):
# class TrwIspService(DeclarativeBase):
# class TrwBas(DeclarativeBase):
# class TrwDomain(DeclarativeBase):
# class TrwDomainAttr(DeclarativeBase):
# class TrwSsid(DeclarativeBase):
# class TrwRadius(DeclarativeBase):
# class TrwTemplate(DeclarativeBase):
# class TrwOperateLog(DeclarativeBase):
# class TrwOSTypes(DeclarativeBase):
# 
# Path: toughwlan/manage/settings.py
# def redis_conf(config):
#     eredis_url = os.environ.get("REDIS_URL")
#     eredis_port = os.environ.get("REDIS_PORT")
#     eredis_pwd = os.environ.get("REDIS_PWD")
#     eredis_db = os.environ.get("REDIS_DB")
# 
#     is_update = any([eredis_url,eredis_port,eredis_pwd,eredis_db])
# 
#     if eredis_url:
#         config['redis']['host'] = eredis_url
#     if eredis_port:
#         config['redis']['port'] = int(eredis_port)
#     if eredis_pwd:
#         config['redis']['passwd'] = eredis_pwd 
#     if eredis_db:
#         config['redis']['db'] = int(eredis_db)
#     if is_update:
#         config.save()
# 
#     return config['redis']


# Path: toughwlan/manage/portal/portald.py
import sys
import time
import os
from twisted.python import log
from twisted.internet import task
from twisted.internet import protocol
from twisted.internet import reactor
from txportal.packet import cmcc, huawei
from toughlib import utils, logger
from toughlib import redis_cache
from toughwlan import models
from toughwlan.manage.settings import redis_conf

            self.transport.write(str(resp), (host, port))
        except:
            pass
            
    
    def datagramReceived(self, datagram, (host, port)):
        try:
            nas = self.get_nas(host)
            ac_addr = nas['ip_addr']
            ac_port = int(nas['ac_port'])
            secret = utils.safestr(nas['bas_secret'])
            _vendor= utils.safestr(nas['portal_vendor'])
            if _vendor not in ('cmccv1','cmccv2','huaweiv1','huaweiv2'):
                self.render_error(msg=u"AC server portal_vendor {0} not support ".format(_vendor))
                return

            vendor = client.PortalClient.vendors.get(_vendor)
            req = vendor.proto(
                secret=secret,
                packet=datagram,
                source=(host, port)
            )

            logger.info("Received portal packet from %s:%s: %s"%(host,port,utils.safestr(req)))
            if req.type in self.actions:
                self.actions[req.type](req, vendor, secret, (host, port))
            else:
                logger.error('Not support packet from ac host ' + host)

        except Exception as err:

            logger.error('Dropping invalid packet from %s: %s' % ((host, port), utils.safestr(err)))