Skip to content

Instantly share code, notes, and snippets.

@BANKA2017
Last active August 18, 2020 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BANKA2017/62582a6ab6abd18e22f1f6f0e949516b to your computer and use it in GitHub Desktop.
Save BANKA2017/62582a6ab6abd18e22f1f6f0e949516b to your computer and use it in GitHub Desktop.
百度扫码登录
<?php
/* KDboT
* @banka2017 & KD·NETWORK
*/
class getBDUSS{
public function getqrcode(){
$get_qrcode = json_decode(file_get_contents('https://passport.baidu.com/v2/api/getqrcode?lp=pc'),true);
if(isset($get_qrcode["imgurl"]) && isset($get_qrcode["sign"])){
$r = ["sign" => $get_qrcode["sign"], "imgurl" => $get_qrcode["imgurl"]];
}else{
$r = ["sign" => null, "imgurl" => null];
}
return $r;
}
public function get_real_bduss(string $sign){
$r = ["status" => false, "bduss" => ""];
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,'https://passport.baidu.com/channel/unicast?channel_id='.$sign.'&callback=');
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.78 Safari/537.36');
curl_setopt($ch,CURLOPT_TIMEOUT,10);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$f_bduss = curl_exec($ch);
curl_close($ch);
if ($f_bduss) {
$w_success = json_decode(str_replace(array("(",")"),'',$f_bduss),true);
if(!$w_success["errno"]){
$f_bduss = json_decode($w_success["channel_v"],true);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,'https://passport.baidu.com/v3/login/main/qrbdusslogin?bduss='.$f_bduss["v"]);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.78 Safari/537.36');
curl_setopt($ch,CURLOPT_HEADER,true);
curl_setopt($ch,CURLOPT_NOBODY,true);
curl_setopt($ch,CURLOPT_TIMEOUT,10);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$s_bduss = curl_exec($ch);
curl_close($ch);
if (preg_match('/BDUSS=([\w-]+);/',$s_bduss,$bduss)) {
$r["status"] = true;
$r["bduss"] = $bduss[1];
}
}
}
return $r;
}
}
'use strict'
//const ORIGIN = ''//Environment Variables
const AllowedReferrer = ''// 'a.com|b.com|c.com' multiple domains is supported
const init = {
status: 403,
headers: {
'content-type': 'application/json;charset=UTF-8',
'access-control-allow-origin': (typeof ORIGIN !== 'undefined') ? ORIGIN : '*',
'access-control-allow-methods': 'GET',
'X-XSS-Protection' :'1; mode=block',
'X-Frame-Options': 'sameorigin',
},
}
addEventListener('fetch', event => {
return event.respondWith(switchRouter(event.request.url, event))
})
async function switchRouter(url) {
const parseUrl = new URL(url)
const _searchParams = parseUrl.searchParams
let resp = {
"errno": -1,
"msg": "Forbidden",
"data": []
}
if (AllowedReferrer === '' || RegExp('(^|\.)(' + AllowedReferrer + ')$').test(parseUrl.host)) {
init.status = 200
switch (_searchParams.get("m")) {
case "getqrcode":
resp.data = await getqrcode()
if (resp.data.sign) {
resp.errno = 0
resp.msg = "Success"
}
break
case "getbduss":
resp.data = await getBduss(_searchParams.get("sign"))
if (!!resp.data.status === !!resp.data.bduss.length) {
resp.errno = 0
resp.msg = "Success"
} else {
resp.msg = "Invalid QR Code or timeout"
}
break
}
}
return new Response(JSON.stringify(resp), init)
}
async function getqrcode() {
const response = await (await fetch("https://passport.baidu.com/v2/api/getqrcode?lp=pc")).json()
return { sign: response.sign, imgurl: response.imgurl }
}
async function getBduss(sign) {
let resp = { status: 1, bduss: "", msg: "" }
let response = await (await fetch("https://passport.baidu.com/channel/unicast?channel_id=" + sign + "&callback=")).text()
if (response.length) {
const errno = parseInt(/"errno":([\-0-9]+)(?:,|})/.exec(response)[1])
if (errno === 0) {
const channel_v = JSON.parse(/"channel_v":"(.*)"}\)/.exec(response)[1].replace(/\\/gm, ''))
if (channel_v.status) {
resp.status = 0
resp.msg = "Continue"
} else {
const cookies = ((await fetch('https://passport.baidu.com/v3/login/main/qrbdusslogin?bduss=' + channel_v.v)).headers.get("set-cookie")).toString()
if (/BDUSS=([\w\-~=]+);/.test(cookies)) {
resp.status = 2
resp.msg = "Success"
resp.bduss = /BDUSS=([\w\-~=]+);/.exec(cookies)[1]
}
}
} else {
resp.status = errno
}
} else {
resp.status = -1
resp.msg = "Invalid QR Code"
}
return resp
}
@BANKA2017
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment