Skip to content

Commit

Permalink
Shadowsocks protocol supported (#2)
Browse files Browse the repository at this point in the history
* systemd support

* refactory

* intrusive_list

* use intrusive list

* use macro buffer size controls

* add visual stuio ignores

* add session buffer size macro control

* remove boost third party

* shadowsocks protocol supported
  • Loading branch information
miximtor authored Apr 17, 2019
1 parent 7ffc07d commit 2b4b924
Show file tree
Hide file tree
Showing 35 changed files with 1,370 additions and 1,037 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,8 @@
# Clion build outputs
.idea*
cmake-build-*

#Visual Studio build outputs

CMakeSettings.json
.vs*
64 changes: 47 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,42 +1,53 @@
cmake_minimum_required(VERSION 3.9.2)

project(msocks)

set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_STATIC_RUNTIME ON)

set(Boost_ARCHITECTURE -x64)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})


find_package(Threads REQUIRED)
find_package(Botan2 2.9.0 REQUIRED)
find_package(Boost 1.69 COMPONENTS system coroutine random thread REQUIRED)
if (BOTAN2_FOUND)
message("Use Botan2: ${BOTAN2_LIBRARIES}")
endif()
if (Boost_FOUND)
message("Use Boost: ${Boost_LIBRARIES}")
endif()

include_directories(${Boost_INCLUDE_DIRS})
add_subdirectory(third_party/spdlog)
include_directories(${BOOST_LIBRARIES})
include_directories(${BOTAN2_INCLUDE_DIRS})
include_directories(third_party/spdlog/include)
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(include)

add_definitions(-DBOOST_ASIO_NO_DEPRECATED)
add_definitions(-DBOOST_COROUTINES_NO_DEPRECATION_WARNING)
if (MSVC)
add_definitions(-D_WIN32_WINNT=0x0601)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
set(CompilerFlags
CMAKE_CXX_FLAGS_DEBUG
CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_MINSIZEREL
CMAKE_CXX_FLAGS_RELWITHDEBINFO
CMAKE_C_FLAGS_DEBUG
CMAKE_C_FLAGS_RELEASE
CMAKE_C_FLAGS_MINSIZEREL
CMAKE_C_FLAGS_RELWITHDEBINFO
)
# REPLACE MD(MultiThread DLL) TO MT (MultiThread)
foreach(CompilerFlag ${CompilerFlags})
string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}")
endforeach()
endif ()


if (WIN32)
set(WIN_EXTRA_LIB ws2_32 wsock32)
else()
set(WIN_EXTRA_LIB)
endif()

file(GLOB MSOCKS_INCLUDE include/msocks/*hpp)
file(GLOB MSOCKS_INCLUDE_ENDPOINT include/msocks/endpoint/*.hpp)
file(GLOB MSOCKS_INCLUDE_SESSION include/msocks/session/*.hpp)
file(GLOB MSOCKS_INCLUDE_UTILITY include/msocks/utility/*.hpp)
file(GLOB MSOCKS_SRC_SESSION src/session/*.cpp)
file(GLOB MSOCKS_SRC_ENDPOINT src/endpoint/*.cpp)
file(GLOB MSOCKS_SRC_UTILITY src/utility/*.cpp)

add_executable(msocks
${MSOCKS_INCLUDE}
Expand All @@ -45,7 +56,26 @@ add_executable(msocks
${MSOCKS_INCLUDE_UTILITY}
${MSOCKS_SRC_SESSION}
${MSOCKS_SRC_ENDPOINT}
${MSOCKS_SRC_UTILITY}
src/main.cpp)

target_link_options(msocks PRIVATE -fstack-protector)
target_link_libraries(msocks Boost::coroutine Boost::random Boost::system Boost::thread Botan2::Botan2 ${CMAKE_THREAD_LIBS_INIT} ${WIN_EXTRA_LIB})
target_link_libraries(msocks ${BOTAN2_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})

if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
target_link_libraries(msocks stdc++)
endif ()

target_link_libraries(msocks Boost::coroutine Boost::system Boost::random Boost::thread)

if (WIN32)
target_link_libraries(msocks ws2_32 wsock32)
if (MSVC)
target_link_libraries(msocks bcrypt)
endif ()
endif ()


if (UNIX)
install(TARGETS msocks DESTINATION ${CMAKE_INSTALL_BINDIR}/bin)
install(FILES daemon/msocksd.service DESTINATION /etc/systemd/system/)
endif ()
10 changes: 8 additions & 2 deletions daemon/msocksd.service
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
[Unit]
Description = msocks daemon service

[Service]
ExecStart=/usr/bin/msocks
Type = simple
ExecStart=/usr/bin/msocks s 0.0.0.0 4500 123456 4096
ExecStop=/bin/kill -TERM $MAINPID

[Install]
WantedBy=multi-user.target
66 changes: 66 additions & 0 deletions include/msocks/endpoint/basic_endpoint.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// Created by maxtorm on 2019/4/13.
//
#pragma once

#include <boost/asio/spawn.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <spdlog/spdlog.h>

using namespace boost::asio;
using namespace boost::system;

namespace msocks
{

class basic_endpoint : public noncopyable
{
protected:

explicit basic_endpoint(io_context& ioc) :
ioc_(ioc), acceptor_(ioc)
{}

template <typename SessionCreate>
void start_service(SessionCreate create, const ip::tcp::endpoint& ep)
{
spawn(
ioc_,
[create(std::move(create)), this, ep](yield_context yield)
{
do_async_accept(create, ep, yield);
});
}

io_context& ioc_;
ip::tcp::acceptor acceptor_;

private:

template <typename SessionCreate>
void do_async_accept(SessionCreate create, const ip::tcp::endpoint ep, yield_context yield)
{
try
{
acceptor_.open(ep.protocol());
acceptor_.set_option(ip::tcp::socket::reuse_address(true));
acceptor_.bind(ep);
acceptor_.listen();
while (true)
{
ip::tcp::socket s(ioc_);
acceptor_.async_accept(s, yield);
auto session = create(std::move(s));
session->go();
}
}
catch (system_error & e)
{
std::stringstream ss;
ss << ep;
spdlog::error("endpoint {}: error {}", ss.str(), e.what());
}
}
};
}

23 changes: 0 additions & 23 deletions include/msocks/endpoint/client.hpp

This file was deleted.

41 changes: 41 additions & 0 deletions include/msocks/endpoint/client_endpoint.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// Created by maxtorm on 2019/4/13.
//

#pragma once
#include <msocks/endpoint/basic_endpoint.hpp>

namespace msocks
{

struct client_config
{
client_config() : timeout(0)
{
};
std::string local_address;
uint16_t local_port = 0;
std::string remote_address;
uint16_t remote_port = 0;
std::vector<uint8_t> key;
std::string method;
boost::posix_time::seconds timeout;
};

class client_endpoint final : public basic_endpoint
{
public:
client_endpoint(io_context& ioc, client_config cfg) :
basic_endpoint(ioc),
cfg_(std::move(cfg))
{}

void start();

private:
client_config cfg_;

};

}

49 changes: 0 additions & 49 deletions include/msocks/endpoint/endpoint.hpp

This file was deleted.

24 changes: 0 additions & 24 deletions include/msocks/endpoint/server.hpp

This file was deleted.

38 changes: 38 additions & 0 deletions include/msocks/endpoint/server_endpoint.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// Created by maxtorm on 2019/4/13.
//
#pragma once

#include <msocks/endpoint/basic_endpoint.hpp>
#include <msocks/session/pool.hpp>
#include <msocks/session/server_session.hpp>
#include <msocks/utility/rate_limiter.hpp>

namespace msocks
{

struct server_endpoint_config
{
server_endpoint_config() : timeout(0) {};
std::string server_address;
uint16_t server_port = 0;
size_t speed_limit = 0;
std::vector<uint8_t> key;
bool no_delay = true;
std::string method;
boost::posix_time::seconds timeout;
};

class server_endpoint final : public basic_endpoint
{
public:
server_endpoint(io_context& ioc, pool<server_session>& session_pool, server_endpoint_config cfg);

void start();
private:
pool<server_session>& session_pool_;
server_endpoint_config cfg_;
std::shared_ptr<utility::rate_limiter> limiter_;
};

}
Loading

0 comments on commit 2b4b924

Please sign in to comment.