-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ref: move DIG parser to a helper file * fix: tests typig errors * add: trace unit test * add: cmd/dig: shared handler * add: trace dig parser * add: implement trace support * fix: missing query.trace validation rules * add: dns unit test
- Loading branch information
1 parent
21f13e9
commit 32e2988
Showing
9 changed files
with
728 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { | ||
SECTION_REG_EXP, | ||
NEW_LINE_REG_EXP, | ||
SharedDigParser, | ||
DnsSection, | ||
DnsParseLoopResponse, | ||
} from './shared.js'; | ||
|
||
export type DnsParseResponse = DnsParseLoopResponse & { | ||
rawOutput: string; | ||
}; | ||
|
||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
const QUERY_TIME_REG_EXP = /Query\s+time:\s+(\d+)/g; | ||
const RESOLVER_REG_EXP = /SERVER:.*\((.*?)\)/g; | ||
/* eslint-enable @typescript-eslint/naming-convention */ | ||
|
||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
export const ClassicDigParser = { | ||
parse(rawOutput: string): Error | DnsParseResponse { | ||
const lines = rawOutput.split(NEW_LINE_REG_EXP); | ||
|
||
if (lines.length < 6) { | ||
const message = lines[lines.length - 2]; | ||
|
||
if (!message || message.length < 2) { | ||
return new Error(rawOutput); | ||
} | ||
|
||
return new Error(message); | ||
} | ||
|
||
return { | ||
...ClassicDigParser.parseLoop(lines), | ||
rawOutput, | ||
}; | ||
}, | ||
|
||
parseLoop(lines: string[]): DnsParseLoopResponse { | ||
const result: DnsParseLoopResponse = { | ||
header: [], | ||
answer: [], | ||
server: '', | ||
time: 0, | ||
}; | ||
|
||
let section = 'header'; | ||
for (const line of lines) { | ||
const time = ClassicDigParser.getQueryTime(line); | ||
if (time !== undefined) { | ||
result.time = time; | ||
} | ||
|
||
const serverMatch = ClassicDigParser.getResolverServer(line); | ||
if (serverMatch) { | ||
result.server = serverMatch; | ||
} | ||
|
||
let sectionChanged = false; | ||
if (line.length === 0) { | ||
section = ''; | ||
} else { | ||
const sectionMatch = SECTION_REG_EXP.exec(line); | ||
|
||
if (sectionMatch && sectionMatch.length >= 2) { | ||
section = String(sectionMatch[2]).toLowerCase(); | ||
sectionChanged = true; | ||
} | ||
} | ||
|
||
if (!section) { | ||
continue; | ||
} | ||
|
||
if (!result[section]) { | ||
result[section] = []; | ||
} | ||
|
||
if (!sectionChanged && line) { | ||
if (section === 'header') { | ||
result[section]!.push(line); | ||
} else { | ||
const sectionResult = ClassicDigParser.parseSection(line.split(/\s+/g), section); | ||
(result[section] as DnsSection[]).push(sectionResult); | ||
} | ||
} | ||
} | ||
|
||
return { | ||
answer: result.answer, | ||
server: result.server, | ||
time: result.time, | ||
}; | ||
}, | ||
|
||
parseSection(values: string[], section: string): DnsSection { | ||
if (!['answer', 'additional'].includes(section)) { | ||
return {}; | ||
} | ||
|
||
return SharedDigParser.parseSection(values); | ||
}, | ||
|
||
getQueryTime(line: string): number | undefined { | ||
const result = QUERY_TIME_REG_EXP.exec(line); | ||
|
||
if (!result) { | ||
return; | ||
} | ||
|
||
return Number(result[1]); | ||
}, | ||
|
||
getResolverServer(line: string): string | undefined { | ||
const result = RESOLVER_REG_EXP.exec(line); | ||
|
||
if (!result) { | ||
return; | ||
} | ||
|
||
return String(result[1]); | ||
}, | ||
}; | ||
|
||
export default ClassicDigParser; |
Oops, something went wrong.