CoolFace
Apppublic

Sakky1/bingo

sourceHugging Facemitupdated 3y agoView on Hugging Face
0likes
create.ts48 linesDownload Raw Back to api
1'use server'2 3import { NextApiRequest, NextApiResponse } from 'next'4import { fetch, debug } from '@/lib/isomorphic'5import { createHeaders, randomIP } from '@/lib/utils'6import { sleep } from '@/lib/bots/bing/utils'7 8const API_ENDPOINT = 'https://www.bing.com/turing/conversation/create'9// const API_ENDPOINT = 'https://edgeservices.bing.com/edgesvc/turing/conversation/create';10 11export default async function handler(req: NextApiRequest, res: NextApiResponse) {12  try {13    let count = 014    const headers = createHeaders(req.cookies)15    do {16      headers['x-forwarded-for'] = headers['x-forwarded-for'] || randomIP()17      debug(`try ${count+1}`, headers['x-forwarded-for'])18      const response = await fetch(API_ENDPOINT, { method: 'GET', headers })19      if (response.status === 200) {20        res.setHeader('set-cookie', [headers.cookie, `BING_IP=${headers['x-forwarded-for']}`]21          .map(cookie => `${cookie}; Max-Age=${86400 * 30}; Path=/; SameSite=None; Secure`))22        debug('headers', headers)23        res.writeHead(200, {24          'Content-Type': 'application/json',25        })26        res.end(await response.text())27        return28      }29      await sleep(2000)30      headers['x-forwarded-for'] = ''31    } while(count++ < 10)32    res.end(JSON.stringify({33      result: {34        value: 'TryLater',35        message: `Please try again after a while`36      }37    }))38  } catch (e) {39    console.log('error', e)40    return res.end(JSON.stringify({41      result: {42        value: 'UnauthorizedRequest',43        message: `${e}`44      }45    }))46  }47}48