@fett/synology-api 0.0.2 → 0.0.3
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 +44 -5
- package/lib/core.d.ts +3 -0
- package/lib/core.js +3 -1
- package/lib/helpers.d.ts +2 -1
- package/lib/helpers.js +32 -24
- package/lib/types/core.d.ts +8 -0
- package/lib/types/core.js +9 -0
- package/lib/types/index.d.ts +1 -0
- package/lib/types/index.js +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -7,7 +7,9 @@
|
|
|
7
7
|
|
|
8
8
|
⚠️ The project is under development ...
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
**API Document:** [chrissong1994.github.io/synology-api/](chrissong1994.github.io/synology-api/)
|
|
11
|
+
|
|
12
|
+
Synology Api Javascript wrapper can be used in Browser、CLI or Nodejs to interact with Synology NAS.
|
|
11
13
|
You can use domain or ip address, also supports Synology Quick Connect connect Synology server.
|
|
12
14
|
All apis from [https://kb.synology.cn](https://kb.synology.cn/zh-cn/search?query=API&services%5B%5D=File_Station)
|
|
13
15
|
|
|
@@ -17,21 +19,58 @@ All apis from [https://kb.synology.cn](https://kb.synology.cn/zh-cn/search?query
|
|
|
17
19
|
npm install @fett/synology-api
|
|
18
20
|
```
|
|
19
21
|
|
|
22
|
+
## Configuration
|
|
23
|
+
|
|
24
|
+
`SynologyApi` instance parameters description
|
|
25
|
+
|
|
26
|
+
| Parameter | Type | Description | Default |
|
|
27
|
+
| :--------------------: | :-----------------: | :----------------------------------------------------------- | :-----: |
|
|
28
|
+
| server | string | Synology NAS address or QuickConnectId | - |
|
|
29
|
+
| quickConnectServerType | proxy \| wan \| lan | QuickConnect server type when connecting via QuickConnect ID | proxy |
|
|
30
|
+
| username | string | Synology NAS username | - |
|
|
31
|
+
| password | string | Synology NAS password | - |
|
|
32
|
+
|
|
33
|
+
You can choose to connect to the Synology server using either a **QuickConnectId** or **Synology server address**, for example:
|
|
34
|
+
|
|
35
|
+
### Connect via QuickConnectId
|
|
36
|
+
|
|
37
|
+
```js
|
|
38
|
+
const synologyApi = new SynologyApi({
|
|
39
|
+
server: "QuickConnectId",
|
|
40
|
+
quickConnectServerType: "lan", // my server is in LAN
|
|
41
|
+
username: "username",
|
|
42
|
+
password: "password",
|
|
43
|
+
});
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Connect via Synology server address
|
|
47
|
+
|
|
48
|
+
```js
|
|
49
|
+
const synologyApi = new SynologyApi({
|
|
50
|
+
server: "https://192.168.1.1:5001",
|
|
51
|
+
username: "username",
|
|
52
|
+
password: "password",
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
20
56
|
## Use In Browser or Node.js
|
|
21
57
|
|
|
58
|
+
```bash
|
|
59
|
+
npm install @fett/synology-api
|
|
60
|
+
```
|
|
61
|
+
|
|
22
62
|
First you need to confirm that you can access across domains,for example in the React Native environment
|
|
23
63
|
|
|
24
64
|
```js
|
|
25
65
|
import SynologyApi from '@fett/synology-api';
|
|
26
66
|
|
|
27
67
|
const synologyApi = new SynologyApi(
|
|
28
|
-
|
|
68
|
+
server: "https://192.168.1.1:5001", // or QuickConnectId
|
|
29
69
|
username: "username",
|
|
30
70
|
password: "password",
|
|
31
71
|
);
|
|
32
72
|
|
|
33
73
|
const info = await synologyApi.FileStation.getInfo();
|
|
34
|
-
console.log(info);
|
|
35
74
|
```
|
|
36
75
|
|
|
37
76
|
## Use In CLI
|
|
@@ -70,13 +109,13 @@ Commands:
|
|
|
70
109
|
add a connect configuration
|
|
71
110
|
|
|
72
111
|
```bash
|
|
73
|
-
syno config add
|
|
112
|
+
syno config add ConnetionName --server=https://192.168.1.1:5001 --username=admin --password=password
|
|
74
113
|
```
|
|
75
114
|
|
|
76
115
|
then you can use it and exec command
|
|
77
116
|
|
|
78
117
|
```bash
|
|
79
|
-
syno config use
|
|
118
|
+
syno config use ConnetionName
|
|
80
119
|
|
|
81
120
|
syno fs getInfo --pretty # print file system info
|
|
82
121
|
|
package/lib/core.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { AxiosRequestConfig } from "axios";
|
|
2
2
|
import { BaseSynologyApi } from "./modules/index.js";
|
|
3
|
+
import { QuickConnectServerType } from "./types/index.js";
|
|
3
4
|
interface Agent {
|
|
4
5
|
http?: {
|
|
5
6
|
host: string;
|
|
@@ -14,6 +15,7 @@ export interface SynologyApiOptions {
|
|
|
14
15
|
server: string;
|
|
15
16
|
username: string;
|
|
16
17
|
password: string;
|
|
18
|
+
quickConnectServerType?: QuickConnectServerType;
|
|
17
19
|
agent?: Agent;
|
|
18
20
|
}
|
|
19
21
|
export interface SynologyApiInfoData {
|
|
@@ -35,6 +37,7 @@ export declare class SynologyApi extends BaseSynologyApi {
|
|
|
35
37
|
agent?: Agent;
|
|
36
38
|
baseUrl: string;
|
|
37
39
|
isConnecting: boolean;
|
|
40
|
+
quickConnectServerType?: QuickConnectServerType;
|
|
38
41
|
private authInfo;
|
|
39
42
|
private apiInfo;
|
|
40
43
|
constructor(options: SynologyApiOptions);
|
package/lib/core.js
CHANGED
|
@@ -6,6 +6,7 @@ import { isHttpUrl, getApiKey, isUndfined } from "./utils/index.js";
|
|
|
6
6
|
import { getServerInfo } from "./helpers.js";
|
|
7
7
|
import { login, logout, getApiInfo } from "./modules/Api/index.js";
|
|
8
8
|
import { resWithErrorCode } from "./errorcodes.js";
|
|
9
|
+
import { QuickConnectServerType } from "./types/index.js";
|
|
9
10
|
export class SynologyApi extends BaseSynologyApi {
|
|
10
11
|
constructor(options) {
|
|
11
12
|
super();
|
|
@@ -15,13 +16,14 @@ export class SynologyApi extends BaseSynologyApi {
|
|
|
15
16
|
this.server = options.server;
|
|
16
17
|
this.username = options.username;
|
|
17
18
|
this.password = options.password;
|
|
19
|
+
this.quickConnectServerType = options.quickConnectServerType ?? QuickConnectServerType.proxy;
|
|
18
20
|
this.baseUrl = `${this.server}/webapi/`;
|
|
19
21
|
this.agent = options.agent ?? undefined;
|
|
20
22
|
}
|
|
21
23
|
async connect() {
|
|
22
24
|
// if quickconnect id
|
|
23
25
|
if (!isHttpUrl(this.server)) {
|
|
24
|
-
this.server = await getServerInfo(this.server);
|
|
26
|
+
this.server = await getServerInfo(this.server, this.quickConnectServerType);
|
|
25
27
|
this.baseUrl = `${this.server}/webapi/`;
|
|
26
28
|
}
|
|
27
29
|
try {
|
package/lib/helpers.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { QuickConnectServerType } from "./types/index.js";
|
|
1
2
|
export type ServerInfo = {
|
|
2
3
|
env: {
|
|
3
4
|
control_host: string;
|
|
@@ -15,5 +16,5 @@ export type ServerInfo = {
|
|
|
15
16
|
relay_port: number;
|
|
16
17
|
};
|
|
17
18
|
};
|
|
18
|
-
export declare const getServerInfo: (quickConnectId: string) => Promise<
|
|
19
|
+
export declare const getServerInfo: (quickConnectId: string, quickConnectServerType: QuickConnectServerType) => Promise<any>;
|
|
19
20
|
export declare const pingpang: (server: string) => Promise<boolean>;
|
package/lib/helpers.js
CHANGED
|
@@ -1,33 +1,40 @@
|
|
|
1
1
|
import Axios from "axios";
|
|
2
2
|
import { GLOBAL_QUICK_CONNECT_URL, QUICK_CONNECT_PINGPANG_API } from "./constants.js";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
3
|
+
import { QuickConnectServerType } from "./types/index.js";
|
|
4
|
+
const getServersFromServerInfo = async (serverInfo, quickConnectServerType) => {
|
|
5
|
+
const serverMap = {
|
|
6
|
+
[QuickConnectServerType.proxy]: undefined,
|
|
7
|
+
[QuickConnectServerType.lan]: undefined,
|
|
8
|
+
[QuickConnectServerType.wan]: undefined,
|
|
9
|
+
};
|
|
10
|
+
// proxy server
|
|
11
|
+
if (serverInfo?.service?.relay_ip) {
|
|
12
|
+
serverMap[QuickConnectServerType.proxy] =
|
|
13
|
+
`http://${serverInfo.service.relay_ip}:${serverInfo.service.relay_port}`;
|
|
12
14
|
}
|
|
13
15
|
// WAN IP
|
|
14
16
|
if (serverInfo?.server?.external?.ip) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
if (res) {
|
|
18
|
-
return server;
|
|
19
|
-
}
|
|
17
|
+
serverMap[QuickConnectServerType.wan] =
|
|
18
|
+
`http://${serverInfo.server.external.ip}:${serverInfo.service.port}`;
|
|
20
19
|
}
|
|
21
|
-
//
|
|
22
|
-
if (serverInfo?.
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
20
|
+
// lan ip
|
|
21
|
+
if (serverInfo?.server?.interface?.[0]) {
|
|
22
|
+
serverMap[QuickConnectServerType.lan] =
|
|
23
|
+
`http://${serverInfo.server.interface?.[0].ip}:${serverInfo.service.port}`;
|
|
24
|
+
}
|
|
25
|
+
const server = serverMap[quickConnectServerType];
|
|
26
|
+
if (!server) {
|
|
27
|
+
return Promise.reject(`${quickConnectServerType} server not found`);
|
|
28
|
+
}
|
|
29
|
+
const res = await pingpang(server);
|
|
30
|
+
if (!res) {
|
|
31
|
+
return Promise.reject(`${server} server can not connect`);
|
|
32
|
+
}
|
|
33
|
+
if (res) {
|
|
34
|
+
return server;
|
|
28
35
|
}
|
|
29
36
|
};
|
|
30
|
-
export const getServerInfo = async (quickConnectId) => {
|
|
37
|
+
export const getServerInfo = async (quickConnectId, quickConnectServerType) => {
|
|
31
38
|
const params = {
|
|
32
39
|
version: 1,
|
|
33
40
|
id: "dsm",
|
|
@@ -44,11 +51,12 @@ export const getServerInfo = async (quickConnectId) => {
|
|
|
44
51
|
platform: "web",
|
|
45
52
|
command: "request_tunnel",
|
|
46
53
|
};
|
|
54
|
+
// get replay tunnel
|
|
47
55
|
const result = (await Axios.post(`https://${serverInfo.env.control_host}/Serv.php`, relayRequestParams)).data;
|
|
48
|
-
return getServersFromServerInfo(result);
|
|
56
|
+
return getServersFromServerInfo(result, quickConnectServerType);
|
|
49
57
|
}
|
|
50
58
|
else {
|
|
51
|
-
return getServersFromServerInfo(serverInfo);
|
|
59
|
+
return getServersFromServerInfo(serverInfo, quickConnectServerType);
|
|
52
60
|
}
|
|
53
61
|
};
|
|
54
62
|
// pingpang
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* quick connect server type
|
|
3
|
+
* */
|
|
4
|
+
export var QuickConnectServerType;
|
|
5
|
+
(function (QuickConnectServerType) {
|
|
6
|
+
QuickConnectServerType["proxy"] = "proxy";
|
|
7
|
+
QuickConnectServerType["wan"] = "wan";
|
|
8
|
+
QuickConnectServerType["lan"] = "lan";
|
|
9
|
+
})(QuickConnectServerType || (QuickConnectServerType = {}));
|
package/lib/types/index.d.ts
CHANGED
package/lib/types/index.js
CHANGED