Skip to content

Commit

Permalink
fix: discard more than one HTTP headers from Vmess+HTTP when reading …
Browse files Browse the repository at this point in the history
…or outputting a Clash subscription
  • Loading branch information
geekdada committed Mar 5, 2024
1 parent 368c151 commit ebed482
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 3 deletions.
17 changes: 16 additions & 1 deletion src/provider/ClashProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,11 @@ export const parseClashConfig = (

break
case 'http':
vmessNode.httpOpts = item['http-opts']
vmessNode.httpOpts = {
...item['http-opts'],
headers:
resolveVmessHttpHeaders(item['http-opts'].headers) || {},
}

break
case 'grpc':
Expand Down Expand Up @@ -493,3 +497,14 @@ function resolveUdpRelay(val?: boolean, defaultVal = false): boolean {
}
return defaultVal
}

function resolveVmessHttpHeaders(
headers: Record<string, string[]>,
): Record<string, string> {
return Object.keys(headers).reduce((acc, key) => {
if (headers[key].length) {
acc[key] = headers[key][0]
}
return acc
}, {} as Record<string, string>)
}
115 changes: 115 additions & 0 deletions src/provider/__tests__/ClashProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,121 @@ foo: bar
scope.done()
})

test('vmess Configurations', (t) => {
t.deepEqual(
parseClashConfig([
{
type: 'vmess',
name: 'vmess',
server: 'server',
port: 443,
uuid: 'uuid',
alterId: 32,
cipher: 'auto',
network: 'http',
'http-opts': {
path: ['/path'],
headers: {
host: ['v2ray.com'],
},
},
},
]),
[
{
type: NodeTypeEnum.Vmess,
nodeName: 'vmess',
hostname: 'server',
port: 443,
uuid: 'uuid',
alterId: '32',
method: 'auto',
network: 'http',
tls: false,
udpRelay: false,
httpOpts: {
path: ['/path'],
headers: {
host: 'v2ray.com',
},
},
},
],
)

t.deepEqual(
parseClashConfig([
{
type: 'vmess',
name: 'vmess',
server: 'server',
port: 443,
uuid: 'uuid',
alterId: 32,
cipher: 'auto',
network: 'grpc',
'grpc-opts': {
'grpc-service-name': 'service',
},
},
]),
[
{
type: NodeTypeEnum.Vmess,
nodeName: 'vmess',
hostname: 'server',
port: 443,
uuid: 'uuid',
alterId: '32',
method: 'auto',
network: 'grpc',
tls: false,
udpRelay: false,
grpcOpts: {
serviceName: 'service',
},
},
],
)

t.deepEqual(
parseClashConfig([
{
type: 'vmess',
name: 'vmess',
server: 'server',
port: 443,
uuid: 'uuid',
alterId: 32,
cipher: 'auto',
network: 'h2',
'h2-opts': {
path: '/path',
host: ['v2ray.com'],
},
},
]),
[
{
type: NodeTypeEnum.Vmess,
nodeName: 'vmess',
hostname: 'server',
port: 443,
uuid: 'uuid',
alterId: '32',
method: 'auto',
network: 'h2',
tls: false,
udpRelay: false,
h2Opts: {
path: '/path',
host: ['v2ray.com'],
},
},
],
)
})

test('snell Configurations', (t) => {
t.deepEqual(
parseClashConfig([
Expand Down
2 changes: 1 addition & 1 deletion src/utils/__tests__/clash.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ test('getClashNodes', async (t) => {
uuid: '1386f85e-657b-4d6e-9d56-78badb75e1fd',
'http-opts': {
headers: {
host: 'example.com',
host: ['example.com'],
},
method: 'GET',
path: ['/'],
Expand Down
16 changes: 15 additions & 1 deletion src/utils/clash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,12 @@ function nodeListMapper(nodeConfig: PossibleNodeConfigType) {
break

case 'http':
vmessNode['http-opts'] = nodeConfig.httpOpts
vmessNode['http-opts'] = {
...nodeConfig.httpOpts,
headers: resolveVmessHttpHeadersFromSurgioConfig(
nodeConfig.httpOpts?.headers || {},
),
}
break

case 'grpc':
Expand Down Expand Up @@ -421,3 +426,12 @@ function nodeListMapper(nodeConfig: PossibleNodeConfigType) {
return null
}
}

function resolveVmessHttpHeadersFromSurgioConfig(
headers: Record<string, string>,
): Record<string, string[]> {
return Object.keys(headers).reduce((acc, key) => {
acc[key] = [headers[key]]
return acc
}, {} as Record<string, string[]>)
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -623,5 +623,6 @@ export const getHeader = (
const headerKey = Object.keys(headers).find(
(k) => k.toLowerCase() === lowerCaseKey,
)

return headerKey ? headers[headerKey] : undefined
}

0 comments on commit ebed482

Please sign in to comment.