Skip to content

Commit 941bd54

Browse files
authored
Merge pull request #17 from lxrite/auth-key
Use auth_key authentication instead of HTTP Proxy authentication
2 parents 6f1427e + 8e3b4a3 commit 941bd54

16 files changed

+185
-300
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ add_library(jsonxx STATIC third_party/jsonxx/jsonxx.cc)
2626
include_directories("third_party/curi/src")
2727
add_subdirectory(third_party/curi/src)
2828

29-
add_executable(ahpc src/http_proxy_client_main.cpp src/http_proxy_client.cpp src/http_proxy_client_config.cpp src/http_proxy_client_connection.cpp)
29+
add_executable(ahpc src/http_proxy_client_main.cpp src/http_proxy_client.cpp src/http_proxy_client_config.cpp src/http_proxy_client_connection.cpp src/hash_utils.cpp)
3030
target_link_libraries(ahpc ${OPENSSL_LIBRARIES} jsonxx)
3131

32-
add_executable(ahps src/http_proxy_server_main.cpp src/http_proxy_server.cpp src/http_proxy_server_config.cpp src/http_proxy_server_connection.cpp src/http_header_parser.cpp src/base64.cpp src/authentication.cpp)
32+
add_executable(ahps src/http_proxy_server_main.cpp src/http_proxy_server.cpp src/http_proxy_server_config.cpp src/http_proxy_server_connection.cpp src/http_header_parser.cpp src/hash_utils.cpp src/authentication.cpp)
3333
target_link_libraries(ahps ${OPENSSL_LIBRARIES} jsonxx curi)
3434

3535
if(UNIX)

README.md

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ AHP(Azure Http Proxy)是一款高速、安全、轻量级和跨平台的HTTP代
1010
- 对目标域名的解析在服务端进行,可以解决本地DNS污染的问题
1111
- 服务端同时支持多种数据加密方式,数据加密方式可由客户端任意指定,客户端可以权衡机器性能以及安全需求选择合适的加密方式
1212
- 多线程并发处理,充分利用多处理器的优势,能同时处理成千上万的并发连接
13-
- 多用户支持,允许为每个用户使用独立的帐号和密码
13+
- 多用户支持,允许为每个用户使用独立的auth_key `(1.1及以上版本)`
1414

1515
## 编译和安装
1616

@@ -24,7 +24,7 @@ AHP使用了部分C++17特性,所以对编译器的版本有较高要求,下
2424

2525
### 安装依赖
2626

27-
- OpenSSL
27+
- OpenSSL >= 3.0
2828

2929
#### Linux
3030

@@ -56,7 +56,6 @@ AHP使用自动化构建工具CMake来实现跨平台构建
5656

5757
- CMake >= 2.8
5858

59-
Windows下可以使用cmake-gui.exe,Linux或其他类Unix系统可以使用下面的命令编译
6059
```shell
6160
$ cd azure-http-proxy
6261
$ mkdir build
@@ -94,15 +93,10 @@ $ openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem
9493
"timeout": 240,
9594
"workers": 4,
9695
"auth": true,
97-
"users": [
98-
{
99-
"username": "username1",
100-
"password": "password1"
101-
},
102-
{
103-
"username": "foobar",
104-
"password": "bazqux"
105-
}
96+
"auth_key_list": [
97+
"testing_key",
98+
"Bob",
99+
"Alice"
106100
]
107101
}
108102
```
@@ -111,11 +105,11 @@ $ openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem
111105
----------------|--------------------|------------------|-----------|
112106
bind_address | 服务端绑定的IP地址 | 否 | "0.0.0.0" |
113107
listen_port | 服务端绑定的端口 | 否 | 8090 |
114-
rsa_private_key | RSA私钥 | 是 | 无 |
108+
rsa_private_key | RSA私钥 | 是 | 无 |
115109
timeout | 超时时间(秒) | 否 | 240 |
116110
workers | 并发工作线程数 | 否 | 4 |
117111
auth | 启用代理身份验证 | 否 | false |
118-
users | 用户列表 | auth为true时必选 | 无 |
112+
auth_key_list | auth_key列表 | auth为true时必选 | 无 |
119113

120114
### 配置客户端
121115

@@ -129,7 +123,8 @@ users | 用户列表 | auth为true时必选 | 无 |
129123
"rsa_public_key": "-----BEGIN PUBLIC KEY----- ...... -----END PUBLIC KEY-----",
130124
"cipher": "aes-256-cfb",
131125
"timeout": 240,
132-
"workers": 2
126+
"workers": 2,
127+
"auth_key": "testing_key"
133128
}
134129
```
135130

@@ -143,6 +138,7 @@ rsa_public_key | RSA公钥 | 是 | 无
143138
cipher | 加密方法 | 否 | "aes-256-cfb" |
144139
timeout | 超时时间(秒) | 否 | 240 |
145140
workers | 并发工作线程数 | 否 | 2 |
141+
auth_key | 用于身份验证的字符串 | 否 | 值为空字符串或没有这个字段时,请求不携带auth_key,仅当server的auth为false时才能成功建立连接|
146142

147143
#### 支持的加密方法
148144

example/client.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
"rsa_public_key": "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxPbPBU61RYBI0rUDVso+\nTzkQ7bXO1j4GWxbYZ3nEL6sLbrftv7rpYq5uPLi9DdJ3ZoEUjxlnO+VUOOtm7LpR\nGmWqUQdNnYHuiZU1UuH7pDIXejQwwSC698FB1kwnoxV4LICkiA1a4qucqlnG8nN6\ngBFs3/1K2DuUs0Hg1hZKlkOq/ONR82XGhXkB/HVwmfgQlZpVbWHQDsZiOv1SUnQW\n8Zs6E/JmW6llBkWtsQT9nQ2uzcV1JGzV0ltB4N0CMC8u2zv/LLTSgS4IKrVicAqO\n9TWkGOFmGowV7PpEAEQC1WcBXThLpUYk2QqiSvTTLTdFNmwEH+hKa1ZBPqOcaTA1\ndQIDAQAB\n-----END PUBLIC KEY-----",
77
"cipher": "aes-256-cfb",
88
"timeout": 240,
9-
"workers": 2
9+
"workers": 2,
10+
"auth_key": "testing_key"
1011
}

example/server.json

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,11 @@
55
"timeout": 240,
66
"workers": 4,
77
"auth": true,
8-
"users": [
9-
{
10-
"username": "username1",
11-
"password": "password1"
12-
},
13-
{
14-
"username": "foobar",
15-
"password": "bazqux"
16-
}
8+
"auth_key_list": [
9+
"testing_key",
10+
"Bob",
11+
"Alice",
12+
"+ZI1w$u9N65lTw*nL@$",
13+
"SoHPa4xNMBWoXxSOp+6snUtqtdXFH(MO"
1714
]
1815
}

src/authentication.cpp

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,54 +9,28 @@
99
#include <iterator>
1010

1111
#include "authentication.hpp"
12-
#include "base64.hpp"
12+
#include "hash_utils.hpp"
1313

1414
namespace azure_proxy {
1515

1616
authentication::authentication()
1717
{
1818
}
1919

20-
auth_result authentication::auth_basic(const std::string::const_iterator begin, const std::string::const_iterator end) const
20+
bool authentication::auth(const auth_key_hash_t& auth_key_hash) const
2121
{
22-
std::string authorization;
23-
try {
24-
azure_proxy::encoding::base64_decode(begin, end, std::back_inserter(authorization));
25-
}
26-
catch (const azure_proxy::encoding::decode_base64_error&) {
27-
return auth_result::error;
28-
}
29-
auto colon_pos = authorization.find(':');
30-
if (colon_pos == std::string::npos) {
31-
return auth_result::error;
32-
}
33-
std::string username(authorization.begin(), authorization.begin() + colon_pos);
34-
std::string password(authorization.begin() + colon_pos + 1, authorization.end());
35-
auto iter = this->users_map.find(username);
36-
if (iter != this->users_map.end() && std::get<1>(*iter) == password) {
37-
return auth_result::ok;
38-
}
39-
return auth_result::incorrect;
22+
return this->auth_keys_map.find(auth_key_hash) != this->auth_keys_map.end();
4023
}
4124

42-
auth_result authentication::auth(const std::string& value) const
25+
void authentication::add_auth_key(const std::string& auth_key)
4326
{
44-
if (value.size() > 6 && std::equal(value.begin(), value.begin() + 6, "Basic ")) {
45-
return this->auth_basic(value.begin() + 6, value.end());
46-
}
47-
else {
48-
return auth_result::error;
49-
}
27+
auto auth_key_hash = hash_utils::sha256(reinterpret_cast<const unsigned char*>(auth_key.data()), auth_key.size());
28+
this->auth_keys_map[auth_key_hash] = auth_key;
5029
}
5130

52-
void authentication::add_user(const std::string& username, const std::string& password)
31+
void authentication::remove_all_auth_keys()
5332
{
54-
this->users_map[username] = password;
55-
}
56-
57-
void authentication::remove_all_users()
58-
{
59-
this->users_map.clear();
33+
this->auth_keys_map.clear();
6034
}
6135

6236
authentication& authentication::get_instance()

src/authentication.hpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,22 @@
88
#ifndef AZURE_AUTHENTICATION_HPP
99
#define AZURE_AUTHENTICATION_HPP
1010

11+
#include <array>
1112
#include <map>
1213
#include <string>
1314

1415
namespace azure_proxy {
1516

16-
enum class auth_result {
17-
ok,
18-
incorrect,
19-
error
20-
};
17+
using auth_key_hash_t = std::array<unsigned char, 32>;
2118

2219
class authentication {
23-
std::map<std::string, std::string> users_map;
20+
std::map<auth_key_hash_t, std::string> auth_keys_map;
2421
private:
2522
authentication();
26-
auth_result auth_basic(const std::string::const_iterator begin, const std::string::const_iterator end) const;
2723
public:
28-
auth_result auth(const std::string& value) const;
29-
void add_user(const std::string& username, const std::string& password);
30-
void remove_all_users();
24+
bool auth(const auth_key_hash_t& auth_key_hash) const;
25+
void add_auth_key(const std::string& auth_key);
26+
void remove_all_auth_keys();
3127

3228
static authentication& get_instance();
3329
};

src/base64.cpp

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/base64.hpp

Lines changed: 0 additions & 106 deletions
This file was deleted.

src/hash_utils.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* hash_utils.cpp:
3+
*
4+
* Copyright (C) 2023 Light Lin <[email protected]> All Rights Reserved.
5+
*
6+
*/
7+
8+
#include "hash_utils.hpp"
9+
#include <openssl/sha.h>
10+
11+
namespace azure_proxy {
12+
namespace hash_utils {
13+
14+
std::array<unsigned char, 32> sha256(const unsigned char* data, std::size_t count) {
15+
std::array<unsigned char, 32> result;
16+
SHA256(data, count, result.data());
17+
return result;
18+
}
19+
20+
} // namespace hash_utils
21+
} // namespace azure_proxy

0 commit comments

Comments
 (0)