piclist 1.3.3 → 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 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,26 +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
- } else {
196
- config = {
197
- port: 36677
198
- }
199
- this.config = config
200
- picgo.saveConfig({
201
- 'settings.server': config
202
- })
203
- }
204
252
  this.httpServer = http.createServer(this.handleRequest)
205
253
  }
206
254
 
207
- checkIfConfigIsValid(config) {
208
- return config && config.port && config.host && config.enable !== undefined
209
- }
210
-
211
255
  handleRequest(request, response) {
212
256
  if (request.method === 'OPTIONS') {
213
257
  handleResponse({ response })
@@ -293,10 +337,10 @@ class Server {
293
337
  if (typeof port === 'string') {
294
338
  port = parseInt(port, 10)
295
339
  }
296
- this.httpServer.listen(port, '0.0.0.0').on('error', async err => {
340
+ this.httpServer.listen(port, defaultHost).on('error', async err => {
297
341
  if (err.errno === 'EADDRINUSE') {
298
342
  try {
299
- await axios.post(ensureHTTPLink(`${this.config.host}:${port}/heartbeat`))
343
+ await axios.post(ensureHTTPLink(`${defaultHost}:${port}/heartbeat`))
300
344
  this.shutdown(true)
301
345
  } catch (e) {
302
346
  console.log(`[PicList Server] ${port} is busy, trying with port ${port + 1}`)
@@ -307,8 +351,8 @@ class Server {
307
351
  }
308
352
 
309
353
  startup() {
310
- console.log('startup', this.config.enable)
311
- this.listen(this.config.port)
354
+ console.log('startup')
355
+ this.listen(defaultPort)
312
356
  }
313
357
 
314
358
  shutdown(hasStarted) {