<commit_before>/**
 * @author <NAME>
 * @since v1
 */
load('mod/core/context.js')
load('mod/utils/auth_helper.js')
load('mod/utils/acl_helper.js')
load('mod/utils/domain_utils.js')
load('mod/core/resources.js')

function Processor(sipProvider, sipStack, headerFactory, messageFactory, addressFactory, contactHeader, locationService,
    registrarService, accountManagerService, resourcesAPI, config) {
    //const config = config
    const InetAddress = Packages.java.net.InetAddress
    const SipListener = Packages.javax.sip.SipListener
    const Request = Packages.javax.sip.message.Request
    const Response = Packages.javax.sip.message.Response
    const RouteHeader = Packages.javax.sip.header.RouteHeader
    const ToHeader = Packages.javax.sip.header.ToHeader
    const ContactHeader = Packages.javax.sip.header.ContactHeader
    const ExpiresHeader = Packages.javax.sip.header.ExpiresHeader
    const ViaHeader = Packages.javax.sip.header.ViaHeader
    const CSeqHeader = Packages.javax.sip.header.CSeqHeader
    const AuthorizationHeader = Packages.javax.sip.header.AuthorizationHeader
    const LogManager = Packages.org.apache.logging.log4j.LogManager

    const LOG = LogManager.getLogger()
    const ctxtList = new java.util.ArrayList()
    const localhost = InetAddress.getLocalHost().getHostAddress()
    const authHelper =  new AuthHelper(headerFactory)

    const defaultDomainAcl = config.defaultDomainAcl

    function register(request, transaction) {
        const contactHeader = request.getHeader(ContactHeader.NAME)
        const contactURI = contactHeader.getAddress().getURI()
        const expH = request.getHeader(ExpiresHeader.NAME) || contactHeader.getExpires()
        const authHeader = request.getHeader(AuthorizationHeader.NAME)

        if (authHeader == null) {
            const unauthorized = messageFactory.createResponse(Response.UNAUTHORIZED, request)
            unauthorized.addHeader(authHelper.generateChallenge())
            transaction.sendResponse(unauthorized)
            LOG.trace(unauthorized)
        } else {
            if (registrarService.register(request)) {
                const ok = messageFactory.createResponse(Response.OK, request)
                ok.addHeader(contactHeader)
                ok.addHeader(expH)
                transaction.sendResponse(ok)
                LOG.trace("\n" + ok)
            } else {
                const unauthorized = messageFactory.createResponse(Response.UNAUTHORIZED, request)
                unauthorized.addHeader(authHelper.generateChallenge(headerFactory))
                transaction.sendResponse(unauthorized)
                LOG.trace(unauthorized)
            }
        }
    }

    function cancel(request, st) {
        const iterator = ctxtList.iterator()

        while (iterator.hasNext()) {
            const ctxt = iterator.next()
            if (ctxt.st.getBranchId()
                .equals(st.getBranchId())) {

                let originRequest = ctxt.rin
                let originResponse = messageFactory.createResponse(Response.REQUEST_TERMINATED, originRequest)
                let cancelResponse = messageFactory.createResponse(Response.OK, request)
                let cancelRequest = ctxt.ct.createCancel()
                let ct = sipProvider.getNewClientTransaction(cancelRequest)

                ctxt.st.sendResponse(originResponse)
                st.sendResponse(cancelResponse)
                ct.sendRequest()

                LOG.trace(originResponse)
                LOG.trace(cancelResponse)
                LOG.trace(cancelRequest)
            }
        }
    }

    function unavailable(request, transaction) {
        LOG.debug("Unable to find contact: " + request.getHeader(ContactHeader.NAME))
        transaction.sendResponse(messageFactory.createResponse(Response.NOT_FOUND, request))
    }

    function reject(request, transaction) {
        LOG.debug("Connection rejected: " + request.getHeader(ContactHeader.NAME))
        transaction.sendResponse(messageFactory.createResponse(Response.UNAUTHORIZED, request))
    }

    this.listener = new SipListener() {
        processRequest: e => {
            const rin = e.getRequest()
            const method = rin.getMethod()
            const routeHeader = rin.getHeader(RouteHeader.NAME)
            const toHeader = rin.getHeader(ToHeader.NAME)
            const tgtURI = toHeader.getAddress().getURI()
            const contactHeader = rin.getHeader(ContactHeader.NAME)

            let st = e.getServerTransaction()
            let proxyHost

            // Edge proxy
            if (routeHeader != null) {
                const sipURI = routeHeader.getAddress().getURI()
                proxyHost = sipURI.getHost()
            } else {
                proxyHost = localhost;
            }

            if (st == null && !method.equals(Request.ACK)) {
                st = sipProvider.getNewServerTransaction(rin)
            }

            const domain = resourcesAPI.findDomain(tgtURI.getHost())
            if (domain == null) throw Packages.java.lang.RuntimeException('Unregistered domain ' + tgtURI.getHost())

            if(!new DomainUtil(domain, defaultDomainAcl).isDomainAllow(domain, tgtURI.getHost())) {
                LOG.trace("Host " + contactURI.getHost() + " has been rejected by " + domain.uri)
                reject(rin, st)
            }

            if (method.equals(Request.REGISTER)) {
                register(rin, st)
            } else if(method.equals(Request.CANCEL)) {
                cancel(rin, st)
            } else {
                const rout = rin.clone()

                // Last proxy in route
                if (proxyHost.equals(localhost)) {
                    const viaHeader = headerFactory.createViaHeader(proxyHost, config.port, 'udp', null)
                    rout.removeFirst(RouteHeader.NAME)
                    rout.addFirst(viaHeader)
                }

                const uri = locationService.get(tgtURI)

                if (uri == null) {
                    unavailable(rin, st)
                    return
                }

                rout.setRequestURI(uri)

                // Not need transaction
                if(method.equals(Request.ACK)) {<commit_msg>Reject request if domain does not exist<commit_after>/**
 * @author <NAME>
 * @since v1
 */
load('mod/core/context.js')
load('mod/utils/auth_helper.js')
load('mod/utils/acl_helper.js')
load('mod/utils/domain_utils.js')
load('mod/core/resources.js')

function Processor(sipProvider, sipStack, headerFactory, messageFactory, addressFactory, contactHeader, locationService,
    registrarService, accountManagerService, resourcesAPI, config) {
    //const config = config
    const InetAddress = Packages.java.net.InetAddress
    const SipListener = Packages.javax.sip.SipListener
    const Request = Packages.javax.sip.message.Request
    const Response = Packages.javax.sip.message.Response
    const RouteHeader = Packages.javax.sip.header.RouteHeader
    const ToHeader = Packages.javax.sip.header.ToHeader
    const ContactHeader = Packages.javax.sip.header.ContactHeader
    const ExpiresHeader = Packages.javax.sip.header.ExpiresHeader
    const ViaHeader = Packages.javax.sip.header.ViaHeader
    const CSeqHeader = Packages.javax.sip.header.CSeqHeader
    const AuthorizationHeader = Packages.javax.sip.header.AuthorizationHeader
    const LogManager = Packages.org.apache.logging.log4j.LogManager

    const LOG = LogManager.getLogger()
    const ctxtList = new java.util.ArrayList()
    const localhost = InetAddress.getLocalHost().getHostAddress()
    const authHelper =  new AuthHelper(headerFactory)

    const defaultDomainAcl = config.defaultDomainAcl

    function register(request, transaction) {
        const contactHeader = request.getHeader(ContactHeader.NAME)
        const contactURI = contactHeader.getAddress().getURI()
        const expH = request.getHeader(ExpiresHeader.NAME) || contactHeader.getExpires()
        const authHeader = request.getHeader(AuthorizationHeader.NAME)

        if (authHeader == null) {
            const unauthorized = messageFactory.createResponse(Response.UNAUTHORIZED, request)
            unauthorized.addHeader(authHelper.generateChallenge())
            transaction.sendResponse(unauthorized)
            LOG.trace(unauthorized)
        } else {
            if (registrarService.register(request)) {
                const ok = messageFactory.createResponse(Response.OK, request)
                ok.addHeader(contactHeader)
                ok.addHeader(expH)
                transaction.sendResponse(ok)
                LOG.trace("\n" + ok)
            } else {
                const unauthorized = messageFactory.createResponse(Response.UNAUTHORIZED, request)
                unauthorized.addHeader(authHelper.generateChallenge(headerFactory))
                transaction.sendResponse(unauthorized)
                LOG.trace(unauthorized)
            }
        }
    }

    function cancel(request, st) {
        const iterator = ctxtList.iterator()

        while (iterator.hasNext()) {
            const ctxt = iterator.next()
            if (ctxt.st.getBranchId()
                .equals(st.getBranchId())) {

                let originRequest = ctxt.rin
                let originResponse = messageFactory.createResponse(Response.REQUEST_TERMINATED, originRequest)
                let cancelResponse = messageFactory.createResponse(Response.OK, request)
                let cancelRequest = ctxt.ct.createCancel()
                let ct = sipProvider.getNewClientTransaction(cancelRequest)

                ctxt.st.sendResponse(originResponse)
                st.sendResponse(cancelResponse)
                ct.sendRequest()

                LOG.trace(originResponse)
                LOG.trace(cancelResponse)
                LOG.trace(cancelRequest)
            }
        }
    }

    function unavailable(request, transaction) {
        LOG.debug("Unable to find contact: " + request.getHeader(ContactHeader.NAME))
        transaction.sendResponse(messageFactory.createResponse(Response.NOT_FOUND, request))
    }

    function reject(request, transaction) {
        LOG.debug("Connection rejected: " + request.getHeader(ContactHeader.NAME))
        transaction.sendResponse(messageFactory.createResponse(Response.UNAUTHORIZED, request))
    }

    this.listener = new SipListener() {
        processRequest: e => {
            const rin = e.getRequest()
            const method = rin.getMethod()
            const routeHeader = rin.getHeader(RouteHeader.NAME)
            const toHeader = rin.getHeader(ToHeader.NAME)
            const tgtURI = toHeader.getAddress().getURI()
            const contactHeader = rin.getHeader(ContactHeader.NAME)

            let st = e.getServerTransaction()
            let proxyHost

            // Edge proxy
            if (routeHeader != null) {
                const sipURI = routeHeader.getAddress().getURI()
                proxyHost = sipURI.getHost()
            } else {
                proxyHost = localhost;
            }

            if (st == null && !method.equals(Request.ACK)) {
                st = sipProvider.getNewServerTransaction(rin)
            }

            const domain = resourcesAPI.findDomain(tgtURI.getHost())
            if (domain == null) reject(rin, st)

            if(!new DomainUtil(domain, defaultDomainAcl).isDomainAllow(domain, tgtURI.getHost())) {
                LOG.trace("Host " + contactURI.getHost() + " has been rejected by " + domain.uri)
                reject(rin, st)
            }

            if (method.equals(Request.REGISTER)) {
                register(rin, st)
            } else if(method.equals(Request.CANCEL)) {
                cancel(rin, st)
            } else {
                const rout = rin.clone()

                // Last proxy in route
                if (proxyHost.equals(localhost)) {
                    const viaHeader = headerFactory.createViaHeader(proxyHost, config.port, 'udp', null)
                    rout.removeFirst(RouteHeader.NAME)
                    rout.addFirst(viaHeader)
                }

                const uri = locationService.get(tgtURI)

                if (uri == null) {
                    unavailable(rin, st)
                    return
                }

                rout.setRequestURI(uri)

                // Not need transaction
                if(method.equals(Request.ACK)) {
{'fraction_non_alphanumeric': 0.09450213314014329, 'fraction_numerical': 0.0003219834178539805, 'mean_word_length': 2.194651581383389, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3267812', 'n_tokens_mistral': 3308, 'n_tokens_neox': 3188, 'n_words': 582}