Skip to content

Commit da0ff23

Browse files
committed
fix: attach client real ip
1 parent 0a1eea5 commit da0ff23

File tree

10 files changed

+31
-24
lines changed

10 files changed

+31
-24
lines changed

src/app/(note-topic)/notes/(topic-detail)/topics/[slug]/layout.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { Metadata } from 'next'
44
import { QueryHydrate } from '~/components/common/QueryHydrate'
55
import { NormalContainer } from '~/components/layout/container/Normal'
66
import { isShallowEqualArray } from '~/lib/_'
7-
import { attachUA } from '~/lib/attach-ua'
7+
import { attachUAAndRealIp } from '~/lib/attach-ua'
88
import { getQueryClient } from '~/lib/query-client.server'
99

1010
import { getTopicQuery } from './query'
@@ -14,7 +14,7 @@ export const generateMetadata = async (
1414
slug: string
1515
}>,
1616
) => {
17-
attachUA()
17+
attachUAAndRealIp()
1818
const queryClient = getQueryClient()
1919

2020
const query = getTopicQuery(props.params.slug)
@@ -30,7 +30,7 @@ export default async function Layout(
3030
slug: string
3131
}>,
3232
) {
33-
attachUA()
33+
attachUAAndRealIp()
3434
const queryClient = getQueryClient()
3535
const query = getTopicQuery(props.params.slug)
3636
const queryKey = query.queryKey

src/app/(note-topic)/notes/topics/layout.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { Metadata } from 'next'
44
import { QueryHydrate } from '~/components/common/QueryHydrate'
55
import { NormalContainer } from '~/components/layout/container/Normal'
66
import { isShallowEqualArray } from '~/lib/_'
7-
import { attachUA } from '~/lib/attach-ua'
7+
import { attachUAAndRealIp } from '~/lib/attach-ua'
88
import { getQueryClient } from '~/lib/query-client.server'
99

1010
import { topicsQuery } from './query'
@@ -18,7 +18,7 @@ export default async function Layout(
1818
slug: string
1919
}>,
2020
) {
21-
attachUA()
21+
attachUAAndRealIp()
2222
const queryClient = getQueryClient()
2323

2424
await queryClient.fetchQuery(topicsQuery)

src/app/(page-detail)/[slug]/layout.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { BottomToUpTransitionView } from '~/components/ui/transition/BottomToUpT
99
import { OnlyMobile } from '~/components/ui/viewport/OnlyMobile'
1010
import { CommentAreaRootLazy } from '~/components/widgets/comment'
1111
import { TocFAB } from '~/components/widgets/toc/TocFAB'
12-
import { attachUA } from '~/lib/attach-ua'
12+
import { attachUAAndRealIp } from '~/lib/attach-ua'
1313
import { getOgUrl } from '~/lib/helper.server'
1414
import { getSummaryFromMd } from '~/lib/markdown'
1515
import { getQueryClient } from '~/lib/query-client.server'
@@ -32,7 +32,7 @@ export const generateMetadata = async ({
3232
}): Promise<Metadata> => {
3333
const { slug } = params
3434
try {
35-
attachUA()
35+
attachUAAndRealIp()
3636
const data = await getQueryClient().fetchQuery(queries.page.bySlug(slug))
3737
const { title, text } = data
3838
const description = getSummaryFromMd(text ?? '')
@@ -67,7 +67,7 @@ interface PageParams {
6767
}
6868

6969
export default async (props: NextPageParams<PageParams>) => {
70-
attachUA()
70+
attachUAAndRealIp()
7171
const {
7272
params: { slug },
7373
} = props

src/app/categories/[slug]/layout.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { Metadata } from 'next'
44
import { QueryHydrate } from '~/components/common/QueryHydrate'
55
import { NormalContainer } from '~/components/layout/container/Normal'
66
import { isShallowEqualArray } from '~/lib/_'
7-
import { attachUA } from '~/lib/attach-ua'
7+
import { attachUAAndRealIp } from '~/lib/attach-ua'
88
import { getQueryClient } from '~/lib/query-client.server'
99

1010
import { getPageBySlugQuery } from './query'
@@ -14,7 +14,7 @@ export const generateMetadata = async (
1414
slug: string
1515
}>,
1616
) => {
17-
attachUA()
17+
attachUAAndRealIp()
1818
const queryClient = getQueryClient()
1919

2020
const query = getPageBySlugQuery(props.params.slug)
@@ -30,7 +30,7 @@ export default async function Layout(
3030
slug: string
3131
}>,
3232
) {
33-
attachUA()
33+
attachUAAndRealIp()
3434
const queryClient = getQueryClient()
3535
const query = getPageBySlugQuery(props.params.slug)
3636
const queryKey = query.queryKey

src/app/layout.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { ScrollTop } from '~/components/common/ScrollTop'
1313
import { Root } from '~/components/layout/root/Root'
1414
import { SearchPanelWithHotKey } from '~/components/widgets/shared/SearchFAB'
1515
import { TocAutoScroll } from '~/components/widgets/toc/TocAutoScroll'
16-
import { attachUA } from '~/lib/attach-ua'
16+
import { attachUAAndRealIp } from '~/lib/attach-ua'
1717
import { sansFont, serifFont } from '~/lib/fonts'
1818
import { getQueryClient } from '~/lib/query-client.server'
1919
import { AggregationProvider } from '~/providers/root/aggregation-data-provider'
@@ -118,7 +118,7 @@ type Props = {
118118
}
119119

120120
export default async function RootLayout(props: Props) {
121-
attachUA()
121+
attachUAAndRealIp()
122122
const { children } = props
123123

124124
const queryClient = getQueryClient()

src/app/notes/[id]/layout.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { NoteFontSettingFab } from '~/components/widgets/note/NoteFontFab'
88
import { NoteMainContainer } from '~/components/widgets/note/NoteMainContainer'
99
import { TocFAB } from '~/components/widgets/toc/TocFAB'
1010
import { REQUEST_QUERY } from '~/constants/system'
11-
import { attachUA } from '~/lib/attach-ua'
11+
import { attachUAAndRealIp } from '~/lib/attach-ua'
1212
import { getOgUrl } from '~/lib/helper.server'
1313
import { getSummaryFromMd } from '~/lib/markdown'
1414
import { getQueryClient } from '~/lib/query-client.server'
@@ -31,7 +31,7 @@ export const generateMetadata = async ({
3131
}
3232
}): Promise<Metadata> => {
3333
try {
34-
attachUA()
34+
attachUAAndRealIp()
3535
const { data } = await getQueryClient().fetchQuery(
3636
queries.note.byNid(params.id),
3737
)
@@ -68,7 +68,7 @@ export default async (
6868
id: string
6969
}>,
7070
) => {
71-
attachUA()
71+
attachUAAndRealIp()
7272
const header = headers()
7373
const searchParams = new URLSearchParams(header.get(REQUEST_QUERY) || '')
7474
const id = props.params.id

src/app/posts/(post-detail)/[category]/[slug]/layout.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { BottomToUpTransitionView } from '~/components/ui/transition/BottomToUpT
99
import { OnlyMobile } from '~/components/ui/viewport/OnlyMobile'
1010
import { CommentAreaRootLazy } from '~/components/widgets/comment'
1111
import { TocFAB } from '~/components/widgets/toc/TocFAB'
12-
import { attachUA } from '~/lib/attach-ua'
12+
import { attachUAAndRealIp } from '~/lib/attach-ua'
1313
import { getOgUrl } from '~/lib/helper.server'
1414
import { getSummaryFromMd } from '~/lib/markdown'
1515
import { getQueryClient } from '~/lib/query-client.server'
@@ -26,7 +26,7 @@ export const generateMetadata = async ({
2626
}): Promise<Metadata> => {
2727
const { category, slug } = params
2828
try {
29-
attachUA()
29+
attachUAAndRealIp()
3030
const data = await getQueryClient().fetchQuery(
3131
queries.post.bySlug(category, slug),
3232
)
@@ -69,7 +69,7 @@ interface PageParams {
6969
}
7070

7171
export default async (props: NextPageParams<PageParams>) => {
72-
attachUA()
72+
attachUAAndRealIp()
7373
const {
7474
params: { category, slug },
7575
} = props

src/app/timeline/layout.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { TimelineType } from '@mx-space/api-client'
77
import { QueryHydrate } from '~/components/common/QueryHydrate'
88
import { SearchFAB } from '~/components/widgets/shared/SearchFAB'
99
import { REQUEST_QUERY } from '~/constants/system'
10-
import { attachUA } from '~/lib/attach-ua'
10+
import { attachUAAndRealIp } from '~/lib/attach-ua'
1111
import { getQueryClient } from '~/lib/query-client.server'
1212
import { apiClient } from '~/lib/request'
1313

@@ -16,7 +16,7 @@ export const metadata = {
1616
}
1717

1818
export default async (props: NextPageParams<{}, PropsWithChildren>) => {
19-
attachUA()
19+
attachUAAndRealIp()
2020
const header = headers()
2121
const query = header.get(REQUEST_QUERY)
2222

src/lib/attach-ua.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@ import { $axios } from '~/lib/request'
55

66
import PKG from '../../package.json'
77

8-
export const attachUA = () => {
8+
export const attachUAAndRealIp = () => {
99
if (!isServer) return
1010
const { get } = headers()
1111

1212
const ua = get('user-agent')
13+
const ip =
14+
get('x-real-ip') ||
15+
get('x-forwarded-for') ||
16+
get('remote-addr') ||
17+
get('cf-connecting-ip')
18+
$axios.defaults.headers.common['X-Real-IP'] = ip
19+
$axios.defaults.headers.common['X-Forwarded-For'] = ip
1320
$axios.defaults.headers.common[
1421
'User-Agent'
1522
] = `${ua} NextJS/v${PKG.dependencies.next} ${PKG.name}/${PKG.version}`

src/lib/define-metadata.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { Metadata } from 'next'
66
import { getQueryClient } from '~/lib/query-client.server'
77
import { queries } from '~/queries/definition'
88

9-
import { attachUA } from './attach-ua'
9+
import { attachUAAndRealIp } from './attach-ua'
1010

1111
export const defineMetadata = <T extends Record<string, string>>(
1212
fn: (
@@ -17,7 +17,7 @@ export const defineMetadata = <T extends Record<string, string>>(
1717
const handler = async ({ params }: { params: T }): Promise<Metadata> => {
1818
const getData = async () => {
1919
const queryClient = getQueryClient()
20-
attachUA()
20+
attachUAAndRealIp()
2121
return await queryClient.fetchQuery({
2222
...queries.aggregation.root(),
2323
})

0 commit comments

Comments
 (0)