1
+ #!/usr/bin/env node
2
+
1
3
const fs = require ( 'fs' ) ;
2
4
const CryptoJS = require ( 'crypto-js' ) ;
3
5
const axios = require ( 'axios' ) ;
4
6
const { HttpsProxyAgent } = require ( 'https-proxy-agent' ) ;
5
7
6
- // 解密函数,暴露给外部使用
7
8
function decryptJson ( encryptedContent , secretKey ) {
8
9
const decrypted = CryptoJS . AES . decrypt ( encryptedContent , secretKey ) ;
9
10
return decrypted . toString ( CryptoJS . enc . Utf8 ) ;
10
11
}
11
12
12
- // 通过HTTP获取远程JSON文件并解密
13
13
async function decryptJsonFromUrl ( url , secretKey , proxyConfig = null ) {
14
14
try {
15
15
const axiosConfig = { } ;
@@ -34,76 +34,26 @@ async function decryptJsonFromUrl(url, secretKey, proxyConfig = null) {
34
34
}
35
35
}
36
36
37
- // 解析命令行参数
38
- const args = process . argv . slice ( 2 ) ;
39
- let inputFilePath = null ;
40
- let outputFilePath = './decrypted.json' ;
41
- let secretKey = 'your-default-secret-key' ;
42
- let url = null ;
43
- let proxyConfig = null ;
44
-
45
- // 参数处理
46
- args . forEach ( ( arg , index ) => {
47
- if ( arg . startsWith ( 'http' ) ) {
48
- url = arg ; // 如果第一个参数是URL
49
- } else if ( index === 0 && ! arg . startsWith ( 'http' ) && ! arg . includes ( ':' ) ) {
50
- inputFilePath = arg ; // 第一个参数是本地文件路径
51
- } else if ( index === 1 && ! arg . includes ( ':' ) ) {
52
- outputFilePath = arg ; // 第二个参数是输出文件路径
53
- } else if ( index === 2 && ! arg . includes ( ':' ) ) {
54
- secretKey = arg ; // 第三个参数是密钥
55
- } else if ( arg . includes ( ':' ) ) {
56
- // 处理代理配置参数
57
- const [ host , port ] = arg . split ( ':' ) ;
58
- proxyConfig = {
59
- host : host ,
60
- port : parseInt ( port , 10 )
61
- } ;
37
+ function decryptJsonFile ( inputFilePath , outputFilePath = './decrypted.json' , secretKey = 'your-default-secret-key' ) {
38
+ if ( ! inputFilePath ) {
39
+ throw new Error ( 'Please provide an input file path.' ) ;
62
40
}
63
- } ) ;
64
-
65
- // 检查是否提供了输入文件路径或URL
66
- if ( ! inputFilePath && ! url ) {
67
- console . error ( 'Please provide an input file path or URL.' ) ;
68
- process . exit ( 1 ) ;
69
- }
70
41
71
- if ( url ) {
72
- // 如果提供了URL,从远程获取并解密JSON
73
- decryptJsonFromUrl ( url , secretKey , proxyConfig )
74
- . then ( decryptedContent => {
75
- console . log ( 'Decrypted JSON content:' , decryptedContent ) ;
76
- fs . writeFile ( outputFilePath , decryptedContent , 'utf8' , ( err ) => {
77
- if ( err ) {
78
- console . error ( 'Error writing the decrypted JSON file:' , err ) ;
79
- return ;
80
- }
81
- console . log ( 'Decrypted JSON has been saved to:' , outputFilePath ) ;
82
- } ) ;
83
- } )
84
- . catch ( err => {
85
- console . error ( 'Error during decryption from URL:' , err ) ;
86
- } ) ;
87
- } else {
88
- // 读取加密后的JSON文件并解密
89
42
fs . readFile ( inputFilePath , 'utf8' , ( err , data ) => {
90
43
if ( err ) {
91
44
console . error ( 'Error reading the encrypted JSON file:' , err ) ;
92
45
return ;
93
46
}
94
47
95
48
try {
96
- // 解析加密的JSON内容
97
49
const encryptedContent = JSON . parse ( data ) . encrypted ;
98
50
99
51
if ( ! encryptedContent ) {
100
52
throw new Error ( 'No encrypted content found in the JSON file.' ) ;
101
53
}
102
54
103
- // 使用解密函数解密内容
104
55
const decryptedString = decryptJson ( encryptedContent , secretKey ) ;
105
56
106
- // 将解密后的内容写入新的JSON文件
107
57
fs . writeFile ( outputFilePath , decryptedString , 'utf8' , ( err ) => {
108
58
if ( err ) {
109
59
console . error ( 'Error writing the decrypted JSON file:' , err ) ;
@@ -117,8 +67,52 @@ if (url) {
117
67
} ) ;
118
68
}
119
69
120
- // 导出解密函数
70
+ // 如果是命令行调用
71
+ if ( require . main === module ) {
72
+ const args = process . argv . slice ( 2 ) ;
73
+ let inputFilePath = null ;
74
+ let outputFilePath = './decrypted.json' ;
75
+ let secretKey = 'your-default-secret-key' ;
76
+ let url = null ;
77
+ let proxyConfig = null ;
78
+
79
+ args . forEach ( ( arg , index ) => {
80
+ if ( arg . startsWith ( 'http' ) ) {
81
+ url = arg ;
82
+ } else if ( index === 0 && ! arg . startsWith ( 'http' ) && ! arg . includes ( ':' ) ) {
83
+ inputFilePath = arg ;
84
+ } else if ( index === 1 && ! arg . includes ( ':' ) ) {
85
+ outputFilePath = arg ;
86
+ } else if ( index === 2 && ! arg . includes ( ':' ) ) {
87
+ secretKey = arg ;
88
+ } else if ( arg . includes ( ':' ) ) {
89
+ const [ host , port ] = arg . split ( ':' ) ;
90
+ proxyConfig = { host, port : parseInt ( port , 10 ) } ;
91
+ }
92
+ } ) ;
93
+
94
+ if ( url ) {
95
+ decryptJsonFromUrl ( url , secretKey , proxyConfig )
96
+ . then ( decryptedContent => {
97
+ console . log ( 'Decrypted JSON content:' , decryptedContent ) ;
98
+ fs . writeFile ( outputFilePath , decryptedContent , 'utf8' , ( err ) => {
99
+ if ( err ) {
100
+ console . error ( 'Error writing the decrypted JSON file:' , err ) ;
101
+ return ;
102
+ }
103
+ console . log ( 'Decrypted JSON has been saved to:' , outputFilePath ) ;
104
+ } ) ;
105
+ } )
106
+ . catch ( err => {
107
+ console . error ( 'Error during decryption from URL:' , err ) ;
108
+ } ) ;
109
+ } else {
110
+ decryptJsonFile ( inputFilePath , outputFilePath , secretKey ) ;
111
+ }
112
+ }
113
+
121
114
module . exports = {
122
115
decryptJson,
123
- decryptJsonFromUrl
116
+ decryptJsonFromUrl,
117
+ decryptJsonFile
124
118
} ;
0 commit comments