Skip to content

Commit

Permalink
Add IPv6 support for hostnames (#807)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fengrui-Liu committed Feb 1, 2024
1 parent 9188752 commit fc19c4c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
7 changes: 6 additions & 1 deletion packages/service/support/permission/auth/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AuthUserTypeEnum } from '@fastgpt/global/support/permission/constant';
import { parseHeaderCert } from '../controller';
import { AuthModeType } from '../type';
import { authOutLinkValid } from './outLink';
import { isIPv6 } from 'net';

export const authCert = async (props: AuthModeType) => {
const result = await parseHeaderCert(props);
Expand Down Expand Up @@ -34,7 +35,11 @@ export async function authCertOrShareId({

/* auth the request from local service */
export const authRequestFromLocal = ({ req }: AuthModeType) => {
const host = `${process.env.HOSTNAME || 'localhost'}:${process.env.PORT || 3000}`;
const host =
process.env.HOSTNAME && isIPv6(process.env.HOSTNAME)
? `[${process.env.HOSTNAME}]:${process.env.PORT || 3000}`
: `${process.env.HOSTNAME || 'localhost'}:${process.env.PORT || 3000}`;

if (host !== req.headers.host) {
return Promise.reject('Invalid request');
}
Expand Down
9 changes: 7 additions & 2 deletions projects/app/src/service/common/api/request.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios, { Method, InternalAxiosRequestConfig, AxiosResponse } from 'axios';

import { isIPv6 } from 'net';
interface ConfigType {
headers?: { [key: string]: string };
hold?: boolean;
Expand Down Expand Up @@ -78,7 +78,12 @@ export function request(url: string, data: any, config: ConfigType, method: Meth

return instance
.request({
baseURL: `http://${process.env.HOSTNAME || 'localhost'}:${process.env.PORT || 3000}`,
baseURL: `http://${
process.env.HOSTNAME && isIPv6(process.env.HOSTNAME)
? `[${process.env.HOSTNAME}]:${process.env.PORT || 3000}`
: `${process.env.HOSTNAME || 'localhost'}:${process.env.PORT || 3000}`
}`,

url,
method,
data: ['POST', 'PUT'].includes(method) ? data : null,
Expand Down
10 changes: 8 additions & 2 deletions projects/app/src/service/moduleDispatch/tools/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { ModuleDispatchProps } from '@fastgpt/global/core/module/type.d';
import { ModuleInputKeyEnum, ModuleOutputKeyEnum } from '@fastgpt/global/core/module/constants';
import axios from 'axios';
import { flatDynamicParams } from '../utils';
import { isIPv6 } from 'net';

export type HttpRequestProps = ModuleDispatchProps<{
[ModuleInputKeyEnum.abandon_httpUrl]: string;
Expand Down Expand Up @@ -132,7 +133,12 @@ async function fetchData({
}): Promise<Record<string, any>> {
const { data: response } = await axios<Record<string, any>>({
method,
baseURL: `http://${process.env.HOSTNAME || 'localhost'}:${process.env.PORT || 3000}`,
baseURL: `http://${
process.env.HOSTNAME && isIPv6(process.env.HOSTNAME)
? `[${process.env.HOSTNAME}]:${process.env.PORT || 3000}`
: `${process.env.HOSTNAME || 'localhost'}:${process.env.PORT || 3000}`
}`,

url,
headers: {
'Content-Type': 'application/json',
Expand All @@ -142,7 +148,7 @@ async function fetchData({
data: method === 'POST' ? body : {}
});

/*
/*
parse the json:
{
user: {
Expand Down

0 comments on commit fc19c4c

Please sign in to comment.