Skip to content

Commit

Permalink
parse user agent from header
Browse files Browse the repository at this point in the history
  • Loading branch information
hanydd committed Oct 30, 2024
1 parent 0774318 commit 5bdd741
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/routes/postPortVideo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { config } from "../config";
import * as youtubeID from "../utils/youtubeID";
import * as biliID from "../utils/bilibiliID";
import { getVideoDetails } from "../utils/getVideoDetails";
import { parseUserAgent } from "../utils/userAgent";
import { parseUserAgentFromHeaders } from "../utils/userAgent";
import { getMatchVideoUUID, getPortSegmentUUID } from "../utils/getSubmissionUUID";
import { isUserVIP } from "../utils/isUserVIP";
import { Logger } from "../utils/logger";
Expand Down Expand Up @@ -175,7 +175,7 @@ export async function postPortVideo(req: Request, res: Response): Promise<Respon

// prepare to be saved
const isVIP = await isUserVIP(userID);
const userAgent = req.query.userAgent ?? req.body.userAgent ?? parseUserAgent(req.get("user-agent")) ?? "";
const userAgent = req.query.userAgent ?? req.body.userAgent ?? parseUserAgentFromHeaders(req.headers) ?? "";
const timeSubmitted = Date.now();
const matchVideoUUID = getMatchVideoUUID(bvID, ytbID, userID, paramBiliDuration, ytbDuration, timeSubmitted);
const startingVotes = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/routes/postSkipSegments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { getReputation } from "../utils/reputation";
import { HashedUserID, UserID } from "../types/user.model";
import { isUserVIP } from "../utils/isUserVIP";
import { isUserTempVIP } from "../utils/isUserTempVIP";
import { parseUserAgent } from "../utils/userAgent";
import { parseUserAgentFromHeaders } from "../utils/userAgent";
import { getService } from "../utils/getService";
import axios from "axios";
import { vote } from "./voteOnSponsorTime";
Expand Down Expand Up @@ -485,7 +485,7 @@ function preprocessInput(req: Request) {
segment.segment = segment.segment.map((time) => typeof segment.segment[0] === "string" ? time?.replace(",", ".") : time);
});

const userAgent = req.query.userAgent ?? req.body.userAgent ?? parseUserAgent(req.get("user-agent")) ?? "";
const userAgent = req.query.userAgent ?? req.body.userAgent ?? parseUserAgentFromHeaders(req.headers) ?? "";

return { videoID, userID, service, videoDuration, videoDurationParam, segments, userAgent };
}
Expand Down
17 changes: 6 additions & 11 deletions src/utils/userAgent.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
export function parseUserAgent(userAgent: string): string {
const ua = userAgent.toLowerCase();
import { IncomingHttpHeaders } from "http";

if (ua.match(/(com.google.android.youtube\/)|(com.vanced.android.youtube\/)|(^YouTube\/)|(^Dalvik\/)/)) {
return `Vanced/${ua.match(/.android.youtube\/([^\s]+)/)[1]}`;
export function parseUserAgentFromHeaders(headers: IncomingHttpHeaders): string {
if (!headers.origin) {
return "";
}

if (ua.match(/(mpv_sponsorblock\/)|(^python-requests)|(^GuzzleHttp\/)|(^PostmanRuntime\/)/)) {
return ua;
}

return "";
}
return headers.origin.split("://").at(-1) + (headers["x-ext-version"] ? `/${headers["x-ext-version"]}` : "");
}

0 comments on commit 5bdd741

Please sign in to comment.