From f64cd796b1c44fc24d5c456b1f40b6b32dc651df Mon Sep 17 00:00:00 2001 From: JiPai Date: Tue, 21 May 2024 16:08:12 +0800 Subject: [PATCH 1/2] fix: fix ip string isn't pure --- src/pages/Requests/components/RequestModal.tsx | 4 ++-- src/pages/Scripting/Evaluate/index.tsx | 1 - src/utils/index.ts | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/pages/Requests/components/RequestModal.tsx b/src/pages/Requests/components/RequestModal.tsx index 0fc6c72..b5f3afc 100644 --- a/src/pages/Requests/components/RequestModal.tsx +++ b/src/pages/Requests/components/RequestModal.tsx @@ -25,7 +25,7 @@ import { TabsTrigger, } from '@/components/ui/tabs' import { RequestItem } from '@/types' -import { isFalsy, isTruthy } from '@/utils' +import { isFalsy, isTruthy, onlyIP } from '@/utils' import fetcher from '@/utils/fetcher' import MethodBadge from './MethodBadge' @@ -153,7 +153,7 @@ const RequestModal: React.FC = ({ req, ...props }) => {
{t('requests.remote_ip')}
diff --git a/src/pages/Scripting/Evaluate/index.tsx b/src/pages/Scripting/Evaluate/index.tsx index fab98d6..02dfa4e 100644 --- a/src/pages/Scripting/Evaluate/index.tsx +++ b/src/pages/Scripting/Evaluate/index.tsx @@ -1,7 +1,6 @@ import React, { lazy, Suspense, useCallback, useState } from 'react' import { toast } from 'react-hot-toast' import { useTranslation } from 'react-i18next' -import { css } from '@emotion/react' import { LifeBuoy } from 'lucide-react' import CodeContent from '@/components/CodeContent' diff --git a/src/utils/index.ts b/src/utils/index.ts index e38b60a..b42fc05 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -16,3 +16,17 @@ export const forceRefresh = async (): Promise => { window.location.reload() } + +/** + * The following IP formats can be handled: + * 1.1.1.1(Proxy), + * 1.1.1.1 (Proxy), + * 2001:0db8:85a3:0000:0000:8a2e:0370:7334(Proxy), + * 2001:0db8:85a3:0000:0000:8a2e:0370:7334 (Proxy), + */ +export const onlyIP = (ip: string) => { + const ipAddressRegex = + /(?:\d{1,3}\.){3}\d{1,3}|(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}/g + const matchArray = ip.match(ipAddressRegex) + return matchArray?.length ? matchArray[0] : ip +} From 8d316a7de0875812c92fc7a2ce169b32ffd8fd3a Mon Sep 17 00:00:00 2001 From: JiPai Date: Tue, 21 May 2024 16:43:09 +0800 Subject: [PATCH 2/2] feat: memo pure IP string --- src/pages/Requests/components/RequestModal.tsx | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/pages/Requests/components/RequestModal.tsx b/src/pages/Requests/components/RequestModal.tsx index b5f3afc..e01cf23 100644 --- a/src/pages/Requests/components/RequestModal.tsx +++ b/src/pages/Requests/components/RequestModal.tsx @@ -1,4 +1,4 @@ -import React, { useCallback } from 'react' +import React, { useCallback, useMemo } from 'react' import { toast } from 'react-hot-toast' import { useTranslation } from 'react-i18next' import { css } from '@emotion/react' @@ -66,6 +66,11 @@ const RequestModal: React.FC = ({ req, ...props }) => { [t], ) + const pureIP = useMemo( + () => req?.remoteAddress && onlyIP(req.remoteAddress), + [req?.remoteAddress], + ) + const content = req ? ( <> @@ -153,7 +158,7 @@ const RequestModal: React.FC = ({ req, ...props }) => {
{t('requests.remote_ip')}