From f207cc328d687e4ce5b4e45a786042fbde8fde3b Mon Sep 17 00:00:00 2001 From: Stephen Zhou <38493346+hyoban@users.noreply.github.com> Date: Wed, 19 Nov 2025 21:54:29 +0800 Subject: [PATCH] feat: secure image proxy in plan --- .../src/components/ui/media/Media.tsx | 2 +- .../layer/renderer/src/lib/img-proxy.ts | 16 ++++++++++++- .../entry-column/Items/media-gallery.tsx | 2 +- .../renderer/src/modules/feed/feed-icon.tsx | 3 ++- apps/mobile/src/components/ui/image/utils.ts | 3 ++- apps/mobile/src/lib/img-proxy.ts | 23 +++++++++++++++++++ locales/settings/en.json | 1 + locales/settings/ja.json | 1 + locales/settings/zh-CN.json | 1 + locales/settings/zh-TW.json | 1 + packages/internal/utils/src/img-proxy.ts | 22 ++++++++++++++++-- 11 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 apps/mobile/src/lib/img-proxy.ts diff --git a/apps/desktop/layer/renderer/src/components/ui/media/Media.tsx b/apps/desktop/layer/renderer/src/components/ui/media/Media.tsx index c2f54629d..d93c8cfdd 100644 --- a/apps/desktop/layer/renderer/src/components/ui/media/Media.tsx +++ b/apps/desktop/layer/renderer/src/components/ui/media/Media.tsx @@ -1,5 +1,4 @@ import { nextFrame } from "@follow/utils/dom" -import { getImageProxyUrl } from "@follow/utils/img-proxy" import { cn } from "@follow/utils/utils" import { ErrorBoundary } from "@sentry/react" import { useForceUpdate } from "motion/react" @@ -9,6 +8,7 @@ import { memo, use, useEffect, useMemo, useRef, useState } from "react" import { Blurhash, BlurhashCanvas } from "react-blurhash" import { useEventCallback } from "usehooks-ts" +import { getImageProxyUrl } from "~/lib/img-proxy" import { saveImageDimensionsToDb } from "~/store/image/db" import { useMediaContainerWidth, usePreviewMedia } from "./hooks" diff --git a/apps/desktop/layer/renderer/src/lib/img-proxy.ts b/apps/desktop/layer/renderer/src/lib/img-proxy.ts index b186c51d0..195f657d7 100644 --- a/apps/desktop/layer/renderer/src/lib/img-proxy.ts +++ b/apps/desktop/layer/renderer/src/lib/img-proxy.ts @@ -1,9 +1,23 @@ import { WEB_BUILD } from "@follow/shared/constants" -import { replaceImgUrlIfNeed as replaceImgUrlIfNeedUtils } from "@follow/utils/img-proxy" +import { role as getUserRole } from "@follow/store/user/getters" +import { + getImageProxyUrl as getImageProxyUrlUtils, + replaceImgUrlIfNeed as replaceImgUrlIfNeedUtils, +} from "@follow/utils/img-proxy" export const replaceImgUrlIfNeed = (url?: string) => { return replaceImgUrlIfNeedUtils({ url, inBrowser: WEB_BUILD, + userRole: getUserRole(), + }) +} + +export const getImageProxyUrl = ( + params: Omit[0], "userRole">, +) => { + return getImageProxyUrlUtils({ + ...params, + userRole: getUserRole(), }) } diff --git a/apps/desktop/layer/renderer/src/modules/entry-column/Items/media-gallery.tsx b/apps/desktop/layer/renderer/src/modules/entry-column/Items/media-gallery.tsx index a72b1a77b..319952462 100644 --- a/apps/desktop/layer/renderer/src/modules/entry-column/Items/media-gallery.tsx +++ b/apps/desktop/layer/renderer/src/modules/entry-column/Items/media-gallery.tsx @@ -1,10 +1,10 @@ import { useEntry } from "@follow/store/entry/hooks" -import { getImageProxyUrl } from "@follow/utils/img-proxy" import { cn } from "@follow/utils/utils" import { useMemo } from "react" import { usePreviewMedia } from "~/components/ui/media/hooks" import { Media } from "~/components/ui/media/Media" +import { getImageProxyUrl } from "~/lib/img-proxy" import { jotaiStore } from "~/lib/jotai" import { socialMediaContentWidthAtom } from "../atoms/social-media-content-width" diff --git a/apps/desktop/layer/renderer/src/modules/feed/feed-icon.tsx b/apps/desktop/layer/renderer/src/modules/feed/feed-icon.tsx index fd62bc16b..d865bea80 100644 --- a/apps/desktop/layer/renderer/src/modules/feed/feed-icon.tsx +++ b/apps/desktop/layer/renderer/src/modules/feed/feed-icon.tsx @@ -2,13 +2,14 @@ import { PlatformIcon } from "@follow/components/ui/platform-icon/index.jsx" import type { FeedModel } from "@follow/store/feed/types" import { getBackgroundGradient } from "@follow/utils/color" -import { getImageProxyUrl } from "@follow/utils/img-proxy" import { cn, getUrlIcon } from "@follow/utils/utils" import * as AvatarPrimitive from "@radix-ui/react-avatar" import { m } from "motion/react" import type { ReactNode } from "react" import { useMemo } from "react" +import { getImageProxyUrl } from "~/lib/img-proxy" + const { Avatar, AvatarFallback, AvatarImage } = AvatarPrimitive // Size-responsive border radius utility function diff --git a/apps/mobile/src/components/ui/image/utils.ts b/apps/mobile/src/components/ui/image/utils.ts index 1f10bb744..4631a7da1 100644 --- a/apps/mobile/src/components/ui/image/utils.ts +++ b/apps/mobile/src/components/ui/image/utils.ts @@ -1,5 +1,5 @@ import { createBuildSafeHeaders } from "@follow/utils/headers" -import { getImageProxyUrl, IMAGE_PROXY_URL } from "@follow/utils/img-proxy" +import { IMAGE_PROXY_URL } from "@follow/utils/img-proxy" import ImageEditor from "@react-native-community/image-editor" import * as FileSystem from "expo-file-system" import type { ImageProps, ImageSource } from "expo-image" @@ -8,6 +8,7 @@ import * as Sharing from "expo-sharing" import { useCallback } from "react" import { Image } from "react-native" +import { getImageProxyUrl } from "@/src/lib/img-proxy" import { isAndroid, isNative } from "@/src/lib/platform" import { proxyEnv } from "@/src/lib/proxy-env" import { toast } from "@/src/lib/toast" diff --git a/apps/mobile/src/lib/img-proxy.ts b/apps/mobile/src/lib/img-proxy.ts new file mode 100644 index 000000000..3f97566ba --- /dev/null +++ b/apps/mobile/src/lib/img-proxy.ts @@ -0,0 +1,23 @@ +import { role as getUserRole } from "@follow/store/user/getters" +import { + getImageProxyUrl as getImageProxyUrlUtils, + replaceImgUrlIfNeed as replaceImgUrlIfNeedUtils, +} from "@follow/utils/img-proxy" + +export const getImageProxyUrl = ( + params: Omit[0], "userRole">, +) => { + return getImageProxyUrlUtils({ + ...params, + userRole: getUserRole(), + }) +} + +export const replaceImgUrlIfNeed = ( + params: Omit[0], "userRole">, +) => { + return replaceImgUrlIfNeedUtils({ + ...params, + userRole: getUserRole(), + }) +} diff --git a/locales/settings/en.json b/locales/settings/en.json index e502bea10..68a5546d2 100644 --- a/locales/settings/en.json +++ b/locales/settings/en.json @@ -583,6 +583,7 @@ "plan.features.MAX_SUBSCRIPTIONS": "Feed Subscriptions", "plan.features.PRIORITY_SUPPORT": "Priority Support", "plan.features.PRIVATE_SUBSCRIPTION": "Private Subscriptions", + "plan.features.SECURE_IMAGE_PROXY": "Secure Image Proxy", "privacy.privacy": "Privacy", "privacy.terms": "Terms", "profile.avatar.cropInstructions": "Drag the crop area to adjust your avatar", diff --git a/locales/settings/ja.json b/locales/settings/ja.json index e3f8d6050..869653624 100644 --- a/locales/settings/ja.json +++ b/locales/settings/ja.json @@ -578,6 +578,7 @@ "plan.features.MAX_SUBSCRIPTIONS": "フィードサブスクリプション", "plan.features.PRIORITY_SUPPORT": "優先サポート", "plan.features.PRIVATE_SUBSCRIPTION": "プライベートサブスクリプション", + "plan.features.SECURE_IMAGE_PROXY": "セキュア画像プロキシ", "privacy.privacy": "プライバシー", "privacy.terms": "利用規約", "profile.avatar.cropInstructions": "ドラッグしてトリミングエリアを調整します", diff --git a/locales/settings/zh-CN.json b/locales/settings/zh-CN.json index 10f3f2d54..c5dab244a 100644 --- a/locales/settings/zh-CN.json +++ b/locales/settings/zh-CN.json @@ -577,6 +577,7 @@ "plan.features.MAX_SUBSCRIPTIONS": "订阅源数量", "plan.features.PRIORITY_SUPPORT": "优先支持", "plan.features.PRIVATE_SUBSCRIPTION": "私有订阅", + "plan.features.SECURE_IMAGE_PROXY": "安全图片代理", "privacy.privacy": "隐私政策", "privacy.terms": "服务条款", "profile.avatar.cropInstructions": "拖动裁剪区域以调整头像", diff --git a/locales/settings/zh-TW.json b/locales/settings/zh-TW.json index 2e24b705c..42be8ae47 100644 --- a/locales/settings/zh-TW.json +++ b/locales/settings/zh-TW.json @@ -583,6 +583,7 @@ "plan.features.MAX_SUBSCRIPTIONS": "訂閱源數量", "plan.features.PRIORITY_SUPPORT": "優先支援", "plan.features.PRIVATE_SUBSCRIPTION": "私有訂閱", + "plan.features.SECURE_IMAGE_PROXY": "安全圖片代理", "privacy.privacy": "隱私政策", "privacy.terms": "服務條款", "profile.avatar.cropInstructions": "拖曳裁切區域以調整你的頭像", diff --git a/packages/internal/utils/src/img-proxy.ts b/packages/internal/utils/src/img-proxy.ts index 62494173d..ac4c1fb0e 100644 --- a/packages/internal/utils/src/img-proxy.ts +++ b/packages/internal/utils/src/img-proxy.ts @@ -1,5 +1,10 @@ export const IMAGE_PROXY_URL = "https://webp.follow.is" +const canUseImageProxy = (userRole?: string | null): boolean => { + if (!userRole) return false + return userRole === "plus" || userRole === "pro" +} + export const imageRefererMatches = [ { url: /^https:\/\/\w+\.sinaimg\.cn/, @@ -40,15 +45,28 @@ export const getImageProxyUrl = ({ url, width, height, + userRole, }: { url: string width?: number height?: number + userRole?: string | null }) => { + if (!canUseImageProxy(userRole)) { + return url + } return `${IMAGE_PROXY_URL}?url=${encodeURIComponent(url)}&width=${width ? Math.round(width) : ""}&height=${height ? Math.round(height) : ""}` } -export const replaceImgUrlIfNeed = ({ url, inBrowser }: { url?: string; inBrowser?: boolean }) => { +export const replaceImgUrlIfNeed = ({ + url, + inBrowser, + userRole, +}: { + url?: string + inBrowser?: boolean + userRole?: string | null +}) => { if (!url) return url for (const rule of webpCloudPublicServicesMatches) { @@ -59,7 +77,7 @@ export const replaceImgUrlIfNeed = ({ url, inBrowser }: { url?: string; inBrowse for (const rule of imageRefererMatches) { if ((inBrowser || rule.force) && rule.url.test(url)) { - return getImageProxyUrl({ url, width: 0, height: 0 }) + return getImageProxyUrl({ url, width: 0, height: 0, userRole }) } } return url