mirror of
https://github.com/yhl452493373/frpc-panel.git
synced 2026-04-04 14:27:01 +08:00
serialize config sections to string;
add and update with different url
This commit is contained in:
@@ -2,6 +2,15 @@ var loadProxyInfo = (function ($) {
|
|||||||
var i18n = {};
|
var i18n = {};
|
||||||
//param names in Basic tab
|
//param names in Basic tab
|
||||||
var basicParamNames = ['name', 'type', 'local_ip', 'local_port', 'custom_domains', 'subdomain', 'remote_port', 'use_encryption', 'use_compression'];
|
var basicParamNames = ['name', 'type', 'local_ip', 'local_port', 'custom_domains', 'subdomain', 'remote_port', 'use_encryption', 'use_compression'];
|
||||||
|
//param need to convert type
|
||||||
|
var intParamNames = ['local_port', 'health_check_timeout_s', 'health_check_max_failed', 'health_check_interval_s'];
|
||||||
|
var booleanParamNames = ['use_encryption', 'use_compression'];
|
||||||
|
var stringArrayParamNames = ['custom_domains', 'locations']
|
||||||
|
var mapParamPrefixes = [
|
||||||
|
{name: 'metas', prefix: 'meta_'},
|
||||||
|
{name: 'plugin_params', prefix: 'plugin_'},
|
||||||
|
{name: 'headers', prefix: 'header_'}
|
||||||
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get proxy info
|
* get proxy info
|
||||||
@@ -46,6 +55,7 @@ var loadProxyInfo = (function ($) {
|
|||||||
|
|
||||||
var $section = $('#content > section');
|
var $section = $('#content > section');
|
||||||
var cols = [
|
var cols = [
|
||||||
|
{type: 'checkbox'},
|
||||||
{field: 'name', title: 'Name', sort: true},
|
{field: 'name', title: 'Name', sort: true},
|
||||||
{field: 'type', title: 'Type', sort: true},
|
{field: 'type', title: 'Type', sort: true},
|
||||||
{field: 'local_ip', title: 'Local Ip', sort: true},
|
{field: 'local_ip', title: 'Local Ip', sort: true},
|
||||||
@@ -92,7 +102,9 @@ var loadProxyInfo = (function ($) {
|
|||||||
|
|
||||||
switch (obj.event) {
|
switch (obj.event) {
|
||||||
case 'add':
|
case 'add':
|
||||||
proxyPopup(type, null);
|
proxyPopup(type, {
|
||||||
|
type: type
|
||||||
|
}, false);
|
||||||
break
|
break
|
||||||
case 'remove':
|
case 'remove':
|
||||||
// batchRemovePopup(data);
|
// batchRemovePopup(data);
|
||||||
@@ -109,7 +121,8 @@ var loadProxyInfo = (function ($) {
|
|||||||
|
|
||||||
switch (obj.event) {
|
switch (obj.event) {
|
||||||
case 'update':
|
case 'update':
|
||||||
proxyPopup(type, data);
|
data.oldName = data.name;
|
||||||
|
proxyPopup(type, data, true);
|
||||||
break;
|
break;
|
||||||
case 'remove':
|
case 'remove':
|
||||||
// removePopup(data);
|
// removePopup(data);
|
||||||
@@ -119,11 +132,12 @@ var loadProxyInfo = (function ($) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* add proxy popup
|
* addOrUpdate proxy popup
|
||||||
* @param type proxy type
|
* @param type proxy type
|
||||||
* @param data {Map<string,object>} proxy data
|
* @param data {Map<string,object>} proxy data
|
||||||
|
* @param update update flag. true - update, false - add
|
||||||
*/
|
*/
|
||||||
function proxyPopup(type, data) {
|
function proxyPopup(type, data, update) {
|
||||||
var basicData = {};
|
var basicData = {};
|
||||||
var extraData = [];
|
var extraData = [];
|
||||||
if (data != null) {
|
if (data != null) {
|
||||||
@@ -155,8 +169,15 @@ var loadProxyInfo = (function ($) {
|
|||||||
btn: ['Confirm', 'Cancel'],
|
btn: ['Confirm', 'Cancel'],
|
||||||
btn1: function (index) {
|
btn1: function (index) {
|
||||||
if (layui.form.validate('#addProxyTemplate')) {
|
if (layui.form.validate('#addProxyTemplate')) {
|
||||||
var formData = layui.form.val('addProxyTemplate');
|
var formData = layui.form.val('addProxyForm');
|
||||||
add(formData, index);
|
var $items = $('#addProxyForm .extra-param-tab-item .extra-param-item');
|
||||||
|
$items.each(function () {
|
||||||
|
var name = $(this).find('input').first().val();
|
||||||
|
var value = $(this).find('input').last().val();
|
||||||
|
formData[name] = value;
|
||||||
|
});
|
||||||
|
|
||||||
|
addOrUpdate(type, formData, index, update);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
btn2: function (index) {
|
btn2: function (index) {
|
||||||
@@ -169,6 +190,43 @@ var loadProxyInfo = (function ($) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* repack form data
|
||||||
|
* @param formData
|
||||||
|
* @returns {*}
|
||||||
|
*/
|
||||||
|
function repackData(formData) {
|
||||||
|
mapParamPrefixes.forEach(function (temp) {
|
||||||
|
var name = temp.name;
|
||||||
|
var prefix = temp.prefix;
|
||||||
|
for (var key in formData) {
|
||||||
|
if (key !== name && key.startsWith(prefix)) {
|
||||||
|
formData[name] = formData[name] || {};
|
||||||
|
var newKey = key.replace(prefix, '');
|
||||||
|
formData[name][newKey] = formData[key];
|
||||||
|
delete formData[key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
intParamNames.forEach(function (paramName) {
|
||||||
|
if (formData.hasOwnProperty(paramName) && formData[paramName] !== '') {
|
||||||
|
formData[paramName] = parseInt(formData[paramName]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
booleanParamNames.forEach(function (paramName) {
|
||||||
|
if (formData.hasOwnProperty(paramName) && formData[paramName] !== '') {
|
||||||
|
formData[paramName] = formData[paramName] === 'true';
|
||||||
|
}
|
||||||
|
});
|
||||||
|
stringArrayParamNames.forEach(function (paramName) {
|
||||||
|
if (formData.hasOwnProperty(paramName) && formData[paramName] !== '') {
|
||||||
|
formData[paramName] = formData[paramName].split(',');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return formData;
|
||||||
|
}
|
||||||
|
|
||||||
function proxyPopupSuccess(layero, index, that) {
|
function proxyPopupSuccess(layero, index, that) {
|
||||||
layui.form.render(null, 'addProxyForm');
|
layui.form.render(null, 'addProxyForm');
|
||||||
layui.form.on('input-affix(addition)', function (obj) {
|
layui.form.on('input-affix(addition)', function (obj) {
|
||||||
@@ -181,15 +239,15 @@ var loadProxyInfo = (function ($) {
|
|||||||
name: name,
|
name: name,
|
||||||
value: value
|
value: value
|
||||||
});
|
});
|
||||||
$paramValue.closest('.layui-tab-item').prepend(formItem);
|
$paramValue.closest('.layui-tab-item').append(formItem);
|
||||||
$paramName.val('');
|
$paramName.val('');
|
||||||
$paramValue.val('');
|
$paramValue.val('');
|
||||||
|
|
||||||
layui.form.render();
|
layui.form.render();
|
||||||
|
|
||||||
var tabContent = $paramValue.closest('.layui-tab-content');
|
// var tabContent = $paramValue.closest('.layui-tab-content');
|
||||||
var scrollHeight = tabContent.prop("scrollHeight");
|
// var scrollHeight = tabContent.prop("scrollHeight");
|
||||||
tabContent.scrollTop(scrollHeight)
|
// tabContent.scrollTop(scrollHeight)
|
||||||
});
|
});
|
||||||
layui.form.on('input-affix(subtraction)', function (obj) {
|
layui.form.on('input-affix(subtraction)', function (obj) {
|
||||||
var $elem = $(obj.elem);
|
var $elem = $(obj.elem);
|
||||||
@@ -198,62 +256,32 @@ var loadProxyInfo = (function ($) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* add proxy action
|
* addOrUpdate proxy action
|
||||||
* @param data {{user:string, token:string, comment:string, enable:boolean, ports:[string|number], domains:[string], subdomains:[string]}} user data
|
* @param type proxy type
|
||||||
|
* @param data proxy data
|
||||||
* @param index popup index
|
* @param index popup index
|
||||||
|
* @param update update flag. true - update, false - add
|
||||||
*/
|
*/
|
||||||
function add(data, index) {
|
function addOrUpdate(type, data, index, update) {
|
||||||
var loading = layui.layer.load();
|
var loading = layui.layer.load();
|
||||||
|
var url = '';
|
||||||
|
if (update) {
|
||||||
|
url = '/update';
|
||||||
|
} else {
|
||||||
|
url = '/add?type=' + type;
|
||||||
|
}
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: '/add',
|
url: url,
|
||||||
type: 'post',
|
type: 'post',
|
||||||
contentType: 'application/json',
|
contentType: 'application/json',
|
||||||
data: JSON.stringify(data),
|
data: JSON.stringify(data),
|
||||||
success: function (result) {
|
|
||||||
// if (result.success) {
|
|
||||||
// reloadTable();
|
|
||||||
// layui.layer.close(index);
|
|
||||||
// layui.layer.msg(i18n['OperateSuccess'], function (index) {
|
|
||||||
// layui.layer.close(index);
|
|
||||||
// });
|
|
||||||
// } else {
|
|
||||||
// errorMsg(result);
|
|
||||||
// }
|
|
||||||
},
|
|
||||||
complete: function () {
|
|
||||||
layui.layer.close(loading);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* update proxy action
|
|
||||||
* @param before {{user:string, token:string, comment:string, enable:boolean, ports:[string|number], domains:[string], subdomains:[string]}} data before update
|
|
||||||
* @param after {{user:string, token:string, comment:string, enable:boolean, ports:[string|number], domains:[string], subdomains:[string]}} data after update
|
|
||||||
*/
|
|
||||||
function update(before, after) {
|
|
||||||
before.ports.forEach(function (port, index) {
|
|
||||||
if (/^\d+$/.test(String(port))) {
|
|
||||||
before.ports[index] = parseInt(String(port));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
after.ports.forEach(function (port, index) {
|
|
||||||
if (/^\d+$/.test(String(port)) && typeof port === "string") {
|
|
||||||
after.ports[index] = parseInt(String(port));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
var loading = layui.layer.load();
|
|
||||||
$.ajax({
|
|
||||||
url: '/update',
|
|
||||||
type: 'post',
|
|
||||||
contentType: 'application/json',
|
|
||||||
data: JSON.stringify({
|
|
||||||
before: before,
|
|
||||||
after: after,
|
|
||||||
}),
|
|
||||||
success: function (result) {
|
success: function (result) {
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
layui.layer.msg(i18n['OperateSuccess']);
|
reloadTable();
|
||||||
|
layui.layer.close(index);
|
||||||
|
layui.layer.msg('OperateSuccess', function (index) {
|
||||||
|
layui.layer.close(index);
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
errorMsg(result);
|
errorMsg(result);
|
||||||
}
|
}
|
||||||
@@ -281,5 +309,36 @@ var loadProxyInfo = (function ($) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* reload user table
|
||||||
|
*/
|
||||||
|
function reloadTable() {
|
||||||
|
// var searchData = layui.form.val('searchForm');
|
||||||
|
var searchData = {};
|
||||||
|
layui.table.reloadData('tokenTable', {
|
||||||
|
where: searchData
|
||||||
|
}, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* show error message popup
|
||||||
|
* @param result
|
||||||
|
*/
|
||||||
|
function errorMsg(result) {
|
||||||
|
layui.layer.msg(result.message);
|
||||||
|
// var reason = i18n['OtherError'];
|
||||||
|
// if (result.code === 1)
|
||||||
|
// reason = i18n['ParamError'];
|
||||||
|
// else if (result.code === 2)
|
||||||
|
// reason = i18n['SaveError'];
|
||||||
|
// else if (result.code === 3)
|
||||||
|
// reason = i18n['FrpServerError'];
|
||||||
|
// else if (result.code === 4)
|
||||||
|
// reason = i18n['ProxyExist'];
|
||||||
|
// else if (result.code === 5)
|
||||||
|
// reason = i18n['ProxyNotExist'];
|
||||||
|
// layui.layer.msg(i18n['OperateFailed'] + ',' + reason)
|
||||||
|
}
|
||||||
|
|
||||||
return loadProxyInfo;
|
return loadProxyInfo;
|
||||||
})(layui.$);
|
})(layui.$);
|
||||||
@@ -163,8 +163,7 @@
|
|||||||
<div class="layui-form-item">
|
<div class="layui-form-item">
|
||||||
<label class="layui-form-label">ProxyType</label>
|
<label class="layui-form-label">ProxyType</label>
|
||||||
<div class="layui-input-block">
|
<div class="layui-input-block">
|
||||||
<input type="text" name="type" value="{{= d.type.toUpperCase() }}" readonly
|
<input type="text" name="type" readonly class="layui-input">
|
||||||
class="layui-input">
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="layui-form-item">
|
<div class="layui-form-item">
|
||||||
@@ -192,7 +191,7 @@
|
|||||||
<div class="layui-form-item layui-form-text http https">
|
<div class="layui-form-item layui-form-text http https">
|
||||||
<label class="layui-form-label">Subdomain</label>
|
<label class="layui-form-label">Subdomain</label>
|
||||||
<div class="layui-input-block">
|
<div class="layui-input-block">
|
||||||
<textarea name="subdomain" placeholder="Subdomain" autocomplete="off"
|
<textarea name="sub_domain" placeholder="Subdomain" autocomplete="off"
|
||||||
class="layui-textarea"></textarea>
|
class="layui-textarea"></textarea>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -226,29 +225,29 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="layui-tab-item extra-param-tab-item">
|
<div class="layui-tab-item extra-param-tab-item">
|
||||||
{{# layui.each(d.extraData, function(index, extra){ }}
|
|
||||||
<div class="layui-form-item">
|
<div class="layui-form-item">
|
||||||
<div class="layui-input-inline">
|
<div class="layui-input-inline">
|
||||||
<input type="text" name="paramName" value="{{= extra.name}}" class="layui-input"
|
<input type="text" class="layui-input" placeholder="paramName">
|
||||||
|
</div>
|
||||||
|
<div class="layui-form-mid">=</div>
|
||||||
|
<div class="layui-input-inline">
|
||||||
|
<input type="text" class="layui-input" lay-filter="addition"
|
||||||
|
lay-affix="addition" placeholder="paramValue">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{{# layui.each(d.extraData, function(index, extra){ }}
|
||||||
|
<div class="layui-form-item extra-param-item">
|
||||||
|
<div class="layui-input-inline">
|
||||||
|
<input type="text" value="{{= extra.name}}" class="layui-input"
|
||||||
placeholder="paramName">
|
placeholder="paramName">
|
||||||
</div>
|
</div>
|
||||||
<div class="layui-form-mid">=</div>
|
<div class="layui-form-mid">=</div>
|
||||||
<div class="layui-input-inline">
|
<div class="layui-input-inline">
|
||||||
<input type="text" name="paramValue" value="{{= extra.value }}" class="layui-input"
|
<input type="text" value="{{= extra.value }}" class="layui-input"
|
||||||
lay-filter="subtraction" lay-affix="subtraction" placeholder="paramValue">
|
lay-filter="subtraction" lay-affix="subtraction" placeholder="paramValue">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{{# }); }}
|
{{# }); }}
|
||||||
<div class="layui-form-item">
|
|
||||||
<div class="layui-input-inline">
|
|
||||||
<input type="text" name="paramName" class="layui-input" placeholder="paramName">
|
|
||||||
</div>
|
|
||||||
<div class="layui-form-mid">=</div>
|
|
||||||
<div class="layui-input-inline">
|
|
||||||
<input type="text" name="paramValue" class="layui-input" lay-filter="addition"
|
|
||||||
lay-affix="addition" placeholder="paramValue">
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -256,13 +255,13 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script type="text/html" id="extraParamAddedTemplate">
|
<script type="text/html" id="extraParamAddedTemplate">
|
||||||
<div class="layui-form-item">
|
<div class="layui-form-item extra-param-item">
|
||||||
<div class="layui-input-inline">
|
<div class="layui-input-inline">
|
||||||
<input type="text" name="paramName" value="{{= d.name}}" class="layui-input" placeholder="paramName">
|
<input type="text" value="{{= d.name}}" class="layui-input" placeholder="paramName">
|
||||||
</div>
|
</div>
|
||||||
<div class="layui-form-mid">=</div>
|
<div class="layui-form-mid">=</div>
|
||||||
<div class="layui-input-inline">
|
<div class="layui-input-inline">
|
||||||
<input type="text" name="paramValue" value="{{= d.value }}" class="layui-input"
|
<input type="text" value="{{= d.value }}" class="layui-input"
|
||||||
lay-filter="subtraction" lay-affix="subtraction" placeholder="paramValue">
|
lay-filter="subtraction" lay-affix="subtraction" placeholder="paramValue">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
package controller
|
package controller
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/fatedier/frp/pkg/config"
|
|
||||||
ginI18n "github.com/gin-contrib/i18n"
|
ginI18n "github.com/gin-contrib/i18n"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/vaughan0/go-ini"
|
"github.com/vaughan0/go-ini"
|
||||||
@@ -77,37 +77,52 @@ func (c *HandleController) MakeLangFunc() func(context *gin.Context) {
|
|||||||
|
|
||||||
func (c *HandleController) MakeAddProxyFunc() func(context *gin.Context) {
|
func (c *HandleController) MakeAddProxyFunc() func(context *gin.Context) {
|
||||||
return func(context *gin.Context) {
|
return func(context *gin.Context) {
|
||||||
proxyType, exist := context.GetQuery("type")
|
proxy := ini.Section{}
|
||||||
if !exist {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
proxy, exist := proxyConfTypeMap[proxyType]
|
|
||||||
if !exist {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
response := OperationResponse{
|
response := OperationResponse{
|
||||||
Success: true,
|
Success: true,
|
||||||
Code: Success,
|
Code: Success,
|
||||||
Message: "user add success",
|
Message: "proxy add success",
|
||||||
}
|
}
|
||||||
|
|
||||||
err := context.BindJSON(&proxy)
|
err := context.BindJSON(&proxy)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
response.Success = false
|
response.Success = false
|
||||||
response.Code = ParamError
|
response.Code = ParamError
|
||||||
response.Message = fmt.Sprintf("user add failed, param error : %v", err)
|
response.Message = fmt.Sprintf("proxy add failed, param error : %v", err)
|
||||||
log.Printf(response.Message)
|
log.Printf(response.Message)
|
||||||
context.JSON(http.StatusOK, &response)
|
context.JSON(http.StatusOK, &response)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = c.reloadFrpc()
|
name := proxy["name"]
|
||||||
if err != nil {
|
|
||||||
|
if trimString(name) == "" {
|
||||||
|
response.Success = false
|
||||||
|
response.Code = ParamError
|
||||||
|
response.Message = fmt.Sprintf("proxy add failed, proxy name invalid")
|
||||||
|
log.Printf(response.Message)
|
||||||
|
context.JSON(http.StatusOK, &response)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, exist := clientProxies[name]; exist {
|
||||||
|
response.Success = false
|
||||||
|
response.Code = ProxyExist
|
||||||
|
response.Message = fmt.Sprintf("proxy add failed, proxy exist")
|
||||||
|
log.Printf(response.Message)
|
||||||
|
context.JSON(http.StatusOK, &response)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
delete(proxy, "name")
|
||||||
|
clientProxies[name] = proxy
|
||||||
|
|
||||||
|
res := c.ReloadFrpc()
|
||||||
|
if !res.Success {
|
||||||
response.Success = false
|
response.Success = false
|
||||||
response.Code = SaveError
|
response.Code = SaveError
|
||||||
response.Message = fmt.Sprintf("add failed, error : %v", err)
|
response.Message = fmt.Sprintf("proxy add failed, error : %v", res.Message)
|
||||||
log.Printf(response.Message)
|
log.Printf(response.Message)
|
||||||
context.JSON(http.StatusOK, &response)
|
context.JSON(http.StatusOK, &response)
|
||||||
return
|
return
|
||||||
@@ -119,20 +134,12 @@ func (c *HandleController) MakeAddProxyFunc() func(context *gin.Context) {
|
|||||||
|
|
||||||
func (c *HandleController) MakeUpdateProxyFunc() func(context *gin.Context) {
|
func (c *HandleController) MakeUpdateProxyFunc() func(context *gin.Context) {
|
||||||
return func(context *gin.Context) {
|
return func(context *gin.Context) {
|
||||||
proxyType, exist := context.GetQuery("type")
|
proxy := ini.Section{}
|
||||||
if !exist {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
proxy, exist := proxyConfTypeMap[proxyType]
|
|
||||||
if !exist {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
response := OperationResponse{
|
response := OperationResponse{
|
||||||
Success: true,
|
Success: true,
|
||||||
Code: Success,
|
Code: Success,
|
||||||
Message: "user update success",
|
Message: "proxy update success",
|
||||||
}
|
}
|
||||||
|
|
||||||
err := context.BindJSON(&proxy)
|
err := context.BindJSON(&proxy)
|
||||||
@@ -145,11 +152,39 @@ func (c *HandleController) MakeUpdateProxyFunc() func(context *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = c.reloadFrpc()
|
oldName := proxy["oldName"]
|
||||||
if err != nil {
|
name := proxy["name"]
|
||||||
|
|
||||||
|
if trimString(oldName) == "" || trimString(name) == "" {
|
||||||
|
response.Success = false
|
||||||
|
response.Code = ParamError
|
||||||
|
response.Message = fmt.Sprintf("proxy add failed, proxy name invalid")
|
||||||
|
log.Printf(response.Message)
|
||||||
|
context.JSON(http.StatusOK, &response)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if oldName != name {
|
||||||
|
if _, exist := clientProxies[name]; exist {
|
||||||
|
response.Success = false
|
||||||
|
response.Code = ProxyExist
|
||||||
|
response.Message = fmt.Sprintf("proxy update failed, proxy exist")
|
||||||
|
log.Printf(response.Message)
|
||||||
|
context.JSON(http.StatusOK, &response)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
delete(proxy, "name")
|
||||||
|
delete(proxy, oldName)
|
||||||
|
delete(clientProxies, oldName)
|
||||||
|
clientProxies[name] = proxy
|
||||||
|
|
||||||
|
res := c.ReloadFrpc()
|
||||||
|
if res.Code != Success {
|
||||||
response.Success = false
|
response.Success = false
|
||||||
response.Code = SaveError
|
response.Code = SaveError
|
||||||
response.Message = fmt.Sprintf("user update failed, error : %v", err)
|
response.Message = fmt.Sprintf("user update failed, error : %v", res.Message)
|
||||||
log.Printf(response.Message)
|
log.Printf(response.Message)
|
||||||
context.JSON(http.StatusOK, &response)
|
context.JSON(http.StatusOK, &response)
|
||||||
return
|
return
|
||||||
@@ -161,15 +196,7 @@ func (c *HandleController) MakeUpdateProxyFunc() func(context *gin.Context) {
|
|||||||
|
|
||||||
func (c *HandleController) MakeRemoveProxyFunc() func(context *gin.Context) {
|
func (c *HandleController) MakeRemoveProxyFunc() func(context *gin.Context) {
|
||||||
return func(context *gin.Context) {
|
return func(context *gin.Context) {
|
||||||
proxyType, exist := context.GetQuery("type")
|
proxy := make(map[string]interface{})
|
||||||
if !exist {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
proxy, exist := proxyConfTypeMap[proxyType]
|
|
||||||
if !exist {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
response := OperationResponse{
|
response := OperationResponse{
|
||||||
Success: true,
|
Success: true,
|
||||||
@@ -187,11 +214,11 @@ func (c *HandleController) MakeRemoveProxyFunc() func(context *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = c.reloadFrpc()
|
res := c.ReloadFrpc()
|
||||||
if err != nil {
|
if !res.Success {
|
||||||
response.Success = false
|
response.Success = false
|
||||||
response.Code = SaveError
|
response.Code = SaveError
|
||||||
response.Message = fmt.Sprintf("user update failed, error : %v", err)
|
response.Message = fmt.Sprintf("user update failed, error : %v", res.Message)
|
||||||
log.Printf(response.Message)
|
log.Printf(response.Message)
|
||||||
context.JSON(http.StatusOK, &response)
|
context.JSON(http.StatusOK, &response)
|
||||||
return
|
return
|
||||||
@@ -269,7 +296,7 @@ func (c *HandleController) MakeProxyFunc() func(context *gin.Context) {
|
|||||||
if serverApi == "/api/config" {
|
if serverApi == "/api/config" {
|
||||||
proxyType, _ := context.GetQuery("type")
|
proxyType, _ := context.GetQuery("type")
|
||||||
content := fmt.Sprintf("%s", res.Data)
|
content := fmt.Sprintf("%s", res.Data)
|
||||||
configure, err := parseConfigure(content, trimString(proxyType))
|
configure, err := c.parseConfigure(content, trimString(proxyType))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
res.Success = false
|
res.Success = false
|
||||||
@@ -283,33 +310,85 @@ func (c *HandleController) MakeProxyFunc() func(context *gin.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfigure(content, proxyType string) (interface{}, error) {
|
func (c *HandleController) ReloadFrpc() ProxyResponse {
|
||||||
|
var client *http.Client
|
||||||
|
var protocol string
|
||||||
|
|
||||||
if proxyType == "none" {
|
if c.CommonInfo.DashboardTls {
|
||||||
common, err := config.UnmarshalClientConfFromIni(content)
|
client = &http.Client{
|
||||||
|
Transport: &http.Transport{
|
||||||
if err != nil {
|
TLSClientConfig: &tls.Config{
|
||||||
return nil, err
|
InsecureSkipVerify: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
protocol = "https://"
|
||||||
return common, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
cfg, err := ini.Load(strings.NewReader(content))
|
|
||||||
proxyList := make(map[string]ini.Section)
|
|
||||||
for name, section := range cfg {
|
|
||||||
if name == "common" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if proxyType != "" && strings.ToLower(section["type"]) != strings.ToLower(proxyType) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
proxyList[name] = section
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
} else {
|
} else {
|
||||||
return proxyList, nil
|
client = http.DefaultClient
|
||||||
|
protocol = "http://"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
res := ProxyResponse{}
|
||||||
|
host := c.CommonInfo.DashboardAddr
|
||||||
|
port := c.CommonInfo.DashboardPort
|
||||||
|
serverApi := "/api/config"
|
||||||
|
requestUrl := protocol + host + ":" + strconv.Itoa(port) + serverApi
|
||||||
|
request, _ := http.NewRequest("PUT", requestUrl, bytes.NewReader(serializeSectionsToString()))
|
||||||
|
username := c.CommonInfo.DashboardUser
|
||||||
|
password := c.CommonInfo.DashboardPwd
|
||||||
|
if trimString(username) != "" && trimString(password) != "" {
|
||||||
|
request.SetBasicAuth(username, password)
|
||||||
|
}
|
||||||
|
|
||||||
|
response, err := client.Do(request)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
res.Code = FrpServerError
|
||||||
|
res.Success = false
|
||||||
|
res.Message = err.Error()
|
||||||
|
log.Print(err)
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
res.Code = response.StatusCode
|
||||||
|
body, err := io.ReadAll(response.Body)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
res.Success = false
|
||||||
|
res.Message = err.Error()
|
||||||
|
} else {
|
||||||
|
if res.Code == http.StatusOK {
|
||||||
|
res.Success = true
|
||||||
|
res.Data = string(body)
|
||||||
|
res.Message = fmt.Sprintf("Proxy to %s success", requestUrl)
|
||||||
|
} else {
|
||||||
|
res.Success = false
|
||||||
|
if res.Code == http.StatusNotFound {
|
||||||
|
res.Message = fmt.Sprintf("Proxy to %s error: url not found", requestUrl)
|
||||||
|
} else {
|
||||||
|
res.Message = fmt.Sprintf("Proxy to %s error: %s", requestUrl, string(body))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log.Printf(res.Message)
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
func serializeSectionsToString() []byte {
|
||||||
|
var build strings.Builder
|
||||||
|
build.WriteString("[common]\n")
|
||||||
|
for key, value := range clientCommon {
|
||||||
|
build.WriteString(fmt.Sprintf("%s = %s\n", key, value))
|
||||||
|
}
|
||||||
|
build.WriteString("\n")
|
||||||
|
|
||||||
|
for name, section := range clientProxies {
|
||||||
|
build.WriteString(fmt.Sprintf("[%s]\n", name))
|
||||||
|
for key, value := range section {
|
||||||
|
build.WriteString(fmt.Sprintf("%s = %s\n", key, value))
|
||||||
|
}
|
||||||
|
build.WriteString("\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
return []byte(build.String())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package controller
|
package controller
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/fatedier/frp/pkg/config"
|
||||||
|
"github.com/vaughan0/go-ini"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -21,6 +23,33 @@ func stringContains(element string, data []string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *HandleController) reloadFrpc() error {
|
func (c *HandleController) parseConfigure(content, proxyType string) (interface{}, error) {
|
||||||
return nil
|
currentProxies := make(map[string]ini.Section)
|
||||||
|
clientProxies = make(map[string]ini.Section)
|
||||||
|
common, err := config.UnmarshalClientConfFromIni(content)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
cfg, err := ini.Load(strings.NewReader(content))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for name, section := range cfg {
|
||||||
|
if name == "common" {
|
||||||
|
clientCommon = section
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if strings.ToLower(section["type"]) == strings.ToLower(proxyType) {
|
||||||
|
currentProxies[name] = section
|
||||||
|
}
|
||||||
|
clientProxies[name] = section
|
||||||
|
delete(clientProxies[name], "name")
|
||||||
|
}
|
||||||
|
|
||||||
|
if proxyType == "none" {
|
||||||
|
return common, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return currentProxies, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
package controller
|
package controller
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/fatedier/frp/pkg/config"
|
"github.com/vaughan0/go-ini"
|
||||||
"github.com/fatedier/frp/pkg/consts"
|
|
||||||
"reflect"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -11,6 +9,8 @@ const (
|
|||||||
ParamError
|
ParamError
|
||||||
SaveError
|
SaveError
|
||||||
FrpServerError
|
FrpServerError
|
||||||
|
ProxyExist
|
||||||
|
ProxyNotExist
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -29,19 +29,13 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
proxyConfTypeMap map[string]reflect.Type
|
clientCommon ini.Section
|
||||||
|
clientProxies map[string]ini.Section
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
proxyConfTypeMap = make(map[string]reflect.Type)
|
clientCommon = ini.Section{}
|
||||||
proxyConfTypeMap[consts.TCPProxy] = reflect.TypeOf(config.TCPProxyConf{})
|
clientProxies = make(map[string]ini.Section)
|
||||||
proxyConfTypeMap[consts.TCPMuxProxy] = reflect.TypeOf(config.TCPMuxProxyConf{})
|
|
||||||
proxyConfTypeMap[consts.UDPProxy] = reflect.TypeOf(config.UDPProxyConf{})
|
|
||||||
proxyConfTypeMap[consts.HTTPProxy] = reflect.TypeOf(config.HTTPProxyConf{})
|
|
||||||
proxyConfTypeMap[consts.HTTPSProxy] = reflect.TypeOf(config.HTTPSProxyConf{})
|
|
||||||
proxyConfTypeMap[consts.STCPProxy] = reflect.TypeOf(config.STCPProxyConf{})
|
|
||||||
proxyConfTypeMap[consts.XTCPProxy] = reflect.TypeOf(config.XTCPProxyConf{})
|
|
||||||
proxyConfTypeMap[consts.SUDPProxy] = reflect.TypeOf(config.SUDPProxyConf{})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type HTTPError struct {
|
type HTTPError struct {
|
||||||
@@ -80,6 +74,10 @@ type ProxyResponse struct {
|
|||||||
Data any `json:"data"`
|
Data any `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ClientProxies struct {
|
||||||
|
Proxy ini.Section `json:"proxy"`
|
||||||
|
}
|
||||||
|
|
||||||
func (e *HTTPError) Error() string {
|
func (e *HTTPError) Error() string {
|
||||||
return e.Err.Error()
|
return e.Err.Error()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user