Skip to content

Commit

Permalink
Merge pull request #22 from PaiJi/fix-request-ip-not-pure
Browse files Browse the repository at this point in the history
fix: fix ip string isn't pure
  • Loading branch information
geekdada authored May 21, 2024
2 parents 97708a9 + 8d316a7 commit b876f5d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/pages/Requests/components/RequestModal.tsx
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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'
Expand Down Expand Up @@ -66,6 +66,11 @@ const RequestModal: React.FC<RequestModalProps> = ({ req, ...props }) => {
[t],
)

const pureIP = useMemo(
() => req?.remoteAddress && onlyIP(req.remoteAddress),
[req?.remoteAddress],
)

const content = req ? (
<>
<DialogHeader>
Expand Down Expand Up @@ -153,7 +158,7 @@ const RequestModal: React.FC<RequestModalProps> = ({ req, ...props }) => {
<div>{t('requests.remote_ip')}</div>
<div>
<a
href={`https://ip.sb/ip/${req.remoteAddress}`}
href={`https://ip.sb/ip/${pureIP}`}
target="_blank"
rel="noreferrer noopener"
>
Expand Down
1 change: 0 additions & 1 deletion src/pages/Scripting/Evaluate/index.tsx
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
14 changes: 14 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,17 @@ export const forceRefresh = async (): Promise<void> => {

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
}

0 comments on commit b876f5d

Please sign in to comment.