• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 
16 #include "napi_utils.h"
17 #include "netstack_log.h"
18 #include "context_key.h"
19 #include "proxy_options.h"
20 
21 namespace OHOS::NetStack::Socket {
22 
GetProxyType(uint32_t typeId)23 static ProxyType GetProxyType(uint32_t typeId)
24 {
25     if (typeId == 0) {
26         return ProxyType::NONE;
27     }
28     if (typeId == 1) {
29         return ProxyType::SOCKS5;
30     }
31     return ProxyType::UNKNOWN;
32 }
33 
ParseOptions(napi_env env,napi_value value)34 int ProxyOptions::ParseOptions(napi_env env, napi_value value)
35 {
36     napi_value options = NapiUtils::GetNamedProperty(env, value, KEY_PROXY);
37     if (NapiUtils::HasNamedProperty(env, options, KEY_TYPE)) {
38         auto typeId = NapiUtils::GetUint32Property(env, options, KEY_TYPE);
39         NETSTACK_LOGI("handle proxy type: %{public}d", typeId);
40 
41         type_ = GetProxyType(typeId);
42         if (type_ == ProxyType::NONE) {
43             return 0;
44         } else if (type_ == ProxyType::UNKNOWN) {
45             NETSTACK_LOGE("invalid proxy type");
46             return -1;
47         }
48     }
49 
50     if (!NapiUtils::HasNamedProperty(env, options, KEY_ADDRESS)) {
51         NETSTACK_LOGE("proxy options has not address");
52         return -1;
53     }
54 
55     if (NapiUtils::HasNamedProperty(env, options, KEY_USERNAME)) {
56         username_ = NapiUtils::GetStringPropertyUtf8(env, options, KEY_USERNAME);
57     }
58     if (NapiUtils::HasNamedProperty(env, options, KEY_PASSWORD)) {
59         password_ = NapiUtils::GetStringPropertyUtf8(env, options, KEY_PASSWORD);
60     }
61 
62     napi_value netAddress = NapiUtils::GetNamedProperty(env, options, KEY_ADDRESS);
63     if (!NapiUtils::HasNamedProperty(env, netAddress, KEY_PORT)) {
64         NETSTACK_LOGE("proxy options has not port");
65         return -1;
66     }
67 
68     std::string addr = NapiUtils::GetStringPropertyUtf8(env, netAddress, KEY_ADDRESS);
69     if (addr.empty()) {
70         NETSTACK_LOGE("proxy options address is empty");
71         return -1;
72     }
73 
74     if (NapiUtils::HasNamedProperty(env, netAddress, KEY_FAMILY)) {
75         uint32_t family = NapiUtils::GetUint32Property(env, netAddress, KEY_FAMILY);
76         address_.SetFamilyByJsValue(family);
77     }
78 
79     address_.SetAddress(addr, false);
80     if (address_.GetAddress().empty()) {
81         NETSTACK_LOGE("proxy options address is invalid");
82         return -1;
83     }
84 
85     uint16_t port = static_cast<uint16_t>(NapiUtils::GetUint32Property(env, netAddress, KEY_PORT));
86     address_.SetPort(port);
87     return 0;
88 }
89 } // namespace OHOS::NetStack::Socket
90