cj-tauri 2.3.2 → 2.3.6
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/cjpm.toml +1 -1
- package/cli/cjpm.toml +12 -1
- package/cli/src/main.cj +8 -0
- package/cli/src/project.cj +15 -16
- package/cli/src/scaffold.cj +1 -1
- package/cli/src/sign_agc.cj +386 -0
- package/cli/src/sign_auto.cj +592 -0
- package/cli/src/sign_cloud.cj +724 -0
- package/cli/src/sign_cmd.cj +444 -0
- package/cli/src/sign_gcm.cj +703 -0
- package/cli/src/sign_login.cj +602 -0
- package/cli/src/sign_offline.cj +1183 -0
- package/cli/src/sign_test.cj +298 -0
- package/cli/src/sign_tool.cj +235 -0
- package/cli/src/sync.cj +90 -25
- package/cli/src/util.cj +26 -1
- package/cli/src/util_path_test.cj +72 -0
- package/cli/templates/app/README.md +1 -0
- package/cli/templates/app-react/README.md +1 -0
- package/cli/templates/app-vue/README.md +1 -0
- package/cli/templates/ohos-skeleton/README.md +19 -1
- package/cli/templates/ohos-skeleton/build-hap.bat +219 -0
- package/package.json +1 -1
- package/src/host_arkweb.cj +10 -0
- package/src/version.cj +1 -1
- package/cli/templates/ohos-skeleton/build-hap.ps1 +0 -205
package/cjpm.toml
CHANGED
package/cli/cjpm.toml
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
cjc-version = "1.0.5"
|
|
3
3
|
name = "cli"
|
|
4
4
|
description = "cj-tauri CLI:脚手架 create / dev / build / run / info"
|
|
5
|
-
version = "2.3.
|
|
5
|
+
version = "2.3.6"
|
|
6
6
|
target-dir = ""
|
|
7
7
|
output-type = "executable"
|
|
8
8
|
compile-option = ""
|
|
@@ -15,6 +15,17 @@
|
|
|
15
15
|
[target.x86_64-unknown-linux-gnu.bin-dependencies]
|
|
16
16
|
path-option = ["${CANGJIE_STDX}"]
|
|
17
17
|
|
|
18
|
+
[target.x86_64-w64-mingw32]
|
|
19
|
+
# -lcrypt32:CLI 的 sign_login / sign_agc 走 stdx.net.tls(HTTPS 登录与签名),它把 stdx.crypto.x509
|
|
20
|
+
# 带进来,那套代码读 Windows 系统证书存储(SystemRootCerts)。CertOpenSystemStoreA /
|
|
21
|
+
# CertEnumCertificatesInStore / CertCloseStore 三个符号都在 crypt32.dll 里,而仓颉默认链接行只给
|
|
22
|
+
# advapi32 / shell32 / user32 / kernel32 —— 少了这个库,Windows 上 `cjpm build` 直接死在链接期:
|
|
23
|
+
# `undefined symbol: __declspec(dllimport) CertOpenSystemStoreA`
|
|
24
|
+
# `>>> referenced by libstdx.crypto.x509.a(rootca.c.o):(DYN_CJ_SystemRootCerts)`
|
|
25
|
+
# 表象像「stdx 装坏了 / 版本不匹配」,其实只差一行链接选项。libcrypt32.a 随 SDK 的
|
|
26
|
+
# third_party/mingw/lib 一起发布,不需要任何外部依赖。
|
|
27
|
+
link-option = "-lcrypt32"
|
|
28
|
+
|
|
18
29
|
[target.x86_64-w64-mingw32.bin-dependencies]
|
|
19
30
|
# 与根 cjpm.toml 同口径:CLI 也 import stdx.encoding.json,Windows 上缺这一段时
|
|
20
31
|
# cjpm 报的是 `root package 'cli' imports package 'stdx.encoding.json' … not added as a
|
package/cli/src/main.cj
CHANGED
|
@@ -19,6 +19,11 @@ func usage(): Unit {
|
|
|
19
19
|
println(" cj-tauri doctor 环境自检逐行给结论(每条自带「缺了就怎么办」)")
|
|
20
20
|
println(" cj-tauri check 校验 cj-tauri.toml(id/窗口/资产存在)+ targets 骨架漂移")
|
|
21
21
|
println(" cj-tauri sync [desktop|ohos] 按清单派生 targets 骨架(缺省两端都派生)")
|
|
22
|
+
println(" cj-tauri sign [--path <ohos工程目录>] [--json]")
|
|
23
|
+
println(" 鸿蒙签名体检(离线):能不能装、缺什么、下一步")
|
|
24
|
+
println(" cj-tauri sign --apply 把本机匹配 UDID+包名的现成材料写回 build-profile.json5(离线)")
|
|
25
|
+
println(" cj-tauri sign --login 浏览器登录 AGC 一次,令牌加密落盘")
|
|
26
|
+
println(" cj-tauri sign --auto [--newKeys] 全自动签名:设备→材料→证书→profile→写回")
|
|
22
27
|
println(" cj-tauri help 显示本帮助")
|
|
23
28
|
println("")
|
|
24
29
|
println("环境变量:")
|
|
@@ -122,6 +127,9 @@ main(args: Array<String>): Int64 {
|
|
|
122
127
|
if (sub == "sync") {
|
|
123
128
|
return cmdSync(args)
|
|
124
129
|
}
|
|
130
|
+
if (sub == "sign") {
|
|
131
|
+
return cmdSign(args)
|
|
132
|
+
}
|
|
125
133
|
eprintln("未知子命令: ${sub}")
|
|
126
134
|
println("")
|
|
127
135
|
usage()
|
package/cli/src/project.cj
CHANGED
|
@@ -10,7 +10,7 @@ import std.collection.*
|
|
|
10
10
|
|
|
11
11
|
/** CLI 版本:须与 cli/cjpm.toml 的 version、CHANGELOG 顶部版本一致(scripts/check-version.sh 校验) */
|
|
12
12
|
func cliVersion(): String {
|
|
13
|
-
return "2.3.
|
|
13
|
+
return "2.3.6"
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
/** 一次命令所需的路径上下文 */
|
|
@@ -460,7 +460,7 @@ func runProject(c: AppContext): Int64 {
|
|
|
460
460
|
/** 某个目录下是不是鸿蒙脚手架工程(判据:构建入口脚本 + entry/cjpm.toml 同时在) */
|
|
461
461
|
func isOhosProjectAt(dir: String): Bool {
|
|
462
462
|
let hasEntryToml = exists(joinPath(joinPath(dir, "entry"), "cjpm.toml"))
|
|
463
|
-
return hasEntryToml && (exists(joinPath(dir, "build-hap.sh")) || exists(joinPath(dir, "build-hap.
|
|
463
|
+
return hasEntryToml && (exists(joinPath(dir, "build-hap.sh")) || exists(joinPath(dir, "build-hap.bat")))
|
|
464
464
|
}
|
|
465
465
|
|
|
466
466
|
/**
|
|
@@ -483,30 +483,29 @@ func findOhosProjectRoot(): Option<String> {
|
|
|
483
483
|
}
|
|
484
484
|
|
|
485
485
|
/**
|
|
486
|
-
* 鸿蒙工程:构建入口是工程自带的 `build-hap.sh`(bash)或 `build-hap.
|
|
486
|
+
* 鸿蒙工程:构建入口是工程自带的 `build-hap.sh`(bash)或 `build-hap.bat`(cmd / PowerShell)
|
|
487
487
|
*
|
|
488
488
|
* 跑它而不是自己拼 hvigor 命令:DevEco 探测、cjc/opt 同代、stdx 联接、`--no-daemon`
|
|
489
489
|
* 这些坑全在脚本里(见模板注释),CLI 只负责传参。两个入口语义一致,都在模板里维护、
|
|
490
|
-
* 改动请同步。Windows 优先
|
|
491
|
-
*
|
|
490
|
+
* 改动请同步。Windows 优先 bat(cmd 系统自带,cmd 与 PowerShell 窗口都能跑,bash 要另装
|
|
491
|
+
* Git Bash);bat 不存在才回落 bash。`--install` 走 `run`:编完装到设备并 `aa start`。
|
|
492
492
|
*/
|
|
493
493
|
func buildOhos(root: String, install: Bool): Int64 {
|
|
494
494
|
var argv = ArrayList<String>()
|
|
495
|
-
if (hostIsWindows() && exists(joinPath(root, "build-hap.
|
|
496
|
-
//
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
argv.add("
|
|
501
|
-
argv.add("
|
|
495
|
+
if (hostIsWindows() && exists(joinPath(root, "build-hap.bat"))) {
|
|
496
|
+
// bat 走 cmd.exe /c:cmd 是 Windows 自带、.bat 在 cmd 与 PowerShell 窗口里都能跑,
|
|
497
|
+
// 比 ps1 少了 ExecutionPolicy 那层限制(用户机器策略可能是 Restricted)。
|
|
498
|
+
let script = joinPath(root, "build-hap.bat")
|
|
499
|
+
println("[cj-tauri] 鸿蒙工程:build-hap.bat${if (install) { " --install" } else { "" }}(${root})")
|
|
500
|
+
argv.add("/c")
|
|
501
|
+
argv.add("\"" + script + "\"")
|
|
502
502
|
if (install) {
|
|
503
|
-
argv.add("
|
|
503
|
+
argv.add("--install")
|
|
504
504
|
}
|
|
505
|
-
|
|
506
|
-
let code = execute("powershell.exe", argv.toArray(), workingDirectory: Path(root))
|
|
505
|
+
let code = execute("cmd.exe", argv.toArray(), workingDirectory: Path(root))
|
|
507
506
|
if (code != 0) {
|
|
508
507
|
eprintln("[cj-tauri] 构建失败(exit=${code})")
|
|
509
|
-
eprintln("
|
|
508
|
+
eprintln(" 日志见上方 hvigor 输出")
|
|
510
509
|
}
|
|
511
510
|
return code
|
|
512
511
|
}
|
package/cli/src/scaffold.cj
CHANGED
|
@@ -352,7 +352,7 @@ func cmdCreate(nameArg: String, tpl: String, bundleArg: String, labelArg: String
|
|
|
352
352
|
if (ohos) {
|
|
353
353
|
println(" 鸿蒙端: export CANGJIE_STDX_OHOS=<鸿蒙版 stdx>/linux_ohos_aarch64_cjnative/dynamic/stdx")
|
|
354
354
|
if (hostIsWindows()) {
|
|
355
|
-
println(" cd targets\\ohos && npx cj-tauri build(走 build-hap.
|
|
355
|
+
println(" cd targets\\ohos && npx cj-tauri build(走 build-hap.bat)")
|
|
356
356
|
} else {
|
|
357
357
|
println(" cd targets/ohos && bash build-hap.sh [--install]")
|
|
358
358
|
}
|
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AGC 自动签名 · 常量与 HTTP 层(把 DevEco Studio 的自动签名流程脚本化)。
|
|
3
|
+
*
|
|
4
|
+
* 端点与常量抄自 flutter_flutter 提交 e2793de 的 packages/flutter_tools/lib/src/ohos/signing/
|
|
5
|
+
* (auth.dart / cloud.dart / device.dart / profile.dart / material.dart / keymanager.dart),
|
|
6
|
+
* 每条常量都有出处,不按猜测填。
|
|
7
|
+
*
|
|
8
|
+
* 三条贯穿全文件的纪律:
|
|
9
|
+
* ① AGC 的成功判据是 body 里的 ret.code == 0,不是 HTTP 200;
|
|
10
|
+
* ② 错误文案优先取 ret.msg(可能是对象、也可能是被 JSON 编码过的字符串);
|
|
11
|
+
* ③ 下载必须 HTTPS + 非内网(SSRF 护栏),且先校验 sha256 再落盘。
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
package cli
|
|
15
|
+
|
|
16
|
+
import std.collection.*
|
|
17
|
+
import std.crypto.digest.*
|
|
18
|
+
import std.io.*
|
|
19
|
+
import stdx.crypto.digest.*
|
|
20
|
+
import stdx.encoding.json.*
|
|
21
|
+
import stdx.net.http.*
|
|
22
|
+
import stdx.net.tls.TlsClientConfig
|
|
23
|
+
|
|
24
|
+
/** 登录与 JWT 用的站点(不是 AGC 业务 API 的基址) */
|
|
25
|
+
public let AGC_LOGIN_BASE: String = "https://cn.devecostudio.huawei.com"
|
|
26
|
+
/** AGC 业务 API 基址(证书/设备/profile) */
|
|
27
|
+
public let AGC_API_BASE: String = "https://connect-api.cloud.huawei.com"
|
|
28
|
+
|
|
29
|
+
/** DevEco 系列应用 ID(写死在 flutter 源码里的常量) */
|
|
30
|
+
public let AGC_APP_ID: String = "1009"
|
|
31
|
+
/** DevEco 侧 API 版本号 */
|
|
32
|
+
public let AGC_API_VERSION: String = "1.0.0"
|
|
33
|
+
/** 只支持中国大陆站点账号(回调里 siteId 必须是 "1") */
|
|
34
|
+
public let AGC_COUNTRY: String = "CN"
|
|
35
|
+
/** AGC 成功判据:body 里的 ret.code == 0 */
|
|
36
|
+
public let AGC_SUCCESS_CODE: Int64 = 0
|
|
37
|
+
|
|
38
|
+
/** 登录页路径(后面拼 ?port=&appid=&code=) */
|
|
39
|
+
public let AGC_APPLY_PATH: String = "console/DevEcoIDE/apply"
|
|
40
|
+
/** tempToken → JWT */
|
|
41
|
+
public let AGC_TEMP_TOKEN_PATH: String = "authrouter/auth/api/temptoken/check"
|
|
42
|
+
/** JWT 校验(取 userId / accessToken) */
|
|
43
|
+
public let AGC_JWT_CHECK_PATH: String = "authrouter/auth/api/jwToken/check"
|
|
44
|
+
/** 团队列表 */
|
|
45
|
+
public let AGC_TEAM_LIST_PATH: String = "/api/ups/user-permission-service/v1/user-team-list"
|
|
46
|
+
|
|
47
|
+
/** 证书 / 设备 / profile 端点(cloud.dart 常量) */
|
|
48
|
+
public let AGC_CERT_LIST_PATH: String = "/api/cps/harmony-cert-manage/v1/cert/list"
|
|
49
|
+
public let AGC_CERT_ADD_PATH: String = "/api/cps/harmony-cert-manage/v1/cert/add"
|
|
50
|
+
// 删除证书是 DELETE,且参数必须放请求体({"certIds":[...]}):放 query 一律被回
|
|
51
|
+
// `Body parameter is required.`,且体还要带 Content-Length(实测)。
|
|
52
|
+
public let AGC_CERT_DELETE_PATH: String = "/api/cps/harmony-cert-manage/v1/cert/delete"
|
|
53
|
+
public let AGC_DEVICE_LIST_PATH: String = "/api/cps/device-manage/v1/device/list"
|
|
54
|
+
public let AGC_DEVICE_ADD_PATH: String = "/api/cps/device-manage/v1/device/add"
|
|
55
|
+
public let AGC_PROVISION_ADD_TEST_PATH: String = "/api/cps/provision-manage/v1/ide/test/provision/add"
|
|
56
|
+
public let AGC_PROVISION_DELETE_PATH: String = "/api/cps/provision-manage/v1/provision/delete"
|
|
57
|
+
/** 材料下载:把对象 URL 换成带签名的临时下载链 */
|
|
58
|
+
public let AGC_DOWNLOAD_URL_PATH: String = "/api/amis/app-manage/v1/objects/url/reapply"
|
|
59
|
+
|
|
60
|
+
/** 回调服务只认这个路径(flutter 的 AuthLoopbackServer 同款) */
|
|
61
|
+
public let AGC_CALLBACK_PATH: String = "/callback"
|
|
62
|
+
/** 登录成功后重定向(浏览器上给用户看的页面) */
|
|
63
|
+
public let AGC_LOGIN_SUCCESS_PATH: String = "console/DevEcoCLI/loginSuccess"
|
|
64
|
+
public let AGC_LOGIN_FAILED_PATH: String = "console/DevEcoCLI/loginFailed"
|
|
65
|
+
|
|
66
|
+
// ============================================================
|
|
67
|
+
// 纯函数层(可单测,不联网)
|
|
68
|
+
// ============================================================
|
|
69
|
+
|
|
70
|
+
/** 登录页 URL:…/console/DevEcoIDE/apply?port=<port>&appid=1009&code=<32 位小写 hex> */
|
|
71
|
+
public func oauthLoginUrl(port: Int64, code: String): String {
|
|
72
|
+
return "${AGC_LOGIN_BASE}/${AGC_APPLY_PATH}?port=${port}&appid=${AGC_APP_ID}&code=${code}"
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** tempToken → JWT 的完整 URL(tempToken 按 & 截断后再编码,flutter 同款) */
|
|
76
|
+
public func tempTokenCheckUrl(tempToken: String): String {
|
|
77
|
+
var first = tempToken.trimAscii()
|
|
78
|
+
match (first.indexOf("&")) {
|
|
79
|
+
case Some(at) => if (at > 0) {
|
|
80
|
+
first = first[0..at]
|
|
81
|
+
}
|
|
82
|
+
case None => ()
|
|
83
|
+
}
|
|
84
|
+
return "${AGC_LOGIN_BASE}/${AGC_TEMP_TOKEN_PATH}?tempToken=${urlEncodeSign(first)}" +
|
|
85
|
+
"&site=${AGC_COUNTRY}&version=${AGC_API_VERSION}&appid=${AGC_APP_ID}"
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** JWT 校验地址(无 query) */
|
|
89
|
+
public func jwtCheckUrl(): String {
|
|
90
|
+
return "${AGC_LOGIN_BASE}/${AGC_JWT_CHECK_PATH}"
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** AGC 业务接口的完整 URL(path 以 / 开头) */
|
|
94
|
+
public func agcUrl(path: String): String {
|
|
95
|
+
return "${AGC_API_BASE}${path}"
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** 登录成功 / 失败的重定向地址 */
|
|
99
|
+
public func loginRedirectUrl(success: Bool): String {
|
|
100
|
+
let p = if (success) { AGC_LOGIN_SUCCESS_PATH } else { AGC_LOGIN_FAILED_PATH }
|
|
101
|
+
return "${AGC_LOGIN_BASE}/${p}"
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** 只接受中国大陆站点账号(回调里的 siteId 必须是字符串 "1") */
|
|
105
|
+
public func isChinaSite(siteId: String): Bool {
|
|
106
|
+
return siteId.trimAscii() == "1"
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** JWT 形状校验:xxx.yyy.zzz,三段都由 [\w-] 组成(登录接口成功时响应体本身就是 JWT) */
|
|
110
|
+
public func isJwtShaped(token: String): Bool {
|
|
111
|
+
let t = token.trimAscii()
|
|
112
|
+
if (t == "") {
|
|
113
|
+
return false
|
|
114
|
+
}
|
|
115
|
+
var dots: Int64 = 0
|
|
116
|
+
for (b in t.toArray()) {
|
|
117
|
+
if (b == 46) { // .
|
|
118
|
+
dots += 1
|
|
119
|
+
continue
|
|
120
|
+
}
|
|
121
|
+
let ok = (b >= 65 && b <= 90) || (b >= 97 && b <= 122) || (b >= 48 && b <= 57) ||
|
|
122
|
+
b == 95 || b == 45
|
|
123
|
+
if (!ok) {
|
|
124
|
+
return false
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return dots == 2
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** 解析 query 串成键值表(同一键重复时保留第一次) */
|
|
131
|
+
public func parseQueryParams(query: String): HashMap<String, String> {
|
|
132
|
+
let out = HashMap<String, String>()
|
|
133
|
+
let q = query.trimAscii()
|
|
134
|
+
if (q == "") {
|
|
135
|
+
return out
|
|
136
|
+
}
|
|
137
|
+
for (pair in q.split("&", -1)) {
|
|
138
|
+
if (pair == "") {
|
|
139
|
+
continue
|
|
140
|
+
}
|
|
141
|
+
match (pair.indexOf("=")) {
|
|
142
|
+
case Some(eq) =>
|
|
143
|
+
let k = urlDecodeSign(pair[0..eq])
|
|
144
|
+
let v = urlDecodeSign(pair[(eq + 1)..pair.size])
|
|
145
|
+
if (k != "" && !out.contains(k)) {
|
|
146
|
+
out.add(k, v)
|
|
147
|
+
}
|
|
148
|
+
case None =>
|
|
149
|
+
let bare = urlDecodeSign(pair)
|
|
150
|
+
if (bare != "" && !out.contains(bare)) {
|
|
151
|
+
out.add(bare, "")
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return out
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** percent-decode(%2F → /,+ → 空格);非法转义原样保留,不抛 */
|
|
159
|
+
public func urlDecodeSign(s: String): String {
|
|
160
|
+
let bytes = s.toArray()
|
|
161
|
+
let out = ArrayList<Byte>()
|
|
162
|
+
var i: Int64 = 0
|
|
163
|
+
while (i < bytes.size) {
|
|
164
|
+
let b = bytes[i]
|
|
165
|
+
if (b == 37 && i + 2 < bytes.size) { // %
|
|
166
|
+
let hi = signHexNibbleOf(bytes[i + 1])
|
|
167
|
+
let lo = signHexNibbleOf(bytes[i + 2])
|
|
168
|
+
if (hi >= 0 && lo >= 0) {
|
|
169
|
+
out.add(UInt8((hi << 4) | lo))
|
|
170
|
+
i += 3
|
|
171
|
+
continue
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
if (b == 43) { // + → 空格
|
|
175
|
+
out.add(UInt8(32))
|
|
176
|
+
} else {
|
|
177
|
+
out.add(b)
|
|
178
|
+
}
|
|
179
|
+
i += 1
|
|
180
|
+
}
|
|
181
|
+
return String.fromUtf8(out.toArray())
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** percent-encode(只保留 unreserved 字符,其余转 %XX) */
|
|
185
|
+
public func urlEncodeSign(s: String): String {
|
|
186
|
+
let out = ArrayList<Byte>()
|
|
187
|
+
let digits = "0123456789ABCDEF".toArray()
|
|
188
|
+
for (b in s.toArray()) {
|
|
189
|
+
let unreserved = (b >= 65 && b <= 90) || (b >= 97 && b <= 122) || (b >= 48 && b <= 57) ||
|
|
190
|
+
b == 45 || b == 95 || b == 46 || b == 126
|
|
191
|
+
if (unreserved) {
|
|
192
|
+
out.add(b)
|
|
193
|
+
} else {
|
|
194
|
+
out.add(UInt8(37))
|
|
195
|
+
out.add(digits[(Int64(b) >> 4) & 15])
|
|
196
|
+
out.add(digits[Int64(b) & 15])
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return String.fromUtf8(out.toArray())
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* 证书名(确定性的,让"要不要新建"变成一次名字查找):
|
|
204
|
+
* auto_debug_<teamId 去掉 \ / . :>.cer
|
|
205
|
+
*/
|
|
206
|
+
public func certNameFor(teamId: String): String {
|
|
207
|
+
let out = ArrayList<Byte>()
|
|
208
|
+
for (b in teamId.trimAscii().toArray()) {
|
|
209
|
+
let illegal = b == 92 || b == 47 || b == 46 || b == 58
|
|
210
|
+
out.add(if (illegal) { UInt8(95) } else { b })
|
|
211
|
+
}
|
|
212
|
+
return "auto_debug_${String.fromUtf8(out.toArray())}.cer"
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* profile 名:sha256("{product}_{bundle}_{bundle}") 的前 16 位十六进制。
|
|
217
|
+
* bundle 出现两次不是笔误——那是 DevEco/ark-cli 的确定性命名,改了就与已有 profile 对不上号。
|
|
218
|
+
*/
|
|
219
|
+
public func provisionNameOf(product: String, bundleName: String): String {
|
|
220
|
+
let seed = "${product}_${bundleName}_${bundleName}"
|
|
221
|
+
let hex = toHexLower(digest(SHA256(), seed.toArray()))
|
|
222
|
+
return hex[0..16]
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** AGC 响应是否成功:ret.code == 0(不是 HTTP 200) */
|
|
226
|
+
public func agcSucceeded(root: JsonValue): Bool {
|
|
227
|
+
match (root.kind()) {
|
|
228
|
+
case JsonKind.JsObject => ()
|
|
229
|
+
case _ => return false
|
|
230
|
+
}
|
|
231
|
+
match (root.asObject().get("ret")) {
|
|
232
|
+
case Some(r) => match (r.kind()) {
|
|
233
|
+
case JsonKind.JsObject => match (r.asObject().get("code")) {
|
|
234
|
+
case Some(c) => match (c.kind()) {
|
|
235
|
+
case JsonKind.JsInt => return c.asInt().getValue() == AGC_SUCCESS_CODE
|
|
236
|
+
case _ => return false
|
|
237
|
+
}
|
|
238
|
+
case None => return false
|
|
239
|
+
}
|
|
240
|
+
case _ => return false
|
|
241
|
+
}
|
|
242
|
+
case None => return false
|
|
243
|
+
}
|
|
244
|
+
return false
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** 从 AGC 响应里取错误文案(ret.msg 可能是对象,也可能是被 JSON 编码过的字符串) */
|
|
248
|
+
public func agcErrorMsg(root: JsonValue): String {
|
|
249
|
+
match (root.kind()) {
|
|
250
|
+
case JsonKind.JsObject => ()
|
|
251
|
+
case _ => return ""
|
|
252
|
+
}
|
|
253
|
+
match (root.asObject().get("ret")) {
|
|
254
|
+
case Some(r) => match (r.kind()) {
|
|
255
|
+
case JsonKind.JsObject => match (r.asObject().get("msg")) {
|
|
256
|
+
case Some(m) => match (m.kind()) {
|
|
257
|
+
case JsonKind.JsString => return m.asString().getValue()
|
|
258
|
+
case _ => return m.toString()
|
|
259
|
+
}
|
|
260
|
+
case None => return ""
|
|
261
|
+
}
|
|
262
|
+
case _ => return ""
|
|
263
|
+
}
|
|
264
|
+
case None => return ""
|
|
265
|
+
}
|
|
266
|
+
return ""
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/** AGC 错误码(body 里的 ret.code;不是 HTTP 状态码),取不到给 -1 */
|
|
270
|
+
public func agcErrorCode(root: JsonValue): Int64 {
|
|
271
|
+
match (root.kind()) {
|
|
272
|
+
case JsonKind.JsObject => ()
|
|
273
|
+
case _ => return -1
|
|
274
|
+
}
|
|
275
|
+
match (root.asObject().get("ret")) {
|
|
276
|
+
case Some(r) => match (r.kind()) {
|
|
277
|
+
case JsonKind.JsObject => match (r.asObject().get("code")) {
|
|
278
|
+
case Some(c) => match (c.kind()) {
|
|
279
|
+
case JsonKind.JsInt => return c.asInt().getValue()
|
|
280
|
+
case _ => return -1
|
|
281
|
+
}
|
|
282
|
+
case None => return -1
|
|
283
|
+
}
|
|
284
|
+
case _ => return -1
|
|
285
|
+
}
|
|
286
|
+
case None => return -1
|
|
287
|
+
}
|
|
288
|
+
return -1
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// ============================================================
|
|
292
|
+
// 动作层:HTTP 客户端
|
|
293
|
+
// ============================================================
|
|
294
|
+
|
|
295
|
+
/** 一次 HTTP 请求的最小结果 */
|
|
296
|
+
public class AgcHttpResult {
|
|
297
|
+
public var status: Int64 = 0
|
|
298
|
+
public var body: String = ""
|
|
299
|
+
public var error: String = ""
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* 发一次 GET/POST/DELETE。
|
|
304
|
+
*
|
|
305
|
+
* 三个"少一个就挂"的细节(实测才发现的):
|
|
306
|
+
* · HTTPS 必须显式 tlsConfig(TlsClientConfig()),否则 HttpException: TLS must be configured…;
|
|
307
|
+
* · 必须 .noProxy()——客户端默认读系统 http_proxy/https_proxy,连本机回环都可能被送去代理;
|
|
308
|
+
* · 删除证书是 DELETE 带请求体,用 get() 会把体丢掉,服务端回 Body parameter is required.
|
|
309
|
+
*/
|
|
310
|
+
public func agcHttp(method: String, url: String, headers: ArrayList<String>, body: String,
|
|
311
|
+
timeoutMs: Int64): AgcHttpResult {
|
|
312
|
+
let r = AgcHttpResult()
|
|
313
|
+
let isHttps = url.startsWith("https://")
|
|
314
|
+
let secs = if (timeoutMs <= 0) { 15 } else { timeoutMs / 1000 }
|
|
315
|
+
try {
|
|
316
|
+
var cb = ClientBuilder().readTimeout(Duration.second * secs).writeTimeout(Duration.second * secs)
|
|
317
|
+
if (isHttps) {
|
|
318
|
+
cb = cb.tlsConfig(TlsClientConfig())
|
|
319
|
+
}
|
|
320
|
+
cb = cb.noProxy()
|
|
321
|
+
let client = cb.build()
|
|
322
|
+
var rb = HttpRequestBuilder().url(url)
|
|
323
|
+
let verb = agcUpperAscii(method.trimAscii())
|
|
324
|
+
rb = if (verb == "POST") {
|
|
325
|
+
rb.post()
|
|
326
|
+
} else if (verb == "DELETE") {
|
|
327
|
+
rb.delete()
|
|
328
|
+
} else {
|
|
329
|
+
rb.get()
|
|
330
|
+
}
|
|
331
|
+
for (h in headers) {
|
|
332
|
+
match (h.indexOf(":")) {
|
|
333
|
+
case Some(cut) => if (cut > 0) {
|
|
334
|
+
rb = rb.header(h[0..cut].trimAscii(), h[(cut + 1)..h.size].trimAscii())
|
|
335
|
+
}
|
|
336
|
+
case None => ()
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
if (body != "") {
|
|
340
|
+
rb = rb.header("Content-Type", "application/json;charset=UTF-8")
|
|
341
|
+
rb = rb.body(body)
|
|
342
|
+
}
|
|
343
|
+
let resp = client.send(rb.build())
|
|
344
|
+
r.status = Int64(resp.status)
|
|
345
|
+
r.body = StringReader(resp.body).readToEnd()
|
|
346
|
+
try {
|
|
347
|
+
resp.close()
|
|
348
|
+
} catch (e: Exception) {
|
|
349
|
+
let _ = e
|
|
350
|
+
}
|
|
351
|
+
try {
|
|
352
|
+
client.close()
|
|
353
|
+
} catch (e: Exception) {
|
|
354
|
+
let _ = e
|
|
355
|
+
}
|
|
356
|
+
} catch (e: Exception) {
|
|
357
|
+
r.error = "HTTP 请求失败:${e}"
|
|
358
|
+
}
|
|
359
|
+
return r
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/** ASCII 大写(仓颉没有现成的 toUpperCaseAscii) */
|
|
363
|
+
public func agcUpperAscii(s: String): String {
|
|
364
|
+
let out = ArrayList<Byte>()
|
|
365
|
+
for (b in s.toArray()) {
|
|
366
|
+
if (b >= 97 && b <= 122) {
|
|
367
|
+
out.add(UInt8(Int64(b) - 32))
|
|
368
|
+
} else {
|
|
369
|
+
out.add(b)
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
return String.fromUtf8(out.toArray())
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/** ASCII 小写 */
|
|
376
|
+
public func agcLowerAscii(s: String): String {
|
|
377
|
+
let out = ArrayList<Byte>()
|
|
378
|
+
for (b in s.toArray()) {
|
|
379
|
+
if (b >= 65 && b <= 90) {
|
|
380
|
+
out.add(UInt8(Int64(b) + 32))
|
|
381
|
+
} else {
|
|
382
|
+
out.add(b)
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
return String.fromUtf8(out.toArray())
|
|
386
|
+
}
|