const TLSARecord = require('../index.js')
const https = require('https')

module.exports = (req, res) => {
  let body = ''
  res.setHeader('Content-Type', 'application/json')
  if (req.method === 'POST') {
    req.on('data', chunk => { body += chunk.toString() })
    req.on('end', () => {
        body = JSON.parse(body)
        https.get('https://' + body.domain, (resp) => {
            const tlsaRecord = new TLSARecord(resp.connection.getPeerCertificate().raw)
            const result = {  
                record: tlsaRecord.toString(
                    parseInt(body.usage),
                    parseInt(body.selector),
                    parseInt(body.matchingType),
                    body.domain,
                    parseInt(body.port),
                    body.protocol),
                status: 'ok'
            }
            res.end(JSON.stringify(result))
        }).on("error", err => res.end('{"status":"error"}'))
    })
  } else { res.end('{}') }
}

@@ -116,16 +116,166 @@
 dy = ''%0A
+  res.setHeader('Access-Control-Allow-Origin', '*')%0A  res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')%0A
   res.se
@@ -1109,12 +1109,8 @@
 end(
-'%7B%7D'
 ) %7D%0A

enable cors