@react-native-ohos/react-native-ssl-pinning
v1.7.0-beta.2
Published
React Native module for harmony
Maintainers
Keywords
Readme
模板版本:v0.4.0
[!TIP] Github 地址
请到三方库的 Releases 发布地址查看配套的版本信息:
| 三方库名称 | 三方库版本 | 发布信息 | 支持RN版本 | Autolink | 编译API版本 | 社区基线版本 | npm地址 | | ----------------------------------------------- | ---------------- | --------------------------------------------------------------------------- | --------------- | -------- | ------- | ------ | ------------------------------------------------------------------------------------------ | | @react-native-ohos/react-native-ssl-pinning | ~1.7.0(开发中) | Gitcode Releases | 0.82.* | 是 | API12+ | 1.6.0 | Npm Address | | @react-native-ohos/react-native-ssl-pinning | ~1.6.0 | Gitcode Releases | 0.77.* | 否 | API12+ | 1.5.9 | Npm Address | | @react-native-ohos/react-native-ssl-pinning | ~1.5.8 | Gitcode Releases | 0.72.* | 是 | API12+ | 1.5.7 | Npm Address | | @react-native-oh-tpl/react-native-ssl-pinning | <= 1.5.7-0.0.2@deprecated | Github Releases(deprecated) | 0.72.* | 否 | API12+ | 1.5.7 | Npm Address |
1. 安装与使用
进入到工程目录并输入以下命令:
npm
npm install @react-native-ohos/react-native-ssl-pinningyarn
yarn add @react-native-ohos/react-native-ssl-pinning下面的代码展示了这个库的基本使用场景:
[!WARNING] 使用时 import 的库名不变。
[!TIP] 本示例依赖 react-native-file-selector 库,参照 @react-native-ohos/react-native-file-selector 文档进行引入。
import React from 'react';
import {
SafeAreaView,
StyleSheet,
ScrollView,
View,
Text,
StatusBar,
Button,
Alert,
} from 'react-native';
import {Colors} from 'react-native/Libraries/NewAppScreen';
import {getCookies, fetch, removeCookieByName} from 'react-native-ssl-pinning';
import FileSelector from 'react-native-file-selector';
function SslPingDemo() : React.JSX.Element{
return (
<>
<StatusBar barStyle="dark-content" />
<SafeAreaView>
<ScrollView contentInsetAdjustmentBehavior="automatic" style={styles.scrollView}>
<View style={styles.body}>
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle}>fetch bY Public Key</Text>
<Text style={styles.sectionDescription}>
<Button
title="fetch"
onPress={() => {
fetch("https://jsnjlq.cn", {
method: "GET" ,
timeoutInterval: 10000, // milliseconds
pkPinning: true,
sslPinning: {
certs: ["sha256/kPwnudZVhc+iC/fTd3OPph8uugk1YN5ZsJDAeM2P4UU="]
},
headers: {
Accept: "application/json; charset=utf-8", "Access-Control-Allow-Origin": "*", "e_platform": "mobile",
}
}).then(
(result) => {
Alert.alert(JSON.stringify(result));
},
);
}}
/>
</Text>
</View>
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle}>fetch bY Certificate</Text>
<Text style={styles.sectionDescription}>
<Button
title="fetch"
onPress={() => {
fetch("https://jsnjlq.cn", {
method: "POST" ,
timeoutInterval: 10000, // milliseconds
sslPinning: {
certs: ["cert"]
},
headers: {
Accept: "application/json; charset=utf-8", "Access-Control-Allow-Origin": "*", "e_platform": "mobile",
}
}).then(response => {
Alert.alert(JSON.stringify(response));
}).catch(err => {
console.log(`error: ${err}`)
})
}}
/>
</Text>
</View>
<View style={styles.sectionContainer}>
<FileSelector
onDone={(path: string[]) => {
filePath = path
}}
onCancel={() => {console.log("cancelled");}}
/>
<Text style={styles.sectionTitle} >Multipart request</Text>
<Text style={styles.sectionDescription}>
<Button
title="fetch"
onPress={() => {
let formData = new FormData()
formData.append('username', 'Chris');
let filePathArray = filePath.length > 0 ?filePath[0].split("/") : ["test"]
Alert.alert(JSON.stringify(filePathArray[filePathArray.length - 1]));
formData.append('file', {
name: encodeURIComponent(filePathArray[filePathArray.length - 1]),
fileName: encodeURIComponent(filePathArray[filePathArray.length - 1]),
type: "multipart/form-data",
uri: filePath[0]
})
fetch("http://123.249.28.4:8888/handerOkhttpRequest/parse", {
method: "POST" ,
timeoutInterval: 10000, // milliseconds
body: {
formData: formData,
},
sslPinning: {
certs: ["cert"]
},
headers: {
accept: 'application/json, text/plain, /',
}
})
}}
/>
</Text>
</View>
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle}>getCookies</Text>
<Text style={styles.sectionDescription}>
<Button
title="fetch"
onPress={async () => {
getCookies("jsnjlq.cn").then(
(result) => {
Alert.alert(JSON.stringify(result));
},
);
}}
/>
</Text>
</View>
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle}>remove Cookie</Text>
<Text style={styles.sectionDescription}>
<Button
title="fetch"
onPress={async () => {
removeCookieByName("jsnjlq.cn").then(
(result) => {
Alert.alert(JSON.stringify(result));
},
);
}}
/>
</Text>
</View>
</View>
</ScrollView>
</SafeAreaView>
</>
);
};
const styles = StyleSheet.create({
scrollView: {
backgroundColor: Colors.lighter,
},
engine: {
position: 'absolute',
right: 0,
},
body: {
backgroundColor: Colors.white,
},
sectionContainer: {
marginTop: 32,
paddingHorizontal: 24,
},
sectionTitle: {
fontSize: 24,
fontWeight: '600',
color: Colors.black,
},
sectionDescription: {
marginTop: 8,
fontSize: 18,
fontWeight: '400',
color: Colors.dark,
},
highlight: {
fontWeight: '700',
},
footer: {
color: Colors.dark,
fontSize: 12,
fontWeight: '600',
padding: 4,
paddingRight: 12,
textAlign: 'right',
},
});
export default SslPingDemo;2. Link
| | 是否支持autolink | RN框架版本 | |--------------------------------------|-----------------|------------| | ~1.7.0 | 是 | 0.82 | | ~1.6.0 | 否 | 0.77 | | ~1.5.8 | 是 | 0.72 | | <= 1.5.7-0.0.2@deprecated | 否 | 0.72 |
使用AutoLink的工程需要根据该文档配置,Autolink框架指导文档:https://gitcode.com/openharmony-sig/ohos_react_native/blob/master/docs/zh-cn/Autolinking.md
如您使用的版本支持 Autolink,并且工程已接入 Autolink,可跳过ManualLink配置。
首先需要使用 DevEco Studio 打开项目里的 HarmonyOS 工程 harmony。
2.1. 在工程根目录的 oh-package.json5 添加 overrides 字段
{
...
"overrides": {
"@rnoh/react-native-openharmony" : "./react_native_openharmony"
}
}2.2. 引入原生端代码
目前有两种方法:
- 通过 har 包引入(在 IDE 完善相关功能后该方法会被遗弃,目前首选此方法);
- 直接链接源码。
方法一:通过 har 包引入(推荐)
[!TIP] har 包位于三方库安装路径的
harmony文件夹下。
打开 entry/oh-package.json5,添加以下依赖
"dependencies": {
"@rnoh/react-native-openharmony": "file:../react_native_openharmony",
"@react-native-ohos/react-native-ssl-pinning": "file:../../node_modules/@react-native-ohos/react-native-ssl-pinning/harmony/ssl_pinning.har"
}点击右上角的 sync 按钮
或者在终端执行:
cd entry
ohpm install方法二:直接链接源码
[!TIP] 如需使用直接链接源码,请参考直接链接源码说明
打开终端,执行:
cd entry
ohpm install --no-link2.3. 配置 CMakeLists 和引入 SslPinningPackage
打开 entry/src/main/cpp/CMakeLists.txt,添加:
project(rnapp)
cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_SKIP_BUILD_RPATH TRUE)
set(RNOH_APP_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
set(NODE_MODULES "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../node_modules")
+ set(OH_MODULES "${CMAKE_CURRENT_SOURCE_DIR}/../../../oh_modules")
set(RNOH_CPP_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../react-native-harmony/harmony/cpp")
set(LOG_VERBOSITY_LEVEL 1)
set(CMAKE_ASM_FLAGS "-Wno-error=unused-command-line-argument -Qunused-arguments")
set(CMAKE_CXX_FLAGS "-fstack-protector-strong -Wl,-z,relro,-z,now,-z,noexecstack -s -fPIE -pie")
set(WITH_HITRACE_SYSTRACE 1) # for other CMakeLists.txt files to use
add_compile_definitions(WITH_HITRACE_SYSTRACE)
add_subdirectory("${RNOH_CPP_DIR}" ./rn)
# RNOH_BEGIN: manual_package_linking_1
add_subdirectory("../../../../sample_package/src/main/cpp" ./sample-package)
+ add_subdirectory("${OH_MODULES}/@react-native-ohos/react-native-ssl-pinning/src/main/cpp" ./ssl_pinning)
# RNOH_END: manual_package_linking_1
file(GLOB GENERATED_CPP_FILES "./generated/*.cpp")
add_library(rnoh_app SHARED
${GENERATED_CPP_FILES}
"./PackageProvider.cpp"
"${RNOH_CPP_DIR}/RNOHAppNapiBridge.cpp"
)
target_link_libraries(rnoh_app PUBLIC rnoh)
# RNOH_BEGIN: manual_package_linking_2
target_link_libraries(rnoh_app PUBLIC rnoh_sample_package)
+ target_link_libraries(rnoh_app PUBLIC rnoh_ssl_pinning)
# RNOH_END: manual_package_linking_2打开 entry/src/main/cpp/PackageProvider.cpp,添加:
#include "RNOH/PackageProvider.h"
#include "generated/RNOHGeneratedPackage.h"
#include "SamplePackage.h"
>若使用RN框架版本是0.77,头文件路径如下:
+ #include "generated/SslPinningPackage.h"
>若使用RN框架版本是0.72,头文件路径如下:
+ #include "SslPinningPackage.h"
using namespace rnoh;
std::vector<std::shared_ptr<Package>> PackageProvider::getPackages(Package::Context ctx) {
return {
std::make_shared<RNOHGeneratedPackage>(ctx),
std::make_shared<SamplePackage>(ctx),
+ std::make_shared<SslPinningPackage>(ctx)
};
}2.4. 在 ArkTs 侧引入 SslPinningPackage
打开 entry/src/main/ets/RNPackagesFactory.ts,添加:
...
+ import { SslPinningPackage } from "@react-native-ohos/react-native-ssl-pinning/ts"
export function createRNPackages(ctx: RNPackageContext): RNPackage[] {
return [
new SamplePackage(ctx),
+ new SslPinningPackage(ctx),
];
}2.5. 运行
点击右上角的 sync 按钮
或者在终端执行:
cd entry
ohpm install然后编译、运行即可。
3. 约束与限制
3.1. 兼容性
要使用此库,需要使用正确的 React-Native 和 RNOH 版本。另外,还需要使用配套的 DevEco Studio 和 手机 ROM。
在以下版本验证通过:
- RNOH: 0.72.96; SDK: HarmonyOS 6.0.0 Release SDK; IDE: DevEco Studio 6.0.0.858; ROM: 6.0.0.112;
- RNOH: 0.72.33; SDK: HarmonyOS NEXT B1; IDE: DevEco Studio: 5.0.3.900; ROM: Next.0.0.71;
- RNOH: 0.77.18; SDK: HarmonyOS 6.0.0 Release SDK; IDE: DevEco Studio 6.0.0.858; ROM: 6.0.0.112;
- RNOH: 0.82.7; SDK: HarmonyOS 6.0.1 Release SDK; IDE: DevEco Studio 6.0.1 Release; ROM:6.0.0.328 SP26;
3.2. 权限要求
在 entry 目录下的module.json5中添加权限
打开 entry/src/main/module.json5,添加:
...
"requestPermissions": [
+ {
+ "name": "ohos.permission.INTERNET",
+ }
]4. 属性
[!TIP] "Platform"列表示该属性在原三方库上支持的平台。
[!TIP] "HarmonyOS Support"列为 yes 表示 HarmonyOS 平台支持该属性;no 则表示不支持;partially 表示部分支持。使用方法跨平台一致,效果对标 iOS 或 Android 的效果。
| Name | Description | Type | Required | Platform | HarmonyOS Support | |-------------------------------------|------------------------------------------------|-----------| -------- |------------|-------------------| | removeCookieByName(cookieName) | remove Cookies by cookieName | function | No | All | yes | | getCookies(domain) | query cookies by domain | function | No | All | yes | | fetch(hostname, options, callback) | Initiate a fetch request with the certificate. | function | No | All | yes |
5. 遗留问题
6. 其他
1: 如果使用证书锁定,需要把证书内置到entry目录rawfile文件夹中。 2: 本库从~1.7.0版本开始,提供debug模式调试能力。
7. 开源协议
本项目基于 The MIT License (MIT) ,请自由地享受和参与开源。
