sensitive-word-filter.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // 防止屏蔽,所以用base64编码并直接扔文件里
  2. const sensitiveWordsConfig = {
  3. mask: "*",
  4. mask_word: "",
  5. words: [
  6. "5Lmg6L+R5bmz",
  7. "6IOh6ZSm5rab",
  8. "5rGf5rO95rCR",
  9. "5rip5a625a6d",
  10. "5p2O5YWL5by6",
  11. "5p2O6ZW/5pil",
  12. "5q+b5rO95Lic",
  13. "6YKT5bCP5bmz",
  14. "5ZGo5oGp5p2l",
  15. "6ams5YWL5oCd",
  16. "56S+5Lya5Li75LmJ",
  17. "5YWx5Lqn5YWa",
  18. "5YWx5Lqn5Li75LmJ",
  19. "5aSn6ZmG5a6Y5pa5",
  20. "5YyX5Lqs5pS/5p2D",
  21. "5Lit5Y2O5bid5Zu9",
  22. "5Lit5Zu95pS/5bqc",
  23. "5YWx54uX",
  24. "5YWt5Zub5LqL5Lu2",
  25. "5aSp5a6J6Zeo",
  26. "5YWt5Zub",
  27. "5pS/5rK75bGA5bi45aeU",
  28. "5Lik5Lya",
  29. "5YWx6Z2S5Zui",
  30. "5a2m5r2u",
  31. "5YWr5Lmd",
  32. "5LqM5Y2B5aSn",
  33. "5rCR6L+b5YWa",
  34. "5Y+w54us",
  35. "5Y+w5rm+54us56uL",
  36. "5Y+w5rm+5Zu9",
  37. "5Zu95rCR5YWa",
  38. "5Y+w5rm+5rCR5Zu9",
  39. "5Lit5Y2O5rCR5Zu9",
  40. "cG9ybmh1Yg==",
  41. "UG9ybmh1Yg==",
  42. "W1l5XW91W1BwXW9ybg==",
  43. "cG9ybg==",
  44. "UG9ybg==",
  45. "W1h4XVtWdl1pZGVvcw==",
  46. "W1JyXWVkW1R0XXViZQ==",
  47. "W1h4XVtIaF1hbXN0ZXI=",
  48. "W1NzXXBhbmtbV3ddaXJl",
  49. "W1NzXXBhbmtbQmJdYW5n",
  50. "W1R0XXViZTg=",
  51. "W1l5XW91W0pqXWl6eg==",
  52. "W0JiXXJhenplcnM=",
  53. "W05uXWF1Z2h0eVsgXT9bQWFdbWVyaWNh",
  54. "5L2c54ix",
  55. "5YGa54ix",
  56. "5oCn5Lqk",
  57. "5oCn54ix",
  58. "6Ieq5oWw",
  59. "6Zi06IyO",
  60. "5rer5aaH",
  61. "6IKb5Lqk",
  62. "5Lqk6YWN",
  63. "5oCn5YWz57O7",
  64. "5oCn5rS75Yqo",
  65. "6Imy5oOF",
  66. "6Imy5Zu+",
  67. "5rap5Zu+",
  68. "6KO45L2T",
  69. "5bCP56m0",
  70. "5rer6I2h",
  71. "5oCn54ix",
  72. "57+75aKZ",
  73. "VlBO",
  74. "56eR5a2m5LiK572R",
  75. "5oyC5qKv5a2Q",
  76. "R0ZX"
  77. ],
  78. encoding: "base64",
  79. original_count: 71
  80. }
  81. /**
  82. * 敏感词过滤结果接口
  83. */
  84. interface FilterResult {
  85. /** 是否包含敏感词 */
  86. hasSensitiveWords: boolean;
  87. /** 检测到的敏感词列表 */
  88. detectedWords: string[];
  89. /** 过滤后的文本(敏感词被替换) */
  90. filteredText: string;
  91. /** 原始文本 */
  92. originalText: string;
  93. /** 是否需要跳转到被捕页面 */
  94. shouldRedirectToArrested: boolean;
  95. }
  96. /**
  97. * 敏感词配置接口
  98. */
  99. interface SensitiveWordsConfig {
  100. mask: string;
  101. mask_word: string;
  102. words: string[];
  103. encoding?: string;
  104. original_count?: number;
  105. }
  106. /**
  107. * 敏感词过滤器类
  108. */
  109. export class SensitiveWordFilter {
  110. private sensitiveWords: string[] = [];
  111. private config: SensitiveWordsConfig | null = null;
  112. private isInitialized: boolean = false;
  113. constructor() {
  114. this.config = sensitiveWordsConfig;
  115. this.initialize();
  116. }
  117. /**
  118. * 初始化敏感词列表
  119. */
  120. async initialize(): Promise<boolean> {
  121. try {
  122. if (!this.config || !this.config.words) {
  123. throw new Error('配置文件格式错误');
  124. }
  125. // 如果是编码后的文件,需要解码
  126. if (this.config.encoding === 'base64') {
  127. this.sensitiveWords = this.config.words.map(word =>
  128. Buffer.from(word, 'base64').toString('utf8')
  129. );
  130. } else {
  131. this.sensitiveWords = [...this.config.words];
  132. }
  133. this.isInitialized = true;
  134. console.log(`✅ 敏感词过滤器初始化成功,加载了 ${this.sensitiveWords.length} 个敏感词`);
  135. return true;
  136. } catch (error) {
  137. console.error('❌ 初始化敏感词过滤器失败:', error);
  138. return false;
  139. }
  140. }
  141. /**
  142. * 检查文本中是否包含敏感词
  143. */
  144. checkText(text: string): FilterResult {
  145. if (!this.isInitialized) {
  146. throw new Error('过滤器未初始化,请先调用 initialize() 方法');
  147. }
  148. const detectedWords: string[] = [];
  149. let filteredText = text;
  150. // 检查每个敏感词
  151. for (const word of this.sensitiveWords) {
  152. // 处理正则表达式格式的敏感词
  153. const isRegex = word.includes('[') || word.includes('(') || word.includes('|');
  154. if (isRegex) {
  155. try {
  156. const regex = new RegExp(word, 'gi');
  157. const matches = text.match(regex);
  158. if (matches) {
  159. matches.forEach(match => {
  160. if (!detectedWords.includes(match)) {
  161. detectedWords.push(match);
  162. }
  163. });
  164. // 替换敏感词
  165. filteredText = filteredText.replace(regex, this.getMaskString());
  166. }
  167. } catch (regexError) {
  168. console.error('正则表达式格式错误:', regexError);
  169. // 如果正则表达式格式错误,按普通字符串处理
  170. if (text.toLowerCase().includes(word.toLowerCase())) {
  171. detectedWords.push(word);
  172. filteredText = this.replaceSensitiveWord(filteredText, word);
  173. }
  174. }
  175. } else {
  176. // 普通字符串匹配(忽略大小写)
  177. if (text.toLowerCase().includes(word.toLowerCase())) {
  178. detectedWords.push(word);
  179. filteredText = this.replaceSensitiveWord(filteredText, word);
  180. }
  181. }
  182. }
  183. const hasSensitiveWords = detectedWords.length > 0;
  184. return {
  185. hasSensitiveWords,
  186. detectedWords,
  187. filteredText,
  188. originalText: text,
  189. shouldRedirectToArrested: hasSensitiveWords
  190. };
  191. }
  192. /**
  193. * 替换敏感词
  194. */
  195. private replaceSensitiveWord(text: string, word: string): string {
  196. if (!this.config) return text;
  197. const regex = new RegExp(word, 'gi');
  198. if (this.config.mask_word && this.config.mask_word.trim() !== '') {
  199. // 如果设置了完整替换词,用它替换整个敏感词
  200. return text.replace(regex, this.config.mask_word);
  201. } else {
  202. // 否则用mask字符替换敏感词的每个字符
  203. return text.replace(regex, (match) => this.config!.mask.repeat(match.length));
  204. }
  205. }
  206. /**
  207. * 获取遮罩字符串
  208. */
  209. private getMaskString(): (match: string) => string {
  210. return (match: string) => {
  211. if (this.config?.mask_word && this.config.mask_word.trim() !== '') {
  212. return this.config.mask_word;
  213. } else {
  214. return (this.config?.mask || '*').repeat(match.length);
  215. }
  216. };
  217. }
  218. /**
  219. * 批量检查文本数组
  220. */
  221. checkTextArray(texts: string[]): FilterResult[] {
  222. return texts.map(text => this.checkText(text));
  223. }
  224. /**
  225. * 检查文本并决定是否跳转
  226. */
  227. async checkAndRedirect(text: string, redirectCallback?: () => void): Promise<FilterResult> {
  228. const result = this.checkText(text);
  229. if (result.shouldRedirectToArrested) {
  230. console.warn(`🚨 检测到敏感词: ${result.detectedWords.join(', ')}`);
  231. if (redirectCallback) {
  232. redirectCallback();
  233. } else {
  234. // 如果在浏览器环境中
  235. if (typeof window !== 'undefined' && window.location) {
  236. window.location.href = '/arrested';
  237. } else {
  238. console.log('🚨 应该跳转到 /arrested 页面');
  239. }
  240. }
  241. }
  242. return result;
  243. }
  244. }
  245. /**
  246. * 创建默认的敏感词过滤器实例
  247. */
  248. export const createSensitiveWordFilter = (): SensitiveWordFilter => {
  249. return new SensitiveWordFilter();
  250. };
  251. /**
  252. * 快速检查并跳转函数
  253. */
  254. export const quickCheck = async (
  255. text: string
  256. ): Promise<FilterResult> => {
  257. const filter = createSensitiveWordFilter();
  258. return filter.checkText(text);
  259. };
  260. export default SensitiveWordFilter;