Skip to content
This repository has been archived by the owner on Aug 17, 2021. It is now read-only.

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Qv2ray name = Qv2ray Bot authored and Qv2ray name = Qv2ray Bot committed Jan 19, 2020
0 parents commit 0f179c0
Show file tree
Hide file tree
Showing 16 changed files with 2,033 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Enforce Unix newlines
* text=auto eol=lf
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*.opensdf
/*.sdf
/*.suo
/.vs
x64
x86
/bin
*.user
*.filters
*_i.c
*_i.h
*_p.c
src/dlldata.c
/src/COM+_h.h
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "3rdparty/x2struct"]
path = 3rdparty/x2struct
url = https://github.com/xyz347/x2struct
1 change: 1 addition & 0 deletions 3rdparty/x2struct
Submodule x2struct added at 09d790
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions Qv2ray-NetSpeedPlugin.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29409.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Qv2rayNetSpeedWidget", "src\QvNetSpeedBar.vcxproj", "{132096EE-1897-4D28-82C3-54A082CC3CCF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Releasing|x64 = Releasing|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{132096EE-1897-4D28-82C3-54A082CC3CCF}.Debug|x64.ActiveCfg = Debug|x64
{132096EE-1897-4D28-82C3-54A082CC3CCF}.Debug|x64.Build.0 = Debug|x64
{132096EE-1897-4D28-82C3-54A082CC3CCF}.Releasing|x64.ActiveCfg = Releasing|x64
{132096EE-1897-4D28-82C3-54A082CC3CCF}.Releasing|x64.Build.0 = Releasing|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BDF3029A-1F51-4C01-8891-AA393762D762}
EndGlobalSection
EndGlobal
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Qv2ray-Plugin-WinToolbar
A taskbar toolbox plugin showing necessary info for Qv2ray.

Using COM+, NamedPipe and it's Windows-only

## Issues

- ENGLISH ONLY!

## Special Credit

- [@XhmikosR](https://github.com/XhmikosR/perfmonbar) for the GREAT **perfmonbar** project
- https://github.com/XhmikosR/perfmonbar
67 changes: 67 additions & 0 deletions src/COM+/COM+.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
Copyright (C) 2019 Leroy.H.Y
Copyright (C) 2008 Danny Couture
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "../stdafx.h"
#include <atlbase.h>
#include "../Qv2ray_NetSpeedBar_i.h"
#include "../NetSpeedUI.h"
#include <clocale>

ATL::CComModule _Module;

BEGIN_OBJECT_MAP(ObjectMap)
OBJECT_ENTRY(CLSID_QvNetSpeedBar, QvSpeedBar_UI)
END_OBJECT_MAP()

BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
std::setlocale(LC_ALL, "en_US.UTF-8");
UNREFERENCED_PARAMETER(lpReserved);
if (dwReason == DLL_PROCESS_ATTACH) {
_Module.Init(ObjectMap, hInstance, &LIBID_QvNetSpeedBarLib);
DisableThreadLibraryCalls(hInstance);
}
else if (dwReason == DLL_PROCESS_DETACH) {
_Module.Term();
}
return TRUE; // ok
}

// Used to determine whether the DLL can be unloaded by OLE
STDAPI DllCanUnloadNow()
{
return (_Module.GetLockCount() == 0) ? S_OK : S_FALSE;
}

// Returns a class factory to create an object of the requested type
STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
{
return _Module.GetClassObject(rclsid, riid, ppv);
}

// DllRegisterServer - Adds entries to the system registry
STDAPI DllRegisterServer()
{
return _Module.RegisterServer(TRUE);
}

// DllUnregisterServer - Removes entries from the system registry
STDAPI DllUnregisterServer()
{
return _Module.UnregisterServer(TRUE);
}
5 changes: 5 additions & 0 deletions src/COM+/COM+.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
EXPORTS
DllCanUnloadNow PRIVATE
DllGetClassObject PRIVATE
DllRegisterServer PRIVATE
DllUnregisterServer PRIVATE
59 changes: 59 additions & 0 deletions src/COM+/COM+.idl
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright (C) 2019 Leroy.H.Y
Copyright (C) 2008 Danny Couture

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.

*/

// This file will be processed by the MIDL tool to
// produce the type library (PerfmonBar.tlb) and marshalling code.
midl_pragma warning(disable: 2111) // identifier length exceeds 31 characters
import "oaidl.idl";
import "ocidl.idl";
midl_pragma warning(default: 2111)

[
object,
//{ADA0A5C2-690F-4D11-BDE4-45D054326222}
uuid(398148EC-DE70-4F08-97C9-8B9F4EC1BE6E),
dual,
nonextensible,
helpstring("Qv2ray Network Speed Bar Interface"),
pointer_default(unique)
]

interface IQvNetSpeedBar : IDispatch {
};
[
//{61DF45AB-F973-476A-9A6C-1056B1DDCF98}
uuid(899F7D7A-9065-4D5F-BE09-68D9AC6B8BA9),
version(1.0),
helpstring("Qv2ray Network Speed Bar 1.0 Type Library")
]

library QvNetSpeedBarLib {
importlib("stdole32.tlb");
importlib("stdole2.tlb");

[
//{88C6ABF2-4C63-4B90-98B3-4DCB2615B234}
uuid(6788D578-A1CD-4697-A81A-BAE22F035D99),
helpstring("Qv2ray Network Speed Bar")
]
coclass QvNetSpeedBar
{
[default] interface IQvNetSpeedBar;
};
};
199 changes: 199 additions & 0 deletions src/Configuration.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/*
Copyright (C) 2019 Leroy.H.Y
Copyright (C) 2008 Danny Couture
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "stdafx.h"
#include <atlbase.h>
#include <vector>
#include <shlobj.h>
#include "Configuration.h"
#include <locale>
#include <codecvt>

#define BUFSIZE 10240
DWORD cbToWrite, cbWritten, dwMode;

QvSpeedBar_DataSource::QvSpeedBar_DataSource()
{
_pages.Pages.clear();
static bool ole_initialized = false;
if (!ole_initialized) {
if (SUCCEEDED(CoInitialize(nullptr))) {
ole_initialized = true;
}
}
TryConnectPipe();
}

void QvSpeedBar_DataSource::SetSingleLineMessage(std::string msg) {
QvBarPage page;
QvBarLine line;
line.Message = msg;
page.Lines.push_back(line);
page.OffsetYpx = 12;
_pages.Pages.clear();
_pages.Pages.push_back(page);
}

bool QvSpeedBar_DataSource::TryConnectPipe()
{
if (hQv2rayServerPipe != NULL)
{
bool hResult = PeekNamedPipe(hQv2rayServerPipe, NULL, NULL, NULL, NULL, NULL);
if (hResult) {
return true;
}
}
bool fSuccess = FALSE;
while (1)
{
SetSingleLineMessage("未连接到 Qv2ray");
hQv2rayServerPipe = CreateFile(lpszPipename, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

if (hQv2rayServerPipe != INVALID_HANDLE_VALUE)
return true;

if (GetLastError() != ERROR_PIPE_BUSY)
return false;

if (!WaitNamedPipe(lpszPipename, 5000))
return false;
}
dwMode = PIPE_READMODE_MESSAGE;
fSuccess = SetNamedPipeHandleState(hQv2rayServerPipe, &dwMode, NULL, NULL);
return fSuccess;

}
void QvSpeedBar_DataSource::LoopRead()
{
DWORD cbRead;
char chBuf[BUFSIZE * 2] = { 0 };
BOOL fSuccess = FALSE;

while (!IsTerminating)
{
if (TryConnectPipe()) {
LPCTSTR lpvMessage = _data.c_str();
cbToWrite = (lstrlen(lpvMessage) + 1) * sizeof(TCHAR);

fSuccess = WriteFile(hQv2rayServerPipe, lpvMessage, cbToWrite, &cbWritten, NULL);
do
{
fSuccess = ReadFile(hQv2rayServerPipe, chBuf, BUFSIZE * sizeof(TCHAR), &cbRead, NULL);

if (!fSuccess && GetLastError() != ERROR_MORE_DATA)
{
break;
}

} while (!fSuccess);
auto rcvd = std::string(chBuf);
_data = L"OK";

QvNetSpeedBarConfig config;
x2struct::X::loadjson(rcvd, config, false);
_pages = config;
}
Sleep(500);
}
}

DWORD WINAPI NamePipeThreadFunc(LPVOID data) {
QvSpeedBar_DataSource* source = static_cast<QvSpeedBar_DataSource*>(data);
source->LoopRead();
return 0;
}

std::wstring utf8_to_utf16(const std::string& utf8)
{
try
{
std::vector<unsigned long> unicode;
size_t i = 0;
while (i < utf8.size())
{
unsigned long uni;
size_t todo;
bool error = false;
unsigned char ch = utf8[i++];
if (ch <= 0x7F)
{
uni = ch;
todo = 0;
}
else if (ch <= 0xBF)
{
throw std::logic_error("not a UTF-8 string");
}
else if (ch <= 0xDF)
{
uni = ch & 0x1F;
todo = 1;
}
else if (ch <= 0xEF)
{
uni = ch & 0x0F;
todo = 2;
}
else if (ch <= 0xF7)
{
uni = ch & 0x07;
todo = 3;
}
else
{
throw std::logic_error("not a UTF-8 string");
}
for (size_t j = 0; j < todo; ++j)
{
// Should be j?
if (i == utf8.size())
throw std::logic_error("not a UTF-8 string");
unsigned char ch = utf8[i++];
if (ch < 0x80 || ch > 0xBF)
throw std::logic_error("not a UTF-8 string");
uni <<= 6;
uni += ch & 0x3F;
}
if (uni >= 0xD800 && uni <= 0xDFFF)
throw std::logic_error("not a UTF-8 string");
if (uni > 0x10FFFF)
throw std::logic_error("not a UTF-8 string");
unicode.push_back(uni);
}
std::wstring utf16;
for (size_t i = 0; i < unicode.size(); ++i)
{
unsigned long uni = unicode[i];
if (uni <= 0xFFFF)
{
utf16 += (wchar_t)uni;
}
else
{
uni -= 0x10000;
utf16 += (wchar_t)((uni >> 10) + 0xD800);
utf16 += (wchar_t)((uni & 0x3FF) + 0xDC00);
}
}
return utf16;
}
catch (const std::exception & wex)
{
return utf8_to_utf16(wex.what());
}
}
Loading

0 comments on commit 0f179c0

Please sign in to comment.