Skip to content

Commit

Permalink
socks4 add auth support
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyoko-Jeremie committed Sep 2, 2023
1 parent 9f09734 commit ad61ecd
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/AuthClientManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,26 @@ bool AuthClientManager::needAuth() {
return !authInfo.empty();
}

bool AuthClientManager::haveAuthUser(const std::string_view &user) {
auto &users = authInfo.get<AuthUser::USER>();
return users.contains(std::string{user});
}

std::shared_ptr<AuthClientManager::AuthUser>
AuthClientManager::checkAuthUserOnly(const std::string_view &user) {
auto &users = authInfo.get<AuthUser::USER>();
auto it = users.find(std::string{user});
if (it != users.end()) {
return *it;
} else {
return {};
}
}

std::shared_ptr<AuthClientManager::AuthUser>
AuthClientManager::checkAuth(const std::string_view &user, const std::string_view &pwd) {
auto &userPwd = authInfo.get<AuthUser::USER_PWD>();
auto it = userPwd.find(std::make_tuple(std::string{user}, std::string{pwd}));
//userPwd.contains(std::make_tuple(std::string{user}, std::string{pwd}));
if (it != userPwd.end()) {
return *it;
} else {
Expand Down
4 changes: 4 additions & 0 deletions src/AuthClientManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ class AuthClientManager : public std::enable_shared_from_this<AuthClientManager>

bool needAuth();

bool haveAuthUser(const std::string_view &user);

std::shared_ptr<AuthClientManager::AuthUser> checkAuthUserOnly(const std::string_view &user);

std::shared_ptr<AuthClientManager::AuthUser> checkAuth(const std::string_view &user, const std::string_view &pwd);

std::shared_ptr<AuthClientManager::AuthUser> checkAuth_Base64AuthString(const std::string_view &base64AuthString);
Expand Down
31 changes: 31 additions & 0 deletions src/ProxyHandshakeUtils/Socks4ServerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,37 @@ void Socks4ServerImpl::do_analysis_client_first_socks4_header() {
|
d[3]
);
if (ptr->authClientManager->needAuth()) {
// need auth
if (nullByteIndex[0] <= 8) {
// the len(USERID)==0, USERID not exist
BOOST_LOG_S5B_ID(relayId, error)
<< "do_analysis_client_first_socks4_header need auth but (nullByteIndex[1] <= 8), "
<< " need auth but no USERID";
do_handshake_client_end_error(92);
return;
} else {
// get and check username
auto username = std::string{
d + 8,
d + nullByteIndex[0]
};
BOOST_LOG_S5B_ID(relayId, trace) << "do_analysis_client_first_socks4_header auth username:" << username;

auto au = ptr->authClientManager->checkAuthUserOnly(username);
if (au) {
BOOST_LOG_S5B_ID(relayId, trace)
<< "do_auth_client_read auth ok :[" << username << "]";
ptr->tcpRelaySession->authUser = au;
// ok
} else {
BOOST_LOG_S5B_ID(relayId, trace)
<< "do_auth_client_read auth error :[" << username << "]";
do_handshake_client_end_error(92);
return;
}
}
}
BOOST_LOG_S5B_ID(relayId, trace)
<< "do_analysis_client_first_socks4_header ptr->port:[" << ptr->port << "]";
switch (d[1]) {
Expand Down

0 comments on commit ad61ecd

Please sign in to comment.