Skip to content

Commit

Permalink
完成大部分基础功能
Browse files Browse the repository at this point in the history
  • Loading branch information
vaxilu committed May 30, 2021
1 parent a66dff6 commit 4bae13d
Show file tree
Hide file tree
Showing 12 changed files with 443 additions and 22 deletions.
21 changes: 21 additions & 0 deletions util/reflect_util/reflect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package reflect_util

import "reflect"

func GetFields(t reflect.Type) []reflect.StructField {
num := t.NumField()
fields := make([]reflect.StructField, 0, num)
for i := 0; i < num; i++ {
fields = append(fields, t.Field(i))
}
return fields
}

func GetFieldValues(v reflect.Value) []reflect.Value {
num := v.NumField()
fields := make([]reflect.Value, 0, num)
for i := 0; i < num; i++ {
fields = append(fields, v.Field(i))
}
return fields
}
4 changes: 4 additions & 0 deletions web/assets/css/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
height: 100%;
}

.ant-space {
display: block;
}

.ant-layout-sider-zero-width-trigger {
display: none;
}
Expand Down
23 changes: 23 additions & 0 deletions web/assets/js/model/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,27 @@ class DBInbound {
const inbound = this.toInbound();
return inbound.genLink(address, this.remark);
}
}

class AllSetting {
webListen = "";
webPort = 65432;
webCertFile = "";
webKeyFile = "";
webBasePath = "/";

xrayTemplateConfig = "";

timeLocation = "Asia/Shanghai";

constructor(data) {
if (data == null) {
return
}
ObjectUtil.cloneProps(this, data);
}

equals(other) {
return ObjectUtil.equals(this, other);
}
}
14 changes: 14 additions & 0 deletions web/assets/js/util/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,18 @@ class ObjectUtil {
return obj;
}

static equals(a, b) {
for (const key in a) {
if (!a.hasOwnProperty(key)) {
continue;
}
if (!b.hasOwnProperty(key)) {
return false;
} else if (a[key] !== b[key]) {
return false;
}
}
return true
}

}
24 changes: 24 additions & 0 deletions web/controller/xui.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
"x-ui/database/model"
"x-ui/logger"
"x-ui/web/entity"
"x-ui/web/global"
"x-ui/web/service"
"x-ui/web/session"
Expand All @@ -17,6 +18,7 @@ type XUIController struct {

inboundService service.InboundService
xrayService service.XrayService
settingService service.SettingService

isNeedXrayRestart atomic.Bool
}
Expand All @@ -39,6 +41,8 @@ func (a *XUIController) initRouter(g *gin.RouterGroup) {
g.POST("/inbound/del/:id", a.delInbound)
g.POST("/inbound/update/:id", a.updateInbound)
g.GET("/setting", a.setting)
g.POST("/setting/all", a.getAllSetting)
g.POST("/setting/update", a.updateSetting)
}

func (a *XUIController) startTask() {
Expand Down Expand Up @@ -128,3 +132,23 @@ func (a *XUIController) updateInbound(c *gin.Context) {
a.isNeedXrayRestart.Store(true)
}
}

func (a *XUIController) getAllSetting(c *gin.Context) {
allSetting, err := a.settingService.GetAllSetting()
if err != nil {
jsonMsg(c, "获取设置", err)
return
}
jsonObj(c, allSetting, nil)
}

func (a *XUIController) updateSetting(c *gin.Context) {
allSetting := &entity.AllSetting{}
err := c.ShouldBind(allSetting)
if err != nil {
jsonMsg(c, "修改设置", err)
return
}
err = a.settingService.UpdateAllSetting(allSetting)
jsonMsg(c, "修改设置", err)
}
62 changes: 62 additions & 0 deletions web/entity/entity.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
package entity

import (
"crypto/tls"
"encoding/json"
"net"
"strings"
"time"
"x-ui/util/common"
"x-ui/xray"
)

type Msg struct {
Success bool `json:"success"`
Msg string `json:"msg"`
Expand All @@ -15,3 +25,55 @@ type Pager struct {
Key string `json:"key"`
List interface{} `json:"list"`
}

type AllSetting struct {
WebListen string `json:"webListen" form:"webListen"`
WebPort int `json:"webPort" form:"webPort"`
WebCertFile string `json:"webCertFile" form:"webCertFile"`
WebKeyFile string `json:"webKeyFile" form:"webKeyFile"`
WebBasePath string `json:"webBasePath" form:"webBasePath"`

XrayTemplateConfig string `json:"xrayTemplateConfig" form:"xrayTemplateConfig"`

TimeLocation string `json:"timeLocation" form:"timeLocation"`
}

func (s *AllSetting) CheckValid() error {
if s.WebListen != "" {
ip := net.ParseIP(s.WebListen)
if ip == nil {
return common.NewError("web listen is not valid ip:", s.WebListen)
}
}

if s.WebPort <= 0 || s.WebPort > 65535 {
return common.NewError("web port is not a valid port:", s.WebPort)
}

if s.WebCertFile != "" || s.WebKeyFile != "" {
_, err := tls.LoadX509KeyPair(s.WebCertFile, s.WebKeyFile)
if err != nil {
return common.NewErrorf("cert file <%v> or key file <%v> invalid: %v", s.WebCertFile, s.WebKeyFile, err)
}
}

if !strings.HasPrefix(s.WebBasePath, "/") {
return common.NewErrorf("web base path must start with '/' : <%v>", s.WebBasePath)
}
if !strings.HasSuffix(s.WebBasePath, "/") {
return common.NewErrorf("web base path must end with '/' : <%v>", s.WebBasePath)
}

xrayConfig := &xray.Config{}
err := json.Unmarshal([]byte(s.XrayTemplateConfig), xrayConfig)
if err != nil {
return common.NewError("xray template config invalid:", err)
}

_, err = time.LoadLocation(s.TimeLocation)
if err != nil {
return common.NewError("time location not exist:", s.TimeLocation)
}

return nil
}
2 changes: 1 addition & 1 deletion web/html/xui/common_sider.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</a-menu-item>
<a-menu-item key="{{ .base_path }}xui/inbounds">
<a-icon type="user"></a-icon>
<span>账号列表</span>
<span>入站列表</span>
</a-menu-item>
<a-menu-item key="{{ .base_path }}xui/setting">
<a-icon type="setting"></a-icon>
Expand Down
29 changes: 29 additions & 0 deletions web/html/xui/component/setting.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{{define "component/settingListItem"}}
<a-list-item style="padding: 20px">
<a-row>
<a-col :lg="24" :xl="12">
<a-list-item-meta :title="title" :description="desc"/>
</a-col>
<a-col :lg="24" :xl="12">
<template v-if="type === 'text'">
<a-input :value="value" @input="$emit('input', $event.target.value)"></a-input>
</template>
<template v-else-if="type === 'number'">
<a-input type="number" :value="value" @input="$emit('input', $event.target.value)"></a-input>
</template>
<template v-else-if="type === 'textarea'">
<a-textarea :value="value" @input="$emit('input', $event.target.value)" :auto-size="{ minRows: 6, maxRows: 6 }"></a-textarea>
</template>
</a-col>
</a-row>
</a-list-item>
{{end}}

{{define "component/setting"}}
<script>
Vue.component('setting-list-item', {
props: ["type", "title", "desc", "value"],
template: `{{template "component/settingListItem"}}`,
});
</script>
{{end}}
8 changes: 5 additions & 3 deletions web/html/xui/inbounds.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
<html lang="en">
{{template "head" .}}
<style>
.ant-layout-content {
margin: 24px 16px;
@media (min-width: 769px) {
.ant-layout-content {
margin: 24px 16px;
}
}

.ant-col-sm-24 {
Expand Down Expand Up @@ -133,7 +135,7 @@
delimiters: ['[[', ']]'],
el: '#app',
data: {
ip: location.hostname,
siderDrawer,
spinning: false,
dbInbounds: [],
searchKey: '',
Expand Down
7 changes: 5 additions & 2 deletions web/html/xui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
<html lang="en">
{{template "head" .}}
<style>
.ant-layout-content {
margin: 24px 16px;
@media (min-width: 769px) {
.ant-layout-content {
margin: 24px 16px;
}
}

.ant-col-sm-24 {
Expand Down Expand Up @@ -273,6 +275,7 @@ <h2>请谨慎选择,旧版本可能配置不兼容</h2>
delimiters: ['[[', ']]'],
el: '#app',
data: {
siderDrawer,
status: new Status(),
versionModal,
spinning: false,
Expand Down
Loading

0 comments on commit 4bae13d

Please sign in to comment.