You need to sign in or sign up before continuing.
Commit e26cbc81 authored by nanahira's avatar nanahira

support running on other IP

parent 6b2f67a6
Pipeline #17475 passed with stages
in 2 minutes and 47 seconds
import { Body, Get, Patch, Post, ValidationPipe } from '@nestjs/common'; import { Body, Get, Ip, Patch, Post, ValidationPipe } from '@nestjs/common';
import { import {
ApiBody, ApiBody,
ApiCreatedResponse, ApiCreatedResponse,
...@@ -48,11 +48,12 @@ export class MiddlewareController { ...@@ -48,11 +48,12 @@ export class MiddlewareController {
@ApiOkResponse({ type: StringReturnMessageDto }) @ApiOkResponse({ type: StringReturnMessageDto })
async download( async download(
@Body(new ValidationPipe({ transform: true })) mirrorDto: MirrorDto, @Body(new ValidationPipe({ transform: true })) mirrorDto: MirrorDto,
@Ip() ip: string,
) { ) {
return new ReturnMessageDto( return new ReturnMessageDto(
200, 200,
'success', 'success',
await this.middlewareService.download(mirrorDto), await this.middlewareService.download(mirrorDto, ip),
); );
} }
} }
...@@ -16,7 +16,7 @@ export class MiddlewareService extends ConsoleLogger { ...@@ -16,7 +16,7 @@ export class MiddlewareService extends ConsoleLogger {
async upload(urlDto: PostUrlDto): Promise<string> { async upload(urlDto: PostUrlDto): Promise<string> {
throw new BlankReturnMessageDto(404, 'Not Implemented').toException(); throw new BlankReturnMessageDto(404, 'Not Implemented').toException();
} }
async download(mirror: MirrorDto): Promise<string> { async download(mirror: MirrorDto, ip: string): Promise<string> {
return mirror.url; return mirror.url;
} }
} }
import { IsString } from 'class-validator'; import { IsOptional, IsString } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger'; import { ApiProperty } from '@nestjs/swagger';
export class MirrorDto { export class MirrorDto {
@IsString() @IsString()
@IsOptional()
@ApiProperty({ description: '文件标识路径。' }) @ApiProperty({ description: '文件标识路径。' })
path: string; path?: string;
@IsString() @IsString()
@ApiProperty({ description: '文件地址,middleware 曾经返回的。' }) @ApiProperty({ description: '文件地址,middleware 曾经返回的。' })
url: string; url: string;
@IsString() @IsString()
@IsOptional()
@ApiProperty({ description: 'middleware 标识符' }) @ApiProperty({ description: 'middleware 标识符' })
middleware: string; middleware?: string;
@IsString()
@IsOptional()
@ApiProperty({ description: '下载的客户端 IP' })
clientIp?: string;
} }
...@@ -219,7 +219,7 @@ export class OneTwoThreeService extends MiddlewareService { ...@@ -219,7 +219,7 @@ export class OneTwoThreeService extends MiddlewareService {
} }
} }
async getDownloadUrl(fileInfo: FileInfo) { async getDownloadUrl(fileInfo: FileInfo, ip: string) {
const downloadReq = await lastValueFrom( const downloadReq = await lastValueFrom(
this.http.post<OTTResponse<UploadReq>>( this.http.post<OTTResponse<UploadReq>>(
'https://www.123pan.com/api/file/download_info', 'https://www.123pan.com/api/file/download_info',
...@@ -228,6 +228,7 @@ export class OneTwoThreeService extends MiddlewareService { ...@@ -228,6 +228,7 @@ export class OneTwoThreeService extends MiddlewareService {
responseType: 'json', responseType: 'json',
headers: { headers: {
Authorization: `Bearer ${await this.login()}`, Authorization: `Bearer ${await this.login()}`,
'X-Forwarded-For': ip,
}, },
}, },
), ),
...@@ -238,16 +239,19 @@ export class OneTwoThreeService extends MiddlewareService { ...@@ -238,16 +239,19 @@ export class OneTwoThreeService extends MiddlewareService {
return downloadReq.data.data.DownloadUrl; return downloadReq.data.data.DownloadUrl;
} }
async parse302(url: string) { async parse302(url: string, ip: string) {
if (url.includes('?params=')) { if (url.includes('?params=')) {
const params = url.split('?params=')[1]; const params = url.split('?params=')[1];
const decoded = Buffer.from(params, 'base64').toString('utf-8'); const decoded = Buffer.from(params, 'base64').toString('utf-8');
return this.parse302(decoded); return this.parse302(decoded, ip);
} }
const tmpLinkReq = await lastValueFrom( const tmpLinkReq = await lastValueFrom(
this.http.head(url, { this.http.head(url, {
maxRedirects: 0, maxRedirects: 0,
validateStatus: (c) => c === 302 || c === 301 || c === 200, validateStatus: (c) => c === 302 || c === 301 || c === 200,
headers: {
'X-Forwarded-For': ip,
},
}), }),
); );
if (tmpLinkReq.status !== 200) { if (tmpLinkReq.status !== 200) {
...@@ -256,10 +260,13 @@ export class OneTwoThreeService extends MiddlewareService { ...@@ -256,10 +260,13 @@ export class OneTwoThreeService extends MiddlewareService {
return url; return url;
} }
async download(info) { async download(info, ip: string) {
try { try {
const fileInfo = JSON.parse(info.url) as FileInfo; const fileInfo = JSON.parse(info.url) as FileInfo;
return await this.parse302(await this.getDownloadUrl(fileInfo)); return await this.parse302(
await this.getDownloadUrl(fileInfo, info.ip || ip),
info.ip || ip,
);
} catch (e) { } catch (e) {
this.error(`Download failed: ${e.message}`); this.error(`Download failed: ${e.message}`);
throw new BlankReturnMessageDto(500, e.message).toException(); throw new BlankReturnMessageDto(500, e.message).toException();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment