Enhancements

Add basic support for generating sing-box configs.
Use cpp-httplib as web server.
Optimize codes.
This commit is contained in:
Tindy X committed 2023-11-07 22:12:01 +08:00
1 parent f99748b126
commit bbcb6434e1
32 files changed
+10286 -234

No files matched your search

+106 -56
View File
@@ -13,12 +13,28 @@ int script_context_init(qjs::Context &context);
int script_cleanup(qjs::Context &context);
void script_print_stack(qjs::Context &context);
inline JSValue JS_NewString(JSContext *ctx, const std::string& str)
{
return JS_NewStringLen(ctx, str.c_str(), str.size());
}
inline std::string JS_GetPropertyToString(JSContext *ctx, JSValue v, const char* prop)
{
auto val = JS_GetPropertyStr(ctx, v, prop);
auto valData = JS_ToCString(ctx, val);
std::string result = valData;
JS_FreeCString(ctx, valData);
size_t len;
const char *str = JS_ToCStringLen(ctx, &len, val);
std::string result(str, len);
JS_FreeCString(ctx, str);
JS_FreeValue(ctx, val);
return result;
}
inline std::string JS_GetPropertyToString(JSContext *ctx, JSValueConst obj, uint32_t index) {
JSValue val = JS_GetPropertyUint32(ctx, obj, index);
size_t len;
const char *str = JS_ToCStringLen(ctx, &len, val);
std::string result(str, len);
JS_FreeCString(ctx, str);
JS_FreeValue(ctx, val);
return result;
}
@@ -33,7 +49,7 @@ inline int JS_GetPropertyToInt32(JSContext *ctx, JSValue v, const char* prop, in
return result;
}
inline int JS_GetPropertyToUInt32(JSContext *ctx, JSValue v, const char* prop, uint32_t def_value = 0)
inline uint32_t JS_GetPropertyToUInt32(JSContext *ctx, JSValue v, const char* prop, uint32_t def_value = 0)
{
uint32_t result = def_value;
auto val = JS_GetPropertyStr(ctx, v, prop);
@@ -85,79 +101,98 @@ namespace qjs
}
};
template<>
struct js_traits<StringArray> {
static StringArray unwrap(JSContext *ctx, JSValueConst v) {
StringArray arr;
uint32_t length = JS_GetPropertyToUInt32(ctx, v, "length");
for (uint32_t i = 0; i < length; i++) {
arr.push_back(JS_GetPropertyToString(ctx, v, i));
}
return arr;
}
static JSValue wrap(JSContext *ctx, const StringArray& arr) {
JSValue jsArray = JS_NewArray(ctx);
for (std::size_t i = 0; i < arr.size(); i++) {
JS_SetPropertyUint32(ctx, jsArray, i, JS_NewString(ctx, arr[i]));
}
return jsArray;
}
};
template<>
struct js_traits<Proxy>
{
static JSValue wrap(JSContext *ctx, const Proxy &n) noexcept
{
auto obj = JS_NewObject(ctx);
/*
JS_SetPropertyStr(ctx, obj, "LinkType", JS_NewInt32(ctx, n.linkType));
JS_SetPropertyStr(ctx, obj, "ID", JS_NewInt32(ctx, n.id));
JS_SetPropertyStr(ctx, obj, "GroupID", JS_NewInt32(ctx, n.groupID));
JS_SetPropertyStr(ctx, obj, "Group", JS_NewStringLen(ctx, n.group.c_str(), n.group.size()));
JS_SetPropertyStr(ctx, obj, "Remark", JS_NewStringLen(ctx, n.remarks.c_str(), n.remarks.size()));
JS_SetPropertyStr(ctx, obj, "Server", JS_NewStringLen(ctx, n.server.c_str(), n.server.size()));
JS_SetPropertyStr(ctx, obj, "Port", JS_NewInt32(ctx, n.port));
JS_SetPropertyStr(ctx, obj, "ProxyInfo", JS_NewStringLen(ctx, n.proxyStr.c_str(), n.proxyStr.size()));
*/
JS_SetPropertyStr(ctx, obj, "Type", JS_NewInt32(ctx, n.Type));
JS_SetPropertyStr(ctx, obj, "Id", JS_NewInt32(ctx, n.Id));
JS_SetPropertyStr(ctx, obj, "GroupId", JS_NewInt32(ctx, n.GroupId));
JS_SetPropertyStr(ctx, obj, "Group", JS_NewStringLen(ctx, n.Group.c_str(), n.Group.size()));
JS_SetPropertyStr(ctx, obj, "Remark", JS_NewStringLen(ctx, n.Remark.c_str(), n.Remark.size()));
JS_SetPropertyStr(ctx, obj, "Server", JS_NewStringLen(ctx, n.Hostname.c_str(), n.Hostname.size()));
JS_SetPropertyStr(ctx, obj, "Port", JS_NewInt32(ctx, n.Port));
JSValue obj = JS_NewObjectProto(ctx, JS_NULL);
if (JS_IsException(obj)) {
return obj;
}
JS_SetPropertyStr(ctx, obj, "Username", JS_NewStringLen(ctx, n.Username.c_str(), n.Username.size()));
JS_SetPropertyStr(ctx, obj, "Password", JS_NewStringLen(ctx, n.Password.c_str(), n.Password.size()));
JS_SetPropertyStr(ctx, obj, "EncryptMethod", JS_NewStringLen(ctx, n.EncryptMethod.c_str(), n.EncryptMethod.size()));
JS_SetPropertyStr(ctx, obj, "Plugin", JS_NewStringLen(ctx, n.Plugin.c_str(), n.Plugin.size()));
JS_SetPropertyStr(ctx, obj, "PluginOption", JS_NewStringLen(ctx, n.PluginOption.c_str(), n.PluginOption.size()));
JS_SetPropertyStr(ctx, obj, "Protocol", JS_NewStringLen(ctx, n.Protocol.c_str(), n.Protocol.size()));
JS_SetPropertyStr(ctx, obj, "ProtocolParam", JS_NewStringLen(ctx, n.ProtocolParam.c_str(), n.ProtocolParam.size()));
JS_SetPropertyStr(ctx, obj, "OBFS", JS_NewStringLen(ctx, n.OBFS.c_str(), n.OBFS.size()));
JS_SetPropertyStr(ctx, obj, "OBFSParam", JS_NewStringLen(ctx, n.OBFSParam.c_str(), n.OBFSParam.size()));
JS_SetPropertyStr(ctx, obj, "UserId", JS_NewStringLen(ctx, n.UserId.c_str(), n.UserId.size()));
JS_DefinePropertyValueStr(ctx, obj, "Type", JS_NewInt32(ctx, n.Type), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "Id", JS_NewUint32(ctx, n.Id), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "GroupId", JS_NewUint32(ctx, n.GroupId), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "Group", JS_NewString(ctx, n.Group), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "Remark", JS_NewString(ctx, n.Remark), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "Server", JS_NewString(ctx, n.Hostname), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "Port", JS_NewInt32(ctx, n.Port), JS_PROP_C_W_E);
JS_SetPropertyStr(ctx, obj, "AlterId", JS_NewInt32(ctx, n.AlterId));
JS_SetPropertyStr(ctx, obj, "TransferProtocol", JS_NewStringLen(ctx, n.TransferProtocol.c_str(), n.TransferProtocol.size()));
JS_SetPropertyStr(ctx, obj, "FakeType", JS_NewStringLen(ctx, n.FakeType.c_str(), n.FakeType.size()));
JS_SetPropertyStr(ctx, obj, "TLSSecure", JS_NewBool(ctx, n.TLSSecure));
JS_DefinePropertyValueStr(ctx, obj, "Username", JS_NewString(ctx, n.Username), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "Password", JS_NewString(ctx, n.Password), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "EncryptMethod", JS_NewString(ctx, n.EncryptMethod), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "Plugin", JS_NewString(ctx, n.Plugin), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "PluginOption", JS_NewString(ctx, n.PluginOption), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "Protocol", JS_NewString(ctx, n.Protocol), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "ProtocolParam", JS_NewString(ctx, n.ProtocolParam), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "OBFS", JS_NewString(ctx, n.OBFS), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "OBFSParam", JS_NewString(ctx, n.OBFSParam), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "UserId", JS_NewString(ctx, n.UserId), JS_PROP_C_W_E);
JS_SetPropertyStr(ctx, obj, "Host", JS_NewStringLen(ctx, n.Host.c_str(), n.Host.size()));
JS_SetPropertyStr(ctx, obj, "Path", JS_NewStringLen(ctx, n.Path.c_str(), n.Path.size()));
JS_SetPropertyStr(ctx, obj, "Edge", JS_NewStringLen(ctx, n.Edge.c_str(), n.Edge.size()));
JS_DefinePropertyValueStr(ctx, obj, "AlterId", JS_NewInt32(ctx, n.AlterId), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "TransferProtocol", JS_NewString(ctx, n.TransferProtocol), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "FakeType", JS_NewString(ctx, n.FakeType), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "TLSSecure", JS_NewBool(ctx, n.TLSSecure), JS_PROP_C_W_E);
JS_SetPropertyStr(ctx, obj, "QUICSecure", JS_NewStringLen(ctx, n.QUICSecure.c_str(), n.QUICSecure.size()));
JS_SetPropertyStr(ctx, obj, "QUICSecret", JS_NewStringLen(ctx, n.QUICSecret.c_str(), n.QUICSecret.size()));
JS_DefinePropertyValueStr(ctx, obj, "Host", JS_NewString(ctx, n.Host), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "Path", JS_NewString(ctx, n.Path), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "Edge", JS_NewString(ctx, n.Edge), JS_PROP_C_W_E);
JS_SetPropertyStr(ctx, obj, "UDP", js_traits<tribool>::wrap(ctx, n.UDP));
JS_SetPropertyStr(ctx, obj, "TCPFastOpen", js_traits<tribool>::wrap(ctx, n.TCPFastOpen));
JS_SetPropertyStr(ctx, obj, "AllowInsecure", js_traits<tribool>::wrap(ctx, n.AllowInsecure));
JS_SetPropertyStr(ctx, obj, "TLS13", js_traits<tribool>::wrap(ctx, n.TLS13));
JS_DefinePropertyValueStr(ctx, obj, "QUICSecure", JS_NewString(ctx, n.QUICSecure), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "QUICSecret", JS_NewString(ctx, n.QUICSecret), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "UDP", js_traits<tribool>::wrap(ctx, n.UDP), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "TCPFastOpen", js_traits<tribool>::wrap(ctx, n.TCPFastOpen), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "AllowInsecure", js_traits<tribool>::wrap(ctx, n.AllowInsecure), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "TLS13", js_traits<tribool>::wrap(ctx, n.TLS13), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "SnellVersion", JS_NewInt32(ctx, n.SnellVersion), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "ServerName", JS_NewString(ctx, n.ServerName), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "SelfIP", JS_NewString(ctx, n.SelfIP), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "SelfIPv6", JS_NewString(ctx, n.SelfIPv6), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "PublicKey", JS_NewString(ctx, n.PublicKey), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "PrivateKey", JS_NewString(ctx, n.PrivateKey), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "PreSharedKey", JS_NewString(ctx, n.PreSharedKey), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "DnsServers", js_traits<StringArray>::wrap(ctx, n.DnsServers), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "Mtu", JS_NewUint32(ctx, n.Mtu), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "AllowedIPs", JS_NewString(ctx, n.AllowedIPs), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "KeepAlive", JS_NewUint32(ctx, n.KeepAlive), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "TestUrl", JS_NewString(ctx, n.TestUrl), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "ClientId", JS_NewString(ctx, n.ClientId), JS_PROP_C_W_E);
return obj;
}
static Proxy unwrap(JSContext *ctx, JSValueConst v)
{
Proxy node;
/*
node.linkType = JS_GetPropertyToInt32(ctx, v, "LinkType");
node.id = JS_GetPropertyToInt32(ctx, v, "ID");
node.groupID = JS_GetPropertyToInt32(ctx, v, "GroupID");
node.group = JS_GetPropertyToString(ctx, v, "Group");
node.remarks = JS_GetPropertyToString(ctx, v, "Remark");
node.server = JS_GetPropertyToString(ctx, v, "Server");
node.port = JS_GetPropertyToInt32(ctx, v, "Port");
node.proxyStr = JS_GetPropertyToString(ctx, v, "ProxyInfo");
*/
node.Type = JS_GetPropertyToInt32(ctx, v, "Type");
node.Id = JS_GetPropertyToInt32(ctx, v, "Id");
node.GroupId = JS_GetPropertyToInt32(ctx, v, "GroupId");
node.Group = JS_GetPropertyToString(ctx, v, "Group");
node.Remark = JS_GetPropertyToString(ctx, v, "Remark");
node.Hostname = JS_GetPropertyToString(ctx, v, "Hostname");
node.Hostname = JS_GetPropertyToString(ctx, v, "Server");
node.Port = JS_GetPropertyToUInt32(ctx, v, "Port");
node.Username = JS_GetPropertyToString(ctx, v, "Username");
@@ -187,6 +222,21 @@ namespace qjs
node.AllowInsecure = js_traits<tribool>::JS_GetPropertyToTriBool(ctx, v, "AllowInsecure");
node.TLS13 = js_traits<tribool>::JS_GetPropertyToTriBool(ctx, v, "TLS13");
node.SnellVersion = JS_GetPropertyToInt32(ctx, v, "SnellVersion");
node.ServerName = JS_GetPropertyToString(ctx, v, "ServerName");
node.SelfIP = JS_GetPropertyToString(ctx, v, "SelfIP");
node.SelfIPv6 = JS_GetPropertyToString(ctx, v, "SelfIPv6");
node.PublicKey = JS_GetPropertyToString(ctx, v, "PublicKey");
node.PrivateKey = JS_GetPropertyToString(ctx, v, "PrivateKey");
node.PreSharedKey = JS_GetPropertyToString(ctx, v, "PreSharedKey");
node.DnsServers = js_traits<StringArray>::unwrap(ctx, JS_GetPropertyStr(ctx, v, "DnsServers"));
node.Mtu = JS_GetPropertyToUInt32(ctx, v, "Mtu");
node.AllowedIPs = JS_GetPropertyToString(ctx, v, "AllowedIPs");
node.KeepAlive = JS_GetPropertyToUInt32(ctx, v, "KeepAlive");
node.TestUrl = JS_GetPropertyToString(ctx, v, "TestUrl");
node.ClientId = JS_GetPropertyToString(ctx, v, "ClientId");
return node;
}
};