huaweicloud-devkit 1.0.2-dev.9 → 1.0.2-next.0
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/.agents/plugins/marketplace.json +20 -20
- package/LICENSE +201 -201
- package/README.md +163 -138
- package/README.zh-CN.md +161 -138
- package/bin/setup.cjs +8 -8
- package/integrations/opencode/commands/huaweicloud-doctor.md +14 -14
- package/integrations/opencode/opencode.json +14 -14
- package/integrations/opencode/skills/huaweicloud-core/SKILL.md +32 -32
- package/package.json +46 -44
- package/plugins/huaweicloud-core/.claude-plugin/plugin.json +44 -44
- package/plugins/huaweicloud-core/.codex-plugin/plugin.json +43 -43
- package/plugins/huaweicloud-core/.cursor-plugin/plugin.json +44 -44
- package/plugins/huaweicloud-core/.mcp.json +15 -15
- package/plugins/huaweicloud-core/.workbuddy-plugin/plugin.json +44 -44
- package/plugins/huaweicloud-core/hooks/hooks.json +26 -26
- package/plugins/huaweicloud-core/hooks/huaweicloud-safety.py +136 -136
- package/plugins/huaweicloud-core/safety/policy.json +99 -99
- package/plugins/huaweicloud-core/safety/rules/cloud-risk-rules.json +199 -199
- package/plugins/huaweicloud-core/skills/huawei-apig/SKILL.md +164 -164
- package/plugins/huaweicloud-core/skills/huawei-billing/SKILL.md +48 -48
- package/plugins/huaweicloud-core/skills/huawei-cbr/SKILL.md +50 -50
- package/plugins/huaweicloud-core/skills/huawei-cce/SKILL.md +101 -101
- package/plugins/huaweicloud-core/skills/huawei-cloud-eye/SKILL.md +90 -90
- package/plugins/huaweicloud-core/skills/huawei-cloud-find-skills/SKILL.md +190 -190
- package/plugins/huaweicloud-core/skills/huawei-cloud-find-skills/scripts/search-skills.mjs +124 -124
- package/plugins/huaweicloud-core/skills/huawei-cts/SKILL.md +55 -55
- package/plugins/huaweicloud-core/skills/huawei-dds-dcs/SKILL.md +70 -70
- package/plugins/huaweicloud-core/skills/huawei-deployment/SKILL.md +56 -56
- package/plugins/huaweicloud-core/skills/huawei-dew/SKILL.md +97 -97
- package/plugins/huaweicloud-core/skills/huawei-dew/references/csms-usage.md +29 -29
- package/plugins/huaweicloud-core/skills/huawei-dew/references/kms-usage.md +23 -23
- package/plugins/huaweicloud-core/skills/huawei-ecs/SKILL.md +185 -185
- package/plugins/huaweicloud-core/skills/huawei-ecs/references/create-instance.md +133 -133
- package/plugins/huaweicloud-core/skills/huawei-ecs/references/evs.md +31 -31
- package/plugins/huaweicloud-core/skills/huawei-ecs/references/flavors.md +55 -55
- package/plugins/huaweicloud-core/skills/huawei-ecs/references/troubleshooting.md +52 -52
- package/plugins/huaweicloud-core/skills/huawei-functiongraph/SKILL.md +131 -131
- package/plugins/huaweicloud-core/skills/huawei-functiongraph/references/apig-event-format.md +58 -58
- package/plugins/huaweicloud-core/skills/huawei-functiongraph/references/create-function.md +71 -71
- package/plugins/huaweicloud-core/skills/huawei-functiongraph/references/deploy-workflow.md +119 -119
- package/plugins/huaweicloud-core/skills/huawei-functiongraph/references/triggers.md +142 -142
- package/plugins/huaweicloud-core/skills/huawei-gaussdb/SKILL.md +56 -56
- package/plugins/huaweicloud-core/skills/huawei-getting-started/SKILL.md +65 -65
- package/plugins/huaweicloud-core/skills/huawei-iam/SKILL.md +111 -111
- package/plugins/huaweicloud-core/skills/huawei-iam/references/policy-examples.md +122 -122
- package/plugins/huaweicloud-core/skills/huawei-iam/references/quick-ref.md +56 -56
- package/plugins/huaweicloud-core/skills/huawei-modelarts/SKILL.md +65 -65
- package/plugins/huaweicloud-core/skills/huawei-obs/SKILL.md +133 -127
- package/plugins/huaweicloud-core/skills/huawei-obs/references/bucket-create.md +49 -49
- package/plugins/huaweicloud-core/skills/huawei-obs/references/bucket-lifecycle.md +26 -26
- package/plugins/huaweicloud-core/skills/huawei-obs/references/common-pitfalls.md +100 -100
- package/plugins/huaweicloud-core/skills/huawei-obs/references/single-file-share.md +40 -40
- package/plugins/huaweicloud-core/skills/huawei-obs/references/static-website.md +56 -56
- package/plugins/huaweicloud-core/skills/huawei-rds/SKILL.md +224 -224
- package/plugins/huaweicloud-core/skills/huawei-sandbox/SKILL.md +135 -134
- package/plugins/huaweicloud-core/skills/huawei-smn-dms/SKILL.md +69 -69
- package/plugins/huaweicloud-core/skills/huawei-vpc/SKILL.md +84 -84
- package/plugins/huaweicloud-core/skills/huawei-vpc/references/network.md +76 -76
- package/plugins/huaweicloud-core/skills/huawei-vpc/references/security-group.md +59 -59
- package/plugins/huaweicloud-core/skills/huawei-vpc/references/subnet.md +50 -50
- package/plugins/huaweicloud-core/skills/huawei-waf-aad/SKILL.md +63 -63
- package/plugins/huaweicloud-core/skills/huaweicloud-api-and-sdk/SKILL.md +56 -56
- package/plugins/huaweicloud-core/skills/huaweicloud-api-and-sdk/agents/openai.yaml +4 -4
- package/plugins/huaweicloud-core/skills/huaweicloud-capability-discovery/SKILL.md +51 -47
- package/plugins/huaweicloud-core/skills/huaweicloud-capability-discovery/agents/openai.yaml +4 -4
- package/plugins/huaweicloud-core/skills/huaweicloud-cli-and-auth/SKILL.md +128 -128
- package/plugins/huaweicloud-core/skills/huaweicloud-cli-and-auth/agents/openai.yaml +4 -4
- package/plugins/huaweicloud-core/skills/huaweicloud-core/SKILL.md +110 -95
- package/plugins/huaweicloud-core/skills/huaweicloud-core/agents/openai.yaml +4 -4
- package/plugins/huaweicloud-core/skills/huaweicloud-core/references/report-issue.md +19 -19
- package/plugins/huaweicloud-core/skills/huaweicloud-core/references/select.md +27 -27
- package/plugins/huaweicloud-core/skills/huaweicloud-safety/SKILL.md +60 -60
- package/plugins/huaweicloud-core/skills/huaweicloud-safety/agents/openai.yaml +4 -4
- package/plugins/huaweicloud-core/skills/huaweicloud-troubleshooting/SKILL.md +83 -83
- package/plugins/huaweicloud-core/skills/huaweicloud-troubleshooting/agents/openai.yaml +4 -4
- package/plugins/huaweicloud-core/src/auth/agent-registration.mjs +105 -87
- package/plugins/huaweicloud-core/src/auth/credentials.mjs +95 -95
- package/plugins/huaweicloud-core/src/auth/service.mjs +63 -63
- package/plugins/huaweicloud-core/src/hcloud-cli.mjs +265 -265
- package/plugins/huaweicloud-core/src/mcp-server.mjs +129 -129
- package/plugins/huaweicloud-core/src/proxy/proxy-agent.mjs +155 -0
- package/plugins/huaweicloud-core/src/proxy/proxy-config.mjs +87 -0
- package/plugins/huaweicloud-core/src/risk-rule-engine.mjs +137 -137
- package/plugins/huaweicloud-core/src/safety-policy.mjs +302 -302
- package/plugins/huaweicloud-core/src/sandbox/hdkitservice-api.mjs +91 -87
- package/plugins/huaweicloud-core/src/sandbox/hwlink-api.mjs +162 -153
- package/plugins/huaweicloud-core/src/sandbox/session-manager.mjs +112 -105
- package/plugins/huaweicloud-core/src/search-market.mjs +174 -169
- package/plugins/huaweicloud-core/src/setup-cli.mjs +2017 -1594
- package/plugins/huaweicloud-core/src/tools.mjs +1008 -993
- package/plugins/huaweicloud-core/src/ws-exec/hwlink-exec-client.js +427 -427
- package/plugins/huaweicloud-core/src/ws-exec/hwlink-fair-queue.js +132 -132
- package/plugins/huaweicloud-core/src/ws-exec/hwlink-multiplexer.js +227 -227
- package/plugins/huaweicloud-core/src/ws-exec/hwlink-packet.js +202 -202
- package/plugins/huaweicloud-core/src/ws-exec/hwlink-terminal-channel.js +158 -158
- package/plugins/huaweicloud-core/src/ws-exec/index.js +19 -19
- package/plugins/huaweicloud-core/src/ws-exec/ws-exec-client.js +338 -338
|
@@ -1,202 +1,202 @@
|
|
|
1
|
-
const FIXED_HEADER_LEN = 20;
|
|
2
|
-
const MAX_SEND_CHUNK_SIZE = 8 * 1024;
|
|
3
|
-
|
|
4
|
-
const OpCode = Object.freeze({
|
|
5
|
-
OpCreateTcpTunnel: 0x03,
|
|
6
|
-
OpTcpTunnelData: 0x04,
|
|
7
|
-
OpListenTcpTunnel: 0x05,
|
|
8
|
-
OpReverseCreateTcpTunnel: 0x43,
|
|
9
|
-
OpCmdTerminalResize: 0xfa,
|
|
10
|
-
OpNewCmdTerminal: 0xfb,
|
|
11
|
-
OpCmdTerminalInit: 0xfc,
|
|
12
|
-
OpCmdTerminalData: 0xfd,
|
|
13
|
-
|
|
14
|
-
OpFileSuccess: 0x03 << 8,
|
|
15
|
-
OpSubStreamPing: 0x06 << 8,
|
|
16
|
-
OpSubStreamPong: 0x07 << 8,
|
|
17
|
-
OpTunnelSuccess: 0x08 << 8,
|
|
18
|
-
|
|
19
|
-
OpDisconnect: 0x03 << 16,
|
|
20
|
-
|
|
21
|
-
ErrCmdTerminal: 0x0d << 24,
|
|
22
|
-
ErrTcpTunnel: 0x0e << 24,
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
const ReserveSource = Object.freeze({
|
|
26
|
-
Web: 0x00,
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
let nextId = 1;
|
|
30
|
-
|
|
31
|
-
function nextIdentifier() {
|
|
32
|
-
nextId = ((nextId + 1) & 0xffffffff) >>> 0;
|
|
33
|
-
return nextId;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function isOpTunnelSuccess(op) {
|
|
37
|
-
return (op & 0xff00) === OpCode.OpTunnelSuccess;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function isOpTcpTunnelData(op) {
|
|
41
|
-
return (op & 0xff) === OpCode.OpTcpTunnelData;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function isOpFailed(op) {
|
|
45
|
-
return (op >> 24) !== 0;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function isOpCreateTcpTunnel(op) {
|
|
49
|
-
return (op & 0xff) === OpCode.OpCreateTcpTunnel;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function isOpReverseCreateTcpTunnel(op) {
|
|
53
|
-
return (op & 0xff) === OpCode.OpReverseCreateTcpTunnel;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function isOpCmdTerminalData(op) {
|
|
57
|
-
return (op & 0xff) === OpCode.OpCmdTerminalData;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
function isOpListenTcpTunnel(op) {
|
|
61
|
-
return (op & 0xff) === OpCode.OpListenTcpTunnel;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function isSubStreamPing(op) {
|
|
65
|
-
return (op & 0xff00) === OpCode.OpSubStreamPing;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function hex2(n) {
|
|
69
|
-
return `0x${n.toString(16).padStart(2, '0').toUpperCase()}`;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
function operationToString(op) {
|
|
73
|
-
return [
|
|
74
|
-
hex2((op >> 24) & 0xff),
|
|
75
|
-
hex2((op >> 16) & 0xff),
|
|
76
|
-
hex2((op >> 8) & 0xff),
|
|
77
|
-
hex2(op & 0xff),
|
|
78
|
-
].join(' ');
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
function formatPacketOneLine(packet) {
|
|
82
|
-
const err = (packet.operation >> 24) & 0xff;
|
|
83
|
-
const ctl = (packet.operation >> 16) & 0xff;
|
|
84
|
-
const sync = (packet.operation >> 8) & 0xff;
|
|
85
|
-
const data = packet.operation & 0xff;
|
|
86
|
-
const dataLen = packet.data ? packet.data.length : 0;
|
|
87
|
-
return (
|
|
88
|
-
`[${String(packet.headerLength).padStart(2)}]` +
|
|
89
|
-
`[${hex2(packet.reserve)}]` +
|
|
90
|
-
`[${String(packet.packetLength).padStart(5)}]` +
|
|
91
|
-
`[${hex2(err)} ${hex2(ctl)} ${hex2(sync)} ${hex2(data)}]` +
|
|
92
|
-
`[${String(packet.srcPort).padStart(5)}]` +
|
|
93
|
-
`[${String(packet.dstPort).padStart(5)}]` +
|
|
94
|
-
`[${String(packet.identifier).padStart(10)}]` +
|
|
95
|
-
`[${String(packet.source).padStart(10)}]` +
|
|
96
|
-
`[${String(dataLen).padStart(6)}B]`
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
function toUint8Array(bytes) {
|
|
101
|
-
if (bytes instanceof Uint8Array) return bytes;
|
|
102
|
-
if (bytes instanceof ArrayBuffer) return new Uint8Array(bytes);
|
|
103
|
-
if (ArrayBuffer.isView(bytes)) {
|
|
104
|
-
return new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
105
|
-
}
|
|
106
|
-
throw new TypeError('packet bytes must be an ArrayBuffer or Uint8Array');
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
function createPacket(opts) {
|
|
110
|
-
const {
|
|
111
|
-
operation,
|
|
112
|
-
reserve = 0,
|
|
113
|
-
srcPort = 0,
|
|
114
|
-
dstPort = 0,
|
|
115
|
-
identifier = 0,
|
|
116
|
-
source = 0,
|
|
117
|
-
payload = null,
|
|
118
|
-
} = opts;
|
|
119
|
-
|
|
120
|
-
const payloadLen = payload ? payload.length : 0;
|
|
121
|
-
const total = FIXED_HEADER_LEN + payloadLen;
|
|
122
|
-
const buf = new ArrayBuffer(total);
|
|
123
|
-
const view = new DataView(buf);
|
|
124
|
-
const u8 = new Uint8Array(buf);
|
|
125
|
-
|
|
126
|
-
view.setUint8(0, FIXED_HEADER_LEN / 4);
|
|
127
|
-
view.setUint8(1, reserve);
|
|
128
|
-
view.setUint16(2, total, false);
|
|
129
|
-
view.setUint32(4, operation, false);
|
|
130
|
-
view.setUint16(8, srcPort, false);
|
|
131
|
-
view.setUint16(10, dstPort, false);
|
|
132
|
-
view.setUint32(12, identifier, false);
|
|
133
|
-
view.setUint32(16, source, false);
|
|
134
|
-
|
|
135
|
-
if (payload && payloadLen) {
|
|
136
|
-
u8.set(payload, FIXED_HEADER_LEN);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
return u8;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
function parsePacket(bytes) {
|
|
143
|
-
const u8 = toUint8Array(bytes);
|
|
144
|
-
if (u8.byteLength < FIXED_HEADER_LEN) {
|
|
145
|
-
throw new Error(`packet too short: ${u8.byteLength} bytes`);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
const view = new DataView(u8.buffer, u8.byteOffset, u8.byteLength);
|
|
149
|
-
const headerLength = view.getUint8(0) * 4;
|
|
150
|
-
const reserve = view.getUint8(1);
|
|
151
|
-
const packetLength = view.getUint16(2, false);
|
|
152
|
-
const operation = view.getUint32(4, false);
|
|
153
|
-
const srcPort = view.getUint16(8, false);
|
|
154
|
-
const dstPort = view.getUint16(10, false);
|
|
155
|
-
const identifier = view.getUint32(12, false);
|
|
156
|
-
const source = view.getUint32(16, false);
|
|
157
|
-
|
|
158
|
-
if (headerLength < FIXED_HEADER_LEN) {
|
|
159
|
-
throw new Error(`invalid hwlink packet header length: ${headerLength}`);
|
|
160
|
-
}
|
|
161
|
-
if (packetLength > u8.byteLength) {
|
|
162
|
-
throw new Error(`incomplete hwlink packet: ${packetLength} > ${u8.byteLength}`);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
let data = null;
|
|
166
|
-
if (packetLength > FIXED_HEADER_LEN) {
|
|
167
|
-
data = u8.subarray(FIXED_HEADER_LEN, packetLength);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
return {
|
|
171
|
-
headerLength,
|
|
172
|
-
reserve,
|
|
173
|
-
packetLength,
|
|
174
|
-
operation,
|
|
175
|
-
srcPort,
|
|
176
|
-
dstPort,
|
|
177
|
-
identifier,
|
|
178
|
-
source,
|
|
179
|
-
data,
|
|
180
|
-
};
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
export {
|
|
184
|
-
FIXED_HEADER_LEN,
|
|
185
|
-
MAX_SEND_CHUNK_SIZE,
|
|
186
|
-
OpCode,
|
|
187
|
-
ReserveSource,
|
|
188
|
-
createPacket,
|
|
189
|
-
formatPacketOneLine,
|
|
190
|
-
isOpCmdTerminalData,
|
|
191
|
-
isOpCreateTcpTunnel,
|
|
192
|
-
isOpFailed,
|
|
193
|
-
isOpListenTcpTunnel,
|
|
194
|
-
isOpReverseCreateTcpTunnel,
|
|
195
|
-
isOpTcpTunnelData,
|
|
196
|
-
isOpTunnelSuccess,
|
|
197
|
-
isSubStreamPing,
|
|
198
|
-
nextIdentifier,
|
|
199
|
-
operationToString,
|
|
200
|
-
parsePacket,
|
|
201
|
-
toUint8Array,
|
|
202
|
-
};
|
|
1
|
+
const FIXED_HEADER_LEN = 20;
|
|
2
|
+
const MAX_SEND_CHUNK_SIZE = 8 * 1024;
|
|
3
|
+
|
|
4
|
+
const OpCode = Object.freeze({
|
|
5
|
+
OpCreateTcpTunnel: 0x03,
|
|
6
|
+
OpTcpTunnelData: 0x04,
|
|
7
|
+
OpListenTcpTunnel: 0x05,
|
|
8
|
+
OpReverseCreateTcpTunnel: 0x43,
|
|
9
|
+
OpCmdTerminalResize: 0xfa,
|
|
10
|
+
OpNewCmdTerminal: 0xfb,
|
|
11
|
+
OpCmdTerminalInit: 0xfc,
|
|
12
|
+
OpCmdTerminalData: 0xfd,
|
|
13
|
+
|
|
14
|
+
OpFileSuccess: 0x03 << 8,
|
|
15
|
+
OpSubStreamPing: 0x06 << 8,
|
|
16
|
+
OpSubStreamPong: 0x07 << 8,
|
|
17
|
+
OpTunnelSuccess: 0x08 << 8,
|
|
18
|
+
|
|
19
|
+
OpDisconnect: 0x03 << 16,
|
|
20
|
+
|
|
21
|
+
ErrCmdTerminal: 0x0d << 24,
|
|
22
|
+
ErrTcpTunnel: 0x0e << 24,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
const ReserveSource = Object.freeze({
|
|
26
|
+
Web: 0x00,
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
let nextId = 1;
|
|
30
|
+
|
|
31
|
+
function nextIdentifier() {
|
|
32
|
+
nextId = ((nextId + 1) & 0xffffffff) >>> 0;
|
|
33
|
+
return nextId;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function isOpTunnelSuccess(op) {
|
|
37
|
+
return (op & 0xff00) === OpCode.OpTunnelSuccess;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function isOpTcpTunnelData(op) {
|
|
41
|
+
return (op & 0xff) === OpCode.OpTcpTunnelData;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function isOpFailed(op) {
|
|
45
|
+
return (op >> 24) !== 0;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function isOpCreateTcpTunnel(op) {
|
|
49
|
+
return (op & 0xff) === OpCode.OpCreateTcpTunnel;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function isOpReverseCreateTcpTunnel(op) {
|
|
53
|
+
return (op & 0xff) === OpCode.OpReverseCreateTcpTunnel;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function isOpCmdTerminalData(op) {
|
|
57
|
+
return (op & 0xff) === OpCode.OpCmdTerminalData;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function isOpListenTcpTunnel(op) {
|
|
61
|
+
return (op & 0xff) === OpCode.OpListenTcpTunnel;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function isSubStreamPing(op) {
|
|
65
|
+
return (op & 0xff00) === OpCode.OpSubStreamPing;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function hex2(n) {
|
|
69
|
+
return `0x${n.toString(16).padStart(2, '0').toUpperCase()}`;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function operationToString(op) {
|
|
73
|
+
return [
|
|
74
|
+
hex2((op >> 24) & 0xff),
|
|
75
|
+
hex2((op >> 16) & 0xff),
|
|
76
|
+
hex2((op >> 8) & 0xff),
|
|
77
|
+
hex2(op & 0xff),
|
|
78
|
+
].join(' ');
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function formatPacketOneLine(packet) {
|
|
82
|
+
const err = (packet.operation >> 24) & 0xff;
|
|
83
|
+
const ctl = (packet.operation >> 16) & 0xff;
|
|
84
|
+
const sync = (packet.operation >> 8) & 0xff;
|
|
85
|
+
const data = packet.operation & 0xff;
|
|
86
|
+
const dataLen = packet.data ? packet.data.length : 0;
|
|
87
|
+
return (
|
|
88
|
+
`[${String(packet.headerLength).padStart(2)}]` +
|
|
89
|
+
`[${hex2(packet.reserve)}]` +
|
|
90
|
+
`[${String(packet.packetLength).padStart(5)}]` +
|
|
91
|
+
`[${hex2(err)} ${hex2(ctl)} ${hex2(sync)} ${hex2(data)}]` +
|
|
92
|
+
`[${String(packet.srcPort).padStart(5)}]` +
|
|
93
|
+
`[${String(packet.dstPort).padStart(5)}]` +
|
|
94
|
+
`[${String(packet.identifier).padStart(10)}]` +
|
|
95
|
+
`[${String(packet.source).padStart(10)}]` +
|
|
96
|
+
`[${String(dataLen).padStart(6)}B]`
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function toUint8Array(bytes) {
|
|
101
|
+
if (bytes instanceof Uint8Array) return bytes;
|
|
102
|
+
if (bytes instanceof ArrayBuffer) return new Uint8Array(bytes);
|
|
103
|
+
if (ArrayBuffer.isView(bytes)) {
|
|
104
|
+
return new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
105
|
+
}
|
|
106
|
+
throw new TypeError('packet bytes must be an ArrayBuffer or Uint8Array');
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function createPacket(opts) {
|
|
110
|
+
const {
|
|
111
|
+
operation,
|
|
112
|
+
reserve = 0,
|
|
113
|
+
srcPort = 0,
|
|
114
|
+
dstPort = 0,
|
|
115
|
+
identifier = 0,
|
|
116
|
+
source = 0,
|
|
117
|
+
payload = null,
|
|
118
|
+
} = opts;
|
|
119
|
+
|
|
120
|
+
const payloadLen = payload ? payload.length : 0;
|
|
121
|
+
const total = FIXED_HEADER_LEN + payloadLen;
|
|
122
|
+
const buf = new ArrayBuffer(total);
|
|
123
|
+
const view = new DataView(buf);
|
|
124
|
+
const u8 = new Uint8Array(buf);
|
|
125
|
+
|
|
126
|
+
view.setUint8(0, FIXED_HEADER_LEN / 4);
|
|
127
|
+
view.setUint8(1, reserve);
|
|
128
|
+
view.setUint16(2, total, false);
|
|
129
|
+
view.setUint32(4, operation, false);
|
|
130
|
+
view.setUint16(8, srcPort, false);
|
|
131
|
+
view.setUint16(10, dstPort, false);
|
|
132
|
+
view.setUint32(12, identifier, false);
|
|
133
|
+
view.setUint32(16, source, false);
|
|
134
|
+
|
|
135
|
+
if (payload && payloadLen) {
|
|
136
|
+
u8.set(payload, FIXED_HEADER_LEN);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return u8;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function parsePacket(bytes) {
|
|
143
|
+
const u8 = toUint8Array(bytes);
|
|
144
|
+
if (u8.byteLength < FIXED_HEADER_LEN) {
|
|
145
|
+
throw new Error(`packet too short: ${u8.byteLength} bytes`);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const view = new DataView(u8.buffer, u8.byteOffset, u8.byteLength);
|
|
149
|
+
const headerLength = view.getUint8(0) * 4;
|
|
150
|
+
const reserve = view.getUint8(1);
|
|
151
|
+
const packetLength = view.getUint16(2, false);
|
|
152
|
+
const operation = view.getUint32(4, false);
|
|
153
|
+
const srcPort = view.getUint16(8, false);
|
|
154
|
+
const dstPort = view.getUint16(10, false);
|
|
155
|
+
const identifier = view.getUint32(12, false);
|
|
156
|
+
const source = view.getUint32(16, false);
|
|
157
|
+
|
|
158
|
+
if (headerLength < FIXED_HEADER_LEN) {
|
|
159
|
+
throw new Error(`invalid hwlink packet header length: ${headerLength}`);
|
|
160
|
+
}
|
|
161
|
+
if (packetLength > u8.byteLength) {
|
|
162
|
+
throw new Error(`incomplete hwlink packet: ${packetLength} > ${u8.byteLength}`);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
let data = null;
|
|
166
|
+
if (packetLength > FIXED_HEADER_LEN) {
|
|
167
|
+
data = u8.subarray(FIXED_HEADER_LEN, packetLength);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return {
|
|
171
|
+
headerLength,
|
|
172
|
+
reserve,
|
|
173
|
+
packetLength,
|
|
174
|
+
operation,
|
|
175
|
+
srcPort,
|
|
176
|
+
dstPort,
|
|
177
|
+
identifier,
|
|
178
|
+
source,
|
|
179
|
+
data,
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export {
|
|
184
|
+
FIXED_HEADER_LEN,
|
|
185
|
+
MAX_SEND_CHUNK_SIZE,
|
|
186
|
+
OpCode,
|
|
187
|
+
ReserveSource,
|
|
188
|
+
createPacket,
|
|
189
|
+
formatPacketOneLine,
|
|
190
|
+
isOpCmdTerminalData,
|
|
191
|
+
isOpCreateTcpTunnel,
|
|
192
|
+
isOpFailed,
|
|
193
|
+
isOpListenTcpTunnel,
|
|
194
|
+
isOpReverseCreateTcpTunnel,
|
|
195
|
+
isOpTcpTunnelData,
|
|
196
|
+
isOpTunnelSuccess,
|
|
197
|
+
isSubStreamPing,
|
|
198
|
+
nextIdentifier,
|
|
199
|
+
operationToString,
|
|
200
|
+
parsePacket,
|
|
201
|
+
toUint8Array,
|
|
202
|
+
};
|