Skip to content

Commit 03a11a8

Browse files
committed
Implemented auth handling
1 parent 19d7a5b commit 03a11a8

15 files changed

+320
-198
lines changed

3rdparty/libnstd

Submodule libnstd updated 53 files

CMakeLists.txt

+1-11
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,6 @@ project(gchsd VERSION 0.2.0)
77
set(CDEPLOY_NO_DEBUG_BUILD True)
88
set(CDEPLOY_NO_COMPILER True)
99

10-
find_package(CURL REQUIRED)
11-
if(NOT TARGET CURL::libcurl)
12-
add_library(CURL::libcurl INTERFACE IMPORTED)
13-
set_target_properties(CURL::libcurl
14-
PROPERTIES
15-
INTERFACE_INCLUDE_DIRECTORIES "${CURL_INCLUDE_DIRS}"
16-
INTERFACE_LINK_LIBRARIES "${CURL_LIBRARIES}"
17-
)
18-
endif()
19-
2010
include(CDeploy)
2111

2212
add_subdirectory(3rdparty)
@@ -29,7 +19,7 @@ set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "A mirroring Git HTTP server daemon")
2919
set(CPACK_PACKAGING_INSTALL_PREFIX "/")
3020
#set(CPACK_DEBIAN_FILE_NAME "DEB-DEFAULT")
3121
set(CPACK_DEBIAN_PACKAGE_HOMEPAGE "https://github.com/craflin/gchsd")
32-
set(CPACK_DEBIAN_PACKAGE_DEPENDS "libc6 (>= 2.15),libcurl4")
22+
set(CPACK_DEBIAN_PACKAGE_DEPENDS "libc6 (>= 2.15)")
3323
set(CPACK_DEBIAN_PACKAGE_SECTION "net")
3424

3525
include(CPack)

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ A daemon to mirror remote Git repositories and serve them over HTTP, automatical
77

88
It was developed to be used in CI environments to improve the performance of Git clone operations.
99

10-
The project is a C++/CURL clone of jonasmalacofilho's [git-cache-http-server](https://github.com/jonasmalacofilho/git-cache-http-server).
10+
The project is a C++ clone of jonasmalacofilho's [git-cache-http-server](https://github.com/jonasmalacofilho/git-cache-http-server).
1111

1212
## Build Instructions
1313

1414
* Clone the repository and initialize submodules.
15-
* Install a dev package of curl. (e.g. `libcurl4-openssl-dev`)
1615
* Build the project with `cmake`.
1716
* You can build a `deb` package using the target `package` in CMake.
1817

1918
## Server Setup
2019

20+
* Install `git`.
2121
* Install `gchsd` from the `deb` package.
2222
* Configure a cache directory (default is `/tmp/gchsd`) and listen port (default is `80`) in `/etc/gchsd.conf`.
2323
* Start the `gchsd` daemon with `sudo systemctl start gchsd`.

src/AskPass.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
#include <nstd/String.hpp>
3+
#include <nstd/Console.hpp>
4+
#include <nstd/Process.hpp>
5+
6+
int main(int argc, char* argv[])
7+
{
8+
String arg1;
9+
if (argc > 1)
10+
arg1.attach(argv[1], String::length(argv[1]));
11+
if (arg1.startsWith("Username"))
12+
Console::print(Process::getEnvironmentVariable("GCHSD_USERNAME"));
13+
else if (arg1.startsWith("Password"))
14+
Console::print(Process::getEnvironmentVariable("GCHSD_PASSWORD"));
15+
else
16+
return 1;
17+
return 0;
18+
}

src/Authentications.cpp

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
#include "Authentications.hpp"
3+
4+
#include <nstd/Mutex.hpp>
5+
#include <nstd/HashMap.hpp>
6+
#include <nstd/Time.hpp>
7+
8+
namespace {
9+
typedef HashMap<String, int64> AuthMap;
10+
11+
Mutex _mutex;
12+
AuthMap _authentications;
13+
14+
String getKey(const String& repo, const String& auth)
15+
{
16+
return auth + " " + repo;
17+
}
18+
19+
void _cleanup(int64 now)
20+
{
21+
while (!_authentications.isEmpty())
22+
{
23+
if (now - _authentications.front() < 60 * 60 * 1000) // 1 hour
24+
break;
25+
_authentications.removeFront();
26+
}
27+
}
28+
}
29+
30+
void storeAuth(const String& repo, const String& auth)
31+
{
32+
String key = getKey(repo, auth);
33+
int64 now = Time::time();
34+
{
35+
Mutex::Guard guard(_mutex);
36+
_cleanup(now);
37+
_authentications.remove(key);
38+
_authentications.append(key, now);
39+
}
40+
}
41+
42+
void removeAuth(const String& repo, const String& auth)
43+
{
44+
String key = getKey(repo, auth);
45+
{
46+
Mutex::Guard guard(_mutex);
47+
_authentications.remove(key);
48+
}
49+
}
50+
51+
bool checkAuth(const String& repo, const String& auth)
52+
{
53+
String key = getKey(repo, auth);
54+
int64 now = Time::time();
55+
{
56+
Mutex::Guard guard(_mutex);
57+
_cleanup(now);
58+
return _authentications.contains(key);
59+
}
60+
}

src/Authentications.hpp

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
#pragma once
3+
4+
#include <nstd/String.hpp>
5+
6+
void storeAuth(const String& repo, const String& auth);
7+
void removeAuth(const String& repo, const String& auth);
8+
bool checkAuth(const String& repo, const String& auth);
9+

src/CMakeLists.txt

+23-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11

22
set(sources
3+
Authentications.cpp
4+
Authentications.hpp
35
Address.hpp
4-
HttpRequest.cpp
5-
HttpRequest.hpp
66
Main.cpp
7+
NamedMutex.cpp
8+
NamedMutex.hpp
79
Settings.cpp
810
Settings.hpp
911
Worker.cpp
@@ -15,12 +17,28 @@ add_executable(gchsd
1517
)
1618

1719
target_link_libraries(gchsd PRIVATE
18-
libnstd::Socket CURL::libcurl
20+
libnstd::Socket
1921
)
2022

2123
source_group("" FILES ${sources})
22-
2324
set_property(TARGET gchsd PROPERTY FOLDER "src")
2425

25-
install(TARGETS gchsd DESTINATION usr/sbin)
26+
27+
set(sources
28+
AskPass.cpp
29+
)
30+
31+
add_executable(gchsd-askpass
32+
${sources}
33+
)
34+
target_link_libraries(gchsd-askpass PRIVATE
35+
libnstd::Core
36+
)
37+
38+
source_group("" FILES ${sources})
39+
set_property(TARGET gchsd-askpass PROPERTY FOLDER "src")
40+
41+
42+
43+
install(TARGETS gchsd gchsd-askpass DESTINATION usr/sbin)
2644

src/HttpRequest.cpp

-104
This file was deleted.

src/HttpRequest.hpp

-22
This file was deleted.

src/NamedMutex.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "NamedMutex.hpp"
2+
3+
namespace {
4+
Mutex _mutex;
5+
}
6+
7+
NamedMutexGuard::MutexMap NamedMutexGuard::_objects;
8+
9+
NamedMutexGuard::NamedMutexGuard(const String& name)
10+
{
11+
{
12+
Mutex::Guard guard(_mutex);
13+
_object = _objects.find(name);
14+
if (_object == _objects.end())
15+
_object = _objects.insert(_objects.end(), name);
16+
++_object->count;
17+
}
18+
_object->mutex.lock();
19+
}
20+
21+
NamedMutexGuard::~NamedMutexGuard()
22+
{
23+
_object->mutex.unlock();
24+
{
25+
Mutex::Guard guard(_mutex);
26+
if (--_object->count == 0)
27+
_objects.remove(_object);
28+
}
29+
}

src/NamedMutex.hpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#pragma once
2+
3+
#include <nstd/Mutex.hpp>
4+
#include <nstd/String.hpp>
5+
#include <nstd/PoolMap.hpp>
6+
7+
class NamedMutexGuard
8+
{
9+
public:
10+
NamedMutexGuard(const String& name);
11+
~NamedMutexGuard();
12+
13+
private:
14+
struct NamedMutex
15+
{
16+
Mutex mutex;
17+
usize count;
18+
19+
NamedMutex() : count(0) {}
20+
};
21+
22+
typedef PoolMap<String, NamedMutex> MutexMap;
23+
24+
private:
25+
MutexMap::Iterator _object;
26+
27+
private:
28+
static MutexMap _objects;
29+
};

src/Settings.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
#include <nstd/List.hpp>
66
#include <nstd/Log.hpp>
77
#include <nstd/Directory.hpp>
8+
#include <nstd/Process.hpp>
89

910
Settings::Settings()
10-
: listenAddr{Socket::anyAddress, 80}
11+
: askpassPath(File::getDirectoryName(Process::getExecutablePath()) + "/gchsd-askpass")
12+
, listenAddr{Socket::anyAddress, 80}
1113
, cacheDir(Directory::getTempDirectory() + "/gchsd")
1214
{
1315
;

src/Settings.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
struct Settings
99
{
10+
String askpassPath;
1011
Address listenAddr;
1112
String cacheDir;
1213

0 commit comments

Comments
 (0)