• 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 #ifndef COMMUNICATION_NETSTACK_SOCKS5_INSTANCE_H
17 #define COMMUNICATION_NETSTACK_SOCKS5_INSTANCE_H
18 
19 #include <vector>
20 #include <memory>
21 #include <mutex>
22 #include <string>
23 
24 #include "net_address.h"
25 #include "proxy_options.h"
26 #include "socks5.h"
27 #include "socks5_method.h"
28 #include "socket_exec.h"
29 
30 namespace OHOS {
31 namespace NetStack {
32 namespace Socks5 {
33 class Socks5Instance {
34 public:
35     Socks5Instance() = default;
36     virtual ~Socks5Instance() = default;
37 
38     virtual bool Connect() = 0;
39     virtual bool RemoveHeader(void *data, size_t &len, int af);
40     virtual void AddHeader();
41     virtual std::string GetHeader();
42     virtual void SetHeader(std::string header);
43     virtual void Close();
44 
45     void SetSocks5Option(const std::shared_ptr<Socks5Option> &opt);
46     void SetDestAddress(const Socket::NetAddress &dest);
47     bool IsConnected() const;
48     void UpdateErrorInfo(Socks5Status status);
49     void UpdateErrorInfo(int32_t errCode, const std::string &errMessage);
50     int32_t GetErrorCode() const;
51     std::string GetErrorMessage() const;
52     void OnProxySocketError();
53     void SetSocks5Instance(const std::shared_ptr<Socks5Instance> &socks5Inst);
54     Socket::NetAddress GetProxyBindAddress() const;
55     int GetSocketId() const;
56     Socket::SocketExec::SocketRecvCallback GetProxySocketRecvCallback() const;
57     bool ConnectProxy();
58     void CloseSocket();
59 
60 protected:
61     bool RequestMethod(const std::vector<Socks5MethodType> &methods);
62     std::shared_ptr<Socks5Method> CreateSocks5MethodByType(Socks5MethodType type) const;
63     bool DoConnect(Socks5Command command);
64 
65     int32_t socketId_{SOCKS5_INVALID_SOCKET_FD};
66     Socket::NetAddress dest_{};
67     std::shared_ptr<Socks5Option> options_{nullptr};
68     std::shared_ptr<Socks5Method> method_{nullptr};
69     Socks5AuthState state_{Socks5AuthState::INIT};
70     Socket::NetAddress proxyBindAddr_{};
71     int32_t doConnectCount_{};
72 
73 private:
74     int32_t errorCode_{};
75     std::string errorMessage_{};
76     std::shared_ptr<Socks5Instance> socks5Instance_;
77 };
78 
79 class Socks5TcpInstance final : public Socks5Instance {
80 public:
81     Socks5TcpInstance() = delete;
82     explicit Socks5TcpInstance(int32_t socketId);
83     ~Socks5TcpInstance() = default;
84 
85     bool Connect();
86 };
87 
88 class Socks5UdpInstance final : public Socks5Instance, std::enable_shared_from_this<Socks5UdpInstance> {
89 public:
90     Socks5UdpInstance() = default;
91     ~Socks5UdpInstance();
92 
93     bool Connect() override;
94     bool RemoveHeader(void *data, size_t &len, int af) override;
95     void AddHeader() override;
96     std::string GetHeader() override;
97     void SetHeader(std::string header) override;
98     void Close() override;
99 
100 private:
101     bool CreateSocket();
102 
103     std::string header_;
104 };
105 
106 class Socks5TlsInstance final : public Socks5Instance {
107 public:
108     Socks5TlsInstance() = delete;
109     explicit Socks5TlsInstance(int32_t socketId);
110     ~Socks5TlsInstance() = default;
111 
112     bool Connect();
113 };
114 } // Socks5
115 } // NetStack
116 } // OHOS
117 #endif // COMMUNICATION_NETSTACK_SOCKS5_INSTANCE_H
118