add convert json to toml by toml.js

This commit is contained in:
2023-12-29 19:31:55 +08:00
parent 91c15936ee
commit 83ad03eca3
7 changed files with 8590 additions and 171 deletions

View File

@@ -16,6 +16,21 @@ var loadClientInfo = (function ($) {
type: 'none'
}).done(function (result) {
if (result.success) {
var proxies = [];
result.data.proxies.forEach(function (proxy){
var items = flatJSON(proxy.ProxyConfigurer);
proxies.push(expandJSON(items))
})
var visitors = [];
result.data.visitors.forEach(function (visitor){
var items = flatJSON(visitor.VisitorConfigurer);
visitors.push(expandJSON(items))
})
var newD = $.extend({},result.data,true);
newD.proxies = proxies;
newD.visitors = visitors;
console.log(TOML.stringify(newD))
renderClientInfo(result.data);
} else {
layui.layer.msg(result.message);

View File

@@ -1,120 +1,5 @@
var loadProxyInfo = (function ($) {
var i18n = {}, currentProxyType, currentTitle;
//param names in Basic tab
var basicParams = [
{
name: 'name',
defaultValue: '-'
}, {
name: 'type',
defaultValue: '-'
}, {
name: 'localIP',
defaultValue: '-'
}, {
name: 'localPort',
defaultValue: '-'
}, {
name: 'customDomains',
defaultValue: '-'
}, {
name: 'subdomain',
defaultValue: '-'
}, {
name: 'remotePort',
defaultValue: '-'
}, {
name: 'transport.useEncryption',
defaultValue: 'true',
}, {
name: 'transport.useCompression',
defaultValue: 'true',
}
];
/**
* a.b.c = 1
* a.b.d = [2,3]
* to
* {a: {
* b: {
* c: 1
* d: "[2,3]"
* }
* }
* }
*
* @param obj json object
* @param names all names split from name string like 'a.b.c'
* @param value default value
*/
function expandJSON(obj, names, value) {
var currentIndex = this.index || 0;
var childrenIndex = (currentIndex + 1) > names.length ? null : (currentIndex + 1);
var currentName = names[currentIndex], currentValue = {};
var childrenName = childrenIndex == null ? null : names[childrenIndex];
if (obj.hasOwnProperty(currentName)) {
currentValue = obj[currentName];
} else {
obj[currentName] = currentValue;
}
if (childrenName !== null) {
this.index = childrenIndex;
expandJSON(currentValue, names, value);
} else {
if (value != null) {
if (Array.isArray(value)) {
obj[currentName] = JSON.stringify(value);
} else {
obj[currentName] = value;
}
}
this.index = 0;
}
}
/**
* {a: {
* b: {
* c: 1
* d: [2,3]
* }
* }
* }
* to
* {
* 'a.b.c': 1,
* 'a.b.d': '[2,3]'
* }
*
* @param obj json object
* @returns {*} flatted json key array
*/
function flatJSON(obj) {
var flat = function (obj, prentKey, flattedJSON) {
flattedJSON = flattedJSON || {};
prentKey = prentKey || '';
if (prentKey !== '')
prentKey = prentKey + '.';
for (var key in obj) {
var value = obj[key];
if (typeof value === 'object' && Object.prototype.toString.call(value) === '[object Object]') {
flat(value, prentKey + key, flattedJSON);
} else {
if (prentKey === 'plugin.ClientPluginOptions.')
prentKey = 'plugin.';
if (Array.isArray(value)) {
flattedJSON[prentKey + key] = JSON.stringify(value);
} else {
flattedJSON[prentKey + key] = value;
}
}
}
return flattedJSON;
}
return flat(obj);
}
/**
* get proxy info
@@ -282,9 +167,9 @@ var loadProxyInfo = (function ($) {
basicParams.forEach(function (basicName) {
var name = basicName.name;
if (name.indexOf('.') !== -1) {
var names = name.split('.');
expandJSON(tempData, names, 0, basicName.defaultValue);
expandJSON(basicData, names, 0, null);
var keys = name.split('.');
expandJSONKeys(tempData, keys, basicName.defaultValue);
expandJSONKeys(basicData, keys, null);
}
eval('basicData.' + name + ' = ' + 'tempData.' + name);
@@ -325,6 +210,7 @@ var loadProxyInfo = (function ($) {
var value = $(this).find('input').last().val();
formData[name] = value;
});
addOrUpdate(formData, index, update);
}
},
@@ -375,15 +261,20 @@ var loadProxyInfo = (function ($) {
var loading = layui.layer.load();
var url = '';
if (update) {
url = '/update';
url = '/update?type=' + currentProxyType;
} else {
url = '/add?type=' + currentProxyType;
}
var tomlStr = TOML.stringify(expandJSON(data));
//todo get all proxy and replace or add a new proxy in it
$.ajax({
url: url,
type: 'post',
contentType: 'application/json',
data: JSON.stringify(data),
data: JSON.stringify(expandJSON(data)),
success: function (result) {
if (result.success) {
layui.layer.close(index);

View File

@@ -0,0 +1,183 @@
(function (){
//param names in Basic tab
var basicParams = [
{
name: 'name',
defaultValue: '-'
}, {
name: 'type',
defaultValue: '-'
}, {
name: 'localIP',
defaultValue: '-'
}, {
name: 'localPort',
defaultValue: '-'
}, {
name: 'customDomains',
defaultValue: '-'
}, {
name: 'subdomain',
defaultValue: '-'
}, {
name: 'remotePort',
defaultValue: '-'
}, {
name: 'transport.useEncryption',
defaultValue: 'true',
}, {
name: 'transport.useCompression',
defaultValue: 'true',
}
];
var mapParams = [{
name: 'plugin.ClientPluginOptions',
map: 'plugin'
}];
var paramTypes = {
number: [
'healthCheck.timeoutSeconds',
'healthCheck.maxFailed',
'healthCheck.intervalSeconds',
'localPort',
],
boolean: [
'transport.useEncryption',
'transport.useCompression'
],
array: [
'customDomains',
'locations',
'allowUsers'
],
map: [
'metadatas',
'requestHeaders.set',
'plugin.ClientPluginOptions.requestHeaders.set'
]
}
/**
* a.b.c = 1
* a.b.d = [2,3]
* to
* {a: {
* b: {
* c: 1
* d: "[2,3]"
* }
* }
* }
*
* @param obj json object
* @param keys all keys split from key string like 'a.b.c'
* @param value default value
* @param stringifyArray if true, when value is an array, it will stringify by JSON.stringify(value)
*/
function expandJSONKeys(obj, keys, value, stringifyArray) {
stringifyArray = stringifyArray == null ? true : stringifyArray;
var currentIndex = this.index || 0;
var childrenIndex = (currentIndex + 1) > keys.length ? null : (currentIndex + 1);
var currentKey = keys[currentIndex], currentValue = {};
var childrenKey = childrenIndex == null ? null : keys[childrenIndex];
if (obj.hasOwnProperty(currentKey)) {
currentValue = obj[currentKey];
} else {
obj[currentKey] = currentValue;
}
if (childrenKey != null) {
this.index = childrenIndex;
expandJSONKeys(currentValue, keys, value, stringifyArray);
} else {
if (value != null) {
if (Array.isArray(value) && stringifyArray) {
obj[currentKey] = JSON.stringify(value);
} else {
obj[currentKey] = value;
}
}
this.index = 0;
}
}
function expandJSON(obj) {
var newObj = {};
for (var name in obj) {
var value = obj[name];
if (value === '') {
continue;
}
if (paramTypes.number.indexOf(name) !== -1) {
value = parseInt(value) || 0;
} else if (paramTypes.boolean.indexOf(name) !== -1) {
value = Boolean(value) || false;
} else if (paramTypes.array.indexOf(name) !== -1) {
value = eval('(' + value + ')') || [];
} else {
for (var i = 0; i < paramTypes.map.length; i++) {
var key = paramTypes.map[i];
if (name.startsWith(key)) {
var json = {};
json[name.substring(key.length + 1, name.length)] = value;
value = json;
name = name.substring(0, key.length)
break;
}
}
}
expandJSONKeys(newObj, name.split("."), value, false);
}
return newObj;
}
/**
* {a: {
* b: {
* c: 1
* d: [2,3]
* }
* }
* }
* to
* {
* 'a.b.c': 1,
* 'a.b.d': '[2,3]'
* }
*
* @param obj json object
* @returns {*} flatted json key array
*/
function flatJSON(obj) {
var flat = function (obj, prentKey, flattedJSON) {
flattedJSON = flattedJSON || {};
prentKey = prentKey || '';
if (prentKey !== '')
prentKey = prentKey + '.';
for (var key in obj) {
var value = obj[key];
if (typeof value === 'object' && Object.prototype.toString.call(value) === '[object Object]') {
flat(value, prentKey + key, flattedJSON);
} else {
for (var mapParam of mapParams) {
if (prentKey.startsWith(mapParam.name)) {
prentKey = mapParam.map + '.';
break;
}
}
if (Array.isArray(value)) {
flattedJSON[prentKey + key] = JSON.stringify(value);
} else {
flattedJSON[prentKey + key] = value;
}
}
}
return flattedJSON;
}
return flat(obj);
}
window.expandJSONKeys = expandJSONKeys;
window.expandJSON = expandJSON;
window.flatJSON = flatJSON;
})();

8319
assets/static/js/toml.js Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -6,6 +6,8 @@
<link rel="stylesheet" href="./static/css/layui-theme-dark.css?v=${ .version }">
<link rel="stylesheet" href="./static/css/index.css?v=${ .version }">
<link rel="stylesheet" href="./static/css/color.css?v=${ .version }">
<script src="./static/js/json-process.js?v=${ .version }"></script>
<script src="./static/js/toml.js?v=${ .version }"></script>
<script src="./static/lib/layui/layui.js?v=${ .version }"></script>
<script src="./static/js/index-client-info.js?v=${ .version }"></script>
<script src="./static/js/index-proxy-overview.js?v=${ .version }"></script>