# 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):


import sys, struct
from twisted.internet import reactor
from twisted.internet import defer
from toughlib.dbengine import get_engine
from toughlib import logger, utils,dispatch
from sqlalchemy.orm import scoped_session, sessionmaker
from toughwlan import models
from twisted.names import client, dns
#!/usr/bin/env python
# coding=utf-8

class DDNSProc:

    def __init__(self, config, dbengine=None):
        self.config = config
        self.db = scoped_session(sessionmaker(bind=dbengine, autocommit=False, autoflush=True))

    @defer.inlineCallbacks
    def process(self):
        conn = self.db()
        try:
            nas_list = conn.query(models.TrwBas)
            for nas in nas_list:
                if not nas.dns_name:
                    continue
                results, _, _ = yield client.lookupAddress(nas.dns_name)
                if not results:
                    logger.error("domain {0} resolver empty".format(nas.dns_name))

                if results[0].type == dns.A:
                    ipaddr = ".".join(str(i) for i in struct.unpack("BBBB", results[0].payload.address))
                    if ipaddr:
                        nas.ip_addr = ipaddr
                        conn.commit()

# 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/ddns_task.py
import sys, struct
from twisted.internet import reactor
from twisted.internet import defer
from toughlib.dbengine import get_engine
from toughlib import logger, utils,dispatch
from sqlalchemy.orm import scoped_session, sessionmaker
from toughwlan import models
from twisted.names import client, dns

#!/usr/bin/env python
# coding=utf-8

class DDNSProc:

    def __init__(self, config, dbengine=None):
        self.config = config
        self.db = scoped_session(sessionmaker(bind=dbengine, autocommit=False, autoflush=True))

    @defer.inlineCallbacks
    def process(self):
        conn = self.db()
        try:
            nas_list = conn.query(models.TrwBas)
            for nas in nas_list:
                if not nas.dns_name:
                    continue
                results, _, _ = yield client.lookupAddress(nas.dns_name)
                if not results:
                    logger.error("domain {0} resolver empty".format(nas.dns_name))

                if results[0].type == dns.A:
                    ipaddr = ".".join(str(i) for i in struct.unpack("BBBB", results[0].payload.address))
                    if ipaddr:
                        nas.ip_addr = ipaddr
                        conn.commit()

                        logger.info("domain {0} resolver {1}  success".format(nas.dns_name,ipaddr))