Skip to content

Commit

Permalink
一大波更新,解决了若干Bug,同时又引入了一些无法解决的BUG。
Browse files Browse the repository at this point in the history
  • Loading branch information
maxlv7 committed Jun 22, 2020
1 parent 1e2339c commit 5f2adbd
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 225 deletions.
10 changes: 3 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ cmake_minimum_required(VERSION 3.10)
project(LightSocks)

set(CMAKE_CXX_STANDARD 11)
#aux_source_directory(. SOURCE_CODE)

include_directories(lib/json)
IF (WIN32)
Expand All @@ -21,9 +20,8 @@ ENDIF ()
#添加子库
add_subdirectory(lib/base64)

#add_executable(LightSocks-client ${SOURCE_CODE})
add_executable(LightSocks-client main_client.cpp client.cpp util.cpp)
add_executable(LightSocks-server main_server.cpp server.cpp util cipher.cpp)
add_executable(LightSocks-server main_server.cpp server.cpp util cipher.cpp socks5.cpp socks5.h)

target_link_libraries(LightSocks-client base64)
target_link_libraries(LightSocks-server base64)
Expand All @@ -32,9 +30,7 @@ IF (WIN32)
target_link_libraries(LightSocks-client event.lib)
target_link_libraries(LightSocks-server event.lib)
ELSEIF (APPLE)
MESSAGE(STATUS "Now is Apple systems.")
ELSEIF (UNIX)
MESSAGE(STATUS "Now is UNIX-like OS's. Including aPPLE os x and CygWin")
target_link_libraries(LightSocks-client event)
target_link_libraries(LightSocks-server event)
target_link_libraries(LightSocks-client event.a)
target_link_libraries(LightSocks-server event.a pthread)
ENDIF ()
16 changes: 7 additions & 9 deletions cipher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
//

#include "cipher.h"

#include <iostream>

void Cipher::decrypt(bufferevent *self, bufferevent *partner) {
unsigned char d_password[256] = {0};
for (auto i = 0; i < 256; i++) {
d_password[Cipher::password[i]] = i;
}
while (true) {
unsigned char encode_data[1024]{0};
unsigned char encode_data[2048]{0};
auto data_len = bufferevent_read(self, encode_data, sizeof(encode_data) - 1);
if (data_len <= 0) {
break;
}
unsigned char temp_data[1024]{0};
unsigned char temp_data[2048]{0};
for (auto i = 0; i < data_len; i++) {
temp_data[i] = d_password[encode_data[i]];
}
Expand All @@ -26,12 +26,12 @@ void Cipher::decrypt(bufferevent *self, bufferevent *partner) {

void Cipher::encrypt(bufferevent *self, bufferevent *partner) {
while (true) {
unsigned char data[1024]{0};
unsigned char data[2048]{0};
auto data_len = bufferevent_read(partner, data, sizeof(data) - 1);
if (data_len <= 0) {
break;
}
unsigned char temp_data[1024]{0};
unsigned char temp_data[2048]{0};
for (auto i = 0; i < data_len; i++) {
temp_data[i] = Cipher::password[data[i]];
}
Expand All @@ -40,19 +40,18 @@ void Cipher::encrypt(bufferevent *self, bufferevent *partner) {
}

void Cipher::encrypt_byte(unsigned char bytes[], int len) {
auto t_bytes = new unsigned char[len];
unsigned char t_bytes[32] = {0};
for (int k = 0; k < len; k++) {
t_bytes[k] = bytes[k];
}
for (int i = 0; i < len; ++i) {
bytes[i] = Cipher::password[t_bytes[i]];
}
delete[] t_bytes;
}

void Cipher::decrypt_bytes(unsigned char bytes[], int len) {
unsigned char r_password[256] = {0};
auto t_bytes = new unsigned char[len];
unsigned char t_bytes[32]{0};
for (int k = 0; k < len; k++) {
t_bytes[k] = bytes[k];
}
Expand All @@ -62,7 +61,6 @@ void Cipher::decrypt_bytes(unsigned char bytes[], int len) {
for (int i = 0; i < len; i++) {
bytes[i] = r_password[t_bytes[i]];
}
delete[] t_bytes;
}

unsigned char Cipher::password[256]{0};
Expand Down
2 changes: 0 additions & 2 deletions cipher.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ class Cipher {
Cipher() = default;

public:
// TODO bug fixed
static void encrypt_byte(unsigned char bytes[], int len);

static void decrypt_bytes(unsigned char bytes[], int len);

// TODO bug fixed
static void decrypt(bufferevent *self, bufferevent *partner);

static void encrypt(bufferevent *self, bufferevent *partner);
Expand Down
41 changes: 5 additions & 36 deletions client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ void client::local_event_cb(bufferevent *bev, short what, void *ctx) {
auto partner = static_cast<bufferevent *>(ctx);
//读到文件结尾或者发生错误
if (what & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) {
spdlog::debug("进入本地逻辑");
if (what & BEV_EVENT_ERROR) {
spdlog::debug("本地操作时发生错误");
}
if (partner) {
// flash all pending data
Expand All @@ -25,38 +23,28 @@ void client::local_event_cb(bufferevent *bev, short what, void *ctx) {
/* We still have to flush data from the other
* side, but when that's done, close the other
* side. */
spdlog::debug("进入 local evbuffer");
bufferevent_setcb(partner, 0, close_on_finished_write_cb, remote_event_cb, 0);
bufferevent_disable(partner, EV_READ);
} else {
auto partner_fd = bufferevent_getfd(partner);
auto bev_fd = bufferevent_getfd(bev);
spdlog::debug("关闭fd[{}--{}]", partner_fd, bev_fd);
spdlog::debug("清除远程bev");
bufferevent_free(partner);
}
}
spdlog::debug("清除本地bev");
bufferevent_free(bev);
} else {
if (what & (BEV_EVENT_TIMEOUT | BEV_EVENT_READING)) {
spdlog::debug("读取本地数据超时...");
bufferevent_free(bev);
bufferevent_free(partner);
spdlog::debug("清除本地bev,同时也清除了远程bev");
return;
}
if (what & BEV_EVENT_READING) {
spdlog::debug("本地读出错");
bufferevent_free(bev);
bufferevent_free(partner);
spdlog::debug("清除本地bev,同时也清除了远程bev");
return;
} else {
spdlog::debug("other...");
bufferevent_free(bev);
bufferevent_free(partner);
spdlog::debug("清除本地bev,同时也清除了远程bev");
return;
}
}
Expand Down Expand Up @@ -101,45 +89,30 @@ void client::remote_event_cb(bufferevent *bev, short what, void *ctx) {
return;
}

// if (what & BEV_EVENT_EOF)
// {
// cout << "远程服务端关闭事件发生..." << endl;
// bufferevent_free(bev);
// cout << "清除远程bev" << endl;
// return;
// }
if (what & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) {
spdlog::debug("进入远程逻辑...");
if (what & BEV_EVENT_ERROR) {
spdlog::debug("远程服务端发生错误...");
}
if (partner) {
// flash all pending data
auto lens = evbuffer_get_length(bufferevent_get_output(partner));
spdlog::debug("remote lens -->[{}]", lens);
remote_read_cb(bev, partner);
if (evbuffer_get_length(bufferevent_get_output(partner))) { /* We still have to flush data from the other
if (evbuffer_get_length(bufferevent_get_output(partner))) {
/* We still have to flush data from the other
* side, but when that's done, close the other
* side. */
spdlog::debug("进入 remote evbuffer");
bufferevent_setcb(partner, 0, close_on_finished_write_cb, local_event_cb, 0);
bufferevent_disable(partner, EV_READ);
} else {
auto partner_fd = bufferevent_getfd(partner);
auto bev_fd = bufferevent_getfd(bev);
spdlog::debug("关闭fd[{}--{}]", partner_fd, bev_fd);
spdlog::debug("清除本地bev");
bufferevent_free(partner);
}
}
spdlog::debug("清除远程bev");
bufferevent_free(bev);
} else {
if (what & BEV_EVENT_TIMEOUT && what & BEV_EVENT_READING) {
spdlog::debug("远程服务读取数据端超时...");
bufferevent_free(bev);
bufferevent_free(partner);
spdlog::debug("清除远程bev,同时也清除了本地bev");
return;
}
}
Expand All @@ -153,7 +126,6 @@ void client::remote_read_cb(bufferevent *bev, void *arg) {
auto len = evbuffer_get_length(remote);
if (!partner) {
//把数据从缓冲区移除
spdlog::debug("remote drain 调用");
evbuffer_drain(remote, len);
}
//解密数据
Expand All @@ -169,7 +141,6 @@ void client::remote_read_cb(bufferevent *bev, void *arg) {
for (auto i = 0; i < data_len; i++) {
temp_data[i] = t_data[encode_data[i]];
}
// cout << "解密的data len" << data_len << endl;
evbuffer_add(temp, temp_data, data_len);
len = evbuffer_get_length(remote);
}
Expand All @@ -181,7 +152,6 @@ void client::remote_read_cb(bufferevent *bev, void *arg) {
}

void client::close_on_finished_write_cb(struct bufferevent *bev, void *ctx) {
spdlog::debug("进入close_on_finished_writecb");
struct evbuffer *b = bufferevent_get_output(bev);

if (evbuffer_get_length(b) == 0) {
Expand All @@ -190,7 +160,6 @@ void client::close_on_finished_write_cb(struct bufferevent *bev, void *ctx) {
}

void client::listen_cb(evconnlistener *ev, evutil_socket_t s, sockaddr *sin, int sin_len, void *arg) {
spdlog::debug("本地有新的连接");

auto base = static_cast<event_base *>(arg);
//监听本地客户端,用本地客户端连接的socket
Expand Down Expand Up @@ -219,7 +188,7 @@ void client::listen_cb(evconnlistener *ev, evutil_socket_t s, sockaddr *sin, int
(sockaddr *) &sin_remote,
sizeof(sin_remote));
if (re == 0) {
spdlog::debug("connect事件完成");
spdlog::debug("connect success!");
}
//设置回调和事件

Expand Down Expand Up @@ -283,13 +252,13 @@ void client::init() {
(sockaddr *) &sin_local,
sizeof(sin_local));
if (ev_listen) {
spdlog::info("成功监听地址-->socks5://127.0.0.1:{}", local_port);
spdlog::info("Listen on-->socks5://127.0.0.1:{}", local_port);
//资源清理
event_base_dispatch(base_loop);
evconnlistener_free(ev_listen);
event_base_free(base_loop);
} else {
spdlog::warn("监听端口[{}]失败!", local_port);
spdlog::warn("Listen port [{}] error!", local_port);
}


Expand Down
16 changes: 8 additions & 8 deletions main_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,29 @@ int main(int argc, char const *argv[]) {
DynamicJsonDocument doc{1024};
auto ret = Util::readJsonConfig(config_name, doc);
if (!ret) {
spdlog::warn("加载配置文件[{}]失败", config_name);
spdlog::warn("load config.json[{}] error!", config_name);
doc.clear();
std::ofstream outfile;
const char config_json[] = R"({"ip":"23.102.255.234","remote_port":7009,"local_port":7878,"password":"******"})";
const char config_json[] = R"({"ip":"your server ip","remote_port":7009,"local_port":7878,"password":"your server passwrod"})";
outfile.open("config.json");
outfile << config_json;
spdlog::info("已在当前文件夹自动生成了[config.json]", config_name);
spdlog::info("[config.json] has been automatically generated in the current folder", config_name);
outfile.close();
#ifdef _WIN32
system("pause");
#endif
return 0;
}
spdlog::info("成功加载配置文件[{}]", config_name);
spdlog::info("successful load file[{}]", config_name);

const char *ip = doc["ip"];
const char *password = doc["password"];
const int local_port = doc["local_port"];
const int remote_port = doc["remote_port"];
spdlog::info("读取到IP:{}", ip);
spdlog::info("读取到密码:{}", password);
spdlog::info("读取到本地端口:{}", local_port);
spdlog::info("读取到远程端口:{}", remote_port);
spdlog::info("get IP:{}", ip);
spdlog::info("get password:{}", password);
spdlog::info("get local port:{}", local_port);
spdlog::info("get remote port:{}", remote_port);
client::setConfig(ip, local_port, remote_port, password);
doc.clear();
#ifdef _WIN32
Expand Down
10 changes: 5 additions & 5 deletions main_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,21 @@ int main(int argc, char **argv) {
DynamicJsonDocument doc{1024};
auto ret = Util::readJsonConfig(config_name, doc);
if (!ret) {
spdlog::warn("加载配置文件[{}]失败", config_name);
spdlog::warn("load config file [{}] error!", config_name);
std::ofstream outfile;
doc["listen_port"] = 7009;
doc["password"] = Util::genPassword();
outfile.open("config_server.json");
serializeJson(doc, outfile);
outfile.close();
spdlog::info("已在当前文件夹下自动生成了[config_server.json]", config_name);
spdlog::info("[config_server.json] has been automatically generated in the current folder", config_name);
}
//read config from json
spdlog::info("成功加载配置文件[{}]", config_name);
spdlog::info("successful load config file [{}]", config_name);
const int listen_port = doc["listen_port"];
const char *password = doc["password"];
spdlog::info("读取到监听地址:[::]:{}", listen_port);
spdlog::info("读取到密码:{}", password);
spdlog::info("get listen address:[::]:{}", listen_port);
spdlog::info("get password:{}", password);
doc.clear();
server::setConfig(listen_port, password);
server::run();
Expand Down
Loading

0 comments on commit 5f2adbd

Please sign in to comment.