Skip to content

Commit f369784

Browse files
authored
CLI: Supports arguments (#16)
* CLI: Supports arguments * Fix MSVC build * Fix help message
1 parent 5b69770 commit f369784

File tree

9 files changed

+297
-129
lines changed

9 files changed

+297
-129
lines changed

README.md

Lines changed: 72 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ AHP使用了部分C++17特性,所以对编译器的版本有较高要求,下
2222
- GCC >= 7.3
2323
- Clang >= 6.0
2424

25-
如果你想要使用低版本的编译器(C++11)来编译,可以试试[这个分支](https://github.com/lxrite/azure-http-proxy/tree/cpp11)
26-
2725
### 安装依赖
2826

2927
- OpenSSL
@@ -34,31 +32,38 @@ AHP使用了部分C++17特性,所以对编译器的版本有较高要求,下
3432

3533
##### Ubuntu
3634

37-
$ apt-get install libssl-dev
35+
```shell
36+
$ apt-get install libssl-dev
37+
```
3838

39-
##### Fedora
39+
##### CentOS
4040

41-
$ yum install openssl
42-
$ yum install openssl-devel
41+
```shell
42+
$ yum install openssl
43+
$ yum install openssl-devel
44+
```
4345

4446
#### Windows
4547

4648
Windows可以使用[vcpkg](https://github.com/Microsoft/vcpkg)来安装OpenSSL。
4749

48-
$ vcpkg install openssl
50+
```shell
51+
$ vcpkg install openssl
52+
```
4953

5054
### 编译
5155
AHP使用自动化构建工具CMake来实现跨平台构建
5256

5357
- CMake >= 2.8
5458

5559
Windows下可以使用cmake-gui.exe,Linux或其他类Unix系统可以使用下面的命令编译
56-
57-
$ cd azure-http-proxy
58-
$ mkdir build
59-
$ cd build
60-
$ cmake -DCMAKE_BUILD_TYPE=Release ..
61-
$ cmake --build .
60+
```shell
61+
$ cd azure-http-proxy
62+
$ mkdir build
63+
$ cd build
64+
$ cmake -DCMAKE_BUILD_TYPE=Release ..
65+
$ cmake --build .
66+
```
6267

6368
如果编译成功会生成ahpc(客户端)和ahps(服务端)。
6469

@@ -71,34 +76,36 @@ OpenWrt/LEDE 编译参考 [openwrt-ahp](https://github.com/lxrite/openwrt-ahp)
7176
注意:不要使用示例配置中的RSA私钥和公钥,因为私钥一公开就是不安全的了。
7277

7378
如果你要运行的是服务端,那么你首先需要生成一对RSA密钥对,AHP支持任意长度不小于1024位的RSA密钥。下面的命令使用openssl生成2048位的私钥和公钥
74-
75-
$ openssl genrsa -out rsa_private_key.pem 2048
76-
$ openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem
79+
```shell
80+
$ openssl genrsa -out rsa_private_key.pem 2048
81+
$ openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem
82+
```
7783

7884
服务端保留私钥并将公钥告诉客户端。
7985

8086
### 配置服务端
8187

82-
编辑server.json文件,Windows下应将此文件放到ahps.exe同目录下,Linux或其他类Unix系统将此文件放到~/.ahps/server.json。
83-
88+
编辑`server.json`文件
89+
```json
90+
{
91+
"bind_address": "0.0.0.0",
92+
"listen_port": 8090,
93+
"rsa_private_key": "-----BEGIN RSA PRIVATE KEY----- ...... -----END RSA PRIVATE KEY-----",
94+
"timeout": 240,
95+
"workers": 4,
96+
"auth": true,
97+
"users": [
8498
{
85-
"bind_address": "0.0.0.0",
86-
"listen_port": 8090,
87-
"rsa_private_key": "-----BEGIN RSA PRIVATE KEY----- ...... -----END RSA PRIVATE KEY-----",
88-
"timeout": 240,
89-
"workers": 4,
90-
"auth": true,
91-
"users": [
92-
{
93-
"username": "username1",
94-
"password": "password1"
95-
},
96-
{
97-
"username": "foobar",
98-
"password": "bazqux"
99-
}
100-
]
99+
"username": "username1",
100+
"password": "password1"
101+
},
102+
{
103+
"username": "foobar",
104+
"password": "bazqux"
101105
}
106+
]
107+
}
108+
```
102109

103110
字段名 | 描述 | 是否必选 | 默认值 |
104111
----------------|--------------------|------------------|-----------|
@@ -112,18 +119,19 @@ users | 用户列表 | auth为true时必选 | 无 |
112119

113120
### 配置客户端
114121

115-
编辑client.json文件,Windows下应将此文件放到ahpc.exe或ahpc-gui.exe同目录下,Linux或其他类Unix系统将此文件放到~/.ahpc/client.json。
116-
117-
{
118-
"proxy_server_address": "127.0.0.1",
119-
"proxy_server_port": 8090,
120-
"bind_address": "127.0.0.1",
121-
"listen_port": 8089,
122-
"rsa_public_key": "-----BEGIN PUBLIC KEY----- ...... -----END PUBLIC KEY-----",
123-
"cipher": "aes-256-cfb",
124-
"timeout": 240,
125-
"workers": 2
126-
}
122+
编辑`client.json`文件
123+
```json
124+
{
125+
"proxy_server_address": "127.0.0.1",
126+
"proxy_server_port": 8090,
127+
"bind_address": "127.0.0.1",
128+
"listen_port": 8089,
129+
"rsa_public_key": "-----BEGIN PUBLIC KEY----- ...... -----END PUBLIC KEY-----",
130+
"cipher": "aes-256-cfb",
131+
"timeout": 240,
132+
"workers": 2
133+
}
134+
```
127135

128136
字段名 | 描述 | 是否必选 | 默认值 |
129137
---------------------|----------------------|------------------|---------------|
@@ -152,31 +160,35 @@ workers | 并发工作线程数 | 否 | 2
152160

153161
### 运行服务端
154162

155-
Linux或其他类Unix系统
156-
157-
$ ./ahps
158-
159-
Windows
160-
161-
$ ahps.exe
162-
163+
Linux或其他类Unix系统
164+
```shell
165+
$ ./ahps -c server.json
166+
```
167+
168+
Windows
169+
```shell
170+
$ ahps.exe -c server.json
171+
```
172+
163173
### 运行客户端
164174

165175
Linux或其他类Unix系统
166-
167-
$ ./ahpc
176+
```shell
177+
$ ./ahpc -c client.json
178+
```
168179

169180
Windows
170-
171-
$ ahpc.exe
181+
```shell
182+
$ ahpc.exe -c client.json
183+
```
172184

173185
## 使用Docker
174-
``` bash
186+
```shell
175187
# 使用拉取到本地的源码进行构建
176188
docker build . -t lxrite/azure-http-proxy
177189
# 或者使用URL自动拉取源码构建
178190
docker build -t lxrite/azure-http-proxy https://github.com/lxrite/azure-http-proxy.git
179191

180192
# 启动 ahps
181-
docker run -d -p 8090:8090 --mount type=bind,source=$PWD/server.json,target=/root/.ahps/server.json lxrite/azure-http-proxy ahps
193+
docker run -d -p 8090:8090 --mount type=bind,source=$PWD/server.json,target=/data/ahp/server.json lxrite/azure-http-proxy ahps -c /data/ahp/server.json
182194
```

src/http_proxy_client_config.cpp

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,11 @@
99
#include <cctype>
1010
#include <fstream>
1111
#include <memory>
12+
#include <string>
1213

1314
#ifdef _WIN32
15+
#include <codecvt>
1416
#include <Windows.h>
15-
#else
16-
extern "C" {
17-
#include <unistd.h>
18-
#include <sys/types.h>
19-
#include <pwd.h>
20-
}
2117
#endif
2218

2319
#include "encrypt.hpp"
@@ -29,7 +25,7 @@ namespace azure_proxy {
2925
http_proxy_client_config::http_proxy_client_config()
3026
{}
3127

32-
bool http_proxy_client_config::load_config(const std::string& config_data)
28+
bool http_proxy_client_config::load_config_data(const std::string& config_data)
3329
{
3430
bool rollback = true;
3531
std::shared_ptr<bool> auto_rollback(&rollback, [this](bool* rollback) {
@@ -134,17 +130,12 @@ bool http_proxy_client_config::load_config(const std::string& config_data)
134130
return true;
135131
}
136132

137-
bool http_proxy_client_config::load_config()
133+
bool http_proxy_client_config::load_config(const std::string& config_path)
138134
{
139135
std::string config_data;
140136
#ifdef _WIN32
141-
wchar_t path_buffer[MAX_PATH];
142-
if (GetModuleFileNameW(NULL, path_buffer, MAX_PATH) == 0 || GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
143-
std::cerr << "Failed to get retrieve the path of the executable file" << std::endl;
144-
}
145-
std::wstring config_file_path(path_buffer);
146-
config_file_path.resize(config_file_path.find_last_of(L'\\') + 1);
147-
config_file_path += L"client.json";
137+
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
138+
std::wstring config_file_path = converter.from_bytes(config_path);
148139
std::shared_ptr<std::remove_pointer<HANDLE>::type> config_file_handle(
149140
CreateFileW(config_file_path.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL),
150141
[](HANDLE native_handle) {
@@ -158,7 +149,7 @@ bool http_proxy_client_config::load_config()
158149
}
159150
char ch;
160151
DWORD size_read = 0;
161-
BOOL read_result = ReadFile(config_file_handle.get(), &ch, 1, &size_read, NULL);
152+
BOOL read_result = ReadFile(config_file_handle.get(), &ch, 1, &size_read, NULL);
162153
while (read_result != FALSE && size_read != 0) {
163154
config_data.push_back(ch);
164155
read_result = ReadFile(config_file_handle.get(), &ch, 1, &size_read, NULL);
@@ -168,18 +159,6 @@ bool http_proxy_client_config::load_config()
168159
return false;
169160
}
170161
#else
171-
auto bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
172-
if (bufsize == -1) {
173-
bufsize = 16384;
174-
}
175-
std::unique_ptr<char[]> buf(new char[bufsize]);
176-
passwd pwd, *result = nullptr;
177-
getpwuid_r(getuid(), &pwd, buf.get(), bufsize, &result);
178-
if (result == nullptr) {
179-
return false;
180-
}
181-
std::string config_path = pwd.pw_dir;
182-
config_path += "/.ahpc/client.json";
183162
std::ifstream ifile(config_path.c_str());
184163
if (!ifile.is_open()) {
185164
std::cerr << "Failed to open \"" << config_path << "\"" << std::endl;
@@ -190,7 +169,7 @@ bool http_proxy_client_config::load_config()
190169
config_data.push_back(ch);
191170
}
192171
#endif
193-
return this->load_config(config_data);
172+
return this->load_config_data(config_data);
194173
}
195174

196175
const std::string& http_proxy_client_config::get_proxy_server_address() const

src/http_proxy_client_config.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ class http_proxy_client_config {
2929
return std::any_cast<T>(iter->second);
3030
}
3131
http_proxy_client_config();
32-
bool load_config(const std::string& config_data);
32+
bool load_config_data(const std::string& config_data);
3333
public:
34-
bool load_config();
34+
bool load_config(const std::string& config_path);
3535
const std::string& get_proxy_server_address() const;
3636
unsigned short get_proxy_server_port() const;
3737
const std::string& get_bind_address() const;

0 commit comments

Comments
 (0)