Compare commits

...

65 Commits

Author SHA1 Message Date
刘祥超
b0d1f2c8ea 版本号修改为1.3.9 2024-05-20 09:51:19 +08:00
刘祥超
94de773773 修改copyright 2024-05-17 18:30:33 +08:00
刘祥超
da8d7f1437 设置文件句柄最大缓存为65535 2024-05-16 20:40:41 +08:00
刘祥超
777bbdf50b 优化代码 2024-05-11 09:23:54 +08:00
刘祥超
f5596b4f0c 增加edge-node top命令,用来调用系统的top命令并传入edge-node进程ID 2024-05-08 19:42:55 +08:00
刘祥超
480d165538 WAF区域封禁增加“允许搜索引擎”选项 2024-05-08 17:52:20 +08:00
刘祥超
870d18a5ac 搜索引擎识别增加facebook 2024-05-08 17:18:48 +08:00
刘祥超
731f213310 WAF规则集中增加“允许搜索引擎”选项,可以快速允许搜索引擎访问 2024-05-08 16:45:28 +08:00
刘祥超
f1b3a7463d 优化ttlcache相关代码 2024-05-08 11:10:56 +08:00
刘祥超
5020985b96 KV缓存索引增加内存缓存 2024-05-08 10:57:36 +08:00
刘祥超
a0ac33af5a Partial Content新创建时不对比Content-MD5 2024-05-08 10:30:47 +08:00
刘祥超
ffe3f248a9 Partial Content每次写入缓存时都检查Content-MD5是否一致 2024-05-07 20:15:47 +08:00
刘祥超
02dc42d8e0 缓存策略增加“允许读取不完整的Partial Content”选项 2024-05-07 20:06:06 +08:00
刘祥超
b68d92d506 上传IP增加失败重试 2024-05-07 17:55:28 +08:00
刘祥超
1144ca8a8b Partial Content从源站读取数据时验证本地缓存的Content-MD5是否一致 2024-05-07 17:27:10 +08:00
刘祥超
0b216090f1 修复修改后的Partial Content Range范围多1字节的问题 2024-05-07 16:32:53 +08:00
刘祥超
b1e609b2b4 优化一处注释 2024-05-07 16:21:43 +08:00
刘祥超
8de74b62e7 优化Partial Content内容缓存,现在可以使用“部分文件缓存+部分回源”的方式提供内容 2024-05-07 16:20:22 +08:00
刘祥超
02da06dffd WAF增加“请求报头最大长度”参数 2024-05-06 21:06:55 +08:00
刘祥超
bd417a3795 网站全局设置中增加“XFF中最多地址数”选项 2024-05-06 14:05:12 +08:00
刘祥超
42e1e0c3df 再次提升节点默认读写线程数限制 2024-05-05 22:12:28 +08:00
刘祥超
216dacc348 IP名单优化
* 增加IP灰名单,用于仅记录并观察IP
* 优化IP名单同步版本号管理
* WAF记录IP动作优先记录到网站和策略相关的IP名单中
2024-05-05 19:10:46 +08:00
刘祥超
968eac6046 增加默认的并发读写线程数 2024-05-03 18:03:15 +08:00
刘祥超
9a3740dc00 自动安装nftables时尝试使用apt-get 2024-05-03 15:51:08 +08:00
刘祥超
ef1626de53 优化代码 2024-05-03 15:31:40 +08:00
刘祥超
5eb122f0a3 UDP连接也检查IP名单 2024-05-03 15:11:54 +08:00
刘祥超
6e2c5aabc2 自定义页面增加是否“启用系统自定义页面”选项 2024-05-03 11:22:50 +08:00
刘祥超
1a1ae239b3 版本号修改为1.3.8.2 2024-05-03 11:22:16 +08:00
刘祥超
95f42ebd37 如果已经有足够的硬盘写入速度测试数据,则不再执行测试 2024-05-02 11:49:51 +08:00
刘祥超
7111d23f11 edge-node accesslog命令输出增加发送内容字节数、请求来源、请求UserAgent 2024-05-01 19:29:07 +08:00
刘祥超
e42c8452ff 优化并发读写相关代码 2024-05-01 15:53:49 +08:00
刘祥超
fe6e5ba5f9 优化并发读写限制 2024-05-01 12:42:35 +08:00
刘祥超
406d5de482 从内存刷新到磁盘时无需并发写限制 2024-04-30 21:23:44 +08:00
刘祥超
f66e672bb4 读取缓存时总是尝试打开文件,不受并发读的限制 2024-04-30 20:26:34 +08:00
刘祥超
7252d00c8e 版本号修改为1.3.8 2024-04-30 19:09:57 +08:00
刘祥超
7ec4656aea 可以在集群设置中修改节点最大并发读/写数 2024-04-30 19:09:40 +08:00
刘祥超
33156fed2a 延长读写线程超时时间/增加相关测试用例 2024-04-30 17:24:08 +08:00
刘祥超
e8538d4e34 bfs commit 2024-04-30 12:38:43 +08:00
刘祥超
c7d60e01b8 在请求上下文中停用统计的时候也停用对应的指标统计 2024-04-30 00:05:32 +08:00
刘祥超
bbb4b7d31a 版本修改为1.3.7 2024-04-29 23:12:12 +08:00
刘祥超
aa2467c7cf 增加读写线程限制相关测试用例 2024-04-29 23:10:02 +08:00
刘祥超
fa02713ab5 限制读写线程最小值为4,最大值为32 2024-04-29 22:51:51 +08:00
刘祥超
973324ae8f 写入和删除缓存文件时增加线程数限制 2024-04-29 22:36:26 +08:00
刘祥超
7febc6aaf3 读取文件时增加线程数限制 2024-04-29 22:01:55 +08:00
刘祥超
1cede74db3 bfs:修复并发读提示ErrClosed都问题 2024-04-28 19:02:59 +08:00
刘祥超
0dbbc12eb7 bfs:实现对FileHeader中Block数据的compaction 2024-04-28 15:38:13 +08:00
刘祥超
b272de2122 bfs:对FileHeader的压缩和解压使用Pool管理 2024-04-28 10:06:29 +08:00
刘祥超
962cbde4e9 bfs:弹出BFile的时候确保没有正在被使用 2024-04-27 20:55:04 +08:00
刘祥超
6226e31cdc bfs:实现FileHeader的lazy load 2024-04-27 20:11:50 +08:00
刘祥超
fb8c78553a bfs:同一个Hash同时只能有一个Writer,避免多线程读冲突 2024-04-27 18:27:49 +08:00
刘祥超
801f5d4525 bfs:实现maxOpenFiles 2024-04-27 17:29:12 +08:00
刘祥超
04007bf8f1 bfs: 增加读写线程限制 2024-04-27 07:09:14 +08:00
刘祥超
8c044faace bfs: bfs.FS增加锁 (experimental) 2024-04-26 19:26:22 +08:00
刘祥超
f262e76f96 bfs commit (exprimental) 2024-04-26 18:44:29 +08:00
刘祥超
ef057b106e 初步实现bfs原型(仅用于实验) 2024-04-26 17:16:32 +08:00
刘祥超
508f8cbae0 调整KV相关选项尺寸 2024-04-26 17:15:55 +08:00
刘祥超
d8e4ff3d01 增强KV字节编码安全性 2024-04-24 14:33:15 +08:00
刘祥超
f247e57e76 优化KV相关日志 2024-04-24 09:03:15 +08:00
刘祥超
3581ce3763 优化KV相关错误提示/可以从路径直接加载数据库 2024-04-23 11:56:00 +08:00
刘祥超
b66c70de66 ${requestMethod}变量增加默认值GET 2024-04-22 21:32:53 +08:00
刘祥超
d3a4f14999 版本号修改为1.3.6.1 2024-04-22 18:46:29 +08:00
刘祥超
00a7324640 增加缓存索引相关测试用例 2024-04-22 18:46:21 +08:00
刘祥超
d8ea1809dc 优化代码 2024-04-22 12:38:18 +08:00
刘祥超
7f5088958e 版本号修改为1.3.6 2024-04-22 10:51:19 +08:00
刘祥超
eff928d988 修复非80/443端口回源跟随无法正确识别地址的问题 2024-04-22 10:51:11 +08:00
425 changed files with 5679 additions and 908 deletions

View File

@@ -9,6 +9,7 @@ import (
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
"github.com/TeaOSLab/EdgeNode/internal/nodes"
"github.com/TeaOSLab/EdgeNode/internal/utils"
executils "github.com/TeaOSLab/EdgeNode/internal/utils/exec"
fsutils "github.com/TeaOSLab/EdgeNode/internal/utils/fs"
"github.com/iwind/TeaGo/Tea"
_ "github.com/iwind/TeaGo/bootstrap"
@@ -22,8 +23,10 @@ import (
_ "net/http/pprof"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
"syscall"
"time"
)
@@ -31,7 +34,7 @@ func main() {
var app = apps.NewAppCmd().
Version(teaconst.Version).
Product(teaconst.ProductName).
Usage(teaconst.ProcessName + " [-v|start|stop|restart|status|quit|test|reload|service|daemon|config|pprof|accesslog|uninstall]").
Usage(teaconst.ProcessName + " [-v|start|stop|restart|status|quit|test|reload|service|daemon|config|pprof|top|accesslog|uninstall]").
Usage(teaconst.ProcessName + " [trackers|goman|conns|gc|bandwidth|disk|cache.garbage]").
Usage(teaconst.ProcessName + " [ip.drop|ip.reject|ip.remove|ip.close] IP")
@@ -552,7 +555,7 @@ func main() {
return
}
err = os.WriteFile(Tea.Root + "/configs/api_node.yaml", configYAML, 0666)
err = os.WriteFile(Tea.Root+"/configs/api_node.yaml", configYAML, 0666)
if err != nil {
fmt.Println("[ERROR]write config failed: " + err.Error())
return
@@ -560,6 +563,37 @@ func main() {
fmt.Println("success")
})
app.On("top", func() {
var sock = gosock.NewTmpSock(teaconst.ProcessName)
reply, err := sock.Send(&gosock.Command{Code: "pid"})
if err != nil {
fmt.Println("[ERROR]not started yet")
return
}
var pid = maps.NewMap(reply.Params).GetInt("pid")
if pid <= 0 {
fmt.Println("[ERROR]invalid pid '" + types.String(pid) + "'")
return
}
topExe, _ := executils.LookPath("top")
if len(topExe) > 0 {
if runtime.GOOS == "linux" {
err = syscall.Exec(topExe, []string{topExe, "-p", types.String(pid)}, os.Environ())
} else if runtime.GOOS == "darwin" {
err = syscall.Exec(topExe, []string{topExe, "-pid", types.String(pid)}, os.Environ())
} else {
fmt.Println("[ERROR]not supported os '" + runtime.GOOS + "'")
return
}
if err != nil {
fmt.Println("[ERROR]start failed: " + err.Error())
}
} else {
fmt.Println("[ERROR]could not found 'top' command in this system")
}
})
app.Run(func() {
var node = nodes.NewNode()
node.Start()

View File

@@ -1,8 +1,8 @@
package apps
import (
"github.com/TeaOSLab/EdgeNode/internal/goman"
"github.com/TeaOSLab/EdgeNode/internal/utils"
"github.com/TeaOSLab/EdgeNode/internal/utils/goman"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/files"
timeutil "github.com/iwind/TeaGo/utils/time"

View File

@@ -1,4 +1,4 @@
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
// Copyright 2023 GoEdge goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
package apps

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
package caches

View File

@@ -1,4 +1,4 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2021 GoEdge goedge.cdn@gmail.com. All rights reserved.
package caches
@@ -55,3 +55,7 @@ func IsCapacityError(err error) bool {
var capacityErr *CapacityError
return errors.As(err, &capacityErr)
}
func IsBusyError(err error) bool {
return err != nil && errors.Is(err, ErrServerIsBusy)
}

View File

@@ -1,4 +1,4 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2021 GoEdge goedge.cdn@gmail.com. All rights reserved.
package caches_test

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
package caches

View File

@@ -1,4 +1,4 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2021 GoEdge goedge.cdn@gmail.com. All rights reserved.
package caches

View File

@@ -1,4 +1,4 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2021 GoEdge goedge.cdn@gmail.com. All rights reserved.
package caches_test
@@ -7,7 +7,7 @@ import (
"github.com/TeaOSLab/EdgeNode/internal/caches"
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
"github.com/TeaOSLab/EdgeNode/internal/utils/testutils"
"github.com/TeaOSLab/EdgeNode/internal/zero"
"github.com/TeaOSLab/EdgeNode/internal/utils/zero"
"github.com/iwind/TeaGo/rands"
"github.com/iwind/TeaGo/types"
"runtime"

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
package caches
@@ -9,6 +9,7 @@ import (
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
"github.com/TeaOSLab/EdgeNode/internal/utils/dbs"
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
fsutils "github.com/TeaOSLab/EdgeNode/internal/utils/fs"
memutils "github.com/TeaOSLab/EdgeNode/internal/utils/mem"
"github.com/iwind/TeaGo/logs"
"github.com/iwind/TeaGo/types"
@@ -593,9 +594,9 @@ func (this *SQLiteFileListDB) shouldRecover() bool {
// 删除数据库文件
func (this *SQLiteFileListDB) deleteDB() {
_ = os.Remove(this.dbPath)
_ = os.Remove(this.dbPath + "-shm")
_ = os.Remove(this.dbPath + "-wal")
_ = fsutils.Remove(this.dbPath)
_ = fsutils.Remove(this.dbPath + "-shm")
_ = fsutils.Remove(this.dbPath + "-wal")
}
// 加载Hash列表

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
package caches_test

View File

@@ -1,10 +1,10 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
package caches
import (
memutils "github.com/TeaOSLab/EdgeNode/internal/utils/mem"
"github.com/TeaOSLab/EdgeNode/internal/zero"
"github.com/TeaOSLab/EdgeNode/internal/utils/zero"
"math/big"
"sync"
)

View File

@@ -1,11 +1,11 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
package caches_test
import (
"github.com/TeaOSLab/EdgeNode/internal/caches"
"github.com/TeaOSLab/EdgeNode/internal/utils/testutils"
"github.com/TeaOSLab/EdgeNode/internal/zero"
"github.com/TeaOSLab/EdgeNode/internal/utils/zero"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/assert"
"github.com/iwind/TeaGo/rands"

View File

@@ -4,9 +4,12 @@ package caches
import (
"fmt"
"github.com/TeaOSLab/EdgeNode/internal/goman"
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
"github.com/TeaOSLab/EdgeNode/internal/utils/fnv"
"github.com/TeaOSLab/EdgeNode/internal/utils/goman"
memutils "github.com/TeaOSLab/EdgeNode/internal/utils/mem"
"github.com/TeaOSLab/EdgeNode/internal/utils/ttlcache"
"github.com/iwind/TeaGo/types"
"strings"
"testing"
@@ -20,19 +23,31 @@ type KVFileList struct {
onAdd func(item *Item)
onRemove func(item *Item)
memCache *ttlcache.Cache[int64]
}
func NewKVFileList(dir string) *KVFileList {
var memGB = memutils.SystemMemoryGB()
if memGB <= 0 {
memGB = 1
}
var maxCachePieces = 32
var maxCacheItems = memGB << 15
var memCache = ttlcache.NewCache[int64](ttlcache.NewPiecesOption(maxCachePieces), ttlcache.NewMaxItemsOption(maxCacheItems))
dir = strings.TrimSuffix(dir, "/")
var stores = [countKVStores]*KVListFileStore{}
for i := 0; i < countKVStores; i++ {
stores[i] = NewKVListFileStore(dir + "/db-" + types.String(i) + ".store")
stores[i] = NewKVListFileStore(dir+"/db-"+types.String(i)+".store", memCache)
}
return &KVFileList{
dir: dir,
stores: stores,
dir: dir,
stores: stores,
memCache: memCache,
}
}
@@ -59,7 +74,7 @@ func (this *KVFileList) Init() error {
// Reset 重置数据
func (this *KVFileList) Reset() error {
// do nothing
this.memCache.Clean()
return nil
}
@@ -74,16 +89,31 @@ func (this *KVFileList) Add(hash string, item *Item) error {
this.onAdd(item)
}
if item.ExpiresAt > 0 {
this.memCache.Write(hash, item.HeaderSize+item.BodySize, min(item.ExpiresAt, fasttime.Now().Unix()+3600))
}
return nil
}
// Exist 检查内容是否存在
func (this *KVFileList) Exist(hash string) (bool, int64, error) {
// read from cache
var cacheItem = this.memCache.Read(hash)
if cacheItem != nil {
return true, cacheItem.Value, nil
}
return this.getStore(hash).ExistItem(hash)
}
// ExistQuick 快速检查内容是否存在
func (this *KVFileList) ExistQuick(hash string) (bool, error) {
// read from cache
if this.memCache.Read(hash) != nil {
return true, nil
}
return this.getStore(hash).ExistQuickItem(hash)
}
@@ -150,6 +180,9 @@ func (this *KVFileList) Remove(hash string) error {
this.onRemove(nil)
}
// remove from cache
this.memCache.Delete(hash)
return nil
}
@@ -204,6 +237,9 @@ func (this *KVFileList) CleanAll() error {
})
}
group.Wait()
this.memCache.Clean()
return lastErr
}
@@ -287,6 +323,9 @@ func (this *KVFileList) Close() error {
})
}
group.Wait()
this.memCache.Destroy()
return lastErr
}

View File

@@ -6,6 +6,7 @@ import (
"errors"
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
"github.com/TeaOSLab/EdgeNode/internal/utils/kvstore"
"github.com/TeaOSLab/EdgeNode/internal/utils/ttlcache"
"github.com/cockroachdb/pebble"
"regexp"
"strings"
@@ -20,11 +21,14 @@ type KVListFileStore struct {
itemsTable *kvstore.Table[*Item]
rawIsReady bool
memCache *ttlcache.Cache[int64]
}
func NewKVListFileStore(path string) *KVListFileStore {
func NewKVListFileStore(path string, memCache *ttlcache.Cache[int64]) *KVListFileStore {
return &KVListFileStore{
path: path,
path: path,
memCache: memCache,
}
}
@@ -106,7 +110,14 @@ func (this *KVListFileStore) ExistItem(hash string) (bool, int64, error) {
return false, -1, nil
}
return item.ExpiresAt > fasttime.Now().Unix(), item.HeaderSize + item.BodySize, nil
if item.ExpiresAt <= fasttime.Now().Unix() {
return false, 0, nil
}
// write to cache
this.memCache.Write(hash, item.HeaderSize+item.BodySize, min(item.ExpiresAt, fasttime.Now().Unix()+3600))
return true, item.HeaderSize + item.BodySize, nil
}
func (this *KVListFileStore) ExistQuickItem(hash string) (bool, error) {
@@ -168,6 +179,7 @@ func (this *KVListFileStore) PurgeItems(count int, callback func(hash string) er
if deleteErr != nil {
return deleteErr
}
this.memCache.Delete(hash)
}
return nil
})
@@ -214,6 +226,7 @@ func (this *KVListFileStore) PurgeLFUItems(count int, callback func(hash string)
if deleteErr != nil {
return deleteErr
}
this.memCache.Delete(hash)
}
return nil
})
@@ -276,6 +289,9 @@ func (this *KVListFileStore) CleanItemsWithPrefix(prefix string) error {
return false, setErr
}
// remove from cache
this.memCache.Delete(item.Key)
return true, nil
})
if err != nil {
@@ -334,6 +350,9 @@ func (this *KVListFileStore) CleanItemsWithWildcardPrefix(prefix string) error {
return false, setErr
}
// remove from cache
this.memCache.Delete(item.Key)
return true, nil
})
if err != nil {
@@ -402,6 +421,9 @@ func (this *KVListFileStore) CleanItemsWithWildcardKey(key string) error {
return false, setErr
}
// remove from cache
this.memCache.Delete(item.Key)
return true, nil
})
if err != nil {

View File

@@ -91,7 +91,7 @@ func TestKVFileList_Add_Many(t *testing.T) {
err := list.Add(stringutil.Md5(strconv.Itoa(i)), &caches.Item{
Type: caches.ItemTypeFile,
Key: "https://www.example.com/index.html" + strconv.Itoa(i),
ExpiresAt: time.Now().Unix() + 60,
ExpiresAt: time.Now().Unix() + 3600,
StaleAt: 0,
HeaderSize: 0,
BodySize: int64(rand.Int() % 1_000_000),
@@ -176,6 +176,32 @@ func TestKVFileList_Exist(t *testing.T) {
}
}
func TestKVFileList_ExistMany(t *testing.T) {
var list = testOpenKVFileList(t)
defer func() {
_ = list.Close()
}()
var countFound int
var count = 10
if testutils.IsSingleTesting() {
count = 2_000_000
}
var before = time.Now()
for i := 0; i < count; i++ {
ok, _, err := list.Exist(stringutil.Md5(strconv.Itoa(i)))
if err != nil {
t.Fatal(err)
}
if ok {
countFound++
}
}
var costSeconds = time.Since(before).Seconds()
t.Log("total:", costSeconds*1000, "ms", "found:", countFound, "qps:", fmt.Sprintf("%.2fK/s", float64(count)/costSeconds/1000), "per read:", fmt.Sprintf("%.4fms", costSeconds*1000/float64(count)))
}
func TestKVFileList_ExistQuick(t *testing.T) {
var list = testOpenKVFileList(t)
defer func() {
@@ -211,6 +237,28 @@ func TestKVFileList_Remove(t *testing.T) {
}
}
func TestKVFileList_RemoveMany(t *testing.T) {
var list = testOpenKVFileList(t)
defer func() {
_ = list.Close()
}()
var count = 10
if testutils.IsSingleTesting() {
count = 2_000_000
}
var before = time.Now()
for i := 0; i < count; i++ {
err := list.Remove(stringutil.Md5(strconv.Itoa(i)))
if err != nil {
t.Fatal(err)
}
}
var costSeconds = time.Since(before).Seconds()
t.Log("total:", costSeconds*1000, "ms", "qps:", fmt.Sprintf("%.2fK/s", float64(count)/costSeconds/1000), "per delete:", fmt.Sprintf("%.4fms", costSeconds*1000/float64(count)))
}
func TestKVFileList_CleanAll(t *testing.T) {
var list = testOpenKVFileList(t)
defer func() {

View File

@@ -1,17 +1,18 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2021 GoEdge goedge.cdn@gmail.com. All rights reserved.
package caches
import (
"database/sql"
"errors"
"github.com/TeaOSLab/EdgeNode/internal/goman"
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
"github.com/TeaOSLab/EdgeNode/internal/ttlcache"
"github.com/TeaOSLab/EdgeNode/internal/utils/dbs"
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
"github.com/TeaOSLab/EdgeNode/internal/utils/fnv"
"github.com/TeaOSLab/EdgeNode/internal/zero"
fsutils "github.com/TeaOSLab/EdgeNode/internal/utils/fs"
"github.com/TeaOSLab/EdgeNode/internal/utils/goman"
"github.com/TeaOSLab/EdgeNode/internal/utils/ttlcache"
"github.com/TeaOSLab/EdgeNode/internal/utils/zero"
"github.com/iwind/TeaGo/types"
"os"
"strings"
@@ -486,7 +487,7 @@ func (this *SQLiteFileList) UpgradeV3(oldDir string, brokenOnError bool) error {
remotelogs.Println("CACHE", "upgrading local database from '"+oldDir+"' ...")
defer func() {
_ = os.Remove(indexDBPath)
_ = fsutils.Remove(indexDBPath)
remotelogs.Println("CACHE", "upgrading local database finished")
}()

View File

@@ -1,10 +1,10 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2021 GoEdge goedge.cdn@gmail.com. All rights reserved.
package caches_test
import (
"github.com/TeaOSLab/EdgeNode/internal/caches"
"github.com/TeaOSLab/EdgeNode/internal/goman"
"github.com/TeaOSLab/EdgeNode/internal/utils/goman"
"github.com/TeaOSLab/EdgeNode/internal/utils/testutils"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/rands"

View File

@@ -1,4 +1,4 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2021 GoEdge goedge.cdn@gmail.com. All rights reserved.
package caches

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
package caches

View File

@@ -1,10 +1,10 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
package caches
import (
"fmt"
"github.com/TeaOSLab/EdgeNode/internal/goman"
"github.com/TeaOSLab/EdgeNode/internal/utils/goman"
"github.com/TeaOSLab/EdgeNode/internal/utils/linkedlist"
memutils "github.com/TeaOSLab/EdgeNode/internal/utils/mem"
"github.com/fsnotify/fsnotify"
@@ -38,6 +38,9 @@ func NewOpenFileCache(maxCount int) (*OpenFileCache, error) {
if maxCount <= 0 {
maxCount = 16384
}
if maxCount > 65535 {
maxCount = 65535
}
var cache = &OpenFileCache{
maxCount: maxCount,

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
package caches_test

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
package caches

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
package caches_test

View File

@@ -1,26 +1,28 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
package caches
import (
"bytes"
"encoding/json"
rangeutils "github.com/TeaOSLab/EdgeNode/internal/utils/ranges"
"github.com/iwind/TeaGo/types"
"strconv"
)
// PartialRanges 内容分区范围定义
type PartialRanges struct {
Version int `json:"version"` // 版本号
Ranges [][2]int64 `json:"ranges"` // 范围
BodySize int64 `json:"bodySize"` // 总长度
Version int `json:"version"` // 版本号
Ranges [][2]int64 `json:"ranges"` // 范围
BodySize int64 `json:"bodySize"` // 总长度
ContentMD5 string `json:"contentMD5"` // 内容md5
}
// NewPartialRanges 获取新对象
func NewPartialRanges(expiresAt int64) *PartialRanges {
return &PartialRanges{
Ranges: [][2]int64{},
Version: 1,
Version: 2,
}
}
@@ -45,6 +47,8 @@ func NewPartialRangesFromData(data []byte) (*PartialRanges, error) {
if commaIndex > 0 {
rs.Ranges = append(rs.Ranges, [2]int64{types.Int64(line[colonIndex+1 : commaIndex]), types.Int64(line[commaIndex+1:])})
}
case "m": // Content-MD5
rs.ContentMD5 = string(line[colonIndex+1:])
}
}
data = data[index+1:]
@@ -101,7 +105,6 @@ func (this *PartialRanges) Add(begin int64, end int64) {
}
// insert
// TODO 将来使用二分法改进
var index = -1
for i, r := range this.Ranges {
if r[0] > begin || (r[0] == begin && r[1] >= end) {
@@ -127,7 +130,6 @@ func (this *PartialRanges) Contains(begin int64, end int64) bool {
return false
}
// TODO 使用二分法查找改进性能
for _, r2 := range this.Ranges {
if r2[0] <= begin && r2[1] >= end {
return true
@@ -143,7 +145,6 @@ func (this *PartialRanges) Nearest(begin int64, end int64) (r [2]int64, ok bool)
return
}
// TODO 使用二分法查找改进性能
for _, r2 := range this.Ranges {
if r2[0] <= begin && r2[1] > begin {
r = [2]int64{begin, this.min(end, r2[1])}
@@ -154,10 +155,28 @@ func (this *PartialRanges) Nearest(begin int64, end int64) (r [2]int64, ok bool)
return
}
// FindRangeAtPosition 查找在某个位置上的范围
func (this *PartialRanges) FindRangeAtPosition(position int64) (r rangeutils.Range, ok bool) {
if len(this.Ranges) == 0 || position < 0 {
return
}
for _, r2 := range this.Ranges {
if r2[0] <= position && r2[1] > position {
return [2]int64{position, r2[1]}, true
}
}
return
}
// 转换为字符串
func (this *PartialRanges) String() string {
var s = "v:" + strconv.Itoa(this.Version) + "\n" + // version
"b:" + this.formatInt64(this.BodySize) + "\n" // bodySize
if len(this.ContentMD5) > 0 {
s += "m:" + this.ContentMD5 + "\n" // Content-MD5
}
for _, r := range this.Ranges {
s += "r:" + this.formatInt64(r[0]) + "," + this.formatInt64(r[1]) + "\n" // range
}

View File

@@ -4,11 +4,11 @@ package caches
import (
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
"github.com/TeaOSLab/EdgeNode/internal/goman"
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
"github.com/TeaOSLab/EdgeNode/internal/utils/fnv"
fsutils "github.com/TeaOSLab/EdgeNode/internal/utils/fs"
"github.com/TeaOSLab/EdgeNode/internal/utils/goman"
memutils "github.com/TeaOSLab/EdgeNode/internal/utils/mem"
"os"
"sync"
)
@@ -91,7 +91,7 @@ func (this *PartialRangesQueue) Get(filename string) ([]byte, error) {
return data, nil
}
return os.ReadFile(filename)
return fsutils.ReadFile(filename)
}
// Delete ranges filename
@@ -119,7 +119,7 @@ func (this *PartialRangesQueue) Dump() {
continue
}
err := os.WriteFile(filename, data, 0666)
err := fsutils.WriteFile(filename, data, 0666)
if err != nil {
remotelogs.Println("PARTIAL_RANGES_QUEUE", "write file '"+filename+"' failed: "+err.Error())
}

View File

@@ -1,8 +1,10 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
package caches_test
import (
"crypto/md5"
"encoding/base64"
"encoding/json"
"github.com/TeaOSLab/EdgeNode/internal/caches"
"github.com/iwind/TeaGo/assert"
@@ -167,8 +169,13 @@ func TestPartialRanges_Encode_String(t *testing.T) {
for i := 0; i < 10; i++ {
r.Ranges = append(r.Ranges, [2]int64{int64(i * 100), int64(i*100 + 100)})
}
var sum = md5.Sum([]byte("123456"))
r.ContentMD5 = base64.StdEncoding.EncodeToString(sum[:])
var before = time.Now()
var data = r.String()
t.Log(data)
t.Log(time.Since(before).Seconds()*1000, "ms")
t.Log(len(data))

View File

@@ -1,6 +1,9 @@
package caches
import "github.com/TeaOSLab/EdgeNode/internal/utils/ranges"
import (
"github.com/TeaOSLab/EdgeNode/internal/utils/ranges"
"io"
)
type ReaderFunc func(n int) (goNext bool, err error)
@@ -41,6 +44,9 @@ type Reader interface {
// ContainsRange 是否包含某个区间内容
ContainsRange(r rangeutils.Range) (r2 rangeutils.Range, ok bool)
// SetNextReader 设置下一个内容Reader
SetNextReader(nextReader io.ReadCloser)
// Close 关闭
Close() error
}

View File

@@ -0,0 +1,14 @@
// Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
package caches
import "io"
type BaseReader struct {
nextReader io.ReadCloser
}
// SetNextReader 设置下一个内容Reader
func (this *BaseReader) SetNextReader(nextReader io.ReadCloser) {
this.nextReader = nextReader
}

View File

@@ -3,6 +3,7 @@ package caches
import (
"encoding/binary"
"errors"
fsutils "github.com/TeaOSLab/EdgeNode/internal/utils/fs"
rangeutils "github.com/TeaOSLab/EdgeNode/internal/utils/ranges"
"github.com/iwind/TeaGo/types"
"io"
@@ -10,7 +11,9 @@ import (
)
type FileReader struct {
fp *os.File
BaseReader
fp *fsutils.File
openFile *OpenFile
openFileCache *OpenFileCache
@@ -28,7 +31,7 @@ type FileReader struct {
isClosed bool
}
func NewFileReader(fp *os.File) *FileReader {
func NewFileReader(fp *fsutils.File) *FileReader {
return &FileReader{fp: fp}
}
@@ -305,7 +308,8 @@ func (this *FileReader) ReadBodyRange(buf []byte, start int64, end int64, callba
}
for {
n, err := this.fp.Read(buf)
var n int
n, err = this.fp.Read(buf)
if n > 0 {
var n2 = int(end-offset) + 1
if n2 <= n {
@@ -341,6 +345,33 @@ func (this *FileReader) ReadBodyRange(buf []byte, start int64, end int64, callba
isOk = true
// 读取下一个Reader
if this.nextReader != nil {
defer func() {
_ = this.nextReader.Close()
}()
for {
var n int
n, err = this.nextReader.Read(buf)
if n > 0 {
goNext, writeErr := callback(n)
if writeErr != nil {
return writeErr
}
if !goNext {
break
}
}
if err != nil {
if err != io.EOF {
return err
}
break
}
}
}
return nil
}
@@ -351,7 +382,7 @@ func (this *FileReader) ContainsRange(r rangeutils.Range) (r2 rangeutils.Range,
// FP 原始的文件句柄
func (this *FileReader) FP() *os.File {
return this.fp
return this.fp.Raw()
}
func (this *FileReader) Close() error {
@@ -366,7 +397,7 @@ func (this *FileReader) Close() error {
} else {
var cacheMeta = make([]byte, len(this.meta))
copy(cacheMeta, this.meta)
this.openFileCache.Put(this.fp.Name(), NewOpenFile(this.fp, cacheMeta, this.header, this.LastModified(), this.bodySize))
this.openFileCache.Put(this.fp.Name(), NewOpenFile(this.fp.Raw(), cacheMeta, this.header, this.LastModified(), this.bodySize))
}
return nil
}
@@ -374,7 +405,7 @@ func (this *FileReader) Close() error {
return this.fp.Close()
}
func (this *FileReader) readToBuff(fp *os.File, buf []byte) (ok bool, err error) {
func (this *FileReader) readToBuff(fp *fsutils.File, buf []byte) (ok bool, err error) {
n, err := fp.Read(buf)
if err != nil {
return false, err
@@ -393,5 +424,5 @@ func (this *FileReader) discard() error {
}
// remove file
return os.Remove(this.fp.Name())
return fsutils.Remove(this.fp.Name())
}

View File

@@ -2,6 +2,7 @@ package caches
import (
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
fsutils "github.com/TeaOSLab/EdgeNode/internal/utils/fs"
"github.com/iwind/TeaGo/Tea"
"os"
"testing"
@@ -36,7 +37,7 @@ func TestFileReader(t *testing.T) {
defer func() {
_ = fp.Close()
}()
reader := NewFileReader(fp)
reader := NewFileReader(fsutils.NewFile(fp, fsutils.FlagRead))
err = reader.Init()
if err != nil {
t.Fatal(err)
@@ -75,7 +76,7 @@ func TestFileReader_ReadHeader(t *testing.T) {
defer func() {
_ = fp.Close()
}()
var reader = NewFileReader(fp)
var reader = NewFileReader(fsutils.NewFile(fp, fsutils.FlagRead))
err = reader.Init()
if err != nil {
if os.IsNotExist(err) {
@@ -138,7 +139,7 @@ func TestFileReader_Range(t *testing.T) {
defer func() {
_ = fp.Close()
}()
reader := NewFileReader(fp)
reader := NewFileReader(fsutils.NewFile(fp, fsutils.FlagRead))
err = reader.Init()
if err != nil {
t.Fatal(err)

View File

@@ -7,6 +7,8 @@ import (
)
type MemoryReader struct {
BaseReader
item *MemoryItem
offset int

View File

@@ -4,10 +4,10 @@ import (
"encoding/binary"
"errors"
"fmt"
fsutils "github.com/TeaOSLab/EdgeNode/internal/utils/fs"
rangeutils "github.com/TeaOSLab/EdgeNode/internal/utils/ranges"
"github.com/iwind/TeaGo/types"
"io"
"os"
)
type PartialFileReader struct {
@@ -17,7 +17,7 @@ type PartialFileReader struct {
rangePath string
}
func NewPartialFileReader(fp *os.File) *PartialFileReader {
func NewPartialFileReader(fp *fsutils.File) *PartialFileReader {
return &PartialFileReader{
FileReader: NewFileReader(fp),
rangePath: PartialRangesFilePath(fp.Name()),
@@ -146,7 +146,7 @@ func (this *PartialFileReader) IsCompleted() bool {
func (this *PartialFileReader) discard() error {
SharedPartialRangesQueue.Delete(this.rangePath)
_ = os.Remove(this.rangePath)
_ = fsutils.Remove(this.rangePath)
return this.FileReader.discard()
}

View File

@@ -10,15 +10,16 @@ import (
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
"github.com/TeaOSLab/EdgeNode/internal/events"
"github.com/TeaOSLab/EdgeNode/internal/goman"
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
"github.com/TeaOSLab/EdgeNode/internal/trackers"
"github.com/TeaOSLab/EdgeNode/internal/utils"
"github.com/TeaOSLab/EdgeNode/internal/utils/bytepool"
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
fsutils "github.com/TeaOSLab/EdgeNode/internal/utils/fs"
"github.com/TeaOSLab/EdgeNode/internal/utils/goman"
memutils "github.com/TeaOSLab/EdgeNode/internal/utils/mem"
setutils "github.com/TeaOSLab/EdgeNode/internal/utils/sets"
"github.com/TeaOSLab/EdgeNode/internal/zero"
"github.com/TeaOSLab/EdgeNode/internal/utils/trackers"
"github.com/TeaOSLab/EdgeNode/internal/utils/zero"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/rands"
"github.com/iwind/TeaGo/types"
@@ -380,6 +381,7 @@ func (this *FileStorage) openReader(key string, allowMemory bool, useStale bool,
// 检查文件记录是否已过期
var estimatedSize int64
var existInList bool
if !useStale {
exists, filesize, err := this.list.Exist(hash)
if err != nil {
@@ -389,6 +391,7 @@ func (this *FileStorage) openReader(key string, allowMemory bool, useStale bool,
return nil, ErrNotFound
}
estimatedSize = filesize
existInList = true
}
// 尝试通过MMAP读取
@@ -412,7 +415,13 @@ func (this *FileStorage) openReader(key string, allowMemory bool, useStale bool,
var err error
if openFile == nil {
if existInList {
fsutils.ReaderLimiter.Ack()
}
fp, err = os.OpenFile(path, os.O_RDONLY, 0444)
if existInList {
fsutils.ReaderLimiter.Release()
}
if err != nil {
if !os.IsNotExist(err) {
return nil, err
@@ -431,12 +440,12 @@ func (this *FileStorage) openReader(key string, allowMemory bool, useStale bool,
var reader Reader
if isPartial {
var partialFileReader = NewPartialFileReader(fp)
var partialFileReader = NewPartialFileReader(fsutils.NewFile(fp, fsutils.FlagRead))
partialFileReader.openFile = openFile
partialFileReader.openFileCache = openFileCache
reader = partialFileReader
} else {
var fileReader = NewFileReader(fp)
var fileReader = NewFileReader(fsutils.NewFile(fp, fsutils.FlagRead))
fileReader.openFile = openFile
fileReader.openFileCache = openFileCache
reader = fileReader
@@ -517,11 +526,6 @@ func (this *FileStorage) openWriter(key string, expiredAt int64, status int, hea
return nil, fmt.Errorf("%w(001)", ErrFileIsWriting)
}
if !isFlushing && !fsutils.WriteReady() {
sharedWritingFileKeyLocker.Unlock()
return nil, ErrServerIsBusy
}
sharedWritingFileKeyMap[key] = zero.New()
sharedWritingFileKeyLocker.Unlock()
defer func() {
@@ -588,9 +592,9 @@ func (this *FileStorage) openWriter(key string, expiredAt int64, status int, hea
// 数据库中是否存在
existsCacheItem, _, _ := this.list.Exist(hash)
if existsCacheItem {
readerFp, err := os.OpenFile(tmpPath, os.O_RDONLY, 0444)
readerFp, err := fsutils.OpenFile(tmpPath, os.O_RDONLY, 0444)
if err == nil {
var partialReader = NewPartialFileReader(readerFp)
var partialReader = NewPartialFileReader(fsutils.NewFile(readerFp, fsutils.FlagRead))
err = partialReader.Init()
_ = partialReader.Close()
if err == nil && partialReader.bodyOffset > 0 {
@@ -621,21 +625,31 @@ func (this *FileStorage) openWriter(key string, expiredAt int64, status int, hea
if isNewCreated && existsFile {
flags |= os.O_TRUNC
}
fsutils.WriteBegin()
writer, err := os.OpenFile(tmpPath, flags, 0666)
fsutils.WriteEnd()
if !isFlushing {
if !fsutils.WriterLimiter.TryAck() {
return nil, ErrServerIsBusy
}
}
fp, err := os.OpenFile(tmpPath, flags, 0666)
if !isFlushing {
fsutils.WriterLimiter.Release()
}
if err != nil {
if os.IsNotExist(err) {
_ = os.MkdirAll(dir, 0777)
// open file again
writer, err = os.OpenFile(tmpPath, flags, 0666)
fsutils.WriterLimiter.Ack()
fp, err = os.OpenFile(tmpPath, flags, 0666)
fsutils.WriterLimiter.Release()
}
if err != nil {
return nil, err
}
}
var writer = fsutils.NewFile(fp, fsutils.FlagWrite)
var removeOnFailure = true
defer func() {
if err != nil {
@@ -646,13 +660,15 @@ func (this *FileStorage) openWriter(key string, expiredAt int64, status int, hea
if !isOk {
_ = writer.Close()
if removeOnFailure {
_ = os.Remove(tmpPath)
_ = fsutils.Remove(tmpPath)
}
}
}()
// 尝试锁定,如果锁定失败,则直接返回
fsutils.WriterLimiter.Ack()
err = syscall.Flock(int(writer.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
fsutils.WriterLimiter.Release()
if err != nil {
removeOnFailure = false
return nil, fmt.Errorf("%w (003)", ErrFileIsWriting)
@@ -689,9 +705,7 @@ func (this *FileStorage) openWriter(key string, expiredAt int64, status int, hea
metaBodySize = bodySize
}
fsutils.WriteBegin()
_, err = writer.Write(metaBytes)
fsutils.WriteEnd()
if err != nil {
return nil, err
}
@@ -990,6 +1004,11 @@ func (this *FileStorage) CanSendfile() bool {
return this.options.EnableSendfile
}
// Options 获取当前缓存存储的选项
func (this *FileStorage) Options() *serverconfigs.HTTPFileCacheStorage {
return this.options
}
// 获取Key对应的文件路径
func (this *FileStorage) keyPath(key string) (hash string, path string, diskIsFull bool) {
hash = stringutil.Md5(key)
@@ -1144,9 +1163,7 @@ func (this *FileStorage) purgeLoop() {
for i := 0; i < times; i++ {
countFound, err := this.list.Purge(purgeCount, func(hash string) error {
path, _ := this.hashPath(hash)
fsutils.WriteBegin()
err := this.removeCacheFile(path)
fsutils.WriteEnd()
if err != nil && !os.IsNotExist(err) {
remotelogs.Error("CACHE", "purge '"+path+"' error: "+err.Error())
}
@@ -1203,9 +1220,7 @@ func (this *FileStorage) purgeLoop() {
var before = time.Now()
err := this.list.PurgeLFU(count, func(hash string) error {
path, _ := this.hashPath(hash)
fsutils.WriteBegin()
err := this.removeCacheFile(path)
fsutils.WriteEnd()
if err != nil && !os.IsNotExist(err) {
remotelogs.Error("CACHE", "purge '"+path+"' error: "+err.Error())
}
@@ -1276,9 +1291,9 @@ func (this *FileStorage) hotLoop() {
size = len(result) / 10
}
var buf = utils.BytePool16k.Get()
var buf = bytepool.Pool16k.Get()
defer utils.BytePool16k.Put(buf)
defer bytepool.Pool16k.Put(buf)
for _, item := range result[:size] {
reader, err := this.openReader(item.Key, false, false, false)
if err != nil {
@@ -1473,7 +1488,7 @@ func (this *FileStorage) removeCacheFile(path string) error {
openFileCache.Close(path)
}
var err = os.Remove(path)
var err = fsutils.Remove(path)
if err == nil || os.IsNotExist(err) {
err = nil
@@ -1485,7 +1500,7 @@ func (this *FileStorage) removeCacheFile(path string) error {
_, statErr := os.Stat(partialPath)
if statErr == nil {
_ = os.Remove(partialPath)
_ = fsutils.Remove(partialPath)
SharedPartialRangesQueue.Delete(partialPath)
}
}

View File

@@ -3,15 +3,15 @@ package caches
import (
"fmt"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeNode/internal/goman"
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
"github.com/TeaOSLab/EdgeNode/internal/trackers"
"github.com/TeaOSLab/EdgeNode/internal/utils"
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
fsutils "github.com/TeaOSLab/EdgeNode/internal/utils/fs"
"github.com/TeaOSLab/EdgeNode/internal/utils/goman"
memutils "github.com/TeaOSLab/EdgeNode/internal/utils/mem"
setutils "github.com/TeaOSLab/EdgeNode/internal/utils/sets"
"github.com/TeaOSLab/EdgeNode/internal/zero"
"github.com/TeaOSLab/EdgeNode/internal/utils/trackers"
"github.com/TeaOSLab/EdgeNode/internal/utils/zero"
"github.com/cespare/xxhash/v2"
"github.com/iwind/TeaGo/types"
"math"
@@ -189,7 +189,7 @@ func (this *MemoryStorage) openWriter(key string, expiresAt int64, status int, h
if isDirty &&
this.parentStorage != nil &&
this.dirtyQueueSize > 0 &&
len(this.dirtyChan) >= this.dirtyQueueSize-int(fsutils.DiskMaxWrites) /** delta **/ { // 缓存时间过长
len(this.dirtyChan) >= this.dirtyQueueSize-64 /** delta **/ { // 缓存时间过长
return nil, ErrWritingQueueFull
}

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
package caches

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
package caches

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
package caches_test

View File

@@ -6,14 +6,13 @@ import (
fsutils "github.com/TeaOSLab/EdgeNode/internal/utils/fs"
"github.com/iwind/TeaGo/types"
"io"
"os"
"strings"
"sync"
)
type FileWriter struct {
storage StorageInterface
rawWriter *os.File
rawWriter *fsutils.File
key string
metaHeaderSize int
@@ -26,9 +25,11 @@ type FileWriter struct {
maxSize int64
endFunc func()
once sync.Once
modifiedBytes int
}
func NewFileWriter(storage StorageInterface, rawWriter *os.File, key string, expiredAt int64, metaHeaderSize int, metaBodySize int64, maxSize int64, endFunc func()) *FileWriter {
func NewFileWriter(storage StorageInterface, rawWriter *fsutils.File, key string, expiredAt int64, metaHeaderSize int, metaBodySize int64, maxSize int64, endFunc func()) *FileWriter {
return &FileWriter{
storage: storage,
key: key,
@@ -43,9 +44,7 @@ func NewFileWriter(storage StorageInterface, rawWriter *os.File, key string, exp
// WriteHeader 写入数据
func (this *FileWriter) WriteHeader(data []byte) (n int, err error) {
fsutils.WriteBegin()
n, err = this.rawWriter.Write(data)
fsutils.WriteEnd()
this.headerSize += int64(n)
if err != nil {
_ = this.Discard()
@@ -79,7 +78,7 @@ func (this *FileWriter) Write(data []byte) (n int, err error) {
var l = len(data)
if l > (2 << 20) {
var offset = 0
const bufferSize = 256 << 10
const bufferSize = 64 << 10
for {
var end = offset + bufferSize
if end > l {
@@ -139,36 +138,30 @@ func (this *FileWriter) Close() error {
// check content length
if this.metaBodySize > 0 && this.bodySize != this.metaBodySize {
_ = this.rawWriter.Close()
_ = os.Remove(path)
_ = fsutils.Remove(path)
return ErrUnexpectedContentLength
}
err := this.WriteHeaderLength(types.Int(this.headerSize))
if err != nil {
fsutils.WriteBegin()
_ = this.rawWriter.Close()
fsutils.WriteEnd()
_ = os.Remove(path)
_ = fsutils.Remove(path)
return err
}
err = this.WriteBodyLength(this.bodySize)
if err != nil {
fsutils.WriteBegin()
_ = this.rawWriter.Close()
fsutils.WriteEnd()
_ = os.Remove(path)
_ = fsutils.Remove(path)
return err
}
fsutils.WriteBegin()
err = this.rawWriter.Close()
fsutils.WriteEnd()
if err != nil {
_ = os.Remove(path)
_ = fsutils.Remove(path)
} else if strings.HasSuffix(path, FileTmpSuffix) {
err = os.Rename(path, strings.Replace(path, FileTmpSuffix, "", 1))
err = fsutils.Rename(path, strings.Replace(path, FileTmpSuffix, "", 1))
if err != nil {
_ = os.Remove(path)
_ = fsutils.Remove(path)
}
}
@@ -181,11 +174,9 @@ func (this *FileWriter) Discard() error {
this.endFunc()
})
fsutils.WriteBegin()
_ = this.rawWriter.Close()
fsutils.WriteEnd()
err := os.Remove(this.rawWriter.Name())
err := fsutils.Remove(this.rawWriter.Name())
return err
}
@@ -211,9 +202,7 @@ func (this *FileWriter) ItemType() ItemType {
}
func (this *FileWriter) write(data []byte) (n int, err error) {
fsutils.WriteBegin()
n, err = this.rawWriter.Write(data)
fsutils.WriteEnd()
this.bodySize += int64(n)
if this.maxSize > 0 && this.bodySize > this.maxSize {
@@ -227,5 +216,6 @@ func (this *FileWriter) write(data []byte) (n int, err error) {
if err != nil {
_ = this.Discard()
}
return
}

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
package caches
@@ -7,12 +7,12 @@ import (
fsutils "github.com/TeaOSLab/EdgeNode/internal/utils/fs"
"github.com/iwind/TeaGo/types"
"io"
"os"
"strings"
"sync"
)
type PartialFileWriter struct {
rawWriter *os.File
rawWriter *fsutils.File
key string
metaHeaderSize int
@@ -31,9 +31,11 @@ type PartialFileWriter struct {
ranges *PartialRanges
rangePath string
writtenBytes int64
}
func NewPartialFileWriter(rawWriter *os.File, key string, expiredAt int64, metaHeaderSize int, metaBodySize int64, isNew bool, isPartial bool, bodyOffset int64, ranges *PartialRanges, endFunc func()) *PartialFileWriter {
func NewPartialFileWriter(rawWriter *fsutils.File, key string, expiredAt int64, metaHeaderSize int, metaBodySize int64, isNew bool, isPartial bool, bodyOffset int64, ranges *PartialRanges, endFunc func()) *PartialFileWriter {
return &PartialFileWriter{
key: key,
rawWriter: rawWriter,
@@ -54,9 +56,7 @@ func (this *PartialFileWriter) WriteHeader(data []byte) (n int, err error) {
if !this.isNew {
return
}
fsutils.WriteBegin()
n, err = this.rawWriter.Write(data)
fsutils.WriteEnd()
this.headerSize += int64(n)
if err != nil {
_ = this.Discard()
@@ -65,9 +65,7 @@ func (this *PartialFileWriter) WriteHeader(data []byte) (n int, err error) {
}
func (this *PartialFileWriter) AppendHeader(data []byte) error {
fsutils.WriteBegin()
_, err := this.rawWriter.Write(data)
fsutils.WriteEnd()
if err != nil {
_ = this.Discard()
} else {
@@ -104,9 +102,7 @@ func (this *PartialFileWriter) WriteHeaderLength(headerLength int) error {
// Write 写入数据
func (this *PartialFileWriter) Write(data []byte) (n int, err error) {
fsutils.WriteBegin()
n, err = this.rawWriter.Write(data)
fsutils.WriteEnd()
this.bodySize += int64(n)
if err != nil {
_ = this.Discard()
@@ -130,11 +126,14 @@ func (this *PartialFileWriter) WriteAt(offset int64, data []byte) error {
// prevent extending too much space in a single writing
var maxOffset = this.ranges.Max()
if offset-maxOffset > 16<<20 {
var extendSizePerStep int64 = 1 << 20
var maxExtendSize int64 = 32 << 20
if fsutils.DiskIsExtremelyFast() {
maxExtendSize = 128 << 20
extendSizePerStep = 4 << 20
} else if fsutils.DiskIsFast() {
maxExtendSize = 64 << 20
extendSizePerStep = 2 << 20
}
if offset-maxOffset > maxExtendSize {
stat, err := this.rawWriter.Stat()
@@ -143,11 +142,8 @@ func (this *PartialFileWriter) WriteAt(offset int64, data []byte) error {
}
// extend min size to prepare for file tail
const extendSizePerStep = 8 << 20
if stat.Size()+extendSizePerStep <= this.bodyOffset+offset+int64(len(data)) {
fsutils.WriteBegin()
_ = this.rawWriter.Truncate(stat.Size() + extendSizePerStep)
fsutils.WriteEnd()
return nil
}
}
@@ -161,15 +157,25 @@ func (this *PartialFileWriter) WriteAt(offset int64, data []byte) error {
this.bodyOffset = SizeMeta + int64(keyLength) + this.headerSize
}
fsutils.WriteBegin()
_, err := this.rawWriter.WriteAt(data, this.bodyOffset+offset)
fsutils.WriteEnd()
n, err := this.rawWriter.WriteAt(data, this.bodyOffset+offset)
if err != nil {
return err
}
this.ranges.Add(offset, end)
// 保存ranges内容到文件当新增数据达到一定量时就更新是为了及时更新ranges文件以便于其他请求能够及时读取到已经缓存的部分内容
this.writtenBytes += int64(n)
if this.writtenBytes > (1 << 20) {
this.writtenBytes = 0
if len(this.rangePath) > 0 {
if this.bodySize > 0 {
this.ranges.BodySize = this.bodySize
}
_ = this.ranges.WriteToFile(this.rangePath)
}
}
return nil
}
@@ -178,6 +184,14 @@ func (this *PartialFileWriter) SetBodyLength(bodyLength int64) {
this.bodySize = bodyLength
}
// SetContentMD5 设置内容MD5
func (this *PartialFileWriter) SetContentMD5(contentMD5 string) {
if strings.Contains(contentMD5, "\n") || len(contentMD5) > 128 {
return
}
this.ranges.ContentMD5 = contentMD5
}
// WriteBodyLength 写入Body长度数据
func (this *PartialFileWriter) WriteBodyLength(bodyLength int64) error {
if this.metaBodySize > 0 && this.metaBodySize == bodyLength {
@@ -204,12 +218,12 @@ func (this *PartialFileWriter) Close() error {
this.endFunc()
})
this.ranges.BodySize = this.bodySize
if this.bodySize > 0 {
this.ranges.BodySize = this.bodySize
}
err := this.ranges.WriteToFile(this.rangePath)
if err != nil {
fsutils.WriteBegin()
_ = this.rawWriter.Close()
fsutils.WriteEnd()
this.remove()
return err
}
@@ -218,25 +232,19 @@ func (this *PartialFileWriter) Close() error {
if this.isNew {
err = this.WriteHeaderLength(types.Int(this.headerSize))
if err != nil {
fsutils.WriteBegin()
_ = this.rawWriter.Close()
fsutils.WriteEnd()
this.remove()
return err
}
err = this.WriteBodyLength(this.bodySize)
if err != nil {
fsutils.WriteBegin()
_ = this.rawWriter.Close()
fsutils.WriteEnd()
this.remove()
return err
}
}
fsutils.WriteBegin()
err = this.rawWriter.Close()
fsutils.WriteEnd()
if err != nil {
this.remove()
}
@@ -250,14 +258,14 @@ func (this *PartialFileWriter) Discard() error {
this.endFunc()
})
fsutils.WriteBegin()
_ = this.rawWriter.Close()
fsutils.WriteEnd()
SharedPartialRangesQueue.Delete(this.rangePath)
_ = os.Remove(this.rangePath)
err := os.Remove(this.rawWriter.Name())
_ = fsutils.Remove(this.rangePath)
err := fsutils.Remove(this.rawWriter.Name())
return err
}
@@ -286,9 +294,14 @@ func (this *PartialFileWriter) IsNew() bool {
return this.isNew && len(this.ranges.Ranges) == 0
}
func (this *PartialFileWriter) Ranges() *PartialRanges {
return this.ranges
}
func (this *PartialFileWriter) remove() {
_ = os.Remove(this.rawWriter.Name())
_ = fsutils.Remove(this.rawWriter.Name())
SharedPartialRangesQueue.Delete(this.rangePath)
_ = os.Remove(this.rangePath)
_ = fsutils.Remove(this.rangePath)
}

View File

@@ -1,9 +1,10 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
package caches_test
import (
"github.com/TeaOSLab/EdgeNode/internal/caches"
fsutils "github.com/TeaOSLab/EdgeNode/internal/utils/fs"
"github.com/iwind/TeaGo/types"
"os"
"testing"
@@ -15,7 +16,7 @@ func TestPartialFileWriter_Write(t *testing.T) {
_ = os.Remove(path)
var reader = func() {
data, err := os.ReadFile(path)
data, err := fsutils.ReadFile(path)
if err != nil {
t.Fatal(err)
}
@@ -27,7 +28,7 @@ func TestPartialFileWriter_Write(t *testing.T) {
t.Fatal(err)
}
var ranges = caches.NewPartialRanges(0)
var writer = caches.NewPartialFileWriter(fp, "test", time.Now().Unix()+86500, -1, -1, true, true, 0, ranges, func() {
var writer = caches.NewPartialFileWriter(fsutils.NewFile(fp, fsutils.FlagWrite), "test", time.Now().Unix()+86500, -1, -1, true, true, 0, ranges, func() {
t.Log("end")
})
_, err = writer.WriteHeader([]byte("header"))

View File

@@ -1,4 +1,4 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2021 GoEdge goedge.cdn@gmail.com. All rights reserved.
package compressions

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
package compressions

View File

@@ -1,4 +1,4 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2021 GoEdge goedge.cdn@gmail.com. All rights reserved.
//go:build !plus || !linux
package compressions

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
package compressions_test

View File

@@ -1,4 +1,4 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2021 GoEdge goedge.cdn@gmail.com. All rights reserved.
package compressions

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
package compressions_test

View File

@@ -1,4 +1,4 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2021 GoEdge goedge.cdn@gmail.com. All rights reserved.
package compressions

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
package compressions_test

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
package compressions

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
package compressions

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
package compressions

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
package compressions

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
package compressions

View File

@@ -1,4 +1,4 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2021 GoEdge goedge.cdn@gmail.com. All rights reserved.
package compressions

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
package compressions_test

View File

@@ -1,4 +1,4 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2021 GoEdge goedge.cdn@gmail.com. All rights reserved.
package compressions

View File

@@ -1,4 +1,4 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2021 GoEdge goedge.cdn@gmail.com. All rights reserved.
package compressions

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
package compressions

View File

@@ -1,4 +1,4 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2021 GoEdge goedge.cdn@gmail.com. All rights reserved.
//go:build !plus || !linux
package compressions

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
package compressions_test

View File

@@ -1,4 +1,4 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2021 GoEdge goedge.cdn@gmail.com. All rights reserved.
package compressions

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
package compressions_test

View File

@@ -1,4 +1,4 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2021 GoEdge goedge.cdn@gmail.com. All rights reserved.
package compressions

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
package compressions_test

View File

@@ -1,10 +1,10 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
package compressions
import (
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
"github.com/TeaOSLab/EdgeNode/internal/goman"
"github.com/TeaOSLab/EdgeNode/internal/utils/goman"
"io"
"time"
)

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
package compressions

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
package compressions

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
package compressions

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
package compressions

View File

@@ -1,4 +1,4 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2021 GoEdge goedge.cdn@gmail.com. All rights reserved.
package compressions

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
package compressions_test

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
package conns

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
package conns

View File

@@ -1,4 +1,4 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2021 GoEdge goedge.cdn@gmail.com. All rights reserved.
//go:build !plus
// +build !plus

View File

@@ -1,4 +1,4 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2021 GoEdge goedge.cdn@gmail.com. All rights reserved.
//go:build plus
// +build plus

View File

@@ -1,14 +1,13 @@
package teaconst
const (
Version = "1.3.5"
Version = "1.3.9"
ProductName = "Edge Node"
ProcessName = "edge-node"
Role = "node"
EncryptKey = "8f983f4d69b83aaa0d74b21a212f6967"
EncryptMethod = "aes-256-cfb"
// SystemdServiceName systemd

View File

@@ -1,4 +1,4 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2021 GoEdge goedge.cdn@gmail.com. All rights reserved.
package teaconst

View File

@@ -1,11 +0,0 @@
package encrypt
import "testing"
func TestMagicKeyEncode(t *testing.T) {
dst := MagicKeyEncode([]byte("Hello,World"))
t.Log("dst:", string(dst))
src := MagicKeyDecode(dst)
t.Log("src:", string(src))
}

View File

@@ -1,8 +0,0 @@
package encrypt
import "testing"
func TestFindMethodInstance(t *testing.T) {
t.Log(NewMethodInstance("a", "b", ""))
t.Log(NewMethodInstance("aes-256-cfb", "123456", ""))
}

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
//go:build linux
package firewalls
@@ -16,7 +16,7 @@ import (
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
"github.com/TeaOSLab/EdgeNode/internal/utils"
executils "github.com/TeaOSLab/EdgeNode/internal/utils/exec"
"github.com/TeaOSLab/EdgeNode/internal/zero"
"github.com/TeaOSLab/EdgeNode/internal/utils/zero"
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/types"
stringutil "github.com/iwind/TeaGo/utils/string"

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
//go:build !linux
// +build !linux

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
package firewalls

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
package firewalls

View File

@@ -1,13 +1,13 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
package firewalls
import (
"fmt"
"github.com/TeaOSLab/EdgeNode/internal/conns"
"github.com/TeaOSLab/EdgeNode/internal/goman"
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
executils "github.com/TeaOSLab/EdgeNode/internal/utils/exec"
"github.com/TeaOSLab/EdgeNode/internal/utils/goman"
"github.com/iwind/TeaGo/types"
"strings"
"time"

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
package firewalls

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
package firewalls

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
//go:build linux
package firewalls
@@ -11,9 +11,9 @@ import (
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
"github.com/TeaOSLab/EdgeNode/internal/events"
"github.com/TeaOSLab/EdgeNode/internal/firewalls/nftables"
"github.com/TeaOSLab/EdgeNode/internal/goman"
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
executils "github.com/TeaOSLab/EdgeNode/internal/utils/exec"
"github.com/TeaOSLab/EdgeNode/internal/utils/goman"
"github.com/google/nftables/expr"
"github.com/iwind/TeaGo/types"
"net"

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
//go:build !linux
// +build !linux

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
//go:build linux
package nftables

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
//go:build linux
package nftables

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
//go:build linux
package nftables_test

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
//go:build linux
package nftables

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
//go:build linux
// +build linux

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
//go:build linux
// +build linux

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
//go:build linux
// +build linux

View File

@@ -1,4 +1,4 @@
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
// Copyright 2023 GoEdge goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
package nftables

View File

@@ -1,4 +1,4 @@
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
// Copyright 2023 GoEdge goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
package nftables_test

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
//go:build linux
package nftables

View File

@@ -1,4 +1,4 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
//go:build linux
package nftables
@@ -8,9 +8,9 @@ import (
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
"github.com/TeaOSLab/EdgeNode/internal/events"
"github.com/TeaOSLab/EdgeNode/internal/goman"
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
executils "github.com/TeaOSLab/EdgeNode/internal/utils/exec"
"github.com/TeaOSLab/EdgeNode/internal/utils/goman"
"github.com/iwind/TeaGo/logs"
"os"
"runtime"
@@ -90,18 +90,26 @@ func (this *Installer) Install() error {
}
var cmd *executils.Cmd
var aptCmd *executils.Cmd
// check dnf
dnfExe, err := executils.LookPath("dnf")
if err == nil {
cmd = executils.NewCmd(dnfExe, "-y", "install", "nftables")
{
dnfExe, err := executils.LookPath("dnf")
if err == nil {
cmd = executils.NewCmd(dnfExe, "-y", "install", "nftables")
}
}
// check apt
if cmd == nil {
aptExe, err := executils.LookPath("apt")
aptGetExe, err := executils.LookPath("apt-get")
if err == nil {
cmd = executils.NewCmd(aptExe, "install", "nftables")
cmd = executils.NewCmd(aptGetExe, "install", "nftables")
}
aptExe, aptErr := executils.LookPath("apt")
if aptErr == nil {
aptCmd = executils.NewCmd(aptExe, "install", "nftables")
}
}
@@ -119,9 +127,19 @@ func (this *Installer) Install() error {
cmd.WithTimeout(10 * time.Minute)
cmd.WithStderr()
err = cmd.Run()
err := cmd.Run()
if err != nil {
return fmt.Errorf("%w: %s", err, cmd.Stderr())
// try 'apt-get' instead of 'apt'
if aptCmd != nil {
cmd = aptCmd
cmd.WithTimeout(10 * time.Minute)
cmd.WithStderr()
err = cmd.Run()
}
if err != nil {
return fmt.Errorf("%w: %s", err, cmd.Stderr())
}
}
remotelogs.Println("NFTABLES", "installed nftables with command '"+cmd.String()+"' successfully")

Some files were not shown because too many files have changed in this diff Show More