piclist 1.3.2 → 1.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +40 -0
- package/bin/picgo-server +66 -29
- package/dist/index.cjs.js +1 -1
- package/dist/index.esm.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -59,6 +59,46 @@ yarn add piclist -D
|
|
|
59
59
|
|
|
60
60
|
## Usage
|
|
61
61
|
|
|
62
|
+
### Server
|
|
63
|
+
|
|
64
|
+
You can use `picgo-server` to start a server, default port is `36677`.
|
|
65
|
+
|
|
66
|
+
Start server:
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
picgo-server
|
|
70
|
+
node ./bin/picgo-server
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
> It's highly recommended to add `--key` to avoid unauthorized access. Example: `picgo-server --key 123456`,
|
|
74
|
+
|
|
75
|
+
Show help:
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
$ picgo-server -h
|
|
79
|
+
|
|
80
|
+
Usage: picgo-server [options]
|
|
81
|
+
|
|
82
|
+
Options:
|
|
83
|
+
|
|
84
|
+
-h, --help Print this help message
|
|
85
|
+
-c, --config Set config path
|
|
86
|
+
-p, --port Set port, default port is 36677
|
|
87
|
+
--host Set host, default host is 0.0.0.0
|
|
88
|
+
-k, --key Set secret key to avoid unauthorized access
|
|
89
|
+
-v, --version Print version number
|
|
90
|
+
|
|
91
|
+
Examples:
|
|
92
|
+
picgo-server -c /path/to/config.json
|
|
93
|
+
picgo-server -k 123456
|
|
94
|
+
picgo-server -c /path/to/config.json -k 123456
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
#### endpoints
|
|
98
|
+
|
|
99
|
+
- `/upload?picbed=xxx&key=xxx` upload picture, `picbed` to set pic-bed, `key` to set secret key
|
|
100
|
+
- `/heartbeat` heartbeat
|
|
101
|
+
|
|
62
102
|
### Use in CLI
|
|
63
103
|
|
|
64
104
|
> PicList-Core uses `SM.MS` as the default upload pic-bed.
|
package/bin/picgo-server
CHANGED
|
@@ -10,7 +10,57 @@ const os = require('os')
|
|
|
10
10
|
const tempDir = path.join(os.homedir(), '.piclist', 'serverTemp')
|
|
11
11
|
fs.ensureDirSync(tempDir)
|
|
12
12
|
|
|
13
|
+
let key = ''
|
|
14
|
+
let defaultPort = 36677
|
|
15
|
+
let defaultHost = '0.0.0.0'
|
|
16
|
+
|
|
13
17
|
const argv = minimist(process.argv.slice(2))
|
|
18
|
+
|
|
19
|
+
if (argv.h || argv.help) {
|
|
20
|
+
showHelp()
|
|
21
|
+
process.exit(0)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (argv.v || argv.version) {
|
|
25
|
+
showVersion()
|
|
26
|
+
process.exit(0)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (argv.k || argv.key) {
|
|
30
|
+
key = String(argv.k || argv.key)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (argv.p || argv.port) {
|
|
34
|
+
defaultPort = Number(argv.p || argv.port) || defaultPort
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (argv.host) {
|
|
38
|
+
defaultHost = String(argv.host)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function showVersion() {
|
|
42
|
+
const pkg = require('../package.json')
|
|
43
|
+
console.log(`PicList-Core ${pkg.version}`)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function showHelp() {
|
|
47
|
+
console.log(`
|
|
48
|
+
Usage: picgo-server [options]
|
|
49
|
+
Options:
|
|
50
|
+
-h, --help Print this help message
|
|
51
|
+
-c, --config Set config path
|
|
52
|
+
-p, --port Set port, default port is 36677
|
|
53
|
+
--host Set host, default host is 0.0.0.0
|
|
54
|
+
-k, --key Set secret key to avoid unauthorized access
|
|
55
|
+
-v, --version Print version number
|
|
56
|
+
|
|
57
|
+
Example:
|
|
58
|
+
picgo-server -c /path/to/config.json
|
|
59
|
+
picgo-server -k 123456
|
|
60
|
+
picgo-server -c /path/to/config.json -k 123456
|
|
61
|
+
`)
|
|
62
|
+
}
|
|
63
|
+
|
|
14
64
|
let configPath = argv.c || argv.config || ''
|
|
15
65
|
if (configPath !== true && configPath !== '') {
|
|
16
66
|
configPath = path.resolve(configPath)
|
|
@@ -86,9 +136,20 @@ const uploadMulter = multer({
|
|
|
86
136
|
storage: multerStorage
|
|
87
137
|
})
|
|
88
138
|
|
|
89
|
-
router.post('/upload', async ({response, list = [], urlparams }) => {
|
|
139
|
+
router.post('/upload', async ({ response, list = [], urlparams }) => {
|
|
90
140
|
try {
|
|
91
141
|
const picbed = urlparams?.get('picbed')
|
|
142
|
+
const passedKey = urlparams?.get('key')
|
|
143
|
+
if (key && key !== passedKey) {
|
|
144
|
+
console.log('[PicList Server] Unauthorized access')
|
|
145
|
+
return handleResponse({
|
|
146
|
+
response,
|
|
147
|
+
body: {
|
|
148
|
+
success: false,
|
|
149
|
+
message: 'Unauthorized access'
|
|
150
|
+
}
|
|
151
|
+
})
|
|
152
|
+
}
|
|
92
153
|
let currentPicBedType = ''
|
|
93
154
|
let needRestore = false
|
|
94
155
|
if (picbed) {
|
|
@@ -188,31 +249,9 @@ router.post('/heartbeat', async ({ response }) => {
|
|
|
188
249
|
|
|
189
250
|
class Server {
|
|
190
251
|
constructor() {
|
|
191
|
-
let config = picgo.getConfig('settings.server')
|
|
192
|
-
const result = this.checkIfConfigIsValid(config)
|
|
193
|
-
if (result) {
|
|
194
|
-
this.config = config
|
|
195
|
-
if (this.config.host === '127.0.0.1') {
|
|
196
|
-
this.config.host = '0.0.0.0'
|
|
197
|
-
}
|
|
198
|
-
} else {
|
|
199
|
-
config = {
|
|
200
|
-
port: 36677,
|
|
201
|
-
host: '0.0.0.0',
|
|
202
|
-
enable: true
|
|
203
|
-
}
|
|
204
|
-
this.config = config
|
|
205
|
-
picgo.saveConfig({
|
|
206
|
-
'settings.server': config
|
|
207
|
-
})
|
|
208
|
-
}
|
|
209
252
|
this.httpServer = http.createServer(this.handleRequest)
|
|
210
253
|
}
|
|
211
254
|
|
|
212
|
-
checkIfConfigIsValid(config) {
|
|
213
|
-
return config && config.port && config.host && config.enable !== undefined
|
|
214
|
-
}
|
|
215
|
-
|
|
216
255
|
handleRequest(request, response) {
|
|
217
256
|
if (request.method === 'OPTIONS') {
|
|
218
257
|
handleResponse({ response })
|
|
@@ -298,10 +337,10 @@ class Server {
|
|
|
298
337
|
if (typeof port === 'string') {
|
|
299
338
|
port = parseInt(port, 10)
|
|
300
339
|
}
|
|
301
|
-
this.httpServer.listen(port,
|
|
340
|
+
this.httpServer.listen(port, defaultHost).on('error', async err => {
|
|
302
341
|
if (err.errno === 'EADDRINUSE') {
|
|
303
342
|
try {
|
|
304
|
-
await axios.post(ensureHTTPLink(`${
|
|
343
|
+
await axios.post(ensureHTTPLink(`${defaultHost}:${port}/heartbeat`))
|
|
305
344
|
this.shutdown(true)
|
|
306
345
|
} catch (e) {
|
|
307
346
|
console.log(`[PicList Server] ${port} is busy, trying with port ${port + 1}`)
|
|
@@ -312,10 +351,8 @@ class Server {
|
|
|
312
351
|
}
|
|
313
352
|
|
|
314
353
|
startup() {
|
|
315
|
-
console.log('startup'
|
|
316
|
-
|
|
317
|
-
this.listen(this.config.port)
|
|
318
|
-
}
|
|
354
|
+
console.log('startup')
|
|
355
|
+
this.listen(defaultPort)
|
|
319
356
|
}
|
|
320
357
|
|
|
321
358
|
shutdown(hasStarted) {
|