1/* 2 * Copyright (C) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import type { AsyncCallback, Callback } from './@ohos.base'; 17import type connection from './@ohos.net.connection'; 18import type _AbilityContext from './application/UIAbilityContext'; 19 20/** 21 * Provides VPN related interfaces. 22 * @namespace vpn 23 * @syscap SystemCapability.Communication.NetManager.Vpn 24 * @since 10 25 */ 26declare namespace vpn { 27 export type LinkAddress = connection.LinkAddress; 28 export type RouteInfo = connection.RouteInfo; 29 export type AbilityContext = _AbilityContext; 30 31 /** 32 * Create a VPN connection using the AbilityContext. 33 * @param { AbilityContext } context - Indicates the context of application or capability. 34 * @returns { VpnConnection } the VpnConnection of the construct VpnConnection instance. 35 * @throws { BusinessError } 202 - Non-system applications use system APIs. 36 * @throws { BusinessError } 401 - Parameter error. 37 * @syscap SystemCapability.Communication.NetManager.Vpn 38 * @systemapi Hide this for inner system use. 39 * @since 10 40 */ 41 function createVpnConnection(context: AbilityContext): VpnConnection; 42 43 /** 44 * Defines a VPN connection. 45 * @interface VpnConnection 46 * @syscap SystemCapability.Communication.NetManager.Vpn 47 * @systemapi Hide this for inner system use. 48 * @since 10 49 */ 50 export interface VpnConnection { 51 /** 52 * Create a VPN network using the VpnConfig. 53 * @permission ohos.permission.MANAGE_VPN 54 * @param { VpnConfig } config - Indicates the {@link VpnConfig} configuration of the VPN network. 55 * @param { AsyncCallback<number> } callback - The callback is used to return file descriptor of VPN interface. 56 * @throws { BusinessError } 201 - Permission denied. 57 * @throws { BusinessError } 202 - Non-system applications use system APIs. 58 * @throws { BusinessError } 401 - Parameter error. 59 * @throws { BusinessError } 2200001 - Invalid parameter value. 60 * @throws { BusinessError } 2200002 - Operation failed. Cannot connect to service. 61 * @throws { BusinessError } 2200003 - System internal error. 62 * @throws { BusinessError } 2203001 - VPN creation denied, please check the user type. 63 * @throws { BusinessError } 2203002 - VPN exist already, please execute destroy first. 64 * @syscap SystemCapability.Communication.NetManager.Vpn 65 * @systemapi Hide this for inner system use. 66 * @since 10 67 */ 68 setUp(config: VpnConfig, callback: AsyncCallback<number>): void; 69 70 /** 71 * Create a VPN network using the VpnConfig. 72 * @permission ohos.permission.MANAGE_VPN 73 * @param { VpnConfig } config - Indicates the {@link VpnConfig} configuration of the VPN network. 74 * @returns { Promise<number> } The promise returns file descriptor of VPN interface. 75 * @throws { BusinessError } 201 - Permission denied. 76 * @throws { BusinessError } 202 - Non-system applications use system APIs. 77 * @throws { BusinessError } 401 - Parameter error. 78 * @throws { BusinessError } 2200001 - Invalid parameter value. 79 * @throws { BusinessError } 2200002 - Operation failed. Cannot connect to service. 80 * @throws { BusinessError } 2200003 - System internal error. 81 * @throws { BusinessError } 2203001 - VPN creation denied, please check the user type. 82 * @throws { BusinessError } 2203002 - VPN exist already, please execute destroy first. 83 * @syscap SystemCapability.Communication.NetManager.Vpn 84 * @systemapi Hide this for inner system use. 85 * @since 10 86 */ 87 setUp(config: VpnConfig): Promise<number>; 88 89 /** 90 * Protect a socket from VPN connections. After protecting, data sent through this socket will go directly to the 91 * underlying network so its traffic will not be forwarded through the VPN. 92 * @permission ohos.permission.MANAGE_VPN 93 * @param { number } socketFd - File descriptor of socket, this socket from @ohos.net.socket. 94 * @param { AsyncCallback<void> } callback - The callback of protect. 95 * @throws { BusinessError } 201 - Permission denied. 96 * @throws { BusinessError } 202 - Non-system applications use system APIs. 97 * @throws { BusinessError } 401 - Parameter error. 98 * @throws { BusinessError } 2200001 - Invalid parameter value. 99 * @throws { BusinessError } 2200002 - Operation failed. Cannot connect to service. 100 * @throws { BusinessError } 2200003 - System internal error. 101 * @throws { BusinessError } 2203004 - Invalid socket file descriptor. 102 * @syscap SystemCapability.Communication.NetManager.Vpn 103 * @systemapi Hide this for inner system use. 104 * @since 10 105 */ 106 protect(socketFd: number, callback: AsyncCallback<void>): void; 107 108 /** 109 * Protect a socket from VPN connections. After protecting, data sent through this socket will go directly to the 110 * underlying network so its traffic will not be forwarded through the VPN. 111 * @permission ohos.permission.MANAGE_VPN 112 * @param { number } socketFd - File descriptor of socket, this socket from @ohos.net.socket. 113 * @returns { Promise<void> } The promise returned by the function. 114 * @throws { BusinessError } 201 - Permission denied. 115 * @throws { BusinessError } 202 - Non-system applications use system APIs. 116 * @throws { BusinessError } 401 - Parameter error. 117 * @throws { BusinessError } 2200001 - Invalid parameter value. 118 * @throws { BusinessError } 2200002 - Operation failed. Cannot connect to service. 119 * @throws { BusinessError } 2200003 - System internal error. 120 * @throws { BusinessError } 2203004 - Invalid socket file descriptor. 121 * @syscap SystemCapability.Communication.NetManager.Vpn 122 * @systemapi Hide this for inner system use. 123 * @since 10 124 */ 125 protect(socketFd: number): Promise<void>; 126 127 /** 128 * Destroy the VPN network. 129 * @permission ohos.permission.MANAGE_VPN 130 * @param { AsyncCallback<void> } callback - The callback of destroy. 131 * @throws { BusinessError } 201 - Permission denied. 132 * @throws { BusinessError } 202 - Non-system applications use system APIs. 133 * @throws { BusinessError } 401 - Parameter error. 134 * @throws { BusinessError } 2200002 - Operation failed. Cannot connect to service. 135 * @throws { BusinessError } 2200003 - System internal error. 136 * @syscap SystemCapability.Communication.NetManager.Vpn 137 * @systemapi Hide this for inner system use. 138 * @since 10 139 */ 140 destroy(callback: AsyncCallback<void>): void; 141 142 /** 143 * Destroy the VPN network. 144 * @permission ohos.permission.MANAGE_VPN 145 * @returns { Promise<void> } The promise returned by the function. 146 * @throws { BusinessError } 201 - Permission denied. 147 * @throws { BusinessError } 202 - Non-system applications use system APIs. 148 * @throws { BusinessError } 401 - Parameter error. 149 * @throws { BusinessError } 2200002 - Operation failed. Cannot connect to service. 150 * @throws { BusinessError } 2200003 - System internal error. 151 * @syscap SystemCapability.Communication.NetManager.Vpn 152 * @systemapi Hide this for inner system use. 153 * @since 10 154 */ 155 destroy(): Promise<void>; 156 } 157 158 /** 159 * Define configuration of the VPN network. 160 * @interface VpnConfig 161 * @syscap SystemCapability.Communication.NetManager.Vpn 162 * @systemapi Hide this for inner system use. 163 * @since 10 164 */ 165 export interface VpnConfig { 166 /** 167 * The array of addresses for VPN interface. 168 * @type {Array<LinkAddress>} 169 * @syscap SystemCapability.Communication.NetManager.Vpn 170 * @systemapi Hide this for inner system use. 171 * @since 10 172 */ 173 addresses: Array<LinkAddress>; 174 175 /** 176 * The array of routes for VPN interface. 177 * @type {?Array<RouteInfo>} 178 * @syscap SystemCapability.Communication.NetManager.Vpn 179 * @systemapi Hide this for inner system use. 180 * @since 10 181 */ 182 routes?: Array<RouteInfo>; 183 184 /** 185 * The array of DNS servers for the VPN network. 186 * @type {?Array<string>} 187 * @syscap SystemCapability.Communication.NetManager.Vpn 188 * @systemapi Hide this for inner system use. 189 * @since 10 190 */ 191 dnsAddresses?: Array<string>; 192 193 /** 194 * The array of search domains for the DNS resolver. 195 * @type {?Array<string>} 196 * @syscap SystemCapability.Communication.NetManager.Vpn 197 * @systemapi Hide this for inner system use. 198 * @since 10 199 */ 200 searchDomains?: Array<string>; 201 202 /** 203 * The maximum transmission unit (MTU) for the VPN interface. 204 * @type {?number} 205 * @syscap SystemCapability.Communication.NetManager.Vpn 206 * @systemapi Hide this for inner system use. 207 * @since 10 208 */ 209 mtu?: number; 210 211 /** 212 * Whether ipv4 is supported. The default value is true. 213 * @type {?boolean} 214 * @syscap SystemCapability.Communication.NetManager.Vpn 215 * @systemapi Hide this for inner system use. 216 * @since 10 217 */ 218 isIPv4Accepted?: boolean; 219 220 /** 221 * Whether ipv6 is supported. The default value is false. 222 * @type {?boolean} 223 * @syscap SystemCapability.Communication.NetManager.Vpn 224 * @systemapi Hide this for inner system use. 225 * @since 10 226 */ 227 isIPv6Accepted?: boolean; 228 229 /** 230 * Whether to use the built-in VPN. The default value is false. 231 * @type {?boolean} 232 * @syscap SystemCapability.Communication.NetManager.Vpn 233 * @systemapi Hide this for inner system use. 234 * @since 10 235 */ 236 isLegacy?: boolean; 237 238 /** 239 * Whether the VPN interface's file descriptor is in blocking/non-blocking mode. The default value is false. 240 * @type {?boolean} 241 * @syscap SystemCapability.Communication.NetManager.Vpn 242 * @systemapi Hide this for inner system use. 243 * @since 10 244 */ 245 isBlocking?: boolean; 246 247 /** 248 * The array of trustlist for the VPN network. The string indicates package name. 249 * @type {?Array<string>} 250 * @syscap SystemCapability.Communication.NetManager.Vpn 251 * @systemapi Hide this for inner system use. 252 * @since 10 253 */ 254 trustedApplications?: Array<string>; 255 256 /** 257 * The array of blocklist for the VPN network. The string indicates package name. 258 * @type {?Array<string>} 259 * @syscap SystemCapability.Communication.NetManager.Vpn 260 * @systemapi Hide this for inner system use. 261 * @since 10 262 */ 263 blockedApplications?: Array<string>; 264 } 265} 266export default vpn; 267 268