Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] master from tindy2013:master #39

Merged
merged 3 commits into from
Aug 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions base/tools/gui/gui.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<!DOCTYPE html>
<html>
<meta charset="utf-8" />
<!-- 生产环境版本,优化了尺寸和速度 -->
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<!-- 开发环境版本,包含了有帮助的命令行警告 -->
<!-- <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> -->
<script src="https://unpkg.com/element-ui@2.9.1/lib/index.js"></script>
<link rel="stylesheet" href="https://unpkg.com/element-ui@2.9.1/lib/theme-chalk/index.css">
<!-- 引入 vue -->
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.11/vue.min.js"></script>

<!-- 引入样式 -->
<!-- <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css"> -->
<link href="https://cdn.bootcdn.net/ajax/libs/element-ui/2.13.2/theme-chalk/index.css" rel="stylesheet">

<!-- 引入组件库 -->
<!-- <script src="https://unpkg.com/element-ui/lib/index.js"></script> -->
<script src="https://cdn.bootcdn.net/ajax/libs/element-ui/2.13.2/index.js"></script>

<body>
<div id="app">
Expand Down
6 changes: 5 additions & 1 deletion src/multithread_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ int _thread_download(std::string host, int port, std::string uri, std::string lo
SOCKET sHost;
std::string request = "GET " + uri + " HTTP/1.1\r\n"
"Host: " + host + "\r\n"
"Connection: close\r\n"
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36\r\n\r\n";

sHost = initSocket(getNetworkType(localaddr), SOCK_STREAM, IPPROTO_TCP);
Expand Down Expand Up @@ -158,7 +159,7 @@ int _thread_download(std::string host, int port, std::string uri, std::string lo
cur_len = SSL_read(ssl, bufRecv, BUF_SIZE - 1);
if(cur_len < 0)
{
if(errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN)
if(errno == EWOULDBLOCK || errno == EAGAIN)
{
continue;
}
Expand Down Expand Up @@ -212,6 +213,7 @@ int _thread_upload(std::string host, int port, std::string uri, std::string loca
int retVal, cur_len;
SOCKET sHost;
std::string request = "POST " + uri + " HTTP/1.1\r\n"
"Connection: close\r\n"
"Content-Length: 134217728\r\n"
"Host: " + host + "\r\n\r\n";
std::string post_data;
Expand Down Expand Up @@ -388,6 +390,7 @@ int perform_test(nodeInfo &node, std::string localaddr, int localport, std::stri
EXIT_FLAG = true; //terminate all threads right now
while(!opened_socket.empty()) //close all sockets
{
shutdown(opened_socket.front(), SD_BOTH);
closesocket(opened_socket.front());
opened_socket.pop();
}
Expand Down Expand Up @@ -483,6 +486,7 @@ int upload_test(nodeInfo &node, std::string localaddr, int localport, std::strin
EXIT_FLAG = true; //terminate worker thread right now
while(!opened_socket.empty()) //close all sockets
{
shutdown(opened_socket.front(), SD_BOTH);
closesocket(opened_socket.front());
opened_socket.pop();
}
Expand Down
50 changes: 47 additions & 3 deletions src/ntt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ std::tuple<uint16_t, uint16_t> socks5_init_udp(SOCKET s, SOCKET udp_s, const std

std::string random_string(string_size length)
{
const std::string CHARACTERS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
const std::string CHARACTERS = "0123456789ABCDEF";

std::random_device random_device;
std::mt19937 generator(random_device());
Expand All @@ -121,13 +121,47 @@ std::string random_string(string_size length)
return random_string;
}

std::string make_transaction_id()
{
//classic stun
//return random_string(16);
std::string id;
id.assign(reinterpret_cast<const char *>(MAGIC_COOKIE), 4);
id += random_string(12);
return id;
}

void parse_xor_address(std::string &address, const std::string &trans_id)
{
if((address.size() != 8 && address.size() != 20) || trans_id.size() != 16)
return;
size_t family = address[1], addr_size = 0;
/// xor port with magic cookie byte 3 and 4
address[2] ^= trans_id[0];
address[3] ^= trans_id[1];
switch(family)
{
case 1: //IPv4
/// xor IPv4 with magic cookie
addr_size = 4;
break;
case 2: //IPv6
/// xor IPv6 with magic cookie & 12 random bytes
addr_size = 16;
break;
}
uint8_t *pAddr = reinterpret_cast<uint8_t*>(address.data()) + 4;
for(size_t i = 0; i < addr_size; i++)
pAddr[i] ^= trans_id[i];
}

STUN_RESPONSE get_stun_response_thru_socks5(SOCKET udp_s, const std::string &server, uint16_t udp_port, const std::string &target_server, uint16_t target_port, const std::string &send_data = "")
{
STUN_RESPONSE response;
int len, sent_len;

char msg_head[] = {0, 1};
std::string message, trans_id_str = random_string(16), send_data_len = long_to_str(send_data.size(), 2);
std::string message, trans_id_str = make_transaction_id(), send_data_len = long_to_str(send_data.size(), 2);
message.assign(msg_head, 2);
message += send_data_len;
message += trans_id_str;
Expand Down Expand Up @@ -182,7 +216,7 @@ STUN_RESPONSE get_stun_response_thru_socks5(SOCKET udp_s, const std::string &ser
{
attr_type = attrs.substr(pos, 2);
attr_length = str_to_int(attrs.substr(pos + 2, 2));
attr_value = attrs.substr(pos + 5, attr_length);
attr_value = attrs.substr(pos + 4, attr_length);
pos += attr_length + 4;
if(attr_length % 4)
attr_length += 4 - (attr_length % 4);
Expand Down Expand Up @@ -213,6 +247,16 @@ STUN_RESPONSE get_stun_response_thru_socks5(SOCKET udp_s, const std::string &ser
response.change_port = port;
response.failed = false;
}
else if(memcmp(attr_type.data(), XOR_MAPPED_ADDRESS, 2) == 0)
{
std::string ip;
uint16_t port;
parse_xor_address(attr_value, trans_id_str);
std::tie(ip, port) = getSocksAddress(attr_value);
response.xor_ip = ip;
response.xor_port = port;
response.failed = false;
}
}
return response;
}
Expand Down
8 changes: 4 additions & 4 deletions src/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,16 +383,16 @@ int putSocksAddress(char **p, const std::string &host, const uint16_t dest_port)
std::tuple<std::string, uint16_t> getSocksAddress(const std::string &data)
{
char cAddr[128] = {};
std::string retAddr, port_str = data.substr(1, 2);
int family = data[0];
std::string retAddr, port_str = data.substr(2, 2);
int family = data[1];
uint16_t port = ntohs(*(short*)port_str.data());
switch(family)
{
case 1: //IPv4
inet_ntop(AF_INET, data.substr(3).data(), cAddr, 127);
inet_ntop(AF_INET, data.data() + 4, cAddr, 127);
break;
case 2: //IPv6
inet_ntop(AF_INET6, data.substr(3).data(), cAddr, 127);
inet_ntop(AF_INET6, data.data() + 4, cAddr, 127);
break;
}
retAddr.assign(cAddr);
Expand Down
3 changes: 3 additions & 0 deletions src/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
#define SOCKADDR_IN sockaddr_in
#define ZeroMemory(d,l) memset((d), 0, (l))
#define ioctlsocket ioctl
#define SD_BOTH SHUT_RDWR
#define SD_RECEIVE SHUT_RD
#define SD_SEND SHUT_WR
#ifndef SA_INTERRUPT
#define SA_INTERRUPT 0 //ignore this setting
#endif
Expand Down
25 changes: 17 additions & 8 deletions src/speedtestutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ void explodeSocks(std::string link, const std::string &custom_port, nodeInfo &no
node.proxyStr = socksConstruct(group, remarks, server, port, username, password);
}

void explodeHTTP(const std::string link, const std::string &custom_port, nodeInfo &node)
void explodeHTTP(const std::string &link, const std::string &custom_port, nodeInfo &node)
{
std::string group, remarks, server, port, username, password;
server = getUrlArg(link, "server");
Expand Down Expand Up @@ -719,7 +719,7 @@ void explodeHTTPSub(std::string link, const std::string &custom_port, nodeInfo &

void explodeTrojan(std::string trojan, const std::string &custom_port, nodeInfo &node)
{
std::string server, port, psk, addition, remark, host;
std::string server, port, psk, addition, group, remark, host;
tribool tfo, scv;
trojan.erase(0, 9);
string_size pos = trojan.rfind("#");
Expand All @@ -746,18 +746,21 @@ void explodeTrojan(std::string trojan, const std::string &custom_port, nodeInfo
host = getUrlArg(addition, "peer");
tfo = getUrlArg(addition, "tfo");
scv = getUrlArg(addition, "allowInsecure");
group = UrlDecode(getUrlArg(addition, "group"));

if(remark.empty())
remark = server + ":" + port;
if(host.empty() && !isIPv4(server) && !isIPv6(server))
host = server;
if(group.empty())
group = TROJAN_DEFAULT_GROUP;

node.linkType = SPEEDTEST_MESSAGE_FOUNDTROJAN;
node.group = TROJAN_DEFAULT_GROUP;
node.group = group;
node.remarks = remark;
node.server = server;
node.port = to_int(port, 1);
node.proxyStr = trojanConstruct(node.group, remark, server, port, psk, host, true, tribool(), tfo, scv);
node.proxyStr = trojanConstruct(group, remark, server, port, psk, host, true, tribool(), tfo, scv);
}

void explodeQuan(const std::string &quan, const std::string &custom_port, nodeInfo &node)
Expand Down Expand Up @@ -1074,9 +1077,15 @@ void explodeClash(Node yamlnode, const std::string &custom_port, std::vector<nod
singleproxy["cipher"] >>= cipher;
singleproxy["password"] >>= password;
singleproxy["protocol"] >>= protocol;
singleproxy["protocolparam"] >>= protoparam;
singleproxy["obfs"] >>= obfs;
singleproxy["obfsparam"] >>= obfsparam;
if(singleproxy["protocol-param"].IsDefined())
singleproxy["protocol-param"] >>= protoparam;
else
singleproxy["protocolparam"] >>= protoparam;
if(singleproxy["obfs-param"].IsDefined())
singleproxy["obfs-param"] >>= obfsparam;
else
singleproxy["obfsparam"] >>= obfsparam;

node.linkType = SPEEDTEST_MESSAGE_FOUNDSSR;
node.proxyStr = ssrConstruct(group, ps, base64_encode(ps), server, port, protocol, cipher, obfs, password, obfsparam, protoparam, ssr_libev, udp, tfo, scv);
Expand Down Expand Up @@ -1140,7 +1149,7 @@ void explodeShadowrocket(std::string rocket, const std::string &custom_port, nod
port = custom_port;
if(port == "0")
return;
remarks = UrlDecode(getUrlArg(addition, "remark"));
remarks = UrlDecode(getUrlArg(addition, "remarks"));
obfs = getUrlArg(addition, "obfs");
if(obfs.size())
{
Expand Down Expand Up @@ -1933,7 +1942,7 @@ int explodeConfContent(const std::string &content, const std::string &custom_por
return SPEEDTEST_ERROR_NONE;
}

void explode(std::string link, bool sslibev, bool ssrlibev, const std::string &custom_port, nodeInfo &node)
void explode(const std::string &link, bool sslibev, bool ssrlibev, const std::string &custom_port, nodeInfo &node)
{
// TODO: replace strFind with startsWith if appropriate
if(strFind(link, "ssr://"))
Expand Down
2 changes: 1 addition & 1 deletion src/speedtestutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void explodeQuan(const std::string &quan, const std::string &custom_port, nodeIn
void explodeShadowrocket(std::string kit, const std::string &custom_port, nodeInfo &node);
void explodeKitsunebi(std::string kit, const std::string &custom_port, nodeInfo &node);
/// Parse a link
void explode(std::string link, bool sslibev, bool ssrlibev, const std::string &custom_port, nodeInfo &node);
void explode(const std::string &link, bool sslibev, bool ssrlibev, const std::string &custom_port, nodeInfo &node);
void explodeSSD(std::string link, bool libev, const std::string &custom_port, std::vector<nodeInfo> &nodes);
void explodeSub(std::string sub, bool sslibev, bool ssrlibev, const std::string &custom_port, std::vector<nodeInfo> &nodes);
int explodeConf(std::string filepath, const std::string &custom_port, bool sslibev, bool ssrlibev, std::vector<nodeInfo> &nodes);
Expand Down