-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
maxlv7
committed
Apr 12, 2020
1 parent
29a1d9c
commit ba426a1
Showing
11 changed files
with
323 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// | ||
// Created by Li on 2020/4/9. | ||
// | ||
|
||
#include "cipher.h" | ||
|
||
|
||
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}; | ||
auto data_len = bufferevent_read(self, encode_data, sizeof(encode_data) - 1); | ||
if (data_len <= 0) { | ||
break; | ||
} | ||
unsigned char temp_data[1024]{0}; | ||
for (auto i = 0; i < data_len; i++) { | ||
temp_data[i] = d_password[encode_data[i]]; | ||
} | ||
bufferevent_write(partner, temp_data, data_len); | ||
} | ||
} | ||
|
||
void Cipher::encrypt(bufferevent *self, bufferevent *partner) { | ||
while (true) { | ||
unsigned char data[1024]{0}; | ||
auto data_len = bufferevent_read(partner, data, sizeof(data) - 1); | ||
if (data_len <= 0) { | ||
break; | ||
} | ||
unsigned char temp_data[1024]{0}; | ||
for (auto i = 0; i < data_len; i++) { | ||
temp_data[i] = Cipher::password[data[i]]; | ||
} | ||
bufferevent_write(self, temp_data, data_len); | ||
} | ||
} | ||
|
||
void Cipher::encrypt_byte(unsigned char bytes[], int len) { | ||
auto t_bytes = new unsigned char(len); | ||
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); | ||
for (int k = 0; k < len; k++) { | ||
t_bytes[k] = bytes[k]; | ||
} | ||
for (auto j = 0; j < 256; j++) { | ||
r_password[Cipher::password[j]] = j; | ||
} | ||
for (int i = 0; i < len; i++) { | ||
bytes[i] = r_password[t_bytes[i]]; | ||
} | ||
delete t_bytes; | ||
} | ||
|
||
unsigned char Cipher::password[256]{0}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// Created by Li on 2020/4/9. | ||
// | ||
|
||
#ifndef LIGHTSOCKS_CIPHER_H | ||
#define LIGHTSOCKS_CIPHER_H | ||
|
||
#include <string> | ||
#include <event2/buffer.h> | ||
#include <event2/bufferevent.h> | ||
|
||
class Cipher { | ||
public: | ||
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); | ||
|
||
public: | ||
static unsigned char password[256]; | ||
}; | ||
|
||
|
||
#endif //LIGHTSOCKS_CIPHER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include "server.h" | ||
#include "spdlog/spdlog.h" | ||
#include "ArduinoJson-v6.15.0.hpp" | ||
#include "util.h" | ||
|
||
#ifndef _WIN32 | ||
|
||
#include <csignal> | ||
#include <fstream> | ||
|
||
#endif | ||
using ArduinoJson::DynamicJsonDocument; | ||
using ArduinoJson::serializeJson; | ||
|
||
int main(int argc, char **argv) { | ||
#ifndef _WIN32 | ||
if (std::signal(SIGPIPE, SIG_IGN) == SIG_ERR) { | ||
spdlog::warn("管道信号发生..."); | ||
} | ||
#endif | ||
spdlog::set_level(spdlog::level::info); | ||
|
||
const char *config_name = "config_server.json"; | ||
DynamicJsonDocument doc{1024}; | ||
auto ret = Util::readJsonConfig(config_name, doc); | ||
if (!ret) { | ||
spdlog::warn("加载配置文件[{}]失败", config_name); | ||
std::ofstream outfile; | ||
doc["listen_port"]=7009; | ||
doc["password"]=Util::genPassword(); | ||
outfile.open("config_server.json"); | ||
serializeJson(doc,outfile); | ||
outfile.close(); | ||
doc.clear(); | ||
spdlog::info("已在当前文件夹下自动生成了[config_server.json]", config_name); | ||
} | ||
//read config from json | ||
spdlog::info("成功加载配置文件[{}]", config_name); | ||
const int listen_port = doc["listen_port"]; | ||
const char *password = doc["password"]; | ||
spdlog::info("读取到监听地址:[::]:{}", listen_port); | ||
spdlog::info("读取到密码:{}", password); | ||
|
||
server::setConfig(listen_port, password); | ||
server::run(); | ||
server::clear(); | ||
} |
Oops, something went wrong.