Skip to content

Commit dd8f05a

Browse files
committed
feat: ssl-pinning适配harmony平台
1 parent ccb5ed0 commit dd8f05a

24 files changed

+1626
-6
lines changed

NativeSslPinningModule.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import type { TurboModule } from "react-native/Libraries/TurboModule/RCTExport";
2+
import { TurboModuleRegistry } from "react-native";
3+
4+
interface Cookies {
5+
[cookieName: string]: string;
6+
}
7+
8+
interface Header {
9+
[headerName: string]: string;
10+
}
11+
12+
interface Options {
13+
body?: string | object,
14+
responseType?: 'text' | 'base64',
15+
credentials?: string,
16+
headers?: string | object,
17+
method?: 'DELETE' | 'GET' | 'POST' | 'PUT',
18+
pkPinning?: boolean,
19+
sslPinning: {
20+
certs: string[]
21+
},
22+
timeoutInterval?: number,
23+
disableAllSecurity?: boolean,
24+
caseSensitiveHeaders?: boolean,
25+
}
26+
27+
interface Response {
28+
bodyString?: string;
29+
data?: string;
30+
headers: Header;
31+
status: number;
32+
url: string;
33+
json: () => Promise<{ [key: string]: any}>;
34+
text: () => Promise<string>;
35+
}
36+
37+
38+
export interface Spec extends TurboModule {
39+
getCookies(domain: string): Promise<Cookies>;
40+
fetch(url: string, options: Options, callback:(err: Object, res: Object) => void): Promise<Response>;
41+
removeCookieByName(cookieName: string): Promise<void>;
42+
}
43+
44+
export default TurboModuleRegistry.get<Spec>("RNSslPinning") as Spec;
45+

harmony/ssl_pinning/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/node_modules
2+
/oh_modules
3+
/.preview
4+
/build
5+
/.cxx
6+
/.test

harmony/ssl_pinning/BuildProfile.ets

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Use these variables when you tailor your ArkTS code. They must be of the const type.
3+
*/
4+
export const HAR_VERSION = '1.0.0';
5+
export const BUILD_MODE_NAME = 'debug';
6+
export const DEBUG = true;
7+
export const TARGET_NAME = 'default';
8+
9+
/**
10+
* BuildProfile Class is used only for compatibility purposes.
11+
*/
12+
export default class BuildProfile {
13+
static readonly HAR_VERSION = HAR_VERSION;
14+
static readonly BUILD_MODE_NAME = BUILD_MODE_NAME;
15+
static readonly DEBUG = DEBUG;
16+
static readonly TARGET_NAME = TARGET_NAME;
17+
}

harmony/ssl_pinning/Index.ets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./ts"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"targets": [
3+
{
4+
"name": "default"
5+
}
6+
]
7+
}

harmony/ssl_pinning/hvigorfile.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { harTasks } from '@ohos/hvigor-ohos-plugin';
2+
3+
export default {
4+
system: harTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
5+
plugins:[] /* Custom plugin to extend the functionality of Hvigor. */
6+
}

harmony/ssl_pinning/oh-package.json5

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "@react-native-oh-tpl/react-native-ssl-pinning",
3+
"version": "1.5.7",
4+
"description": "Please describe the basic information.",
5+
"main": "Index.ets",
6+
"author": "",
7+
"license": "Apache-2.0",
8+
"dependencies": {
9+
"@rnoh/react-native-openharmony": "file:../react_native_openharmony",
10+
"@ohos/httpclient": "2.0.2"
11+
}
12+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* MIT License
3+
*
4+
* Copyright (C) 2024 Huawei Device Co., Ltd.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
import { Chain, Interceptor, Response, Type, Utils } from '@ohos/httpclient';
26+
27+
export class CustomInterceptor implements Interceptor {
28+
async intercept(chain: Chain): Promise<Response> {
29+
return new Promise<Response>(async function (resolve, reject) {
30+
const userRequest = chain.requestI()
31+
var parsedUrl: string = userRequest.url.getUrl();
32+
if (null != userRequest.params) {
33+
var isbegin = true;
34+
Object.keys(userRequest.params).forEach(function (key) {
35+
console.info('Params key - ' + key + ', value - '
36+
+ userRequest.params[key].toString());
37+
parsedUrl = parsedUrl.toString().concat(isbegin ? '?' : '&');
38+
parsedUrl = parsedUrl.concat(key).concat('=').concat(userRequest.params[key].toString());
39+
isbegin = false;
40+
})
41+
}
42+
let response = chain.proceedI(userRequest)
43+
response.then((data) => {
44+
userRequest.cookieJar.saveFromResponse(data, parsedUrl, userRequest.cookieManager)
45+
})
46+
47+
response.then((data) => {
48+
resolve(data)
49+
}).catch((err) => {
50+
reject(err)
51+
})
52+
})
53+
}
54+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* MIT License
3+
*
4+
* Copyright (C) 2024 Huawei Device Co., Ltd.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
export class FetchOptions {
26+
body?: Object | string;
27+
responseType?: string;
28+
credentials?: string;
29+
method?: string;
30+
pkPinning?: boolean;
31+
headers?: Header;
32+
sslPinning: { certs: string[] };
33+
timeoutInterval?: number;
34+
disableAllSecurity?: boolean;
35+
caseSensitiveHeaders?: boolean
36+
}
37+
38+
export class CookiesItem {
39+
name: string;
40+
value: string;
41+
42+
constructor(name: string, value: string) {
43+
this.name = name;
44+
this.value = value;
45+
}
46+
47+
putString(name: string, value: string) {
48+
this.name = name;
49+
this.value = value;
50+
}
51+
}
52+
53+
export interface Cookies {
54+
[cookieName: string]: string;
55+
}
56+
57+
export interface Header {
58+
[headerName: string]: string;
59+
}
60+
61+
62+
export interface FetchResponse {
63+
bodyString?: string;
64+
data?: string;
65+
headers: Header;
66+
status: number;
67+
url: string;
68+
json: () => Promise<{ [key: string]: any }>;
69+
text: () => Promise<string>;
70+
}
71+
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* MIT License
3+
*
4+
* Copyright (C) 2024 Huawei Device Co., Ltd.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
import hilog from '@ohos.hilog';
26+
27+
export class Logger {
28+
private domain: number;
29+
private prefix: string;
30+
private format: string = '%{public}s, %{public}s';
31+
private isDebug: boolean;
32+
33+
/**
34+
* constructor.
35+
*
36+
* @param Prefix Identifies the log tag.
37+
* @param domain Domain Indicates the service domain, which is a hexadecimal integer ranging from 0x0 to 0xFFFFF.
38+
*/
39+
constructor(prefix: string = 'MyApp', domain: number = 0xFF00, isDebug = false) {
40+
this.prefix = prefix;
41+
this.domain = domain;
42+
this.isDebug = isDebug;
43+
}
44+
45+
debug(...args: string[]): void {
46+
if (this.isDebug) {
47+
hilog.debug(this.domain, this.prefix, this.format, args);
48+
}
49+
}
50+
51+
info(...args: string[]): void {
52+
hilog.info(this.domain, this.prefix, this.format, args);
53+
}
54+
55+
warn(...args: string[]): void {
56+
hilog.warn(this.domain, this.prefix, this.format, args);
57+
}
58+
59+
error(...args: string[]): void {
60+
hilog.error(this.domain, this.prefix, this.format, args);
61+
}
62+
}
63+
64+
export default new Logger('CalendarEvents', 0xFF00, false)

0 commit comments

Comments
 (0)