From 292d5b89d4890fc87db5302d30fab3bec5b8f2bb Mon Sep 17 00:00:00 2001
From: sprov <47310637+sprov065@users.noreply.github.com>
Date: Mon, 26 Jul 2021 13:29:29 +0800
Subject: [PATCH] 0.3.0
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 增加到期时间限制
- 新增配置面板 https 访问后,http 自动跳转 https(同端口)
- 降低获取系统连接数的 cpu 使用率
- 优化界面
- VMess 协议 alterId 默认改为 0
- 修复旧版本 iOS 系统白屏问题
- 修复重启面板后 xray 没有启动的问题
---
config/version | 2 +-
logger/logger.go | 10 +++-
util/sys/a.s | 0
util/sys/psutil.go | 8 +++
util/sys/sys_darwin.go | 23 ++++++++
util/sys/sys_linux.go | 70 +++++++++++++++++++++++
web/assets/js/model/models.js | 83 +++++++++++++++++----------
web/assets/js/model/xray.js | 2 +-
web/assets/js/util/utils.js | 29 +++++-----
web/controller/inbound.go | 2 +-
web/html/xui/form/inbound.html | 13 +++++
web/html/xui/inbounds.html | 92 ++++++++++++++++++++++--------
web/html/xui/index.html | 26 ++++-----
web/job/check_inbound_job.go | 25 ++++++++
web/job/check_xray_running_job.go | 25 ++++++++
web/job/xray_traffic_job.go | 30 ++++++++++
web/network/auto_https_listener.go | 21 +++++++
web/network/autp_https_conn.go | 67 ++++++++++++++++++++++
web/service/inbound.go | 4 +-
web/service/server.go | 15 ++---
web/service/xray.go | 7 ++-
web/web.go | 65 ++++++---------------
22 files changed, 473 insertions(+), 146 deletions(-)
create mode 100644 util/sys/a.s
create mode 100644 util/sys/psutil.go
create mode 100644 util/sys/sys_darwin.go
create mode 100644 util/sys/sys_linux.go
create mode 100644 web/job/check_inbound_job.go
create mode 100644 web/job/check_xray_running_job.go
create mode 100644 web/job/xray_traffic_job.go
create mode 100644 web/network/auto_https_listener.go
create mode 100644 web/network/autp_https_conn.go
diff --git a/config/version b/config/version
index 341cf11faf..9325c3ccda 100644
--- a/config/version
+++ b/config/version
@@ -1 +1 @@
-0.2.0
\ No newline at end of file
+0.3.0
\ No newline at end of file
diff --git a/logger/logger.go b/logger/logger.go
index bc7c3b7c7e..cb5e8360d3 100644
--- a/logger/logger.go
+++ b/logger/logger.go
@@ -7,16 +7,22 @@ import (
var logger *logging.Logger
+func init() {
+ InitLogger(logging.INFO)
+}
+
func InitLogger(level logging.Level) {
format := logging.MustStringFormatter(
`%{time:2006/01/02 15:04:05} %{level} - %{message}`,
)
- logger = logging.MustGetLogger("x-ui")
+ newLogger := logging.MustGetLogger("x-ui")
backend := logging.NewLogBackend(os.Stderr, "", 0)
backendFormatter := logging.NewBackendFormatter(backend, format)
backendLeveled := logging.AddModuleLevel(backendFormatter)
backendLeveled.SetLevel(level, "")
- logger.SetBackend(backendLeveled)
+ newLogger.SetBackend(backendLeveled)
+
+ logger = newLogger
}
func Debug(args ...interface{}) {
diff --git a/util/sys/a.s b/util/sys/a.s
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/util/sys/psutil.go b/util/sys/psutil.go
new file mode 100644
index 0000000000..645f839a97
--- /dev/null
+++ b/util/sys/psutil.go
@@ -0,0 +1,8 @@
+package sys
+
+import (
+ _ "unsafe"
+)
+
+//go:linkname HostProc github.com/shirou/gopsutil/internal/common.HostProc
+func HostProc(combineWith ...string) string
diff --git a/util/sys/sys_darwin.go b/util/sys/sys_darwin.go
new file mode 100644
index 0000000000..d61a38a2ba
--- /dev/null
+++ b/util/sys/sys_darwin.go
@@ -0,0 +1,23 @@
+// +build darwin
+
+package sys
+
+import (
+ "github.com/shirou/gopsutil/net"
+)
+
+func GetTCPCount() (int, error) {
+ stats, err := net.Connections("tcp")
+ if err != nil {
+ return 0, err
+ }
+ return len(stats), nil
+}
+
+func GetUDPCount() (int, error) {
+ stats, err := net.Connections("udp")
+ if err != nil {
+ return 0, err
+ }
+ return len(stats), nil
+}
diff --git a/util/sys/sys_linux.go b/util/sys/sys_linux.go
new file mode 100644
index 0000000000..843d9b0080
--- /dev/null
+++ b/util/sys/sys_linux.go
@@ -0,0 +1,70 @@
+// +build linux
+
+package sys
+
+import (
+ "bytes"
+ "fmt"
+ "io"
+ "os"
+)
+
+func getLinesNum(filename string) (int, error) {
+ file, err := os.Open(filename)
+ if err != nil {
+ return 0, err
+ }
+ defer file.Close()
+
+ sum := 0
+ buf := make([]byte, 8192)
+ for {
+ n, err := file.Read(buf)
+
+ var buffPosition int
+ for {
+ i := bytes.IndexByte(buf[buffPosition:], '\n')
+ if i < 0 || n == buffPosition {
+ break
+ }
+ buffPosition += i + 1
+ sum++
+ }
+
+ if err == io.EOF {
+ return sum, nil
+ } else if err != nil {
+ return sum, err
+ }
+ }
+}
+
+func GetTCPCount() (int, error) {
+ root := HostProc()
+
+ tcp4, err := getLinesNum(fmt.Sprintf("%v/net/tcp", root))
+ if err != nil {
+ return tcp4, err
+ }
+ tcp6, err := getLinesNum(fmt.Sprintf("%v/net/tcp6", root))
+ if err != nil {
+ return tcp4 + tcp6, nil
+ }
+
+ return tcp4 + tcp6, nil
+}
+
+func GetUDPCount() (int, error) {
+ root := HostProc()
+
+ udp4, err := getLinesNum(fmt.Sprintf("%v/net/udp", root))
+ if err != nil {
+ return udp4, err
+ }
+ udp6, err := getLinesNum(fmt.Sprintf("%v/net/udp6", root))
+ if err != nil {
+ return udp4 + udp6, nil
+ }
+
+ return udp4 + udp6, nil
+}
diff --git a/web/assets/js/model/models.js b/web/assets/js/model/models.js
index e0ad55d85f..4d26f1f08a 100644
--- a/web/assets/js/model/models.js
+++ b/web/assets/js/model/models.js
@@ -1,14 +1,18 @@
class User {
- username = "";
- password = "";
+
+ constructor() {
+ this.username = "";
+ this.password = "";
+ }
}
class Msg {
- success = false;
- msg = "";
- obj = null;
constructor(success, msg, obj) {
+ this.success = false;
+ this.msg = "";
+ this.obj = null;
+
if (success != null) {
this.success = success;
}
@@ -22,24 +26,25 @@ class Msg {
}
class DBInbound {
- id = 0;
- userId = 0;
- up = 0;
- down = 0;
- total = 0;
- remark = "";
- enable = true;
- expiryTime = 0;
-
- listen = "";
- port = 0;
- protocol = "";
- settings = "";
- streamSettings = "";
- tag = "";
- sniffing = "";
constructor(data) {
+ this.id = 0;
+ this.userId = 0;
+ this.up = 0;
+ this.down = 0;
+ this.total = 0;
+ this.remark = "";
+ this.enable = true;
+ this.expiryTime = 0;
+
+ this.listen = "";
+ this.port = 0;
+ this.protocol = "";
+ this.settings = "";
+ this.streamSettings = "";
+ this.tag = "";
+ this.sniffing = "";
+
if (data == null) {
return;
}
@@ -86,6 +91,25 @@ class DBInbound {
return address;
}
+ get _expiryTime() {
+ if (this.expiryTime === 0) {
+ return null;
+ }
+ return moment(this.expiryTime);
+ }
+
+ set _expiryTime(t) {
+ if (t == null) {
+ this.expiryTime = 0;
+ } else {
+ this.expiryTime = t.valueOf();
+ }
+ }
+
+ get isExpiry() {
+ return this.expiryTime < new Date().getTime();
+ }
+
toInbound() {
let settings = {};
if (!ObjectUtil.isEmpty(this.settings)) {
@@ -132,17 +156,18 @@ class DBInbound {
}
class AllSetting {
- webListen = "";
- webPort = 54321;
- webCertFile = "";
- webKeyFile = "";
- webBasePath = "/";
- xrayTemplateConfig = "";
+ constructor(data) {
+ this.webListen = "";
+ this.webPort = 54321;
+ this.webCertFile = "";
+ this.webKeyFile = "";
+ this.webBasePath = "/";
- timeLocation = "Asia/Shanghai";
+ this.xrayTemplateConfig = "";
+
+ this.timeLocation = "Asia/Shanghai";
- constructor(data) {
if (data == null) {
return
}
diff --git a/web/assets/js/model/xray.js b/web/assets/js/model/xray.js
index 586a7b5a4f..66a5980f40 100644
--- a/web/assets/js/model/xray.js
+++ b/web/assets/js/model/xray.js
@@ -1165,7 +1165,7 @@ Inbound.VmessSettings = class extends Inbound.Settings {
}
};
Inbound.VmessSettings.Vmess = class extends XrayCommonClass {
- constructor(id=RandomUtil.randomUUID(), alterId=64) {
+ constructor(id=RandomUtil.randomUUID(), alterId=0) {
super();
this.id = id;
this.alterId = alterId;
diff --git a/web/assets/js/util/utils.js b/web/assets/js/util/utils.js
index 9c6d38e941..6b4e5ed942 100644
--- a/web/assets/js/util/utils.js
+++ b/web/assets/js/util/utils.js
@@ -77,18 +77,19 @@ class PromiseUtil {
}
+const seq = [
+ 'a', 'b', 'c', 'd', 'e', 'f', 'g',
+ 'h', 'i', 'j', 'k', 'l', 'm', 'n',
+ 'o', 'p', 'q', 'r', 's', 't',
+ 'u', 'v', 'w', 'x', 'y', 'z',
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
+ 'A', 'B', 'C', 'D', 'E', 'F', 'G',
+ 'H', 'I', 'J', 'K', 'L', 'M', 'N',
+ 'O', 'P', 'Q', 'R', 'S', 'T',
+ 'U', 'V', 'W', 'X', 'Y', 'Z'
+];
+
class RandomUtil {
- static seq = [
- 'a', 'b', 'c', 'd', 'e', 'f', 'g',
- 'h', 'i', 'j', 'k', 'l', 'm', 'n',
- 'o', 'p', 'q', 'r', 's', 't',
- 'u', 'v', 'w', 'x', 'y', 'z',
- '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
- 'A', 'B', 'C', 'D', 'E', 'F', 'G',
- 'H', 'I', 'J', 'K', 'L', 'M', 'N',
- 'O', 'P', 'Q', 'R', 'S', 'T',
- 'U', 'V', 'W', 'X', 'Y', 'Z'
- ];
static randomIntRange(min, max) {
return parseInt(Math.random() * (max - min) + min, 10);
@@ -101,7 +102,7 @@ class RandomUtil {
static randomSeq(count) {
let str = '';
for (let i = 0; i < count; ++i) {
- str += this.seq[this.randomInt(62)];
+ str += seq[this.randomInt(62)];
}
return str;
}
@@ -109,7 +110,7 @@ class RandomUtil {
static randomLowerAndNum(count) {
let str = '';
for (let i = 0; i < count; ++i) {
- str += this.seq[this.randomInt(36)];
+ str += seq[this.randomInt(36)];
}
return str;
}
@@ -121,7 +122,7 @@ class RandomUtil {
if (index <= 9) {
str += index;
} else {
- str += this.seq[index - 10];
+ str += seq[index - 10];
}
}
return str;
diff --git a/web/controller/inbound.go b/web/controller/inbound.go
index 07dacfe784..3cb3b6b6df 100644
--- a/web/controller/inbound.go
+++ b/web/controller/inbound.go
@@ -37,7 +37,7 @@ func (a *InboundController) startTask() {
c := webServer.GetCron()
c.AddFunc("@every 10s", func() {
if a.xrayService.IsNeedRestartAndSetFalse() {
- err := a.xrayService.RestartXray()
+ err := a.xrayService.RestartXray(false)
if err != nil {
logger.Error("restart xray failed:", err)
}
diff --git a/web/html/xui/form/inbound.html b/web/html/xui/form/inbound.html
index 64da61e90b..30872e4880 100644
--- a/web/html/xui/form/inbound.html
+++ b/web/html/xui/form/inbound.html
@@ -39,6 +39,19 @@