mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-09-15 21:00:27 +00:00
boxdd: Add insecure mode
This commit is contained in:
@@ -43,7 +43,7 @@ func NewTransport(ctx context.Context, logger log.ContextLogger, tag string, opt
|
||||
files = append(files, defaultFile)
|
||||
} else {
|
||||
for _, path := range options.Path {
|
||||
files = append(files, NewFile(filemanager.BasePath(ctx, os.ExpandEnv(path))))
|
||||
files = append(files, NewFile(ctx, filemanager.BasePath(ctx, os.ExpandEnv(path))))
|
||||
}
|
||||
}
|
||||
if options.Predefined != nil {
|
||||
|
||||
@@ -2,15 +2,16 @@ package hosts
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net/netip"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
"github.com/sagernet/sing/service/filemanager"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
@@ -18,6 +19,7 @@ import (
|
||||
const cacheMaxAge = 5 * time.Second
|
||||
|
||||
type File struct {
|
||||
ctx context.Context
|
||||
path string
|
||||
access sync.Mutex
|
||||
byName map[string][]netip.Addr
|
||||
@@ -26,8 +28,9 @@ type File struct {
|
||||
size int64
|
||||
}
|
||||
|
||||
func NewFile(path string) *File {
|
||||
func NewFile(ctx context.Context, path string) *File {
|
||||
return &File{
|
||||
ctx: ctx,
|
||||
path: path,
|
||||
}
|
||||
}
|
||||
@@ -37,7 +40,7 @@ func NewDefault() (*File, error) {
|
||||
if err != nil {
|
||||
return nil, E.Cause(err, "resolve default hosts path")
|
||||
}
|
||||
return NewFile(defaultPathResolved), nil
|
||||
return NewFile(context.Background(), defaultPathResolved), nil
|
||||
}
|
||||
|
||||
func (f *File) Lookup(name string) []netip.Addr {
|
||||
@@ -52,7 +55,7 @@ func (f *File) update() {
|
||||
if now.Before(f.expire) && len(f.byName) > 0 {
|
||||
return
|
||||
}
|
||||
stat, err := os.Stat(f.path)
|
||||
stat, err := filemanager.Stat(f.ctx, f.path)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -61,7 +64,7 @@ func (f *File) update() {
|
||||
return
|
||||
}
|
||||
byName := make(map[string][]netip.Addr)
|
||||
file, err := os.Open(f.path)
|
||||
file, err := filemanager.Open(f.ctx, f.path)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package hosts
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/netip"
|
||||
"os"
|
||||
"runtime"
|
||||
@@ -13,7 +14,7 @@ import (
|
||||
|
||||
func TestHosts(t *testing.T) {
|
||||
t.Parallel()
|
||||
require.Equal(t, []netip.Addr{netip.AddrFrom4([4]byte{127, 0, 0, 1}), netip.IPv6Loopback()}, NewFile("testdata/hosts").Lookup("localhost"))
|
||||
require.Equal(t, []netip.Addr{netip.AddrFrom4([4]byte{127, 0, 0, 1}), netip.IPv6Loopback()}, NewFile(context.Background(), "testdata/hosts").Lookup("localhost"))
|
||||
if runtime.GOOS != "windows" {
|
||||
defaultPathResolved, err := defaultPath()
|
||||
if err != nil {
|
||||
@@ -21,7 +22,7 @@ func TestHosts(t *testing.T) {
|
||||
}
|
||||
content, readErr := os.ReadFile(defaultPathResolved)
|
||||
require.NoError(t, readErr)
|
||||
hFile := NewFile(defaultPathResolved)
|
||||
hFile := NewFile(context.Background(), defaultPathResolved)
|
||||
if len(hFile.Lookup("localhost")) == 0 {
|
||||
t.Fatal("failed to resolve localhost: ", defaultPathResolved, ": \n", string(content))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user