Skip to content

Commit

Permalink
feat: 给请求加上同源referer
Browse files Browse the repository at this point in the history
  • Loading branch information
chenhb23 committed Jul 17, 2024
1 parent 94a1649 commit f57dae4
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lanzouyun-pan",
"version": "3.5.1",
"version": "3.5.2",
"private": true,
"main": "build/main/index.js",
"description": "蓝奏云网盘客户端",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lanzouyun-pan",
"version": "3.5.1",
"version": "3.5.2",
"private": true,
"main": "build/main/index.js",
"description": "蓝奏云网盘客户端",
Expand Down
4 changes: 3 additions & 1 deletion src/common/core/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as http from '../http'
import cheerio from 'cheerio'
import {Matcher} from './matcher'
import type {Config} from '../../renderer/store/Config'
import store from '../store'

enum PROFILE_EL {
个性域名 = '个性域名', // domain
Expand Down Expand Up @@ -39,7 +40,7 @@ export async function profile() {
return {...prev, verification: $my('#phone_id', el).text().trim()}
}
return prev
}, {} as Pick<Config, 'domain' | 'lastLogin' | 'maxSize' | 'verification'> & {supportList: string[]})
}, {} as Pick<Config, 'referer' | 'domain' | 'lastLogin' | 'maxSize' | 'verification'> & {supportList: string[]})

const iframe = $main('iframe').attr('src')
const mainPage = await http.request.get(iframe).text()
Expand All @@ -48,5 +49,6 @@ export async function profile() {
return {
...profiles,
more: ajaxData,
referer: new URL(iframe, store.get('lanzouUrl')).href,
}
}
14 changes: 13 additions & 1 deletion src/common/http.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import got from 'got'
import {cookieJar, shareCookieJar} from './cookie'
import config from '../project.config'
import store from './store'
import {delay} from './util'
import electronApi from '../renderer/electronApi'
import {message} from 'antd'
import {config} from '../renderer/store/Config'

const base = got.extend({
headers: {
Expand Down Expand Up @@ -78,6 +78,18 @@ const base = got.extend({
export const request = got.extend(base, {
cookieJar,
prefixUrl: store.get('lanzouUrl'),
hooks: {
beforeRequest: [
options => {
if (config.referer && (!options.headers['referer'] || !options.headers['Referer'])) {
const url = typeof options.url === 'string' ? new URL(options.url) : options.url
if (url.origin === new URL(config.referer).origin) {
options.headers['referer'] = config.referer
}
}
},
],
},
})

export const share = got.extend(base, {
Expand Down
1 change: 1 addition & 0 deletions src/renderer/store/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type BaseProps<T> = Partial<Pick<T, {[P in keyof T]: T[P] extends (...args: any)

export class Config {
@persist domain = '' // https://wwn.lanzouf.com
@persist referer = ''
@persist lastLogin = '' // "2022-04-23 21:03:29"
// @persist('list') supportList: string[] = [] // ['tar']
@persist verification = '' // 188****8888
Expand Down
4 changes: 3 additions & 1 deletion src/renderer/store/task/UploadTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ export class UploadTask implements BaseTask {
const encoder = new FormDataEncoder(form)
return {
from: Readable.from(encoder),
to: http.request.stream.post('html5up.php', {headers: encoder.headers}),
to: http.request.stream.post('html5up.php', {
headers: {Referer: config.referer, ...encoder.headers},
}),
}
}

Expand Down

0 comments on commit f57dae4

Please sign in to comment.