Skip to content

Commit

Permalink
代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
Jrohy committed Apr 9, 2023
1 parent e64931b commit 5d55d49
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 23 deletions.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ exit:
menuList := []string{"trojan管理", "用户管理", "安装管理", "web管理", "查看配置", "生成json"}
switch util.LoopInput("请选择: ", menuList, false) {
case 1:
trojan.ControllMenu()
trojan.ControlMenu()
case 2:
trojan.UserMenu()
case 3:
Expand Down
4 changes: 2 additions & 2 deletions core/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package core
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"trojan/asset"
)

Expand Down Expand Up @@ -42,7 +42,7 @@ func WriteClient(port int, password, domain, writePath string) bool {
fmt.Println(err)
return false
}
if err = ioutil.WriteFile(writePath, outData, 0644); err != nil {
if err = os.WriteFile(writePath, outData, 0644); err != nil {
fmt.Println(err)
return false
}
Expand Down
4 changes: 2 additions & 2 deletions core/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"errors"
"fmt"
mysqlDriver "github.com/go-sql-driver/mysql"
"io/ioutil"
"io"
"log"
"time"

Expand Down Expand Up @@ -70,7 +70,7 @@ CREATE TABLE IF NOT EXISTS users (
// GetDB 获取mysql数据库连接
func (mysql *Mysql) GetDB() *sql.DB {
// 屏蔽mysql驱动包的日志输出
mysqlDriver.SetLogger(log.New(ioutil.Discard, "", 0))
mysqlDriver.SetLogger(log.New(io.Discard, "", 0))
conn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", mysql.Username, mysql.Password, mysql.ServerAddr, mysql.ServerPort, mysql.Database)
db, err := sql.Open("mysql", conn)
if err != nil {
Expand Down
15 changes: 7 additions & 8 deletions core/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"os"
"strings"
"trojan/util"
Expand All @@ -19,8 +18,8 @@ func (mysql *Mysql) UpgradeDB() error {
return errors.New("can't connect mysql")
}
var field string
error := db.QueryRow("SHOW COLUMNS FROM users LIKE 'passwordShow';").Scan(&field)
if error == sql.ErrNoRows {
err := db.QueryRow("SHOW COLUMNS FROM users LIKE 'passwordShow';").Scan(&field)
if err == sql.ErrNoRows {
fmt.Println(util.Yellow("正在进行数据库升级, 请稍等.."))
if _, err := db.Exec("ALTER TABLE users ADD COLUMN passwordShow VARCHAR(255) NOT NULL AFTER password;"); err != nil {
fmt.Println(err)
Expand All @@ -43,8 +42,8 @@ func (mysql *Mysql) UpgradeDB() error {
}
}
}
error = db.QueryRow("SHOW COLUMNS FROM users LIKE 'useDays';").Scan(&field)
if error == sql.ErrNoRows {
err = db.QueryRow("SHOW COLUMNS FROM users LIKE 'useDays';").Scan(&field)
if err == sql.ErrNoRows {
fmt.Println(util.Yellow("正在进行数据库升级, 请稍等.."))
if _, err := db.Exec(`
ALTER TABLE users
Expand All @@ -56,10 +55,10 @@ ADD COLUMN expiryDate char(10) DEFAULT '';
}
}
var tableName string
error = db.QueryRow(fmt.Sprintf(
err = db.QueryRow(fmt.Sprintf(
"SELECT * FROM information_schema.TABLES WHERE TABLE_NAME = 'users' AND TABLE_SCHEMA = '%s' ",
mysql.Database) + " AND TABLE_COLLATION LIKE 'utf8%';").Scan(&tableName)
if error == sql.ErrNoRows {
if err == sql.ErrNoRows {
tempFile := "temp.sql"
mysql.DumpSql(tempFile)
mysql.ExecSql(tempFile)
Expand Down Expand Up @@ -96,7 +95,7 @@ INSERT INTO users(username, password, passwordShow, quota, download, upload, use
// ExecSql 执行sql
func (mysql *Mysql) ExecSql(filePath string) error {
db := mysql.GetDB()
fileByte, err := ioutil.ReadFile(filePath)
fileByte, err := os.ReadFile(filePath)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions trojan/trojan.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"trojan/util"
)

// ControllMenu Trojan控制菜单
func ControllMenu() {
// ControlMenu Trojan控制菜单
func ControlMenu() {
fmt.Println()
tType := Type()
if tType == "trojan" {
Expand Down
4 changes: 2 additions & 2 deletions util/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package util
import (
"bufio"
"fmt"
"io/ioutil"
"io"
"net/http"
"os/exec"
"strings"
Expand Down Expand Up @@ -92,7 +92,7 @@ func RunWebShell(webShellPath string) {
fmt.Println(err.Error())
}
defer resp.Body.Close()
installShell, err := ioutil.ReadAll(resp.Body)
installShell, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println(err.Error())
}
Expand Down
6 changes: 3 additions & 3 deletions util/linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package util
import (
"bufio"
"fmt"
"io/ioutil"
"io"
"math/rand"
"net"
"net/http"
Expand All @@ -27,7 +27,7 @@ func PortIsUse(port int) bool {
// RandomPort 获取没占用的随机端口
func RandomPort() int {
for {
rand.Seed(time.Now().UnixNano())
rand.New(rand.NewSource(time.Now().UnixNano()))
newPort := rand.Intn(65536)
if !PortIsUse(newPort) {
return newPort
Expand All @@ -54,7 +54,7 @@ func GetLocalIP() string {
resp, _ = http.Get("http://icanhazip.com")
}
defer resp.Body.Close()
s, _ := ioutil.ReadAll(resp.Body)
s, _ := io.ReadAll(resp.Body)
return string(s)
}

Expand Down
4 changes: 2 additions & 2 deletions web/controller/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ func UpdateResetDay(day uint) *ResponseBody {
return &responseBody
}

// SheduleTask 定时任务
func SheduleTask() {
// ScheduleTask 定时任务
func ScheduleTask() {
loc, _ := time.LoadLocation("Asia/Shanghai")
c = cron.New(cron.WithLocation(loc))
c.AddFunc("@daily", func() {
Expand Down
2 changes: 1 addition & 1 deletion web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func Start(host string, port, timeout int, isSSL bool) {
userRouter(router)
dataRouter(router)
commonRouter(router)
controller.SheduleTask()
controller.ScheduleTask()
controller.CollectTask()
util.OpenPort(port)
if isSSL {
Expand Down

0 comments on commit 5d55d49

Please sign in to comment.