Initial commit (code only without large binaries)
This commit is contained in:
7
EdgeDNS/internal/utils/command.go
Normal file
7
EdgeDNS/internal/utils/command.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package utils
|
||||
|
||||
// 命令定义
|
||||
type Command struct {
|
||||
Name string
|
||||
Args []string
|
||||
}
|
||||
61
EdgeDNS/internal/utils/command_executor.go
Normal file
61
EdgeDNS/internal/utils/command_executor.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
// 命令执行器
|
||||
type CommandExecutor struct {
|
||||
commands []*Command
|
||||
}
|
||||
|
||||
// 获取新对象
|
||||
func NewCommandExecutor() *CommandExecutor {
|
||||
return &CommandExecutor{}
|
||||
}
|
||||
|
||||
// 添加命令
|
||||
func (this *CommandExecutor) Add(command string, arg ...string) {
|
||||
this.commands = append(this.commands, &Command{
|
||||
Name: command,
|
||||
Args: arg,
|
||||
})
|
||||
}
|
||||
|
||||
// 执行命令
|
||||
func (this *CommandExecutor) Run() (output string, err error) {
|
||||
if len(this.commands) == 0 {
|
||||
return "", errors.New("no commands no run")
|
||||
}
|
||||
var lastCmd *exec.Cmd = nil
|
||||
var lastData []byte = nil
|
||||
for _, command := range this.commands {
|
||||
cmd := exec.Command(command.Name, command.Args...)
|
||||
stdout := bytes.NewBuffer([]byte{})
|
||||
cmd.Stdout = stdout
|
||||
if lastCmd != nil {
|
||||
cmd.Stdin = bytes.NewBuffer(lastData)
|
||||
}
|
||||
err = cmd.Start()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
err = cmd.Wait()
|
||||
if err != nil {
|
||||
_, ok := err.(*exec.ExitError)
|
||||
if ok {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
return "", err
|
||||
}
|
||||
lastData = stdout.Bytes()
|
||||
|
||||
lastCmd = cmd
|
||||
}
|
||||
|
||||
return string(bytes.TrimSpace(lastData)), nil
|
||||
}
|
||||
192
EdgeDNS/internal/utils/dbs/batch.go
Normal file
192
EdgeDNS/internal/utils/dbs/batch.go
Normal file
@@ -0,0 +1,192 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package dbs
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"github.com/TeaOSLab/EdgeDNS/internal/remotelogs"
|
||||
"time"
|
||||
)
|
||||
|
||||
type batchItem struct {
|
||||
query string
|
||||
args []any
|
||||
}
|
||||
|
||||
type Batch struct {
|
||||
db *DB
|
||||
n int
|
||||
|
||||
enableStat bool
|
||||
|
||||
onFail func(err error)
|
||||
|
||||
queue chan *batchItem
|
||||
closeEvent chan bool
|
||||
|
||||
isClosed bool
|
||||
}
|
||||
|
||||
func NewBatch(db *DB, n int) *Batch {
|
||||
var batch = &Batch{
|
||||
db: db,
|
||||
n: n,
|
||||
queue: make(chan *batchItem, 16),
|
||||
closeEvent: make(chan bool, 1),
|
||||
}
|
||||
db.batches = append(db.batches, batch)
|
||||
return batch
|
||||
}
|
||||
|
||||
func (this *Batch) EnableStat(b bool) {
|
||||
this.enableStat = b
|
||||
}
|
||||
|
||||
func (this *Batch) OnFail(callback func(err error)) {
|
||||
this.onFail = callback
|
||||
}
|
||||
|
||||
func (this *Batch) Add(query string, args ...any) {
|
||||
if this.isClosed {
|
||||
return
|
||||
}
|
||||
this.queue <- &batchItem{
|
||||
query: query,
|
||||
args: args,
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Batch) Exec() {
|
||||
var n = this.n
|
||||
if n <= 0 {
|
||||
n = 4
|
||||
}
|
||||
|
||||
var ticker = time.NewTicker(100 * time.Millisecond)
|
||||
var count = 0
|
||||
var lastTx *sql.Tx
|
||||
For:
|
||||
for {
|
||||
// closed
|
||||
if this.isClosed {
|
||||
if lastTx != nil {
|
||||
_ = this.commitTx(lastTx)
|
||||
lastTx = nil
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
select {
|
||||
case item := <-this.queue:
|
||||
if lastTx == nil {
|
||||
lastTx = this.beginTx()
|
||||
if lastTx == nil {
|
||||
continue For
|
||||
}
|
||||
}
|
||||
|
||||
err := this.execItem(lastTx, item)
|
||||
if err != nil {
|
||||
if IsClosedErr(err) {
|
||||
return
|
||||
}
|
||||
this.processErr(item.query, err)
|
||||
}
|
||||
|
||||
count++
|
||||
|
||||
if count == n {
|
||||
count = 0
|
||||
err = this.commitTx(lastTx)
|
||||
lastTx = nil
|
||||
if err != nil {
|
||||
if IsClosedErr(err) {
|
||||
return
|
||||
}
|
||||
this.processErr("commit", err)
|
||||
}
|
||||
}
|
||||
case <-ticker.C:
|
||||
if lastTx == nil || count == 0 {
|
||||
continue For
|
||||
}
|
||||
count = 0
|
||||
err := this.commitTx(lastTx)
|
||||
lastTx = nil
|
||||
if err != nil {
|
||||
if IsClosedErr(err) {
|
||||
return
|
||||
}
|
||||
this.processErr("commit", err)
|
||||
}
|
||||
case <-this.closeEvent:
|
||||
// closed
|
||||
|
||||
if lastTx != nil {
|
||||
_ = this.commitTx(lastTx)
|
||||
lastTx = nil
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Batch) close() {
|
||||
this.isClosed = true
|
||||
|
||||
select {
|
||||
case this.closeEvent <- true:
|
||||
default:
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Batch) beginTx() *sql.Tx {
|
||||
if !this.db.BeginUpdating() {
|
||||
return nil
|
||||
}
|
||||
|
||||
tx, err := this.db.Begin()
|
||||
if err != nil {
|
||||
this.processErr("begin transaction", err)
|
||||
this.db.EndUpdating()
|
||||
return nil
|
||||
}
|
||||
return tx
|
||||
}
|
||||
|
||||
func (this *Batch) commitTx(tx *sql.Tx) error {
|
||||
// always commit without checking database closing status
|
||||
this.db.EndUpdating()
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (this *Batch) execItem(tx *sql.Tx, item *batchItem) error {
|
||||
// check database status
|
||||
if this.db.BeginUpdating() {
|
||||
defer this.db.EndUpdating()
|
||||
} else {
|
||||
return errDBIsClosed
|
||||
}
|
||||
|
||||
if this.enableStat {
|
||||
defer SharedQueryStatManager.AddQuery(item.query).End()
|
||||
}
|
||||
|
||||
_, err := tx.Exec(item.query, item.args...)
|
||||
return err
|
||||
}
|
||||
|
||||
func (this *Batch) processErr(prefix string, err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if this.onFail != nil {
|
||||
this.onFail(err)
|
||||
} else {
|
||||
remotelogs.Error("SQLITE_BATCH", prefix+": "+err.Error())
|
||||
}
|
||||
}
|
||||
252
EdgeDNS/internal/utils/dbs/db.go
Normal file
252
EdgeDNS/internal/utils/dbs/db.go
Normal file
@@ -0,0 +1,252 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package dbs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
teaconst "github.com/TeaOSLab/EdgeDNS/internal/const"
|
||||
"github.com/TeaOSLab/EdgeDNS/internal/events"
|
||||
"github.com/TeaOSLab/EdgeDNS/internal/remotelogs"
|
||||
fsutils "github.com/TeaOSLab/EdgeDNS/internal/utils/fs"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var errDBIsClosed = errors.New("the database is closed")
|
||||
|
||||
type DB struct {
|
||||
locker *fsutils.Locker
|
||||
rawDB *sql.DB
|
||||
dsn string
|
||||
|
||||
statusLocker sync.Mutex
|
||||
countUpdating int32
|
||||
|
||||
isClosing bool
|
||||
|
||||
enableStat bool
|
||||
|
||||
batches []*Batch
|
||||
}
|
||||
|
||||
func OpenWriter(dsn string) (*DB, error) {
|
||||
return open(dsn, true)
|
||||
}
|
||||
|
||||
func OpenReader(dsn string) (*DB, error) {
|
||||
return open(dsn, false)
|
||||
}
|
||||
|
||||
func open(dsn string, lock bool) (*DB, error) {
|
||||
if teaconst.IsQuiting {
|
||||
return nil, errors.New("can not open database when process is quiting")
|
||||
}
|
||||
|
||||
// decode path
|
||||
var path = dsn
|
||||
var queryIndex = strings.Index(dsn, "?")
|
||||
if queryIndex >= 0 {
|
||||
path = path[:queryIndex]
|
||||
}
|
||||
path = strings.TrimSpace(strings.TrimPrefix(path, "file:"))
|
||||
|
||||
// locker
|
||||
var locker *fsutils.Locker
|
||||
if lock {
|
||||
locker = fsutils.NewLocker(path)
|
||||
err := locker.Lock()
|
||||
if err != nil {
|
||||
remotelogs.Warn("DB", "lock '"+path+"' failed: "+err.Error())
|
||||
locker = nil
|
||||
}
|
||||
}
|
||||
|
||||
// check if closed successfully last time, if not we recover it
|
||||
var walPath = path + "-wal"
|
||||
_, statWalErr := os.Stat(walPath)
|
||||
var shouldRecover = statWalErr == nil
|
||||
|
||||
// open
|
||||
rawDB, err := sql.Open("sqlite3", dsn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if shouldRecover {
|
||||
err = rawDB.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// open again
|
||||
rawDB, err = sql.Open("sqlite3", dsn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
var db = NewDB(rawDB, dsn)
|
||||
db.locker = locker
|
||||
return db, nil
|
||||
}
|
||||
|
||||
func NewDB(rawDB *sql.DB, dsn string) *DB {
|
||||
var db = &DB{
|
||||
rawDB: rawDB,
|
||||
dsn: dsn,
|
||||
}
|
||||
|
||||
events.On(events.EventQuit, func() {
|
||||
_ = db.Close()
|
||||
})
|
||||
events.On(events.EventTerminated, func() {
|
||||
_ = db.Close()
|
||||
})
|
||||
|
||||
return db
|
||||
}
|
||||
|
||||
func (this *DB) SetMaxOpenConns(n int) {
|
||||
this.rawDB.SetMaxOpenConns(n)
|
||||
}
|
||||
|
||||
func (this *DB) EnableStat(b bool) {
|
||||
this.enableStat = b
|
||||
}
|
||||
|
||||
func (this *DB) Begin() (*sql.Tx, error) {
|
||||
// check database status
|
||||
if this.BeginUpdating() {
|
||||
defer this.EndUpdating()
|
||||
} else {
|
||||
return nil, errDBIsClosed
|
||||
}
|
||||
|
||||
return this.rawDB.Begin()
|
||||
}
|
||||
|
||||
func (this *DB) Prepare(query string) (*Stmt, error) {
|
||||
stmt, err := this.rawDB.Prepare(query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var s = NewStmt(this, stmt, query)
|
||||
if this.enableStat {
|
||||
s.EnableStat()
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (this *DB) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) {
|
||||
// check database status
|
||||
if this.BeginUpdating() {
|
||||
defer this.EndUpdating()
|
||||
} else {
|
||||
return nil, errDBIsClosed
|
||||
}
|
||||
|
||||
if this.enableStat {
|
||||
defer SharedQueryStatManager.AddQuery(query).End()
|
||||
}
|
||||
|
||||
return this.rawDB.ExecContext(ctx, query, args...)
|
||||
}
|
||||
|
||||
func (this *DB) Exec(query string, args ...any) (sql.Result, error) {
|
||||
// check database status
|
||||
if this.BeginUpdating() {
|
||||
defer this.EndUpdating()
|
||||
} else {
|
||||
return nil, errDBIsClosed
|
||||
}
|
||||
|
||||
if this.enableStat {
|
||||
defer SharedQueryStatManager.AddQuery(query).End()
|
||||
}
|
||||
return this.rawDB.Exec(query, args...)
|
||||
}
|
||||
|
||||
func (this *DB) Query(query string, args ...any) (*sql.Rows, error) {
|
||||
if this.enableStat {
|
||||
defer SharedQueryStatManager.AddQuery(query).End()
|
||||
}
|
||||
return this.rawDB.Query(query, args...)
|
||||
}
|
||||
|
||||
func (this *DB) QueryRow(query string, args ...any) *sql.Row {
|
||||
if this.enableStat {
|
||||
defer SharedQueryStatManager.AddQuery(query).End()
|
||||
}
|
||||
return this.rawDB.QueryRow(query, args...)
|
||||
}
|
||||
|
||||
// Close the database
|
||||
func (this *DB) Close() error {
|
||||
// check database status
|
||||
this.statusLocker.Lock()
|
||||
if this.isClosing {
|
||||
this.statusLocker.Unlock()
|
||||
return nil
|
||||
}
|
||||
this.isClosing = true
|
||||
this.statusLocker.Unlock()
|
||||
|
||||
// waiting for updating operations to finish
|
||||
for {
|
||||
this.statusLocker.Lock()
|
||||
var countUpdating = this.countUpdating
|
||||
this.statusLocker.Unlock()
|
||||
if countUpdating <= 0 {
|
||||
break
|
||||
}
|
||||
time.Sleep(1 * time.Millisecond)
|
||||
}
|
||||
|
||||
for _, batch := range this.batches {
|
||||
batch.close()
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if this.locker != nil {
|
||||
_ = this.locker.Release()
|
||||
}
|
||||
}()
|
||||
|
||||
// print log
|
||||
/**if len(this.dsn) > 0 {
|
||||
u, _ := url.Parse(this.dsn)
|
||||
if u != nil && len(u.Path) > 0 {
|
||||
remotelogs.Debug("DB", "close '"+u.Path)
|
||||
}
|
||||
}**/
|
||||
|
||||
return this.rawDB.Close()
|
||||
}
|
||||
|
||||
func (this *DB) BeginUpdating() bool {
|
||||
this.statusLocker.Lock()
|
||||
defer this.statusLocker.Unlock()
|
||||
|
||||
if this.isClosing {
|
||||
return false
|
||||
}
|
||||
|
||||
this.countUpdating++
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *DB) EndUpdating() {
|
||||
this.statusLocker.Lock()
|
||||
this.countUpdating--
|
||||
this.statusLocker.Unlock()
|
||||
}
|
||||
|
||||
func (this *DB) RawDB() *sql.DB {
|
||||
return this.rawDB
|
||||
}
|
||||
17
EdgeDNS/internal/utils/dbs/db_test.go
Normal file
17
EdgeDNS/internal/utils/dbs/db_test.go
Normal file
@@ -0,0 +1,17 @@
|
||||
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package dbs_test
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParseDSN(t *testing.T) {
|
||||
var dsn = "file:/home/cache/p43/.indexes/db-3.db?cache=private&mode=ro&_journal_mode=WAL&_sync=OFF&_cache_size=88000"
|
||||
u, err := url.Parse(dsn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(u.Path) // expect: :/home/cache/p43/.indexes/db-3.db
|
||||
}
|
||||
24
EdgeDNS/internal/utils/dbs/query_label.go
Normal file
24
EdgeDNS/internal/utils/dbs/query_label.go
Normal file
@@ -0,0 +1,24 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package dbs
|
||||
|
||||
import "time"
|
||||
|
||||
type QueryLabel struct {
|
||||
manager *QueryStatManager
|
||||
query string
|
||||
before time.Time
|
||||
}
|
||||
|
||||
func NewQueryLabel(manager *QueryStatManager, query string) *QueryLabel {
|
||||
return &QueryLabel{
|
||||
manager: manager,
|
||||
query: query,
|
||||
before: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
func (this *QueryLabel) End() {
|
||||
var cost = time.Since(this.before).Seconds()
|
||||
this.manager.AddCost(this.query, cost)
|
||||
}
|
||||
30
EdgeDNS/internal/utils/dbs/query_stat.go
Normal file
30
EdgeDNS/internal/utils/dbs/query_stat.go
Normal file
@@ -0,0 +1,30 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package dbs
|
||||
|
||||
type QueryStat struct {
|
||||
Query string
|
||||
CostMin float64
|
||||
CostMax float64
|
||||
|
||||
CostTotal float64
|
||||
Calls int64
|
||||
}
|
||||
|
||||
func NewQueryStat(query string) *QueryStat {
|
||||
return &QueryStat{
|
||||
Query: query,
|
||||
}
|
||||
}
|
||||
|
||||
func (this *QueryStat) AddCost(cost float64) {
|
||||
if this.CostMin == 0 || this.CostMin > cost {
|
||||
this.CostMin = cost
|
||||
}
|
||||
if this.CostMax == 0 || this.CostMax < cost {
|
||||
this.CostMax = cost
|
||||
}
|
||||
|
||||
this.CostTotal += cost
|
||||
this.Calls++
|
||||
}
|
||||
89
EdgeDNS/internal/utils/dbs/query_stat_manager.go
Normal file
89
EdgeDNS/internal/utils/dbs/query_stat_manager.go
Normal file
@@ -0,0 +1,89 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package dbs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
teaconst "github.com/TeaOSLab/EdgeDNS/internal/const"
|
||||
"github.com/TeaOSLab/EdgeDNS/internal/events"
|
||||
"github.com/TeaOSLab/EdgeDNS/internal/goman"
|
||||
"github.com/iwind/TeaGo/logs"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
if !teaconst.IsMain {
|
||||
return
|
||||
}
|
||||
|
||||
var ticker = time.NewTicker(5 * time.Second)
|
||||
|
||||
events.On(events.EventLoaded, func() {
|
||||
if teaconst.EnableDBStat {
|
||||
goman.New(func() {
|
||||
for range ticker.C {
|
||||
var stats = []string{}
|
||||
for _, stat := range SharedQueryStatManager.TopN(10) {
|
||||
var avg = stat.CostTotal / float64(stat.Calls)
|
||||
var query = stat.Query
|
||||
if len(query) > 128 {
|
||||
query = query[:128]
|
||||
}
|
||||
stats = append(stats, fmt.Sprintf("%.2fms/%.2fms/%.2fms - %d - %s", stat.CostMin*1000, stat.CostMax*1000, avg*1000, stat.Calls, query))
|
||||
}
|
||||
logs.Println("\n========== DB STATS ==========\n" + strings.Join(stats, "\n") + "\n=============================")
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
var SharedQueryStatManager = NewQueryStatManager()
|
||||
|
||||
type QueryStatManager struct {
|
||||
statsMap map[string]*QueryStat // query => *QueryStat
|
||||
locker sync.Mutex
|
||||
}
|
||||
|
||||
func NewQueryStatManager() *QueryStatManager {
|
||||
return &QueryStatManager{
|
||||
statsMap: map[string]*QueryStat{},
|
||||
}
|
||||
}
|
||||
|
||||
func (this *QueryStatManager) AddQuery(query string) *QueryLabel {
|
||||
return NewQueryLabel(this, query)
|
||||
}
|
||||
|
||||
func (this *QueryStatManager) AddCost(query string, cost float64) {
|
||||
this.locker.Lock()
|
||||
defer this.locker.Unlock()
|
||||
|
||||
stat, ok := this.statsMap[query]
|
||||
if !ok {
|
||||
stat = NewQueryStat(query)
|
||||
this.statsMap[query] = stat
|
||||
}
|
||||
stat.AddCost(cost)
|
||||
}
|
||||
|
||||
func (this *QueryStatManager) TopN(n int) []*QueryStat {
|
||||
this.locker.Lock()
|
||||
defer this.locker.Unlock()
|
||||
|
||||
var stats = []*QueryStat{}
|
||||
for _, stat := range this.statsMap {
|
||||
stats = append(stats, stat)
|
||||
}
|
||||
sort.Slice(stats, func(i, j int) bool {
|
||||
return stats[i].CostMax > stats[j].CostMax
|
||||
})
|
||||
|
||||
if len(stats) > n {
|
||||
return stats[:n]
|
||||
}
|
||||
return stats
|
||||
}
|
||||
24
EdgeDNS/internal/utils/dbs/query_stat_manager_test.go
Normal file
24
EdgeDNS/internal/utils/dbs/query_stat_manager_test.go
Normal file
@@ -0,0 +1,24 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package dbs_test
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeDNS/internal/utils/dbs"
|
||||
"github.com/iwind/TeaGo/logs"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestQueryStatManager(t *testing.T) {
|
||||
var manager = dbs.NewQueryStatManager()
|
||||
{
|
||||
var label = manager.AddQuery("sql 1")
|
||||
time.Sleep(1 * time.Second)
|
||||
label.End()
|
||||
}
|
||||
manager.AddQuery("sql 1").End()
|
||||
manager.AddQuery("sql 2").End()
|
||||
for _, stat := range manager.TopN(10) {
|
||||
logs.PrintAsJSON(stat, t)
|
||||
}
|
||||
}
|
||||
97
EdgeDNS/internal/utils/dbs/stmt.go
Normal file
97
EdgeDNS/internal/utils/dbs/stmt.go
Normal file
@@ -0,0 +1,97 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package dbs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
type Stmt struct {
|
||||
db *DB
|
||||
rawStmt *sql.Stmt
|
||||
query string
|
||||
|
||||
enableStat bool
|
||||
}
|
||||
|
||||
func NewStmt(db *DB, rawStmt *sql.Stmt, query string) *Stmt {
|
||||
return &Stmt{
|
||||
db: db,
|
||||
rawStmt: rawStmt,
|
||||
query: query,
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Stmt) EnableStat() {
|
||||
this.enableStat = true
|
||||
}
|
||||
|
||||
func (this *Stmt) ExecContext(ctx context.Context, args ...any) (sql.Result, error) {
|
||||
// check database status
|
||||
if this.db.BeginUpdating() {
|
||||
defer this.db.EndUpdating()
|
||||
} else {
|
||||
return nil, errDBIsClosed
|
||||
}
|
||||
|
||||
if this.enableStat {
|
||||
defer SharedQueryStatManager.AddQuery(this.query).End()
|
||||
}
|
||||
return this.rawStmt.ExecContext(ctx, args...)
|
||||
}
|
||||
|
||||
func (this *Stmt) Exec(args ...any) (sql.Result, error) {
|
||||
// check database status
|
||||
if this.db.BeginUpdating() {
|
||||
defer this.db.EndUpdating()
|
||||
} else {
|
||||
return nil, errDBIsClosed
|
||||
}
|
||||
|
||||
if this.enableStat {
|
||||
defer SharedQueryStatManager.AddQuery(this.query).End()
|
||||
}
|
||||
return this.rawStmt.Exec(args...)
|
||||
}
|
||||
|
||||
func (this *Stmt) QueryContext(ctx context.Context, args ...any) (*sql.Rows, error) {
|
||||
if this.enableStat {
|
||||
defer SharedQueryStatManager.AddQuery(this.query).End()
|
||||
}
|
||||
return this.rawStmt.QueryContext(ctx, args...)
|
||||
}
|
||||
|
||||
func (this *Stmt) Query(args ...any) (*sql.Rows, error) {
|
||||
if this.enableStat {
|
||||
defer SharedQueryStatManager.AddQuery(this.query).End()
|
||||
}
|
||||
rows, err := this.rawStmt.Query(args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var rowsErr = rows.Err()
|
||||
if rowsErr != nil {
|
||||
_ = rows.Close()
|
||||
return nil, rowsErr
|
||||
}
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
func (this *Stmt) QueryRowContext(ctx context.Context, args ...any) *sql.Row {
|
||||
if this.enableStat {
|
||||
defer SharedQueryStatManager.AddQuery(this.query).End()
|
||||
}
|
||||
return this.rawStmt.QueryRowContext(ctx, args...)
|
||||
}
|
||||
|
||||
func (this *Stmt) QueryRow(args ...any) *sql.Row {
|
||||
if this.enableStat {
|
||||
defer SharedQueryStatManager.AddQuery(this.query).End()
|
||||
}
|
||||
return this.rawStmt.QueryRow(args...)
|
||||
}
|
||||
|
||||
func (this *Stmt) Close() error {
|
||||
return this.rawStmt.Close()
|
||||
}
|
||||
7
EdgeDNS/internal/utils/dbs/utils.go
Normal file
7
EdgeDNS/internal/utils/dbs/utils.go
Normal file
@@ -0,0 +1,7 @@
|
||||
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package dbs
|
||||
|
||||
func IsClosedErr(err error) bool {
|
||||
return err == errDBIsClosed
|
||||
}
|
||||
8
EdgeDNS/internal/utils/errors.go
Normal file
8
EdgeDNS/internal/utils/errors.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package utils
|
||||
|
||||
import "github.com/iwind/TeaGo/logs"
|
||||
|
||||
func PrintError(err error) {
|
||||
// TODO 记录调用的文件名、行数
|
||||
logs.Println("[ERROR]" + err.Error())
|
||||
}
|
||||
162
EdgeDNS/internal/utils/exec/cmd.go
Normal file
162
EdgeDNS/internal/utils/exec/cmd.go
Normal file
@@ -0,0 +1,162 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package executils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Cmd struct {
|
||||
name string
|
||||
args []string
|
||||
env []string
|
||||
dir string
|
||||
|
||||
ctx context.Context
|
||||
timeout time.Duration
|
||||
cancelFunc func()
|
||||
|
||||
captureStdout bool
|
||||
captureStderr bool
|
||||
|
||||
stdout *bytes.Buffer
|
||||
stderr *bytes.Buffer
|
||||
|
||||
rawCmd *exec.Cmd
|
||||
}
|
||||
|
||||
func NewCmd(name string, args ...string) *Cmd {
|
||||
return &Cmd{
|
||||
name: name,
|
||||
args: args,
|
||||
}
|
||||
}
|
||||
|
||||
func NewTimeoutCmd(timeout time.Duration, name string, args ...string) *Cmd {
|
||||
return (&Cmd{
|
||||
name: name,
|
||||
args: args,
|
||||
}).WithTimeout(timeout)
|
||||
}
|
||||
|
||||
func (this *Cmd) WithTimeout(timeout time.Duration) *Cmd {
|
||||
this.timeout = timeout
|
||||
|
||||
ctx, cancelFunc := context.WithTimeout(context.Background(), timeout)
|
||||
this.ctx = ctx
|
||||
this.cancelFunc = cancelFunc
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
func (this *Cmd) WithStdout() *Cmd {
|
||||
this.captureStdout = true
|
||||
return this
|
||||
}
|
||||
|
||||
func (this *Cmd) WithStderr() *Cmd {
|
||||
this.captureStderr = true
|
||||
return this
|
||||
}
|
||||
|
||||
func (this *Cmd) WithEnv(env []string) *Cmd {
|
||||
this.env = env
|
||||
return this
|
||||
}
|
||||
|
||||
func (this *Cmd) WithDir(dir string) *Cmd {
|
||||
this.dir = dir
|
||||
return this
|
||||
}
|
||||
|
||||
func (this *Cmd) Start() error {
|
||||
var cmd = this.compose()
|
||||
return cmd.Start()
|
||||
}
|
||||
|
||||
func (this *Cmd) Wait() error {
|
||||
var cmd = this.compose()
|
||||
return cmd.Wait()
|
||||
}
|
||||
|
||||
func (this *Cmd) Run() error {
|
||||
if this.cancelFunc != nil {
|
||||
defer this.cancelFunc()
|
||||
}
|
||||
|
||||
var cmd = this.compose()
|
||||
return cmd.Run()
|
||||
}
|
||||
|
||||
func (this *Cmd) RawStdout() string {
|
||||
if this.stdout != nil {
|
||||
return this.stdout.String()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (this *Cmd) Stdout() string {
|
||||
return strings.TrimSpace(this.RawStdout())
|
||||
}
|
||||
|
||||
func (this *Cmd) RawStderr() string {
|
||||
if this.stderr != nil {
|
||||
return this.stderr.String()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (this *Cmd) Stderr() string {
|
||||
return strings.TrimSpace(this.RawStderr())
|
||||
}
|
||||
|
||||
func (this *Cmd) String() string {
|
||||
if this.rawCmd != nil {
|
||||
return this.rawCmd.String()
|
||||
}
|
||||
var newCmd = exec.Command(this.name, this.args...)
|
||||
return newCmd.String()
|
||||
}
|
||||
|
||||
func (this *Cmd) Process() *os.Process {
|
||||
if this.rawCmd != nil {
|
||||
return this.rawCmd.Process
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *Cmd) compose() *exec.Cmd {
|
||||
if this.rawCmd != nil {
|
||||
return this.rawCmd
|
||||
}
|
||||
|
||||
if this.ctx != nil {
|
||||
this.rawCmd = exec.CommandContext(this.ctx, this.name, this.args...)
|
||||
} else {
|
||||
this.rawCmd = exec.Command(this.name, this.args...)
|
||||
}
|
||||
|
||||
if this.env != nil {
|
||||
this.rawCmd.Env = this.env
|
||||
}
|
||||
|
||||
if len(this.dir) > 0 {
|
||||
this.rawCmd.Dir = this.dir
|
||||
}
|
||||
|
||||
if this.captureStdout {
|
||||
this.stdout = &bytes.Buffer{}
|
||||
this.rawCmd.Stdout = this.stdout
|
||||
}
|
||||
if this.captureStderr {
|
||||
this.stderr = &bytes.Buffer{}
|
||||
this.rawCmd.Stderr = this.stderr
|
||||
}
|
||||
|
||||
return this.rawCmd
|
||||
}
|
||||
61
EdgeDNS/internal/utils/exec/cmd_test.go
Normal file
61
EdgeDNS/internal/utils/exec/cmd_test.go
Normal file
@@ -0,0 +1,61 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package executils_test
|
||||
|
||||
import (
|
||||
executils "github.com/TeaOSLab/EdgeDNS/internal/utils/exec"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestNewTimeoutCmd_Sleep(t *testing.T) {
|
||||
var cmd = executils.NewTimeoutCmd(1*time.Second, "sleep", "3")
|
||||
cmd.WithStdout()
|
||||
cmd.WithStderr()
|
||||
err := cmd.Run()
|
||||
t.Log("error:", err)
|
||||
t.Log("stdout:", cmd.Stdout())
|
||||
t.Log("stderr:", cmd.Stderr())
|
||||
}
|
||||
|
||||
func TestNewTimeoutCmd_Echo(t *testing.T) {
|
||||
var cmd = executils.NewTimeoutCmd(10*time.Second, "echo", "-n", "hello")
|
||||
cmd.WithStdout()
|
||||
cmd.WithStderr()
|
||||
err := cmd.Run()
|
||||
t.Log("error:", err)
|
||||
t.Log("stdout:", cmd.Stdout())
|
||||
t.Log("stderr:", cmd.Stderr())
|
||||
}
|
||||
|
||||
func TestNewTimeoutCmd_Echo2(t *testing.T) {
|
||||
var cmd = executils.NewCmd("echo", "hello")
|
||||
cmd.WithStdout()
|
||||
cmd.WithStderr()
|
||||
err := cmd.Run()
|
||||
t.Log("error:", err)
|
||||
t.Log("stdout:", cmd.Stdout())
|
||||
t.Log("raw stdout:", cmd.RawStdout())
|
||||
t.Log("stderr:", cmd.Stderr())
|
||||
t.Log("raw stderr:", cmd.RawStderr())
|
||||
}
|
||||
|
||||
func TestNewTimeoutCmd_Echo3(t *testing.T) {
|
||||
var cmd = executils.NewCmd("echo", "-n", "hello")
|
||||
err := cmd.Run()
|
||||
t.Log("error:", err)
|
||||
t.Log("stdout:", cmd.Stdout())
|
||||
t.Log("stderr:", cmd.Stderr())
|
||||
}
|
||||
|
||||
func TestCmd_Process(t *testing.T) {
|
||||
var cmd = executils.NewCmd("echo", "-n", "hello")
|
||||
err := cmd.Run()
|
||||
t.Log("error:", err)
|
||||
t.Log(cmd.Process())
|
||||
}
|
||||
|
||||
func TestNewTimeoutCmd_String(t *testing.T) {
|
||||
var cmd = executils.NewCmd("echo", "-n", "hello")
|
||||
t.Log("stdout:", cmd.String())
|
||||
}
|
||||
58
EdgeDNS/internal/utils/exec/look_linux.go
Normal file
58
EdgeDNS/internal/utils/exec/look_linux.go
Normal file
@@ -0,0 +1,58 @@
|
||||
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
//go:build linux
|
||||
|
||||
package executils
|
||||
|
||||
import (
|
||||
"golang.org/x/sys/unix"
|
||||
"io/fs"
|
||||
"os"
|
||||
"os/exec"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// LookPath customize our LookPath() function, to work in broken $PATH environment variable
|
||||
func LookPath(file string) (string, error) {
|
||||
result, err := exec.LookPath(file)
|
||||
if err == nil && len(result) > 0 {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// add common dirs contains executable files these may be excluded in $PATH environment variable
|
||||
var binPaths = []string{
|
||||
"/usr/sbin",
|
||||
"/usr/bin",
|
||||
"/usr/local/sbin",
|
||||
"/usr/local/bin",
|
||||
}
|
||||
|
||||
for _, binPath := range binPaths {
|
||||
var fullPath = binPath + string(os.PathSeparator) + file
|
||||
|
||||
stat, err := os.Stat(fullPath)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if stat.IsDir() {
|
||||
return "", syscall.EISDIR
|
||||
}
|
||||
|
||||
var mode = stat.Mode()
|
||||
if mode.IsDir() {
|
||||
return "", syscall.EISDIR
|
||||
}
|
||||
err = syscall.Faccessat(unix.AT_FDCWD, fullPath, unix.X_OK, unix.AT_EACCESS)
|
||||
if err == nil || (err != syscall.ENOSYS && err != syscall.EPERM) {
|
||||
return fullPath, err
|
||||
}
|
||||
if mode&0111 != 0 {
|
||||
return fullPath, nil
|
||||
}
|
||||
return "", fs.ErrPermission
|
||||
}
|
||||
|
||||
return "", &exec.Error{
|
||||
Name: file,
|
||||
Err: exec.ErrNotFound,
|
||||
}
|
||||
}
|
||||
10
EdgeDNS/internal/utils/exec/look_others.go
Normal file
10
EdgeDNS/internal/utils/exec/look_others.go
Normal file
@@ -0,0 +1,10 @@
|
||||
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
//go:build !linux
|
||||
|
||||
package executils
|
||||
|
||||
import "os/exec"
|
||||
|
||||
func LookPath(file string) (string, error) {
|
||||
return exec.LookPath(file)
|
||||
}
|
||||
13
EdgeDNS/internal/utils/exit.go
Normal file
13
EdgeDNS/internal/utils/exit.go
Normal file
@@ -0,0 +1,13 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeDNS/internal/events"
|
||||
"os"
|
||||
)
|
||||
|
||||
func Exit() {
|
||||
events.Notify(events.EventTerminated)
|
||||
os.Exit(0)
|
||||
}
|
||||
11
EdgeDNS/internal/utils/file.go
Normal file
11
EdgeDNS/internal/utils/file.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// 临时文件
|
||||
func TmpFile(path string) string {
|
||||
return filepath.Clean(Tea.TmpDir() + Tea.DS + path)
|
||||
}
|
||||
82
EdgeDNS/internal/utils/fs/locker.go
Normal file
82
EdgeDNS/internal/utils/fs/locker.go
Normal file
@@ -0,0 +1,82 @@
|
||||
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package fsutils
|
||||
|
||||
import (
|
||||
"os"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Locker struct {
|
||||
path string
|
||||
fp *os.File
|
||||
}
|
||||
|
||||
func NewLocker(path string) *Locker {
|
||||
return &Locker{
|
||||
path: path + ".lock",
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Locker) TryLock() (ok bool, err error) {
|
||||
if this.fp == nil {
|
||||
fp, err := os.OpenFile(this.path, os.O_CREATE|os.O_WRONLY, 0666)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
this.fp = fp
|
||||
}
|
||||
return this.tryLock()
|
||||
}
|
||||
|
||||
func (this *Locker) Lock() error {
|
||||
if this.fp == nil {
|
||||
fp, err := os.OpenFile(this.path, os.O_CREATE|os.O_WRONLY, 0666)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
this.fp = fp
|
||||
}
|
||||
|
||||
for {
|
||||
b, err := this.tryLock()
|
||||
if err != nil {
|
||||
_ = this.fp.Close()
|
||||
return err
|
||||
}
|
||||
if b {
|
||||
return nil
|
||||
}
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Locker) Release() error {
|
||||
err := this.fp.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
this.fp = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *Locker) tryLock() (ok bool, err error) {
|
||||
err = syscall.Flock(int(this.fp.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
|
||||
if err == nil {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
errno, isErrNo := err.(syscall.Errno)
|
||||
if !isErrNo {
|
||||
return
|
||||
}
|
||||
|
||||
if !errno.Temporary() {
|
||||
return
|
||||
}
|
||||
|
||||
err = nil // 不提示错误
|
||||
|
||||
return
|
||||
}
|
||||
24
EdgeDNS/internal/utils/fs/locker_test.go
Normal file
24
EdgeDNS/internal/utils/fs/locker_test.go
Normal file
@@ -0,0 +1,24 @@
|
||||
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package fsutils_test
|
||||
|
||||
import (
|
||||
fsutils "github.com/TeaOSLab/EdgeDNS/internal/utils/fs"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLocker_Lock(t *testing.T) {
|
||||
var path = "/tmp/file-test"
|
||||
var locker = fsutils.NewLocker(path)
|
||||
err := locker.Lock()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = locker.Release()
|
||||
|
||||
var locker2 = fsutils.NewLocker(path)
|
||||
err = locker2.Lock()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
44
EdgeDNS/internal/utils/http.go
Normal file
44
EdgeDNS/internal/utils/http.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// HTTP请求客户端管理
|
||||
var timeoutClientMap = map[time.Duration]*http.Client{} // timeout => Client
|
||||
var timeoutClientLocker = sync.Mutex{}
|
||||
|
||||
// NewHTTPClient 获取一个新的Client
|
||||
func NewHTTPClient(timeout time.Duration) *http.Client {
|
||||
return &http.Client{
|
||||
Timeout: timeout,
|
||||
Transport: &http.Transport{
|
||||
MaxIdleConns: 4096,
|
||||
MaxIdleConnsPerHost: 32,
|
||||
MaxConnsPerHost: 32,
|
||||
IdleConnTimeout: 2 * time.Minute,
|
||||
ExpectContinueTimeout: 1 * time.Second,
|
||||
TLSHandshakeTimeout: 0,
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// SharedHttpClient 获取一个公用的Client
|
||||
func SharedHttpClient(timeout time.Duration) *http.Client {
|
||||
timeoutClientLocker.Lock()
|
||||
defer timeoutClientLocker.Unlock()
|
||||
|
||||
client, ok := timeoutClientMap[timeout]
|
||||
if ok {
|
||||
return client
|
||||
}
|
||||
client = NewHTTPClient(timeout)
|
||||
timeoutClientMap[timeout] = client
|
||||
return client
|
||||
}
|
||||
32
EdgeDNS/internal/utils/http_test.go
Normal file
32
EdgeDNS/internal/utils/http_test.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/iwind/TeaGo/assert"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestNewHTTPClient(t *testing.T) {
|
||||
a := assert.NewAssertion(t)
|
||||
|
||||
client := NewHTTPClient(1 * time.Second)
|
||||
a.IsTrue(client.Timeout == 1*time.Second)
|
||||
|
||||
client2 := NewHTTPClient(1 * time.Second)
|
||||
a.IsTrue(client != client2)
|
||||
}
|
||||
|
||||
func TestSharedHTTPClient(t *testing.T) {
|
||||
a := assert.NewAssertion(t)
|
||||
|
||||
_ = SharedHttpClient(2 * time.Second)
|
||||
_ = SharedHttpClient(3 * time.Second)
|
||||
|
||||
client := SharedHttpClient(1 * time.Second)
|
||||
a.IsTrue(client.Timeout == 1*time.Second)
|
||||
|
||||
client2 := SharedHttpClient(1 * time.Second)
|
||||
a.IsTrue(client == client2)
|
||||
|
||||
t.Log(timeoutClientMap)
|
||||
}
|
||||
27
EdgeDNS/internal/utils/ip.go
Normal file
27
EdgeDNS/internal/utils/ip.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"net"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// IsIPv4 是否为IPv4
|
||||
func IsIPv4(ip string) bool {
|
||||
var data = net.ParseIP(ip)
|
||||
if data == nil {
|
||||
return false
|
||||
}
|
||||
if strings.Contains(ip, ":") {
|
||||
return false
|
||||
}
|
||||
return data.To4() != nil
|
||||
}
|
||||
|
||||
// IsIPv6 是否为IPv6
|
||||
func IsIPv6(ip string) bool {
|
||||
var data = net.ParseIP(ip)
|
||||
if data == nil {
|
||||
return false
|
||||
}
|
||||
return !IsIPv4(ip)
|
||||
}
|
||||
36
EdgeDNS/internal/utils/net.go
Normal file
36
EdgeDNS/internal/utils/net.go
Normal file
@@ -0,0 +1,36 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package utils
|
||||
|
||||
import "sort"
|
||||
|
||||
// MergePorts 聚合端口
|
||||
// 返回 [ [fromPort, toPort], ... ]
|
||||
func MergePorts(ports []int) [][2]int {
|
||||
if len(ports) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
sort.Ints(ports)
|
||||
|
||||
var result = [][2]int{}
|
||||
var lastRange = [2]int{0, 0}
|
||||
var lastPort = -1
|
||||
for _, port := range ports {
|
||||
if port <= 0 /** 只处理有效的端口 **/ || port == lastPort /** 去重 **/ {
|
||||
continue
|
||||
}
|
||||
|
||||
if lastPort < 0 || port != lastPort+1 {
|
||||
lastRange = [2]int{port, port}
|
||||
result = append(result, lastRange)
|
||||
} else { // 如果是连续的
|
||||
lastRange[1] = port
|
||||
result[len(result)-1] = lastRange
|
||||
}
|
||||
|
||||
lastPort = port
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
111
EdgeDNS/internal/utils/service.go
Normal file
111
EdgeDNS/internal/utils/service.go
Normal file
@@ -0,0 +1,111 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/files"
|
||||
"github.com/iwind/TeaGo/logs"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// 服务管理器
|
||||
type ServiceManager struct {
|
||||
Name string
|
||||
Description string
|
||||
|
||||
fp *os.File
|
||||
logger *log.Logger
|
||||
onceLocker sync.Once
|
||||
}
|
||||
|
||||
// 获取对象
|
||||
func NewServiceManager(name, description string) *ServiceManager {
|
||||
manager := &ServiceManager{
|
||||
Name: name,
|
||||
Description: description,
|
||||
}
|
||||
|
||||
// root
|
||||
manager.resetRoot()
|
||||
|
||||
return manager
|
||||
}
|
||||
|
||||
// 设置服务
|
||||
func (this *ServiceManager) setup() {
|
||||
this.onceLocker.Do(func() {
|
||||
logFile := files.NewFile(Tea.Root + "/logs/service.log")
|
||||
if logFile.Exists() {
|
||||
_ = logFile.Delete()
|
||||
}
|
||||
|
||||
//logger
|
||||
fp, err := os.OpenFile(Tea.Root+"/logs/service.log", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
return
|
||||
}
|
||||
this.fp = fp
|
||||
this.logger = log.New(fp, "", log.LstdFlags)
|
||||
})
|
||||
}
|
||||
|
||||
// 记录普通日志
|
||||
func (this *ServiceManager) Log(msg string) {
|
||||
this.setup()
|
||||
if this.logger == nil {
|
||||
return
|
||||
}
|
||||
this.logger.Println("[info]" + msg)
|
||||
}
|
||||
|
||||
// 记录错误日志
|
||||
func (this *ServiceManager) LogError(msg string) {
|
||||
this.setup()
|
||||
if this.logger == nil {
|
||||
return
|
||||
}
|
||||
this.logger.Println("[error]" + msg)
|
||||
}
|
||||
|
||||
// 关闭
|
||||
func (this *ServiceManager) Close() error {
|
||||
if this.fp != nil {
|
||||
return this.fp.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 重置Root
|
||||
func (this *ServiceManager) resetRoot() {
|
||||
if !Tea.IsTesting() {
|
||||
exePath, err := os.Executable()
|
||||
if err != nil {
|
||||
exePath = os.Args[0]
|
||||
}
|
||||
link, err := filepath.EvalSymlinks(exePath)
|
||||
if err == nil {
|
||||
exePath = link
|
||||
}
|
||||
fullPath, err := filepath.Abs(exePath)
|
||||
if err == nil {
|
||||
Tea.UpdateRoot(filepath.Dir(filepath.Dir(fullPath)))
|
||||
}
|
||||
}
|
||||
Tea.SetPublicDir(Tea.Root + Tea.DS + "web" + Tea.DS + "public")
|
||||
Tea.SetViewsDir(Tea.Root + Tea.DS + "web" + Tea.DS + "views")
|
||||
Tea.SetTmpDir(Tea.Root + Tea.DS + "web" + Tea.DS + "tmp")
|
||||
}
|
||||
|
||||
// 保持命令行窗口是打开的
|
||||
func (this *ServiceManager) PauseWindow() {
|
||||
if runtime.GOOS != "windows" {
|
||||
return
|
||||
}
|
||||
|
||||
b := make([]byte, 1)
|
||||
_, _ = os.Stdin.Read(b)
|
||||
}
|
||||
163
EdgeDNS/internal/utils/service_linux.go
Normal file
163
EdgeDNS/internal/utils/service_linux.go
Normal file
@@ -0,0 +1,163 @@
|
||||
//go:build linux
|
||||
// +build linux
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
teaconst "github.com/TeaOSLab/EdgeDNS/internal/const"
|
||||
executils "github.com/TeaOSLab/EdgeDNS/internal/utils/exec"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/files"
|
||||
"os"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"time"
|
||||
)
|
||||
|
||||
var systemdServiceFile = "/etc/systemd/system/edge-dns.service"
|
||||
var initServiceFile = "/etc/init.d/" + teaconst.SystemdServiceName
|
||||
|
||||
// 安装服务
|
||||
func (this *ServiceManager) Install(exePath string, args []string) error {
|
||||
if os.Getgid() != 0 {
|
||||
return errors.New("only root users can install the service")
|
||||
}
|
||||
|
||||
systemd, err := executils.LookPath("systemctl")
|
||||
if err != nil {
|
||||
return this.installInitService(exePath, args)
|
||||
}
|
||||
|
||||
return this.installSystemdService(systemd, exePath, args)
|
||||
}
|
||||
|
||||
// 启动服务
|
||||
func (this *ServiceManager) Start() error {
|
||||
if os.Getgid() != 0 {
|
||||
return errors.New("only root users can start the service")
|
||||
}
|
||||
|
||||
if files.NewFile(systemdServiceFile).Exists() {
|
||||
systemd, err := executils.LookPath("systemctl")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return exec.Command(systemd, "start", teaconst.SystemdServiceName+".service").Start()
|
||||
}
|
||||
return exec.Command("service", teaconst.ProcessName, "start").Start()
|
||||
}
|
||||
|
||||
// 删除服务
|
||||
func (this *ServiceManager) Uninstall() error {
|
||||
if os.Getgid() != 0 {
|
||||
return errors.New("only root users can uninstall the service")
|
||||
}
|
||||
|
||||
if files.NewFile(systemdServiceFile).Exists() {
|
||||
systemd, err := executils.LookPath("systemctl")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// disable service
|
||||
_ = executils.NewTimeoutCmd(10*time.Second, systemd, "disable", teaconst.SystemdServiceName+".service").Start()
|
||||
|
||||
// reload
|
||||
_ = executils.NewTimeoutCmd(10*time.Second, systemd, "daemon-reload")
|
||||
|
||||
return files.NewFile(systemdServiceFile).Delete()
|
||||
}
|
||||
|
||||
f := files.NewFile(initServiceFile)
|
||||
if f.Exists() {
|
||||
return f.Delete()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// install init service
|
||||
func (this *ServiceManager) installInitService(exePath string, args []string) error {
|
||||
shortName := teaconst.SystemdServiceName
|
||||
scriptFile := Tea.Root + "/scripts/" + shortName
|
||||
if !files.NewFile(scriptFile).Exists() {
|
||||
return errors.New("'scripts/" + shortName + "' file not exists")
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(scriptFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
data = regexp.MustCompile("INSTALL_DIR=.+").ReplaceAll(data, []byte("INSTALL_DIR="+Tea.Root))
|
||||
err = os.WriteFile(initServiceFile, data, 0777)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
chkCmd, err := executils.LookPath("chkconfig")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = exec.Command(chkCmd, "--add", teaconst.ProcessName).Start()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// install systemd service
|
||||
func (this *ServiceManager) installSystemdService(systemd, exePath string, args []string) error {
|
||||
shortName := teaconst.SystemdServiceName
|
||||
longName := "GoEdge DNS" // TODO 将来可以修改
|
||||
|
||||
var startCmd = exePath + " daemon"
|
||||
bashPath, _ := executils.LookPath("bash")
|
||||
if len(bashPath) > 0 {
|
||||
startCmd = bashPath + " -c \"" + startCmd + "\""
|
||||
}
|
||||
|
||||
desc := `### BEGIN INIT INFO
|
||||
# Provides: ` + shortName + `
|
||||
# Required-Start: $all
|
||||
# Required-Stop:
|
||||
# Default-Start: 2 3 4 5
|
||||
# Default-Stop:
|
||||
# Short-Description: ` + longName + ` Service
|
||||
### END INIT INFO
|
||||
|
||||
[Unit]
|
||||
Description=` + longName + ` Service
|
||||
Before=shutdown.target
|
||||
After=network-online.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
Restart=always
|
||||
RestartSec=1s
|
||||
ExecStart=` + startCmd + `
|
||||
ExecStop=` + exePath + ` stop
|
||||
ExecReload=` + exePath + ` reload
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target`
|
||||
|
||||
// write file
|
||||
err := os.WriteFile(systemdServiceFile, []byte(desc), 0777)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// stop current systemd service if running
|
||||
_ = executils.NewTimeoutCmd(10*time.Second, "stop", shortName+".service").Start()
|
||||
|
||||
// reload
|
||||
_ = executils.NewTimeoutCmd(10*time.Second, systemd, "daemon-reload").Start()
|
||||
|
||||
// enable
|
||||
var cmd = executils.NewTimeoutCmd(10*time.Second, systemd, "enable", shortName+".service")
|
||||
return cmd.Run()
|
||||
}
|
||||
19
EdgeDNS/internal/utils/service_others.go
Normal file
19
EdgeDNS/internal/utils/service_others.go
Normal file
@@ -0,0 +1,19 @@
|
||||
//go:build !linux && !windows
|
||||
// +build !linux,!windows
|
||||
|
||||
package utils
|
||||
|
||||
// 安装服务
|
||||
func (this *ServiceManager) Install(exePath string, args []string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 启动服务
|
||||
func (this *ServiceManager) Start() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 删除服务
|
||||
func (this *ServiceManager) Uninstall() error {
|
||||
return nil
|
||||
}
|
||||
12
EdgeDNS/internal/utils/service_test.go
Normal file
12
EdgeDNS/internal/utils/service_test.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
teaconst "github.com/TeaOSLab/EdgeDNS/internal/const"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestServiceManager_Log(t *testing.T) {
|
||||
manager := NewServiceManager(teaconst.ProductName, teaconst.ProductName+" Server")
|
||||
manager.Log("Hello, World")
|
||||
manager.LogError("Hello, World")
|
||||
}
|
||||
175
EdgeDNS/internal/utils/service_windows.go
Normal file
175
EdgeDNS/internal/utils/service_windows.go
Normal file
@@ -0,0 +1,175 @@
|
||||
//go:build windows
|
||||
// +build windows
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
teaconst "github.com/TeaOSLab/EdgeDNS/internal/const"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"golang.org/x/sys/windows"
|
||||
"golang.org/x/sys/windows/svc"
|
||||
"golang.org/x/sys/windows/svc/mgr"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
// 安装服务
|
||||
func (this *ServiceManager) Install(exePath string, args []string) error {
|
||||
m, err := mgr.Connect()
|
||||
if err != nil {
|
||||
return fmt.Errorf("connecting: %w please 'Run as administrator' again", err)
|
||||
}
|
||||
defer m.Disconnect()
|
||||
s, err := m.OpenService(this.Name)
|
||||
if err == nil {
|
||||
s.Close()
|
||||
return fmt.Errorf("service %s already exists", this.Name)
|
||||
}
|
||||
|
||||
s, err = m.CreateService(this.Name, exePath, mgr.Config{
|
||||
DisplayName: this.Name,
|
||||
Description: this.Description,
|
||||
StartType: windows.SERVICE_AUTO_START,
|
||||
}, args...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating: %w", err)
|
||||
}
|
||||
defer s.Close()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 启动服务
|
||||
func (this *ServiceManager) Start() error {
|
||||
m, err := mgr.Connect()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer m.Disconnect()
|
||||
s, err := m.OpenService(this.Name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not access service: %w", err)
|
||||
}
|
||||
defer s.Close()
|
||||
err = s.Start("service")
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not start service: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 删除服务
|
||||
func (this *ServiceManager) Uninstall() error {
|
||||
m, err := mgr.Connect()
|
||||
if err != nil {
|
||||
return fmt.Errorf("connecting: %w please 'Run as administrator' again", err)
|
||||
}
|
||||
defer m.Disconnect()
|
||||
s, err := m.OpenService(this.Name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("open service: %w", err)
|
||||
}
|
||||
|
||||
// shutdown service
|
||||
_, err = s.Control(svc.Stop)
|
||||
if err != nil {
|
||||
fmt.Printf("shutdown service: %s\n", err.Error())
|
||||
}
|
||||
|
||||
defer s.Close()
|
||||
err = s.Delete()
|
||||
if err != nil {
|
||||
return fmt.Errorf("deleting: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 运行
|
||||
func (this *ServiceManager) Run() {
|
||||
err := svc.Run(this.Name, this)
|
||||
if err != nil {
|
||||
this.LogError(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// 同服务管理器的交互
|
||||
func (this *ServiceManager) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) {
|
||||
const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown | svc.AcceptPauseAndContinue
|
||||
|
||||
changes <- svc.Status{
|
||||
State: svc.StartPending,
|
||||
}
|
||||
|
||||
changes <- svc.Status{
|
||||
State: svc.Running,
|
||||
Accepts: cmdsAccepted,
|
||||
}
|
||||
|
||||
// start service
|
||||
this.Log("start")
|
||||
this.cmdStart()
|
||||
|
||||
loop:
|
||||
for {
|
||||
select {
|
||||
case c := <-r:
|
||||
switch c.Cmd {
|
||||
case svc.Interrogate:
|
||||
this.Log("cmd: Interrogate")
|
||||
changes <- c.CurrentStatus
|
||||
case svc.Stop, svc.Shutdown:
|
||||
this.Log("cmd: Stop|Shutdown")
|
||||
|
||||
// stop service
|
||||
this.cmdStop()
|
||||
|
||||
break loop
|
||||
case svc.Pause:
|
||||
this.Log("cmd: Pause")
|
||||
|
||||
// stop service
|
||||
this.cmdStop()
|
||||
|
||||
changes <- svc.Status{
|
||||
State: svc.Paused,
|
||||
Accepts: cmdsAccepted,
|
||||
}
|
||||
case svc.Continue:
|
||||
this.Log("cmd: Continue")
|
||||
|
||||
// start service
|
||||
this.cmdStart()
|
||||
|
||||
changes <- svc.Status{
|
||||
State: svc.Running,
|
||||
Accepts: cmdsAccepted,
|
||||
}
|
||||
default:
|
||||
this.LogError(fmt.Sprintf("unexpected control request #%d\r\n", c))
|
||||
}
|
||||
}
|
||||
}
|
||||
changes <- svc.Status{
|
||||
State: svc.StopPending,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 启动Web服务
|
||||
func (this *ServiceManager) cmdStart() {
|
||||
cmd := exec.Command(Tea.Root+Tea.DS+"bin"+Tea.DS+teaconst.SystemdServiceName+".exe", "start")
|
||||
err := cmd.Start()
|
||||
if err != nil {
|
||||
this.LogError(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// 停止Web服务
|
||||
func (this *ServiceManager) cmdStop() {
|
||||
cmd := exec.Command(Tea.Root+Tea.DS+"bin"+Tea.DS+teaconst.SystemdServiceName+".exe", "stop")
|
||||
err := cmd.Start()
|
||||
if err != nil {
|
||||
this.LogError(err.Error())
|
||||
}
|
||||
}
|
||||
10
EdgeDNS/internal/utils/sizes/sizes.go
Normal file
10
EdgeDNS/internal/utils/sizes/sizes.go
Normal file
@@ -0,0 +1,10 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package sizes
|
||||
|
||||
const (
|
||||
K int64 = 1024
|
||||
M = 1024 * K
|
||||
G = 1024 * M
|
||||
T = 1024 * G
|
||||
)
|
||||
17
EdgeDNS/internal/utils/sizes/sizes_test.go
Normal file
17
EdgeDNS/internal/utils/sizes/sizes_test.go
Normal file
@@ -0,0 +1,17 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package sizes_test
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeDNS/internal/utils/sizes"
|
||||
"github.com/iwind/TeaGo/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSizes(t *testing.T) {
|
||||
var a = assert.NewAssertion(t)
|
||||
a.IsTrue(sizes.K == 1024)
|
||||
a.IsTrue(sizes.M == 1024*1024)
|
||||
a.IsTrue(sizes.G == 1024*1024*1024)
|
||||
a.IsTrue(sizes.T == 1024*1024*1024*1024)
|
||||
}
|
||||
58
EdgeDNS/internal/utils/string.go
Normal file
58
EdgeDNS/internal/utils/string.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// UnsafeBytesToString convert bytes to string
|
||||
func UnsafeBytesToString(bs []byte) string {
|
||||
return *(*string)(unsafe.Pointer(&bs))
|
||||
}
|
||||
|
||||
// UnsafeStringToBytes convert string to bytes
|
||||
func UnsafeStringToBytes(s string) []byte {
|
||||
return *(*[]byte)(unsafe.Pointer(&s))
|
||||
}
|
||||
|
||||
// FormatAddress format address
|
||||
func FormatAddress(addr string) string {
|
||||
if strings.HasSuffix(addr, "unix:") {
|
||||
return addr
|
||||
}
|
||||
addr = strings.Replace(addr, " ", "", -1)
|
||||
addr = strings.Replace(addr, "\t", "", -1)
|
||||
addr = strings.Replace(addr, ":", ":", -1)
|
||||
addr = strings.TrimSpace(addr)
|
||||
return addr
|
||||
}
|
||||
|
||||
// FormatAddressList format address list
|
||||
func FormatAddressList(addrList []string) []string {
|
||||
result := []string{}
|
||||
for _, addr := range addrList {
|
||||
result = append(result, FormatAddress(addr))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// ToValidUTF8string 去除字符串中的非UTF-8字符
|
||||
func ToValidUTF8string(v string) string {
|
||||
return strings.ToValidUTF8(v, "")
|
||||
}
|
||||
|
||||
// EqualStrings 检查两个字符串slice内容是否一致
|
||||
func EqualStrings(s1 []string, s2 []string) bool {
|
||||
if len(s1) != len(s2) {
|
||||
return false
|
||||
}
|
||||
sort.Strings(s1)
|
||||
sort.Strings(s2)
|
||||
for index, v1 := range s1 {
|
||||
if v1 != s2[index] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
67
EdgeDNS/internal/utils/string_test.go
Normal file
67
EdgeDNS/internal/utils/string_test.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package utils_test
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeDNS/internal/utils"
|
||||
"github.com/iwind/TeaGo/assert"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBytesToString(t *testing.T) {
|
||||
t.Log(utils.UnsafeBytesToString([]byte("Hello,World")))
|
||||
}
|
||||
|
||||
func TestStringToBytes(t *testing.T) {
|
||||
t.Log(string(utils.UnsafeStringToBytes("Hello,World")))
|
||||
}
|
||||
|
||||
func BenchmarkBytesToString(b *testing.B) {
|
||||
var data = []byte("Hello,World")
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = utils.UnsafeBytesToString(data)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkBytesToString2(b *testing.B) {
|
||||
var data = []byte("Hello,World")
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = string(data)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStringToBytes(b *testing.B) {
|
||||
var s = strings.Repeat("Hello,World", 1024)
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = utils.UnsafeStringToBytes(s)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStringToBytes2(b *testing.B) {
|
||||
var s = strings.Repeat("Hello,World", 1024)
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = []byte(s)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormatAddress(t *testing.T) {
|
||||
t.Log(utils.FormatAddress("127.0.0.1:1234"))
|
||||
t.Log(utils.FormatAddress("127.0.0.1 : 1234"))
|
||||
t.Log(utils.FormatAddress("127.0.0.1:1234"))
|
||||
}
|
||||
|
||||
func TestFormatAddressList(t *testing.T) {
|
||||
t.Log(utils.FormatAddressList([]string{
|
||||
"127.0.0.1:1234",
|
||||
"127.0.0.1 : 1234",
|
||||
"127.0.0.1:1234",
|
||||
}))
|
||||
}
|
||||
|
||||
func TestEqualStrings(t *testing.T) {
|
||||
var a = assert.NewAssertion(t)
|
||||
a.IsFalse(utils.EqualStrings([]string{"a"}, []string{"b"}))
|
||||
a.IsFalse(utils.EqualStrings([]string{"a", "b"}, []string{"b"}))
|
||||
a.IsFalse(utils.EqualStrings([]string{"a", "b"}, []string{"a", "b", "c"}))
|
||||
a.IsTrue(utils.EqualStrings([]string{"a", "b"}, []string{"a", "b"}))
|
||||
a.IsTrue(utils.EqualStrings([]string{"a", "b"}, []string{"b", "a"}))
|
||||
}
|
||||
98
EdgeDNS/internal/utils/unzip.go
Normal file
98
EdgeDNS/internal/utils/unzip.go
Normal file
@@ -0,0 +1,98 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Unzip struct {
|
||||
zipFile string
|
||||
targetDir string
|
||||
stripPrefix string
|
||||
}
|
||||
|
||||
func NewUnzip(zipFile string, targetDir string, stripPrefix string) *Unzip {
|
||||
return &Unzip{
|
||||
zipFile: zipFile,
|
||||
targetDir: targetDir,
|
||||
stripPrefix: stripPrefix,
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Unzip) Run() error {
|
||||
if len(this.zipFile) == 0 {
|
||||
return errors.New("zip file should not be empty")
|
||||
}
|
||||
if len(this.targetDir) == 0 {
|
||||
return errors.New("target dir should not be empty")
|
||||
}
|
||||
|
||||
reader, err := zip.OpenReader(this.zipFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer func() {
|
||||
_ = reader.Close()
|
||||
}()
|
||||
|
||||
for _, file := range reader.File {
|
||||
info := file.FileInfo()
|
||||
filename := file.Name
|
||||
if len(this.stripPrefix) > 0 {
|
||||
filename = strings.TrimPrefix(filename, this.stripPrefix)
|
||||
}
|
||||
target := this.targetDir + "/" + filename
|
||||
|
||||
// 目录
|
||||
if info.IsDir() {
|
||||
stat, err := os.Stat(target)
|
||||
if err != nil {
|
||||
if !os.IsNotExist(err) {
|
||||
return err
|
||||
} else {
|
||||
err = os.MkdirAll(target, info.Mode())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
} else if !stat.IsDir() {
|
||||
err = os.MkdirAll(target, info.Mode())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// 文件
|
||||
err := func(file *zip.File, target string) error {
|
||||
fileReader, err := file.Open()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
_ = fileReader.Close()
|
||||
}()
|
||||
|
||||
fileWriter, err := os.OpenFile(target, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, file.FileInfo().Mode())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
_ = fileWriter.Close()
|
||||
}()
|
||||
|
||||
_, err = io.Copy(fileWriter, fileReader)
|
||||
return err
|
||||
}(file, target)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
24
EdgeDNS/internal/utils/version.go
Normal file
24
EdgeDNS/internal/utils/version.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"net"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// VersionToLong 计算版本代号
|
||||
func VersionToLong(version string) uint32 {
|
||||
countDots := strings.Count(version, ".")
|
||||
if countDots == 2 {
|
||||
version += ".0"
|
||||
} else if countDots == 1 {
|
||||
version += ".0.0"
|
||||
} else if countDots == 0 {
|
||||
version += ".0.0.0"
|
||||
}
|
||||
var ip = net.ParseIP(version)
|
||||
if ip == nil || ip.To4() == nil {
|
||||
return 0
|
||||
}
|
||||
return binary.BigEndian.Uint32(ip.To4())
|
||||
}
|
||||
21
EdgeDNS/internal/utils/version_test.go
Normal file
21
EdgeDNS/internal/utils/version_test.go
Normal file
@@ -0,0 +1,21 @@
|
||||
// Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package utils_test
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeDNS/internal/utils"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestVersionToLong(t *testing.T) {
|
||||
for _, v := range []string{
|
||||
"aaa",
|
||||
"1",
|
||||
"1.2",
|
||||
"1.2.3",
|
||||
"1.2.3.4",
|
||||
"1.2.3.4.5",
|
||||
} {
|
||||
t.Log(v, "=>", utils.VersionToLong(v))
|
||||
}
|
||||
}
|
||||
9
EdgeDNS/internal/utils/zero/zero.go
Normal file
9
EdgeDNS/internal/utils/zero/zero.go
Normal file
@@ -0,0 +1,9 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package zero
|
||||
|
||||
type Zero = struct{}
|
||||
|
||||
func New() Zero {
|
||||
return Zero{}
|
||||
}
|
||||
41
EdgeDNS/internal/utils/zero/zero_test.go
Normal file
41
EdgeDNS/internal/utils/zero/zero_test.go
Normal file
@@ -0,0 +1,41 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package zero
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestZero_Chan(t *testing.T) {
|
||||
var stat1 = &runtime.MemStats{}
|
||||
runtime.ReadMemStats(stat1)
|
||||
|
||||
var m = make(chan Zero, 2_000_000)
|
||||
for i := 0; i < 1_000_000; i++ {
|
||||
m <- New()
|
||||
}
|
||||
|
||||
var stat2 = &runtime.MemStats{}
|
||||
runtime.ReadMemStats(stat2)
|
||||
t.Log(stat2.HeapInuse, stat1.HeapInuse, stat2.HeapInuse-stat1.HeapInuse, "B")
|
||||
t.Log(len(m))
|
||||
}
|
||||
|
||||
func TestZero_Map(t *testing.T) {
|
||||
var stat1 = &runtime.MemStats{}
|
||||
runtime.ReadMemStats(stat1)
|
||||
|
||||
var m = map[int]Zero{}
|
||||
for i := 0; i < 1_000_000; i++ {
|
||||
m[i] = New()
|
||||
}
|
||||
|
||||
var stat2 = &runtime.MemStats{}
|
||||
runtime.ReadMemStats(stat2)
|
||||
t.Log((stat2.HeapInuse-stat1.HeapInuse)/1024/1024, "MB")
|
||||
t.Log(len(m))
|
||||
|
||||
_, ok := m[1024]
|
||||
t.Log(ok)
|
||||
}
|
||||
Reference in New Issue
Block a user