fix: use correct client ip header
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { getClientIpFromHeaders } from '@openpanel/common/server/get-client-ip';
|
||||
import type { NextFunction, Request, Response } from 'express';
|
||||
import { getClientIp } from 'request-ip';
|
||||
|
||||
import type { OpenPanelOptions } from '@openpanel/sdk';
|
||||
import { OpenPanel } from '@openpanel/sdk';
|
||||
@@ -22,7 +22,7 @@ export type OpenpanelOptions = OpenPanelOptions & {
|
||||
export default function createMiddleware(options: OpenpanelOptions) {
|
||||
return function middleware(req: Request, res: Response, next: NextFunction) {
|
||||
const sdk = new OpenPanel(options);
|
||||
const ip = getClientIp(req);
|
||||
const ip = getClientIpFromHeaders(req.headers);
|
||||
if (ip) {
|
||||
sdk.api.addHeader('x-client-ip', ip);
|
||||
}
|
||||
@@ -30,20 +30,15 @@ export default function createMiddleware(options: OpenpanelOptions) {
|
||||
sdk.api.addHeader('x-user-agent', req.headers['user-agent'] as string);
|
||||
}
|
||||
|
||||
if (options.getProfileId) {
|
||||
const profileId = options.getProfileId(req);
|
||||
if (profileId) {
|
||||
sdk.identify({
|
||||
profileId,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (options.trackRequest?.(req.url)) {
|
||||
const profileId = options.getProfileId
|
||||
? options.getProfileId(req)
|
||||
: undefined;
|
||||
sdk.track('request', {
|
||||
url: req.url,
|
||||
method: req.method,
|
||||
query: req.query,
|
||||
profileId,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@openpanel/sdk": "workspace:1.0.0-local",
|
||||
"request-ip": "^3.3.0"
|
||||
"@openpanel/common": "workspace:*"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"express": "^4.17.0 || ^5.0.0"
|
||||
|
||||
@@ -8,4 +8,6 @@ export default defineConfig({
|
||||
sourcemap: false,
|
||||
clean: true,
|
||||
minify: true,
|
||||
// Bundle @openpanel/common into the output instead of treating it as external
|
||||
noExternal: ['@openpanel/common'],
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user