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 "socks5_none_method.h"
17
18 #include "netstack_log.h"
19 #include "socks5_utils.h"
20
21 namespace OHOS {
22 namespace NetStack {
23 namespace Socks5 {
RequestAuth(std::int32_t socketId,const std::string & userName,const std::string & password,const Socks5ProxyAddress & proxy)24 bool Socks5NoneMethod::RequestAuth(std::int32_t socketId, const std::string &userName, const std::string &password,
25 const Socks5ProxyAddress &proxy)
26 {
27 static_cast<void>(socketId);
28 static_cast<void>(userName);
29 static_cast<void>(password);
30 static_cast<void>(proxy);
31 return true;
32 }
33
RequestProxy(std::int32_t socketId,Socks5Command command,const Socket::NetAddress & destAddr,const Socks5ProxyAddress & proxy)34 std::pair<bool, Socks5ProxyResponse> Socks5NoneMethod::RequestProxy(std::int32_t socketId, Socks5Command command,
35 const Socket::NetAddress &destAddr, const Socks5ProxyAddress &proxy)
36 {
37 const Socket::NetAddress::Family family{destAddr.GetFamily()};
38 Socks5AddrType addrType;
39 if (family == Socket::NetAddress::Family::IPv4) {
40 addrType = Socks5AddrType::IPV4;
41 } else if (family == Socket::NetAddress::Family::IPv6) {
42 addrType = Socks5AddrType::IPV6;
43 } else {
44 addrType = Socks5AddrType::DOMAIN_NAME;
45 }
46
47 Socks5ProxyRequest request{};
48 request.version_ = SOCKS5_VERSION;
49 request.cmd_ = command;
50 request.reserved_ = 0U;
51 request.addrType_ = addrType;
52 request.destAddr_ = destAddr.GetAddress();
53 request.destPort_ = destAddr.GetPort();
54
55 const socklen_t addrLen{Socks5Utils::GetAddressLen(proxy.netAddress_)};
56 const std::pair<sockaddr *, socklen_t> addrInfo{proxy.addr_, addrLen};
57 Socks5ProxyResponse response{};
58 if (!Socks5Utils::RequestProxyServer(GetSocks5Instance(), socketId, addrInfo, &request, &response)) {
59 NETSTACK_LOGE("RequestProxy failed, socket is %{public}d", socketId);
60 return {false, response};
61 }
62 if (response.status_ != static_cast<uint8_t>(Socks5Status::SUCCESS)) {
63 GetSocks5Instance()->UpdateErrorInfo(Socks5Status::SOCKS5_FAIL_TO_CONNECT_REMOTE);
64 NETSTACK_LOGE("socks5 fail to request proxy, socket is %{public}d", socketId);
65 return {false, response};
66 }
67 return {true, response};
68 }
69
70 } // Socks5
71 } // NetStack
72 } // OHOS
73