Skip to content

Commit

Permalink
添加命令 ’log‘ 用于查看运行时xray日志
Browse files Browse the repository at this point in the history
  • Loading branch information
hsernos committed Oct 13, 2021
1 parent 1226a0d commit 2481e93
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 10 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,17 @@ Txray(目录命名随意)
# 为 xray 添加可执行权限
[root@xxx Xray-linux-64]# chmod u+x xray
```

4.打开终端输入`Txray`回车进入Shell交互 或 继续在末尾添加命令直接运行

```
# 1.shell交互运行,命令可Tab补齐
[root@xxx xxxx]# Txray
# 2.直接运行,如更新节点 (不会进入shell交互)
[root@xxx xxxx]# Txray sub update-node
```

## 稍高级使用
1.单电脑多系统共用同一份配置文件(配置环境变量`TXRAY_HOME`

Expand Down Expand Up @@ -159,8 +162,9 @@ Commands:
version, -v 查看版本
clear 清屏
exit 退出程序
run 启动或重启服务
stop 停止服务
run 启动或重启节点
stop 关闭节点
log 查看运行时xray日志
Usage: run [索引式]
run [索引式] 默认为上一次运行节点,如果选中多个节点,则选择访问 'setting' 中测试国外URL延迟最小的
Expand Down
3 changes: 2 additions & 1 deletion Txray.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

const (
version = "v3.0.1"
version = "v3.0.2"
name = "Txray"
)

Expand All @@ -23,6 +23,7 @@ func init() {
log.GetConsoleZapcore(log.INFO),
log.GetFileZapcore(absPath, log.INFO, 5),
)

}

func beforeOfRun(shell *ishell.Shell) {
Expand Down
4 changes: 1 addition & 3 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
'windows': ['386', 'amd64','arm','arm64'],
'freebsd': ['386', 'amd64', 'arm', 'arm64'],
'openbsd': ['386', 'amd64', 'arm', 'arm64'],
'android':['arm64'],
'dragonfly':['amd64'],

'android':['arm64']
}

Name = 'Txray'
Expand Down
5 changes: 3 additions & 2 deletions cmd/help/help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ Commands:
version, -v 查看版本
clear 清屏
exit 退出程序
run 启动或重启服务
stop 停止服务
run 启动或重启节点
stop 关闭节点
log 查看运行时xray日志

Usage: run [索引式]
run [索引式] 默认为上一次运行节点,如果选中多个节点,则选择访问 'setting' 中测试国外URL延迟最小的
Expand Down
8 changes: 8 additions & 0 deletions cmd/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package cmd

import (
"Txray/cmd/help"
"Txray/xray"

"github.com/abiosoft/ishell"
)

Expand Down Expand Up @@ -30,6 +32,12 @@ func InitShell(shell *ishell.Shell) {
InitRouteShell(shell)
InitServiceShell(shell)
InitAliasShell(shell)
shell.AddCmd(&ishell.Cmd{
Name: "log",
Func: func(c *ishell.Context) {
xray.ShowLog()
},
})
}

// 参数解析
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/go-ole/go-ole v1.2.5 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hpcloud/tail v1.0.0 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mattn/go-colorable v0.1.6 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
Expand All @@ -38,6 +39,8 @@ require (
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf // indirect
golang.org/x/text v0.3.6 // indirect
gopkg.in/fsnotify.v1 v1.4.7 // indirect
gopkg.in/ini.v1 v1.63.2 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO
github.com/hashicorp/mdns v1.0.1/go.mod h1:4gW7WsVCke5TE7EPeYliwHlRUyBtfCwuFwuMg2DmyNY=
github.com/hashicorp/memberlist v0.2.2/go.mod h1:MS2lj3INKhZjWNqd3N0m3J+Jxf3DAOnAH9VT3Sh9MUE=
github.com/hashicorp/serf v0.9.5/go.mod h1:UWDWwZeL5cuWDJdl0C6wrvrUwEqtQ4ZKBKKENpqIUyk=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
Expand Down Expand Up @@ -673,10 +675,14 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/ini.v1 v1.63.2 h1:tGK/CyBg7SMzb60vP1M03vNZ3VDu3wGQJwn7Sxi9r3c=
gopkg.in/ini.v1 v1.63.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
29 changes: 27 additions & 2 deletions xray/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import (
"Txray/core/setting"
"Txray/log"
"bufio"
"os"
"os/exec"
"time"
"os"
"fmt"

"github.com/hpcloud/tail"
)

var Xray *exec.Cmd
Expand Down Expand Up @@ -124,8 +127,30 @@ func Stop() {
}
setting.SetPid(0)
}
logPath := PathJoin(core.GetConfigDir(), "xray_access.log")
file, _ := os.Stat(logPath)
if file != nil {
fileSize := float64(file.Size())/ (1 << 20)
if fileSize > 5 {
os.Remove(logPath)
}
}
}

// 查看xray日志
func ShowLog() {
t, _ := tail.TailFile(PathJoin(core.GetConfigDir(), "xray_access.log"), tail.Config{
ReOpen: true, // 重新打开
Follow: true, // 是否跟随
Location: &tail.SeekInfo{Offset: 0, Whence: 2}, // 从文件的哪个地方开始读
MustExist: false, // 文件不存在不报错
Poll: true,
})

for line := range t.Lines {
fmt.Println(line.Text)
}
}

func readInfo(r *bufio.Reader, lines *[]string) {
for i := 0; i < 20; i++ {
Expand All @@ -140,4 +165,4 @@ func readInfo(r *bufio.Reader, lines *[]string) {
func checkProc(c *exec.Cmd, status chan struct{}) {
c.Wait()
status <- struct{}{}
}
}

0 comments on commit 2481e93

Please sign in to comment.