|
| 1 | +// 提交题解 |
| 2 | +const router = require("koa-router")(); |
| 3 | +const request = require('request'); |
| 4 | + |
| 5 | +const { |
| 6 | + set91Cookie, |
| 7 | + getLeetcodeCookie, |
| 8 | + success, |
| 9 | + fail |
| 10 | +} = require('./utils') |
| 11 | +const { submitUrl, baseUrl } = require('../config/index') |
| 12 | + |
| 13 | +// 设置响应头 |
| 14 | +router.options('/submitCode', async (ctx, next) => { |
| 15 | + ctx.set("Access-Control-Allow-Origin", "http://localhost:8080"); |
| 16 | + ctx.set("Access-Control-Allow-Credentials", "true"); |
| 17 | + ctx.set("Access-Control-Allow-Methods", "*"); |
| 18 | + ctx.set("Access-Control-Allow-Headers", "Content-Type,Access-Token"); |
| 19 | + ctx.set("Access-Control-Expose-Headers", "*"); |
| 20 | + ctx.body = 200; |
| 21 | + ctx.status = 200; |
| 22 | +}); |
| 23 | + |
| 24 | +// 设置响应头 |
| 25 | +router.post('/submitCode', async (ctx, next) => { |
| 26 | + ctx.set("Access-Control-Allow-Origin", "http://localhost:8080"); |
| 27 | + ctx.set("Access-Control-Allow-Credentials", "true"); |
| 28 | + ctx.set("Access-Control-Allow-Methods", "*"); |
| 29 | + ctx.set("Access-Control-Allow-Headers", "Content-Type,Access-Token"); |
| 30 | + ctx.set("Access-Control-Expose-Headers", "*"); |
| 31 | + await next(); |
| 32 | +}); |
| 33 | + |
| 34 | + |
| 35 | +// 用户提交题解 |
| 36 | +// 先校验cookie中是否有账号密码,没有就让用户先输入再提交 |
| 37 | +router.post('/submitCode', async (ctx, next) => { |
| 38 | + const userName = ctx.cookies.get('login') |
| 39 | + const passwd = ctx.cookies.get('password') |
| 40 | + if(!userName || !passwd){ |
| 41 | + return ctx.response.body = fail({ |
| 42 | + message: "请先提交账号名与密码后再提交" |
| 43 | + }); |
| 44 | + } |
| 45 | + await next(); |
| 46 | +}); |
| 47 | + |
| 48 | +// 先试着用cookie中的csrftoken提交下看是否成功, |
| 49 | +router.post('/submitCode', async (ctx, next) => { |
| 50 | + const login = ctx.cookies.get('login') |
| 51 | + const password = ctx.cookies.get('password') |
| 52 | + let LEETCODE_SESSION = ctx.cookies.get('LEETCODE_SESSION') |
| 53 | + let csrftoken = ctx.cookies.get('csrftoken') |
| 54 | + const problemData = formateSubmitData(ctx.request.body) |
| 55 | + |
| 56 | + // 如果这俩cookie有一个不存在就拿账号密码再去lc请求一个新的 |
| 57 | + if(!LEETCODE_SESSION || !csrftoken){ |
| 58 | + const newCookie = await getLeetcodeCookie({login, password}) |
| 59 | + LEETCODE_SESSION = newCookie.LEETCODE_SESSION |
| 60 | + csrftoken = newCookie.csrftoken |
| 61 | + set91Cookie({ |
| 62 | + csrftoken, |
| 63 | + LEETCODE_SESSION |
| 64 | + }, ctx) |
| 65 | + } |
| 66 | + |
| 67 | + // 先试着用cookie中的csrftoken提交下看是否成功, |
| 68 | + let result = await submitSolution(problemData, ctx) |
| 69 | + if(result.success){ |
| 70 | + set91Cookie({ |
| 71 | + csrftoken, |
| 72 | + LEETCODE_SESSION |
| 73 | + }, ctx) |
| 74 | + return ctx.body = success(result) |
| 75 | + } |
| 76 | + |
| 77 | + // 如果403就用账号密码获取最新csrftoken,再提交一遍 |
| 78 | + let { statusCode } = result |
| 79 | + if(statusCode === 403){ |
| 80 | + const newCookie = await getLeetcodeCookie({login, password}) |
| 81 | + LEETCODE_SESSION = newCookie.LEETCODE_SESSION |
| 82 | + csrftoken = newCookie.csrftoken |
| 83 | + |
| 84 | + let retryResult = await submitSolution(problemData, ctx) |
| 85 | + if(retryResult.success){ |
| 86 | + set91Cookie({ |
| 87 | + csrftoken, |
| 88 | + LEETCODE_SESSION |
| 89 | + }, ctx) |
| 90 | + return ctx.body = success(retryResult) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + // 如果还是失败,就提示用户重新输入账号名与密码 |
| 95 | + return ctx.response.body = fail({ |
| 96 | + message: "提交失败,请重新输入账号名与密码后再提交!" |
| 97 | + }); |
| 98 | +}); |
| 99 | + |
| 100 | +function submitSolution(problem, ctx){ |
| 101 | + const LEETCODE_SESSION = ctx.cookies.get('LEETCODE_SESSION') |
| 102 | + const csrftoken = ctx.cookies.get('csrftoken') |
| 103 | + const opts = {} |
| 104 | + opts.method = 'POST'; |
| 105 | + opts.url = submitUrl.replace('$slug', problem.slug); |
| 106 | + opts.headers = {}; |
| 107 | + opts.headers.Origin = baseUrl; |
| 108 | + opts.headers.Referer = problem.link; |
| 109 | + opts.headers.Cookie = `LEETCODE_SESSION=${LEETCODE_SESSION};csrftoken=${csrftoken};`; |
| 110 | + opts.headers['X-csrftoken'] = csrftoken; |
| 111 | + opts.headers['X-Requested-With'] = 'XMLHttpRequest'; |
| 112 | + opts.json = true; |
| 113 | + opts._delay = 1; // in seconds |
| 114 | + opts.body = problem || {}; |
| 115 | + |
| 116 | + return new Promise(res => { |
| 117 | + request(opts, function(e, resp, body) { |
| 118 | + if(e){ |
| 119 | + return res({success: false, statusCode: resp.statusCode}) |
| 120 | + } |
| 121 | + body.success = true |
| 122 | + body.statusCode = resp.statusCode |
| 123 | + return res(body) |
| 124 | + }); |
| 125 | + }) |
| 126 | +} |
| 127 | + |
| 128 | +// 整理提交题解时的数据格式 |
| 129 | +function formateSubmitData(problem = {}){ |
| 130 | + return Object.assign(problem, { |
| 131 | + judge_type: 'large', |
| 132 | + lang: problem.lang, |
| 133 | + question_id: parseInt(problem.id, 10), |
| 134 | + test_mode: false, |
| 135 | + typed_code: problem.code |
| 136 | + }) |
| 137 | +} |
| 138 | + |
| 139 | +module.exports = router; |
0 commit comments