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 {
ApiBody,
ApiCreatedResponse,
......@@ -48,11 +48,12 @@ export class MiddlewareController {
@ApiOkResponse({ type: StringReturnMessageDto })
async download(
@Body(new ValidationPipe({ transform: true })) mirrorDto: MirrorDto,
@Ip() ip: string,
) {
return new ReturnMessageDto(
200,
'success',
await this.middlewareService.download(mirrorDto),
await this.middlewareService.download(mirrorDto, ip),
);
}
}
......@@ -16,7 +16,7 @@ export class MiddlewareService extends ConsoleLogger {
async upload(urlDto: PostUrlDto): Promise<string> {
throw new BlankReturnMessageDto(404, 'Not Implemented').toException();
}
async download(mirror: MirrorDto): Promise<string> {
async download(mirror: MirrorDto, ip: string): Promise<string> {
return mirror.url;
}
}
import { IsString } from 'class-validator';
import { IsOptional, IsString } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';
export class MirrorDto {
@IsString()
@IsOptional()
@ApiProperty({ description: '文件标识路径。' })
path: string;
path?: string;
@IsString()
@ApiProperty({ description: '文件地址,middleware 曾经返回的。' })
url: string;
@IsString()
@IsOptional()
@ApiProperty({ description: 'middleware 标识符' })
middleware: string;
middleware?: string;
@IsString()
@IsOptional()
@ApiProperty({ description: '下载的客户端 IP' })
clientIp?: string;
}
......@@ -219,7 +219,7 @@ export class OneTwoThreeService extends MiddlewareService {
}
}
async getDownloadUrl(fileInfo: FileInfo) {
async getDownloadUrl(fileInfo: FileInfo, ip: string) {
const downloadReq = await lastValueFrom(
this.http.post<OTTResponse<UploadReq>>(
'https://www.123pan.com/api/file/download_info',
......@@ -228,6 +228,7 @@ export class OneTwoThreeService extends MiddlewareService {
responseType: 'json',
headers: {
Authorization: `Bearer ${await this.login()}`,
'X-Forwarded-For': ip,
},
},
),
......@@ -238,16 +239,19 @@ export class OneTwoThreeService extends MiddlewareService {
return downloadReq.data.data.DownloadUrl;
}
async parse302(url: string) {
async parse302(url: string, ip: string) {
if (url.includes('?params=')) {
const params = url.split('?params=')[1];
const decoded = Buffer.from(params, 'base64').toString('utf-8');
return this.parse302(decoded);
return this.parse302(decoded, ip);
}
const tmpLinkReq = await lastValueFrom(
this.http.head(url, {
maxRedirects: 0,
validateStatus: (c) => c === 302 || c === 301 || c === 200,
headers: {
'X-Forwarded-For': ip,
},
}),
);
if (tmpLinkReq.status !== 200) {
......@@ -256,10 +260,13 @@ export class OneTwoThreeService extends MiddlewareService {
return url;
}
async download(info) {
async download(info, ip: string) {
try {
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) {
this.error(`Download failed: ${e.message}`);
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