init
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
"trojan/trojan"
|
||||
)
|
||||
|
||||
// addCmd represents the add command
|
||||
var addCmd = &cobra.Command{
|
||||
Use: "add",
|
||||
Short: "添加用户",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
trojan.AddUser()
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(addCmd)
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
"trojan/trojan"
|
||||
)
|
||||
|
||||
// cleanCmd represents the clean command
|
||||
var cleanCmd = &cobra.Command{
|
||||
Use: "clean",
|
||||
Short: "清空指定用户流量",
|
||||
Long: `传入指定用户名来清空用户流量, 多个用户名空格隔开, 例如:
|
||||
trojan clean zhangsan lisi
|
||||
`,
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
trojan.CleanDataByName(args)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(cleanCmd)
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
"os"
|
||||
)
|
||||
|
||||
// completionCmd represents the completion command
|
||||
var completionCmd = &cobra.Command{
|
||||
Use: "completion",
|
||||
Short: "自动命令补全(支持bash和zsh)",
|
||||
Long: `
|
||||
支持bash和zsh的命令补全
|
||||
a. bash环境添加下面命令到 ~/.bashrc
|
||||
source <(trojan completion bash)
|
||||
|
||||
b. zsh环境添加以下命令到~/.zshrc
|
||||
source <(trojan completion zsh)
|
||||
`,
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(completionCmd)
|
||||
completionCmd.AddCommand(&cobra.Command{Use: "bash", Short: "bash命令补全", Run: func(cmd *cobra.Command, args []string) {
|
||||
rootCmd.GenBashCompletion(os.Stdout)
|
||||
}})
|
||||
completionCmd.AddCommand(&cobra.Command{Use: "zsh", Short: "zsh命令补全", Run: func(cmd *cobra.Command, args []string) {
|
||||
rootCmd.GenZshCompletion(os.Stdout)
|
||||
}})
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
"trojan/trojan"
|
||||
)
|
||||
|
||||
// delCmd represents the del command
|
||||
var delCmd = &cobra.Command{
|
||||
Use: "del",
|
||||
Short: "删除用户",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
trojan.DelUser()
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(delCmd)
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/cobra"
|
||||
"trojan/core"
|
||||
"trojan/util"
|
||||
)
|
||||
|
||||
// exportCmd represents the export command
|
||||
var exportCmd = &cobra.Command{
|
||||
Use: "export",
|
||||
Short: "导出数据库sql文件",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
mysql := core.GetMysql()
|
||||
if err := mysql.DumpSql(args[0]); err != nil {
|
||||
fmt.Println(util.Red(err.Error()))
|
||||
} else {
|
||||
fmt.Println(util.Green("导出sql成功!"))
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(exportCmd)
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/cobra"
|
||||
"trojan/core"
|
||||
"trojan/util"
|
||||
)
|
||||
|
||||
// importCmd represents the import command
|
||||
var importCmd = &cobra.Command{
|
||||
Use: "import",
|
||||
Short: "导入数据库sql文件",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
mysql := core.GetMysql()
|
||||
if err := mysql.ExecSql(args[0]); err != nil {
|
||||
fmt.Println(util.Red(err.Error()))
|
||||
} else {
|
||||
fmt.Println(util.Green("导入sql成功!"))
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(importCmd)
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"trojan/trojan"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// infoCmd represents the info command
|
||||
var infoCmd = &cobra.Command{
|
||||
Use: "info",
|
||||
Short: "用户信息列表",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
trojan.UserList()
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(infoCmd)
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"trojan/util"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var line int
|
||||
|
||||
// logCmd represents the log command
|
||||
var logCmd = &cobra.Command{
|
||||
Use: "log",
|
||||
Short: "查看trojan日志",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
util.Log("trojan", line)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
logCmd.Flags().IntVarP(&line, "line", "n", 300, "查看日志行数")
|
||||
rootCmd.AddCommand(logCmd)
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"trojan/trojan"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// portCmd represents the info command
|
||||
var portCmd = &cobra.Command{
|
||||
Use: "port",
|
||||
Short: "修改trojan端口",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
trojan.ChangePort()
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(portCmd)
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
"trojan/trojan"
|
||||
)
|
||||
|
||||
// restartCmd represents the restart command
|
||||
var restartCmd = &cobra.Command{
|
||||
Use: "restart",
|
||||
Short: "重启trojan",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
trojan.Restart()
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(restartCmd)
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/cobra"
|
||||
"os"
|
||||
"trojan/core"
|
||||
"trojan/trojan"
|
||||
"trojan/util"
|
||||
)
|
||||
|
||||
// rootCmd represents the base command when called without any subcommands
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "trojan",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
mainMenu()
|
||||
},
|
||||
}
|
||||
|
||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
||||
func Execute() {
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func check() {
|
||||
if !util.IsExists("/usr/local/etc/trojan/config.json") {
|
||||
fmt.Println("本机未安装trojan, 正在自动安装...")
|
||||
trojan.InstallTrojan("")
|
||||
core.WritePassword(nil)
|
||||
trojan.InstallTls()
|
||||
trojan.InstallMysql()
|
||||
util.SystemctlRestart("trojan-web")
|
||||
}
|
||||
}
|
||||
|
||||
func mainMenu() {
|
||||
check()
|
||||
exit:
|
||||
for {
|
||||
fmt.Println()
|
||||
fmt.Println(util.Cyan("欢迎使用trojan管理程序"))
|
||||
fmt.Println()
|
||||
menuList := []string{"trojan管理", "用户管理", "安装管理", "web管理", "查看配置", "生成json"}
|
||||
switch util.LoopInput("请选择: ", menuList, false) {
|
||||
case 1:
|
||||
trojan.ControlMenu()
|
||||
case 2:
|
||||
trojan.UserMenu()
|
||||
case 3:
|
||||
trojan.InstallMenu()
|
||||
case 4:
|
||||
trojan.WebMenu()
|
||||
case 5:
|
||||
trojan.UserList()
|
||||
case 6:
|
||||
trojan.GenClientJson()
|
||||
default:
|
||||
break exit
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
"trojan/trojan"
|
||||
)
|
||||
|
||||
// startCmd represents the start command
|
||||
var startCmd = &cobra.Command{
|
||||
Use: "start",
|
||||
Short: "启动trojan",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
trojan.Start()
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(startCmd)
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"trojan/trojan"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// statusCmd represents the status command
|
||||
var statusCmd = &cobra.Command{
|
||||
Use: "status",
|
||||
Short: "查看trojan状态",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
trojan.Status(true)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(statusCmd)
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
"trojan/trojan"
|
||||
)
|
||||
|
||||
// stopCmd represents the stop command
|
||||
var stopCmd = &cobra.Command{
|
||||
Use: "stop",
|
||||
Short: "停止trojan",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
trojan.Stop()
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(stopCmd)
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
"trojan/trojan"
|
||||
)
|
||||
|
||||
// tlsCmd represents the tls command
|
||||
var tlsCmd = &cobra.Command{
|
||||
Use: "tls",
|
||||
Short: "证书安装",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
trojan.InstallTls()
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(tlsCmd)
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
"trojan/trojan"
|
||||
)
|
||||
|
||||
// upgradeCmd represents the update command
|
||||
var updateCmd = &cobra.Command{
|
||||
Use: "update",
|
||||
Short: "更新trojan",
|
||||
Long: "可添加版本号更新特定版本, 比如'trojan update v0.10.0', 不添加版本号则安装最新版",
|
||||
Args: cobra.MaximumNArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
version := ""
|
||||
if len(args) == 1 {
|
||||
version = args[0]
|
||||
}
|
||||
trojan.InstallTrojan(version)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(updateCmd)
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
"trojan/util"
|
||||
)
|
||||
|
||||
// updateWebCmd represents the update command
|
||||
var updateWebCmd = &cobra.Command{
|
||||
Use: "updateWeb",
|
||||
Short: "更新trojan管理程序",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
util.RunWebShell("https://git.io/trojan-install")
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(updateWebCmd)
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/cobra"
|
||||
"trojan/core"
|
||||
"trojan/trojan"
|
||||
)
|
||||
|
||||
// upgradeCmd represents the update command
|
||||
var upgradeCmd = &cobra.Command{
|
||||
Use: "upgrade",
|
||||
Short: "升级数据库和trojan配置文件",
|
||||
}
|
||||
|
||||
func upgradeConfig() {
|
||||
domain, _ := core.GetValue("domain")
|
||||
if domain == "" {
|
||||
return
|
||||
}
|
||||
core.WriteDomain(domain)
|
||||
trojan.Restart()
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(upgradeCmd)
|
||||
upgradeCmd.AddCommand(&cobra.Command{Use: "db", Short: "升级数据库", Run: func(cmd *cobra.Command, args []string) {
|
||||
if err := core.GetMysql().UpgradeDB(); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}})
|
||||
upgradeCmd.AddCommand(&cobra.Command{Use: "config", Short: "升级配置文件", Run: func(cmd *cobra.Command, args []string) {
|
||||
upgradeConfig()
|
||||
}})
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"trojan/trojan"
|
||||
"trojan/util"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// versionCmd represents the Version command
|
||||
var versionCmd = &cobra.Command{
|
||||
Use: "version",
|
||||
Short: "显示版本号",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
upTime := trojan.UpTime()
|
||||
trojanVersion := trojan.Version()
|
||||
fmt.Println()
|
||||
fmt.Printf("Version: %s\n\n", util.Cyan(trojan.MVersion))
|
||||
fmt.Printf("BuildDate: %s\n\n", util.Cyan(trojan.BuildDate))
|
||||
fmt.Printf("GoVersion: %s\n\n", util.Cyan(trojan.GoVersion))
|
||||
fmt.Printf("GitVersion: %s\n\n", util.Cyan(trojan.GitVersion))
|
||||
fmt.Printf("TrojanVersion: %s\n\n", util.Cyan(trojanVersion))
|
||||
fmt.Printf("TrojanUpTime: %s\n\n", util.Cyan(upTime))
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(versionCmd)
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/cobra"
|
||||
"trojan/util"
|
||||
"trojan/web"
|
||||
)
|
||||
|
||||
var (
|
||||
host string
|
||||
port int
|
||||
ssl bool
|
||||
timeout int
|
||||
)
|
||||
|
||||
// webCmd represents the web command
|
||||
var webCmd = &cobra.Command{
|
||||
Use: "web",
|
||||
Short: "以web方式启动",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
web.Start(host, port, timeout, ssl)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
webCmd.Flags().StringVarP(&host, "host", "", "0.0.0.0", "web服务监听地址")
|
||||
webCmd.Flags().IntVarP(&port, "port", "p", 80, "web服务启动端口")
|
||||
webCmd.Flags().BoolVarP(&ssl, "ssl", "", false, "web服务是否以https方式运行")
|
||||
webCmd.Flags().IntVarP(&timeout, "timeout", "t", 120, "登录超时时间(min)")
|
||||
webCmd.AddCommand(&cobra.Command{Use: "stop", Short: "停止trojan-web", Run: func(cmd *cobra.Command, args []string) {
|
||||
util.SystemctlStop("trojan-web")
|
||||
}})
|
||||
webCmd.AddCommand(&cobra.Command{Use: "start", Short: "启动trojan-web", Run: func(cmd *cobra.Command, args []string) {
|
||||
util.SystemctlStart("trojan-web")
|
||||
}})
|
||||
webCmd.AddCommand(&cobra.Command{Use: "restart", Short: "重启trojan-web", Run: func(cmd *cobra.Command, args []string) {
|
||||
util.SystemctlRestart("trojan-web")
|
||||
}})
|
||||
webCmd.AddCommand(&cobra.Command{Use: "status", Short: "查看trojan-web状态", Run: func(cmd *cobra.Command, args []string) {
|
||||
fmt.Println(util.SystemctlStatus("trojan-web"))
|
||||
}})
|
||||
webCmd.AddCommand(&cobra.Command{Use: "log", Short: "查看trojan-web日志", Run: func(cmd *cobra.Command, args []string) {
|
||||
util.Log("trojan-web", 300)
|
||||
}})
|
||||
rootCmd.AddCommand(webCmd)
|
||||
}
|
||||
Reference in New Issue
Block a user