diff --git a/locales/en.yml b/locales/en.yml index 266bd588..94e03462 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -457,4 +457,13 @@ tools: unicode-to-java-entities: title: Unicode Characters to Java Entities Converter - description: Unicode Characters to Java Entities Converter and vice-versa \ No newline at end of file + description: Unicode Characters to Java Entities Converter and vice-versa + + sip-auth: + password: SIP password + password-tips: Enter a password... + message: SIP message + message-tips: Paste your SIP message here... + result: Result + sucess: Sucess + failure: Failure \ No newline at end of file diff --git a/locales/zh.yml b/locales/zh.yml index 77c4b0d0..6ca9c3ca 100644 --- a/locales/zh.yml +++ b/locales/zh.yml @@ -404,3 +404,13 @@ tools: text-to-binary: title: 文本到 ASCII 二进制 description: 将文本转换为其 ASCII 二进制表示形式,反之亦然。 + sip-auth: + title: SIP 认证校验 + description: 这个工具能够帮你验证 SIP 授权头。该工具所有运算过程都在浏览器中进行,并不会通过网络传输敏感信息,所以请放心使用。 + password: SIP 密码 + password-tips: 输入密码... + message: SIP 消息文本 + message-tips: 在此处粘贴您的 SIP 消息... + result: 检测结果 + sucess: 成功 + failure: 失败 diff --git a/src/tools/index.ts b/src/tools/index.ts index cff2980a..ce833307 100644 --- a/src/tools/index.ts +++ b/src/tools/index.ts @@ -118,6 +118,7 @@ import { tool as ttlCalculator } from './ttl-calculator'; import { tool as unicodeFormatter } from './unicode-formatter'; import { tool as raidCalculator } from './raid-calculator'; import { tool as nanoidGenerator } from './nanoid-generator'; +import { tool as sipAuth } from './sip-auth'; import { tool as asciiTextDrawer } from './ascii-text-drawer'; @@ -484,6 +485,7 @@ export const toolsByCategory: ToolCategory[] = [ option43Generator, portNumbers, raidCalculator, + sipAuth, ], }, { diff --git a/src/tools/sip-auth/index.ts b/src/tools/sip-auth/index.ts new file mode 100644 index 00000000..fe0dea98 --- /dev/null +++ b/src/tools/sip-auth/index.ts @@ -0,0 +1,12 @@ +import { ShieldCheck } from '@vicons/tabler'; +import { defineTool } from '../tool'; + +export const tool = defineTool({ + name: 'SIP Authorization', + path: '/sip-auth', + description: 'This tool helps you to verify the SIP authorization header. The tool performs all calculations in the browser and does not transmit sensitive information over the network, so feel free to use it.', + keywords: ['sip', 'authorization'], + component: () => import('./sip-auth.vue'), + icon: ShieldCheck, + createdAt: new Date('2024-04-11'), +}); diff --git a/src/tools/sip-auth/sip-auth.service.test.ts b/src/tools/sip-auth/sip-auth.service.test.ts new file mode 100644 index 00000000..564f553c --- /dev/null +++ b/src/tools/sip-auth/sip-auth.service.test.ts @@ -0,0 +1,30 @@ +import { describe, expect, it } from 'vitest'; +import { getAuthHeaderCheckResult } from './sip-auth.service'; + +const message = `REGISTER sip:172.21.169.226 SIP/2.0 +Via: SIP/2.0/UDP 172.21.160.1:64712;rport;branch=z9hG4bKPja70ba2f5f4464081a3925196ebd4e7f9 +Max-Forwards: 70 +From: ;tag=8db088f1039e49b691e53e5f172ace98 +To: +Call-ID: be95702faee84a8ebc8ccf94c5ca5492 +CSeq: 37808 REGISTER +User-Agent: MicroSIP/3.21.3 +Contact: +Expires: 300 +Allow: PRACK, INVITE, ACK, BYE, CANCEL, UPDATE, INFO, SUBSCRIBE, NOTIFY, REFER, MESSAGE, OPTIONS +Authorization: Digest username="1000", realm="172.21.169.226", nonce="d99a372c-340a-4ec9-91ed-53ad8f318ae0", uri="sip:172.21.169.226", response="7bdeb758d949a3c5e8d125fafbac1d5e", algorit=MD5, cnonce="3a8c445fb7ff48aeb8c422b00afaaddc", qop=auth, nc=00000001 +Content-Length: 0`; + +describe('getAuthHeaderCheckResult', () => { + it('should return "check success" for valid authentication', () => { + const password = '1234'; + const result = getAuthHeaderCheckResult({ message, password }); + expect(result).toBe(true); + }); + + it('should return "check failed" for invalid authentication', () => { + const password = '12345'; + const result = getAuthHeaderCheckResult({ message, password }); + expect(result).toBe(false); + }); +}); diff --git a/src/tools/sip-auth/sip-auth.service.ts b/src/tools/sip-auth/sip-auth.service.ts new file mode 100644 index 00000000..0de35cc6 --- /dev/null +++ b/src/tools/sip-auth/sip-auth.service.ts @@ -0,0 +1,36 @@ +import { MD5 } from 'crypto-js'; + +export { getAuthHeaderCheckResult }; + +function getAuthHeaderCheckResult({ message, password }: { message: string; password: string }) { + let calculatedHash; + const method = extractComponent('^([A-Z]+) sip', message); + const username = extractComponent('username="([^"]+)"', message); + const realm = extractComponent('realm="([^"]+)"', message); + const uri = extractComponent('uri="([^"]+)"', message); + const nonce = extractComponent('[^c]nonce="([^"]+)"', message); + const cnonce = extractComponent('cnonce="([^"]+)"', message); + const nc = extractComponent('nc=([0-9a-f]+)', message); + const qop = extractComponent('qop="?(auth|auth-int)"?', message); + const response = extractComponent('response="([^"]+)"', message); + const ha1 = MD5(`${username}:${realm}:${password}`).toString(); + const ha2 = MD5(`${method}:${uri}`).toString(); + if (qop.toLowerCase() === 'auth') { + calculatedHash = MD5(`${ha1}:${nonce}:${nc}:${cnonce}:${qop}:${ha2}`).toString(); + } + else { + calculatedHash = MD5(`${ha1}:${nonce}:${cnonce}`).toString(); + } + return (response === calculatedHash); +} + +function extractComponent(regex: string, source: string): string { + const processor = new RegExp(regex, 'i'); + const matchResult = processor.exec(source); + if (matchResult !== null && matchResult.length > 1) { + return matchResult[1]; + } + else { + return 'Not Found'; + } +} diff --git a/src/tools/sip-auth/sip-auth.vue b/src/tools/sip-auth/sip-auth.vue new file mode 100644 index 00000000..e7d3575a --- /dev/null +++ b/src/tools/sip-auth/sip-auth.vue @@ -0,0 +1,39 @@ + + +