mirror of
https://github.com/SagerNet/sing-box.git
synced 2026-04-13 20:28:32 +10:00
Compare commits
1 Commits
v1.3.6
...
dev-l3-rou
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9092139e73 |
12
.github/workflows/debug.yml
vendored
12
.github/workflows/debug.yml
vendored
@@ -31,6 +31,12 @@ jobs:
|
|||||||
uses: actions/setup-go@v4
|
uses: actions/setup-go@v4
|
||||||
with:
|
with:
|
||||||
go-version: ${{ steps.version.outputs.go_version }}
|
go-version: ${{ steps.version.outputs.go_version }}
|
||||||
|
- name: Cache go module
|
||||||
|
uses: actions/cache@v3
|
||||||
|
with:
|
||||||
|
path: |
|
||||||
|
~/go/pkg/mod
|
||||||
|
key: go-${{ hashFiles('**/go.sum') }}
|
||||||
- name: Add cache to Go proxy
|
- name: Add cache to Go proxy
|
||||||
run: |
|
run: |
|
||||||
version=`git rev-parse HEAD`
|
version=`git rev-parse HEAD`
|
||||||
@@ -190,6 +196,12 @@ jobs:
|
|||||||
uses: actions/setup-go@v4
|
uses: actions/setup-go@v4
|
||||||
with:
|
with:
|
||||||
go-version: ${{ steps.version.outputs.go_version }}
|
go-version: ${{ steps.version.outputs.go_version }}
|
||||||
|
- name: Cache go module
|
||||||
|
uses: actions/cache@v3
|
||||||
|
with:
|
||||||
|
path: |
|
||||||
|
~/go/pkg/mod
|
||||||
|
key: go-${{ hashFiles('**/go.sum') }}
|
||||||
- name: Build
|
- name: Build
|
||||||
id: build
|
id: build
|
||||||
run: make
|
run: make
|
||||||
|
|||||||
6
.github/workflows/lint.yml
vendored
6
.github/workflows/lint.yml
vendored
@@ -31,6 +31,12 @@ jobs:
|
|||||||
uses: actions/setup-go@v4
|
uses: actions/setup-go@v4
|
||||||
with:
|
with:
|
||||||
go-version: ${{ steps.version.outputs.go_version }}
|
go-version: ${{ steps.version.outputs.go_version }}
|
||||||
|
- name: Cache go module
|
||||||
|
uses: actions/cache@v3
|
||||||
|
with:
|
||||||
|
path: |
|
||||||
|
~/go/pkg/mod
|
||||||
|
key: go-${{ hashFiles('**/go.sum') }}
|
||||||
- name: golangci-lint
|
- name: golangci-lint
|
||||||
uses: golangci/golangci-lint-action@v3
|
uses: golangci/golangci-lint-action@v3
|
||||||
with:
|
with:
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ builds:
|
|||||||
tags:
|
tags:
|
||||||
- with_gvisor
|
- with_gvisor
|
||||||
- with_quic
|
- with_quic
|
||||||
- with_dhcp
|
|
||||||
- with_wireguard
|
- with_wireguard
|
||||||
- with_utls
|
- with_utls
|
||||||
- with_reality_server
|
- with_reality_server
|
||||||
@@ -49,7 +48,6 @@ builds:
|
|||||||
tags:
|
tags:
|
||||||
- with_gvisor
|
- with_gvisor
|
||||||
- with_quic
|
- with_quic
|
||||||
- with_dhcp
|
|
||||||
- with_wireguard
|
- with_wireguard
|
||||||
- with_utls
|
- with_utls
|
||||||
- with_clash_api
|
- with_clash_api
|
||||||
@@ -119,6 +117,8 @@ nfpms:
|
|||||||
dst: /etc/systemd/system/sing-box@.service
|
dst: /etc/systemd/system/sing-box@.service
|
||||||
- src: LICENSE
|
- src: LICENSE
|
||||||
dst: /usr/share/licenses/sing-box/LICENSE
|
dst: /usr/share/licenses/sing-box/LICENSE
|
||||||
|
scripts:
|
||||||
|
postremove: release/config/postremove.sh
|
||||||
source:
|
source:
|
||||||
enabled: false
|
enabled: false
|
||||||
name_template: '{{ .ProjectName }}-{{ .Version }}.source'
|
name_template: '{{ .ProjectName }}-{{ .Version }}.source'
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ RUN set -ex \
|
|||||||
&& apk add git build-base \
|
&& apk add git build-base \
|
||||||
&& export COMMIT=$(git rev-parse --short HEAD) \
|
&& export COMMIT=$(git rev-parse --short HEAD) \
|
||||||
&& export VERSION=$(go run ./cmd/internal/read_tag) \
|
&& export VERSION=$(go run ./cmd/internal/read_tag) \
|
||||||
&& go build -v -trimpath -tags with_gvisor,with_quic,with_dhcp,with_wireguard,with_utls,with_reality_server,with_clash_api,with_acme \
|
&& go build -v -trimpath -tags with_quic,with_wireguard,with_reality_server,with_acme \
|
||||||
-o /go/bin/sing-box \
|
-o /go/bin/sing-box \
|
||||||
-ldflags "-X \"github.com/sagernet/sing-box/constant.Version=$VERSION\" -s -w -buildid=" \
|
-ldflags "-X \"github.com/sagernet/sing-box/constant.Version=$VERSION\" -s -w -buildid=" \
|
||||||
./cmd/sing-box
|
./cmd/sing-box
|
||||||
|
|||||||
5
LICENSE
5
LICENSE
@@ -11,7 +11,4 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|||||||
GNU General Public License for more details.
|
GNU General Public License for more details.
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License
|
You should have received a copy of the GNU General Public License
|
||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
In addition, no derivative work may use the name or imply association
|
|
||||||
with this application without prior consent.
|
|
||||||
21
Makefile
21
Makefile
@@ -1,6 +1,6 @@
|
|||||||
NAME = sing-box
|
NAME = sing-box
|
||||||
COMMIT = $(shell git rev-parse --short HEAD)
|
COMMIT = $(shell git rev-parse --short HEAD)
|
||||||
TAGS ?= with_gvisor,with_quic,with_dhcp,with_wireguard,with_utls,with_reality_server,with_clash_api
|
TAGS ?= with_gvisor,with_quic,with_wireguard,with_utls,with_reality_server,with_clash_api
|
||||||
TAGS_TEST ?= with_gvisor,with_quic,with_wireguard,with_grpc,with_ech,with_utls,with_reality_server,with_shadowsocksr
|
TAGS_TEST ?= with_gvisor,with_quic,with_wireguard,with_grpc,with_ech,with_utls,with_reality_server,with_shadowsocksr
|
||||||
|
|
||||||
GOHOSTOS = $(shell go env GOHOSTOS)
|
GOHOSTOS = $(shell go env GOHOSTOS)
|
||||||
@@ -22,7 +22,7 @@ install:
|
|||||||
fmt:
|
fmt:
|
||||||
@gofumpt -l -w .
|
@gofumpt -l -w .
|
||||||
@gofmt -s -w .
|
@gofmt -s -w .
|
||||||
@gci write --custom-order -s standard -s "prefix(github.com/sagernet/)" -s "default" .
|
@gci write --custom-order -s "standard,prefix(github.com/sagernet/),default" .
|
||||||
|
|
||||||
fmt_install:
|
fmt_install:
|
||||||
go install -v mvdan.cc/gofumpt@latest
|
go install -v mvdan.cc/gofumpt@latest
|
||||||
@@ -48,14 +48,14 @@ proto_install:
|
|||||||
go install -v google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
|
go install -v google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
|
||||||
|
|
||||||
snapshot:
|
snapshot:
|
||||||
go run ./cmd/internal/build goreleaser release --clean --snapshot || exit 1
|
go run ./cmd/internal/build goreleaser release --rm-dist --snapshot || exit 1
|
||||||
mkdir dist/release
|
mkdir dist/release
|
||||||
mv dist/*.tar.gz dist/*.zip dist/*.deb dist/*.rpm dist/release
|
mv dist/*.tar.gz dist/*.zip dist/*.deb dist/*.rpm dist/release
|
||||||
ghr --delete --draft --prerelease -p 1 nightly dist/release
|
ghr --delete --draft --prerelease -p 1 nightly dist/release
|
||||||
rm -r dist
|
rm -r dist
|
||||||
|
|
||||||
release:
|
release:
|
||||||
go run ./cmd/internal/build goreleaser release --clean --skip-publish || exit 1
|
go run ./cmd/internal/build goreleaser release --rm-dist --skip-publish || exit 1
|
||||||
mkdir dist/release
|
mkdir dist/release
|
||||||
mv dist/*.tar.gz dist/*.zip dist/*.deb dist/*.rpm dist/release
|
mv dist/*.tar.gz dist/*.zip dist/*.deb dist/*.rpm dist/release
|
||||||
ghr --delete --draft --prerelease -p 3 $(shell git describe --tags) dist/release
|
ghr --delete --draft --prerelease -p 3 $(shell git describe --tags) dist/release
|
||||||
@@ -77,20 +77,13 @@ test_stdio:
|
|||||||
go mod tidy && \
|
go mod tidy && \
|
||||||
go test -v -tags "$(TAGS_TEST),force_stdio" .
|
go test -v -tags "$(TAGS_TEST),force_stdio" .
|
||||||
|
|
||||||
android:
|
|
||||||
go run ./cmd/internal/build_libbox -target android
|
|
||||||
|
|
||||||
ios:
|
|
||||||
go run ./cmd/internal/build_libbox -target ios
|
|
||||||
|
|
||||||
lib:
|
lib:
|
||||||
go run ./cmd/internal/build_libbox -target android
|
go run ./cmd/internal/build_libbox
|
||||||
go run ./cmd/internal/build_libbox -target ios
|
|
||||||
|
|
||||||
lib_install:
|
lib_install:
|
||||||
go get -v -d
|
go get -v -d
|
||||||
go install -v github.com/sagernet/gomobile/cmd/gomobile@v0.0.0-20230728014906-3de089147f59
|
go install -v github.com/sagernet/gomobile/cmd/gomobile@v0.0.0-20221130124640-349ebaa752ca
|
||||||
go install -v github.com/sagernet/gomobile/cmd/gobind@v0.0.0-20230728014906-3de089147f59
|
go install -v github.com/sagernet/gomobile/cmd/gobind@v0.0.0-20221130124640-349ebaa752ca
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf bin dist sing-box
|
rm -rf bin dist sing-box
|
||||||
|
|||||||
@@ -8,10 +8,6 @@ The universal proxy platform.
|
|||||||
|
|
||||||
https://sing-box.sagernet.org
|
https://sing-box.sagernet.org
|
||||||
|
|
||||||
## Support
|
|
||||||
|
|
||||||
https://community.sagernet.org/c/sing-box/
|
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
```
|
```
|
||||||
@@ -29,7 +25,4 @@ GNU General Public License for more details.
|
|||||||
|
|
||||||
You should have received a copy of the GNU General Public License
|
You should have received a copy of the GNU General Public License
|
||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
In addition, no derivative work may use the name or imply association
|
|
||||||
with this application without prior consent.
|
|
||||||
```
|
```
|
||||||
@@ -13,7 +13,6 @@ type ClashServer interface {
|
|||||||
PreStarter
|
PreStarter
|
||||||
Mode() string
|
Mode() string
|
||||||
StoreSelected() bool
|
StoreSelected() bool
|
||||||
StoreFakeIP() bool
|
|
||||||
CacheFile() ClashCacheFile
|
CacheFile() ClashCacheFile
|
||||||
HistoryStorage() *urltest.HistoryStorage
|
HistoryStorage() *urltest.HistoryStorage
|
||||||
RoutedConnection(ctx context.Context, conn net.Conn, metadata InboundContext, matchedRule Rule) (net.Conn, Tracker)
|
RoutedConnection(ctx context.Context, conn net.Conn, metadata InboundContext, matchedRule Rule) (net.Conn, Tracker)
|
||||||
@@ -23,7 +22,6 @@ type ClashServer interface {
|
|||||||
type ClashCacheFile interface {
|
type ClashCacheFile interface {
|
||||||
LoadSelected(group string) string
|
LoadSelected(group string) string
|
||||||
StoreSelected(group string, selected string) error
|
StoreSelected(group string, selected string) error
|
||||||
FakeIPStorage
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Tracker interface {
|
type Tracker interface {
|
||||||
@@ -31,16 +29,10 @@ type Tracker interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type OutboundGroup interface {
|
type OutboundGroup interface {
|
||||||
Outbound
|
|
||||||
Now() string
|
Now() string
|
||||||
All() []string
|
All() []string
|
||||||
}
|
}
|
||||||
|
|
||||||
type URLTestGroup interface {
|
|
||||||
OutboundGroup
|
|
||||||
URLTest(ctx context.Context, url string) (map[string]uint16, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
func OutboundTag(detour Outbound) string {
|
func OutboundTag(detour Outbound) string {
|
||||||
if group, isGroup := detour.(OutboundGroup); isGroup {
|
if group, isGroup := detour.(OutboundGroup); isGroup {
|
||||||
return group.Now()
|
return group.Now()
|
||||||
|
|||||||
@@ -1,31 +0,0 @@
|
|||||||
package adapter
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/netip"
|
|
||||||
|
|
||||||
"github.com/sagernet/sing-dns"
|
|
||||||
"github.com/sagernet/sing/common/logger"
|
|
||||||
)
|
|
||||||
|
|
||||||
type FakeIPStore interface {
|
|
||||||
Service
|
|
||||||
Contains(address netip.Addr) bool
|
|
||||||
Create(domain string, isIPv6 bool) (netip.Addr, error)
|
|
||||||
Lookup(address netip.Addr) (string, bool)
|
|
||||||
Reset() error
|
|
||||||
}
|
|
||||||
|
|
||||||
type FakeIPStorage interface {
|
|
||||||
FakeIPMetadata() *FakeIPMetadata
|
|
||||||
FakeIPSaveMetadata(metadata *FakeIPMetadata) error
|
|
||||||
FakeIPStore(address netip.Addr, domain string) error
|
|
||||||
FakeIPStoreAsync(address netip.Addr, domain string, logger logger.Logger)
|
|
||||||
FakeIPLoad(address netip.Addr) (string, bool)
|
|
||||||
FakeIPLoadDomain(domain string, isIPv6 bool) (netip.Addr, bool)
|
|
||||||
FakeIPReset() error
|
|
||||||
}
|
|
||||||
|
|
||||||
type FakeIPTransport interface {
|
|
||||||
dns.Transport
|
|
||||||
Store() FakeIPStore
|
|
||||||
}
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
package adapter
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"encoding"
|
|
||||||
"encoding/binary"
|
|
||||||
"io"
|
|
||||||
"net/netip"
|
|
||||||
|
|
||||||
"github.com/sagernet/sing/common"
|
|
||||||
)
|
|
||||||
|
|
||||||
type FakeIPMetadata struct {
|
|
||||||
Inet4Range netip.Prefix
|
|
||||||
Inet6Range netip.Prefix
|
|
||||||
Inet4Current netip.Addr
|
|
||||||
Inet6Current netip.Addr
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *FakeIPMetadata) MarshalBinary() (data []byte, err error) {
|
|
||||||
var buffer bytes.Buffer
|
|
||||||
for _, marshaler := range []encoding.BinaryMarshaler{m.Inet4Range, m.Inet6Range, m.Inet4Current, m.Inet6Current} {
|
|
||||||
data, err = marshaler.MarshalBinary()
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
common.Must(binary.Write(&buffer, binary.BigEndian, uint16(len(data))))
|
|
||||||
buffer.Write(data)
|
|
||||||
}
|
|
||||||
data = buffer.Bytes()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *FakeIPMetadata) UnmarshalBinary(data []byte) error {
|
|
||||||
reader := bytes.NewReader(data)
|
|
||||||
for _, unmarshaler := range []encoding.BinaryUnmarshaler{&m.Inet4Range, &m.Inet6Range, &m.Inet4Current, &m.Inet6Current} {
|
|
||||||
var length uint16
|
|
||||||
common.Must(binary.Read(reader, binary.BigEndian, &length))
|
|
||||||
element := make([]byte, length)
|
|
||||||
_, err := io.ReadFull(reader, element)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = unmarshaler.UnmarshalBinary(element)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -46,7 +46,6 @@ type InboundContext struct {
|
|||||||
SourceGeoIPCode string
|
SourceGeoIPCode string
|
||||||
GeoIPCode string
|
GeoIPCode string
|
||||||
ProcessInfo *process.Info
|
ProcessInfo *process.Info
|
||||||
FakeIP bool
|
|
||||||
|
|
||||||
// dns cache
|
// dns cache
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
|
tun "github.com/sagernet/sing-tun"
|
||||||
N "github.com/sagernet/sing/common/network"
|
N "github.com/sagernet/sing/common/network"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -13,8 +14,12 @@ type Outbound interface {
|
|||||||
Type() string
|
Type() string
|
||||||
Tag() string
|
Tag() string
|
||||||
Network() []string
|
Network() []string
|
||||||
Dependencies() []string
|
|
||||||
N.Dialer
|
N.Dialer
|
||||||
NewConnection(ctx context.Context, conn net.Conn, metadata InboundContext) error
|
NewConnection(ctx context.Context, conn net.Conn, metadata InboundContext) error
|
||||||
NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata InboundContext) error
|
NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata InboundContext) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type IPOutbound interface {
|
||||||
|
Outbound
|
||||||
|
NewIPConnection(ctx context.Context, conn tun.RouteContext, metadata InboundContext) (tun.DirectDestination, error)
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,6 +4,12 @@ type PreStarter interface {
|
|||||||
PreStart() error
|
PreStart() error
|
||||||
}
|
}
|
||||||
|
|
||||||
type PostStarter interface {
|
func PreStart(starter any) error {
|
||||||
PostStart() error
|
if preService, ok := starter.(PreStarter); ok {
|
||||||
|
err := preService.PreStart()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,10 +21,11 @@ type Router interface {
|
|||||||
Outbound(tag string) (Outbound, bool)
|
Outbound(tag string) (Outbound, bool)
|
||||||
DefaultOutbound(network string) Outbound
|
DefaultOutbound(network string) Outbound
|
||||||
|
|
||||||
FakeIPStore() FakeIPStore
|
|
||||||
|
|
||||||
RouteConnection(ctx context.Context, conn net.Conn, metadata InboundContext) error
|
RouteConnection(ctx context.Context, conn net.Conn, metadata InboundContext) error
|
||||||
RoutePacketConnection(ctx context.Context, conn N.PacketConn, metadata InboundContext) error
|
RoutePacketConnection(ctx context.Context, conn N.PacketConn, metadata InboundContext) error
|
||||||
|
RouteIPConnection(ctx context.Context, conn tun.RouteContext, metadata InboundContext) tun.RouteAction
|
||||||
|
|
||||||
|
NatRequired(outbound string) bool
|
||||||
|
|
||||||
GeoIPReader() *geoip.Reader
|
GeoIPReader() *geoip.Reader
|
||||||
LoadGeosite(code string) (Rule, error)
|
LoadGeosite(code string) (Rule, error)
|
||||||
@@ -34,7 +35,6 @@ type Router interface {
|
|||||||
LookupDefault(ctx context.Context, domain string) ([]netip.Addr, error)
|
LookupDefault(ctx context.Context, domain string) ([]netip.Addr, error)
|
||||||
|
|
||||||
InterfaceFinder() control.InterfaceFinder
|
InterfaceFinder() control.InterfaceFinder
|
||||||
UpdateInterfaces() error
|
|
||||||
DefaultInterface() string
|
DefaultInterface() string
|
||||||
AutoDetectInterface() bool
|
AutoDetectInterface() bool
|
||||||
AutoDetectInterfaceFunc() control.Func
|
AutoDetectInterfaceFunc() control.Func
|
||||||
@@ -42,7 +42,9 @@ type Router interface {
|
|||||||
NetworkMonitor() tun.NetworkUpdateMonitor
|
NetworkMonitor() tun.NetworkUpdateMonitor
|
||||||
InterfaceMonitor() tun.DefaultInterfaceMonitor
|
InterfaceMonitor() tun.DefaultInterfaceMonitor
|
||||||
PackageManager() tun.PackageManager
|
PackageManager() tun.PackageManager
|
||||||
|
|
||||||
Rules() []Rule
|
Rules() []Rule
|
||||||
|
IPRules() []IPRule
|
||||||
|
|
||||||
TimeService
|
TimeService
|
||||||
|
|
||||||
@@ -51,8 +53,6 @@ type Router interface {
|
|||||||
|
|
||||||
V2RayServer() V2RayServer
|
V2RayServer() V2RayServer
|
||||||
SetV2RayServer(server V2RayServer)
|
SetV2RayServer(server V2RayServer)
|
||||||
|
|
||||||
ResetNetwork() error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type routerContextKey struct{}
|
type routerContextKey struct{}
|
||||||
@@ -81,7 +81,11 @@ type Rule interface {
|
|||||||
type DNSRule interface {
|
type DNSRule interface {
|
||||||
Rule
|
Rule
|
||||||
DisableCache() bool
|
DisableCache() bool
|
||||||
RewriteTTL() *uint32
|
}
|
||||||
|
|
||||||
|
type IPRule interface {
|
||||||
|
Rule
|
||||||
|
Action() tun.ActionType
|
||||||
}
|
}
|
||||||
|
|
||||||
type InterfaceUpdateListener interface {
|
type InterfaceUpdateListener interface {
|
||||||
|
|||||||
174
box.go
174
box.go
@@ -9,6 +9,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/sagernet/sing-box/adapter"
|
"github.com/sagernet/sing-box/adapter"
|
||||||
|
C "github.com/sagernet/sing-box/constant"
|
||||||
"github.com/sagernet/sing-box/experimental"
|
"github.com/sagernet/sing-box/experimental"
|
||||||
"github.com/sagernet/sing-box/experimental/libbox/platform"
|
"github.com/sagernet/sing-box/experimental/libbox/platform"
|
||||||
"github.com/sagernet/sing-box/inbound"
|
"github.com/sagernet/sing-box/inbound"
|
||||||
@@ -30,25 +31,18 @@ type Box struct {
|
|||||||
outbounds []adapter.Outbound
|
outbounds []adapter.Outbound
|
||||||
logFactory log.Factory
|
logFactory log.Factory
|
||||||
logger log.ContextLogger
|
logger log.ContextLogger
|
||||||
|
logFile *os.File
|
||||||
preServices map[string]adapter.Service
|
preServices map[string]adapter.Service
|
||||||
postServices map[string]adapter.Service
|
postServices map[string]adapter.Service
|
||||||
done chan struct{}
|
done chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Options struct {
|
func New(ctx context.Context, options option.Options, platformInterface platform.Interface) (*Box, error) {
|
||||||
option.Options
|
|
||||||
Context context.Context
|
|
||||||
PlatformInterface platform.Interface
|
|
||||||
}
|
|
||||||
|
|
||||||
func New(options Options) (*Box, error) {
|
|
||||||
ctx := options.Context
|
|
||||||
if ctx == nil {
|
|
||||||
ctx = context.Background()
|
|
||||||
}
|
|
||||||
createdAt := time.Now()
|
createdAt := time.Now()
|
||||||
|
|
||||||
experimentalOptions := common.PtrValueOrDefault(options.Experimental)
|
experimentalOptions := common.PtrValueOrDefault(options.Experimental)
|
||||||
applyDebugOptions(common.PtrValueOrDefault(experimentalOptions.Debug))
|
applyDebugOptions(common.PtrValueOrDefault(experimentalOptions.Debug))
|
||||||
|
|
||||||
var needClashAPI bool
|
var needClashAPI bool
|
||||||
var needV2RayAPI bool
|
var needV2RayAPI bool
|
||||||
if experimentalOptions.ClashAPI != nil && experimentalOptions.ClashAPI.ExternalController != "" {
|
if experimentalOptions.ClashAPI != nil && experimentalOptions.ClashAPI.ExternalController != "" {
|
||||||
@@ -57,21 +51,60 @@ func New(options Options) (*Box, error) {
|
|||||||
if experimentalOptions.V2RayAPI != nil && experimentalOptions.V2RayAPI.Listen != "" {
|
if experimentalOptions.V2RayAPI != nil && experimentalOptions.V2RayAPI.Listen != "" {
|
||||||
needV2RayAPI = true
|
needV2RayAPI = true
|
||||||
}
|
}
|
||||||
var defaultLogWriter io.Writer
|
|
||||||
if options.PlatformInterface != nil {
|
logOptions := common.PtrValueOrDefault(options.Log)
|
||||||
defaultLogWriter = io.Discard
|
|
||||||
}
|
var logFactory log.Factory
|
||||||
logFactory, err := log.New(log.Options{
|
var observableLogFactory log.ObservableFactory
|
||||||
Context: ctx,
|
var logFile *os.File
|
||||||
Options: common.PtrValueOrDefault(options.Log),
|
var logWriter io.Writer
|
||||||
Observable: needClashAPI,
|
if logOptions.Disabled {
|
||||||
DefaultWriter: defaultLogWriter,
|
observableLogFactory = log.NewNOPFactory()
|
||||||
BaseTime: createdAt,
|
logFactory = observableLogFactory
|
||||||
PlatformWriter: options.PlatformInterface,
|
} else {
|
||||||
})
|
switch logOptions.Output {
|
||||||
if err != nil {
|
case "":
|
||||||
return nil, E.Cause(err, "create log factory")
|
if platformInterface != nil {
|
||||||
|
logWriter = io.Discard
|
||||||
|
} else {
|
||||||
|
logWriter = os.Stdout
|
||||||
|
}
|
||||||
|
case "stderr":
|
||||||
|
logWriter = os.Stderr
|
||||||
|
case "stdout":
|
||||||
|
logWriter = os.Stdout
|
||||||
|
default:
|
||||||
|
var err error
|
||||||
|
logFile, err = os.OpenFile(C.BasePath(logOptions.Output), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
logWriter = logFile
|
||||||
|
}
|
||||||
|
logFormatter := log.Formatter{
|
||||||
|
BaseTime: createdAt,
|
||||||
|
DisableColors: logOptions.DisableColor || logFile != nil,
|
||||||
|
DisableTimestamp: !logOptions.Timestamp && logFile != nil,
|
||||||
|
FullTimestamp: logOptions.Timestamp,
|
||||||
|
TimestampFormat: "-0700 2006-01-02 15:04:05",
|
||||||
|
}
|
||||||
|
if needClashAPI {
|
||||||
|
observableLogFactory = log.NewObservableFactory(logFormatter, logWriter, platformInterface)
|
||||||
|
logFactory = observableLogFactory
|
||||||
|
} else {
|
||||||
|
logFactory = log.NewFactory(logFormatter, logWriter, platformInterface)
|
||||||
|
}
|
||||||
|
if logOptions.Level != "" {
|
||||||
|
logLevel, err := log.ParseLevel(logOptions.Level)
|
||||||
|
if err != nil {
|
||||||
|
return nil, E.Cause(err, "parse log level")
|
||||||
|
}
|
||||||
|
logFactory.SetLevel(logLevel)
|
||||||
|
} else {
|
||||||
|
logFactory.SetLevel(log.LevelTrace)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
router, err := route.NewRouter(
|
router, err := route.NewRouter(
|
||||||
ctx,
|
ctx,
|
||||||
logFactory,
|
logFactory,
|
||||||
@@ -79,7 +112,7 @@ func New(options Options) (*Box, error) {
|
|||||||
common.PtrValueOrDefault(options.DNS),
|
common.PtrValueOrDefault(options.DNS),
|
||||||
common.PtrValueOrDefault(options.NTP),
|
common.PtrValueOrDefault(options.NTP),
|
||||||
options.Inbounds,
|
options.Inbounds,
|
||||||
options.PlatformInterface,
|
platformInterface,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, E.Cause(err, "parse route options")
|
return nil, E.Cause(err, "parse route options")
|
||||||
@@ -99,7 +132,7 @@ func New(options Options) (*Box, error) {
|
|||||||
router,
|
router,
|
||||||
logFactory.NewLogger(F.ToString("inbound/", inboundOptions.Type, "[", tag, "]")),
|
logFactory.NewLogger(F.ToString("inbound/", inboundOptions.Type, "[", tag, "]")),
|
||||||
inboundOptions,
|
inboundOptions,
|
||||||
options.PlatformInterface,
|
platformInterface,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, E.Cause(err, "parse inbound[", i, "]")
|
return nil, E.Cause(err, "parse inbound[", i, "]")
|
||||||
@@ -118,7 +151,6 @@ func New(options Options) (*Box, error) {
|
|||||||
ctx,
|
ctx,
|
||||||
router,
|
router,
|
||||||
logFactory.NewLogger(F.ToString("outbound/", outboundOptions.Type, "[", tag, "]")),
|
logFactory.NewLogger(F.ToString("outbound/", outboundOptions.Type, "[", tag, "]")),
|
||||||
tag,
|
|
||||||
outboundOptions)
|
outboundOptions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, E.Cause(err, "parse outbound[", i, "]")
|
return nil, E.Cause(err, "parse outbound[", i, "]")
|
||||||
@@ -126,7 +158,7 @@ func New(options Options) (*Box, error) {
|
|||||||
outbounds = append(outbounds, out)
|
outbounds = append(outbounds, out)
|
||||||
}
|
}
|
||||||
err = router.Initialize(inbounds, outbounds, func() adapter.Outbound {
|
err = router.Initialize(inbounds, outbounds, func() adapter.Outbound {
|
||||||
out, oErr := outbound.New(ctx, router, logFactory.NewLogger("outbound/direct"), "direct", option.Outbound{Type: "direct", Tag: "default"})
|
out, oErr := outbound.New(ctx, router, logFactory.NewLogger("outbound/direct"), option.Outbound{Type: "direct", Tag: "default"})
|
||||||
common.Must(oErr)
|
common.Must(oErr)
|
||||||
outbounds = append(outbounds, out)
|
outbounds = append(outbounds, out)
|
||||||
return out
|
return out
|
||||||
@@ -134,16 +166,10 @@ func New(options Options) (*Box, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if options.PlatformInterface != nil {
|
|
||||||
err = options.PlatformInterface.Initialize(ctx, router)
|
|
||||||
if err != nil {
|
|
||||||
return nil, E.Cause(err, "initialize platform interface")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
preServices := make(map[string]adapter.Service)
|
preServices := make(map[string]adapter.Service)
|
||||||
postServices := make(map[string]adapter.Service)
|
postServices := make(map[string]adapter.Service)
|
||||||
if needClashAPI {
|
if needClashAPI {
|
||||||
clashServer, err := experimental.NewClashServer(ctx, router, logFactory.(log.ObservableFactory), common.PtrValueOrDefault(options.Experimental.ClashAPI))
|
clashServer, err := experimental.NewClashServer(router, observableLogFactory, common.PtrValueOrDefault(options.Experimental.ClashAPI))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, E.Cause(err, "create clash api server")
|
return nil, E.Cause(err, "create clash api server")
|
||||||
}
|
}
|
||||||
@@ -165,6 +191,7 @@ func New(options Options) (*Box, error) {
|
|||||||
createdAt: createdAt,
|
createdAt: createdAt,
|
||||||
logFactory: logFactory,
|
logFactory: logFactory,
|
||||||
logger: logFactory.Logger(),
|
logger: logFactory.Logger(),
|
||||||
|
logFile: logFile,
|
||||||
preServices: preServices,
|
preServices: preServices,
|
||||||
postServices: postServices,
|
postServices: postServices,
|
||||||
done: make(chan struct{}),
|
done: make(chan struct{}),
|
||||||
@@ -211,17 +238,26 @@ func (s *Box) Start() error {
|
|||||||
|
|
||||||
func (s *Box) preStart() error {
|
func (s *Box) preStart() error {
|
||||||
for serviceName, service := range s.preServices {
|
for serviceName, service := range s.preServices {
|
||||||
if preService, isPreService := service.(adapter.PreStarter); isPreService {
|
s.logger.Trace("pre-starting ", serviceName)
|
||||||
s.logger.Trace("pre-start ", serviceName)
|
err := adapter.PreStart(service)
|
||||||
err := preService.PreStart()
|
if err != nil {
|
||||||
if err != nil {
|
return E.Cause(err, "pre-start ", serviceName)
|
||||||
return E.Cause(err, "pre-starting ", serviceName)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
err := s.startOutbounds()
|
for i, out := range s.outbounds {
|
||||||
if err != nil {
|
if starter, isStarter := out.(common.Starter); isStarter {
|
||||||
return err
|
var tag string
|
||||||
|
if out.Tag() == "" {
|
||||||
|
tag = F.ToString(i)
|
||||||
|
} else {
|
||||||
|
tag = out.Tag()
|
||||||
|
}
|
||||||
|
s.logger.Trace("initializing outbound ", tag)
|
||||||
|
err := starter.Start()
|
||||||
|
if err != nil {
|
||||||
|
return E.Cause(err, "initialize outbound/", out.Type(), "[", tag, "]")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return s.router.Start()
|
return s.router.Start()
|
||||||
}
|
}
|
||||||
@@ -245,30 +281,17 @@ func (s *Box) start() error {
|
|||||||
} else {
|
} else {
|
||||||
tag = in.Tag()
|
tag = in.Tag()
|
||||||
}
|
}
|
||||||
s.logger.Trace("initializing inbound/", in.Type(), "[", tag, "]")
|
s.logger.Trace("initializing inbound ", tag)
|
||||||
err = in.Start()
|
err = in.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return E.Cause(err, "initialize inbound/", in.Type(), "[", tag, "]")
|
return E.Cause(err, "initialize inbound/", in.Type(), "[", tag, "]")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Box) postStart() error {
|
|
||||||
for serviceName, service := range s.postServices {
|
for serviceName, service := range s.postServices {
|
||||||
s.logger.Trace("starting ", service)
|
s.logger.Trace("start ", serviceName)
|
||||||
err := service.Start()
|
err = service.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return E.Cause(err, "start ", serviceName)
|
return E.Cause(err, "starting ", serviceName)
|
||||||
}
|
|
||||||
}
|
|
||||||
for serviceName, service := range s.outbounds {
|
|
||||||
if lateService, isLateService := service.(adapter.PostStarter); isLateService {
|
|
||||||
s.logger.Trace("post-starting ", service)
|
|
||||||
err := lateService.PostStart()
|
|
||||||
if err != nil {
|
|
||||||
return E.Cause(err, "post-start ", serviceName)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@@ -283,21 +306,33 @@ func (s *Box) Close() error {
|
|||||||
}
|
}
|
||||||
var errors error
|
var errors error
|
||||||
for serviceName, service := range s.postServices {
|
for serviceName, service := range s.postServices {
|
||||||
s.logger.Trace("closing ", serviceName)
|
|
||||||
errors = E.Append(errors, service.Close(), func(err error) error {
|
errors = E.Append(errors, service.Close(), func(err error) error {
|
||||||
|
s.logger.Trace("closing ", serviceName)
|
||||||
return E.Cause(err, "close ", serviceName)
|
return E.Cause(err, "close ", serviceName)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
for i, in := range s.inbounds {
|
for i, in := range s.inbounds {
|
||||||
s.logger.Trace("closing inbound/", in.Type(), "[", i, "]")
|
var tag string
|
||||||
|
if in.Tag() == "" {
|
||||||
|
tag = F.ToString(i)
|
||||||
|
} else {
|
||||||
|
tag = in.Tag()
|
||||||
|
}
|
||||||
|
s.logger.Trace("closing inbound ", tag)
|
||||||
errors = E.Append(errors, in.Close(), func(err error) error {
|
errors = E.Append(errors, in.Close(), func(err error) error {
|
||||||
return E.Cause(err, "close inbound/", in.Type(), "[", i, "]")
|
return E.Cause(err, "close inbound/", in.Type(), "[", i, "]")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
for i, out := range s.outbounds {
|
for i, out := range s.outbounds {
|
||||||
s.logger.Trace("closing outbound/", out.Type(), "[", i, "]")
|
var tag string
|
||||||
|
if out.Tag() == "" {
|
||||||
|
tag = F.ToString(i)
|
||||||
|
} else {
|
||||||
|
tag = out.Tag()
|
||||||
|
}
|
||||||
|
s.logger.Trace("closing outbound ", tag)
|
||||||
errors = E.Append(errors, common.Close(out), func(err error) error {
|
errors = E.Append(errors, common.Close(out), func(err error) error {
|
||||||
return E.Cause(err, "close outbound/", out.Type(), "[", i, "]")
|
return E.Cause(err, "close inbound/", out.Type(), "[", i, "]")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
s.logger.Trace("closing router")
|
s.logger.Trace("closing router")
|
||||||
@@ -312,12 +347,17 @@ func (s *Box) Close() error {
|
|||||||
return E.Cause(err, "close ", serviceName)
|
return E.Cause(err, "close ", serviceName)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
s.logger.Trace("closing log factory")
|
s.logger.Trace("closing logger")
|
||||||
if err := common.Close(s.logFactory); err != nil {
|
if err := common.Close(s.logFactory); err != nil {
|
||||||
errors = E.Append(errors, err, func(err error) error {
|
errors = E.Append(errors, err, func(err error) error {
|
||||||
return E.Cause(err, "close log factory")
|
return E.Cause(err, "close log factory")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
if s.logFile != nil {
|
||||||
|
errors = E.Append(errors, s.logFile.Close(), func(err error) error {
|
||||||
|
return E.Cause(err, "close log file")
|
||||||
|
})
|
||||||
|
}
|
||||||
return errors
|
return errors
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,79 +0,0 @@
|
|||||||
package box
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/sagernet/sing-box/adapter"
|
|
||||||
"github.com/sagernet/sing/common"
|
|
||||||
E "github.com/sagernet/sing/common/exceptions"
|
|
||||||
F "github.com/sagernet/sing/common/format"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (s *Box) startOutbounds() error {
|
|
||||||
outboundTags := make(map[adapter.Outbound]string)
|
|
||||||
outbounds := make(map[string]adapter.Outbound)
|
|
||||||
for i, outboundToStart := range s.outbounds {
|
|
||||||
var outboundTag string
|
|
||||||
if outboundToStart.Tag() == "" {
|
|
||||||
outboundTag = F.ToString(i)
|
|
||||||
} else {
|
|
||||||
outboundTag = outboundToStart.Tag()
|
|
||||||
}
|
|
||||||
if _, exists := outbounds[outboundTag]; exists {
|
|
||||||
return E.New("outbound tag ", outboundTag, " duplicated")
|
|
||||||
}
|
|
||||||
outboundTags[outboundToStart] = outboundTag
|
|
||||||
outbounds[outboundTag] = outboundToStart
|
|
||||||
}
|
|
||||||
started := make(map[string]bool)
|
|
||||||
for {
|
|
||||||
canContinue := false
|
|
||||||
startOne:
|
|
||||||
for _, outboundToStart := range s.outbounds {
|
|
||||||
outboundTag := outboundTags[outboundToStart]
|
|
||||||
if started[outboundTag] {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
dependencies := outboundToStart.Dependencies()
|
|
||||||
for _, dependency := range dependencies {
|
|
||||||
if !started[dependency] {
|
|
||||||
continue startOne
|
|
||||||
}
|
|
||||||
}
|
|
||||||
started[outboundTag] = true
|
|
||||||
canContinue = true
|
|
||||||
if starter, isStarter := outboundToStart.(common.Starter); isStarter {
|
|
||||||
s.logger.Trace("initializing outbound/", outboundToStart.Type(), "[", outboundTag, "]")
|
|
||||||
err := starter.Start()
|
|
||||||
if err != nil {
|
|
||||||
return E.Cause(err, "initialize outbound/", outboundToStart.Type(), "[", outboundTag, "]")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(started) == len(s.outbounds) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if canContinue {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
currentOutbound := common.Find(s.outbounds, func(it adapter.Outbound) bool {
|
|
||||||
return !started[outboundTags[it]]
|
|
||||||
})
|
|
||||||
var lintOutbound func(oTree []string, oCurrent adapter.Outbound) error
|
|
||||||
lintOutbound = func(oTree []string, oCurrent adapter.Outbound) error {
|
|
||||||
problemOutboundTag := common.Find(oCurrent.Dependencies(), func(it string) bool {
|
|
||||||
return !started[it]
|
|
||||||
})
|
|
||||||
if common.Contains(oTree, problemOutboundTag) {
|
|
||||||
return E.New("circular outbound dependency: ", strings.Join(oTree, " -> "), " -> ", problemOutboundTag)
|
|
||||||
}
|
|
||||||
problemOutbound := outbounds[problemOutboundTag]
|
|
||||||
if problemOutbound == nil {
|
|
||||||
return E.New("dependency[", problemOutbound, "] not found for outbound[", outboundTags[oCurrent], "]")
|
|
||||||
}
|
|
||||||
return lintOutbound(append(oTree, problemOutboundTag), problemOutbound)
|
|
||||||
}
|
|
||||||
return lintOutbound([]string{outboundTags[currentOutbound]}, currentOutbound)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"go/build"
|
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
|
||||||
@@ -12,10 +11,6 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
build_shared.FindSDK()
|
build_shared.FindSDK()
|
||||||
|
|
||||||
if os.Getenv("build.Default.GOPATH") == "" {
|
|
||||||
os.Setenv("GOPATH", build.Default.GOPATH)
|
|
||||||
}
|
|
||||||
|
|
||||||
command := exec.Command(os.Args[1], os.Args[2:]...)
|
command := exec.Command(os.Args[1], os.Args[2:]...)
|
||||||
command.Stdout = os.Stdout
|
command.Stdout = os.Stdout
|
||||||
command.Stderr = os.Stderr
|
command.Stderr = os.Stderr
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
|
||||||
|
|
||||||
_ "github.com/sagernet/gomobile/event/key"
|
_ "github.com/sagernet/gomobile/event/key"
|
||||||
"github.com/sagernet/sing-box/cmd/internal/build_shared"
|
"github.com/sagernet/sing-box/cmd/internal/build_shared"
|
||||||
@@ -39,24 +38,18 @@ func main() {
|
|||||||
var (
|
var (
|
||||||
sharedFlags []string
|
sharedFlags []string
|
||||||
debugFlags []string
|
debugFlags []string
|
||||||
sharedTags []string
|
|
||||||
iosTags []string
|
|
||||||
debugTags []string
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
sharedFlags = append(sharedFlags, "-trimpath")
|
sharedFlags = append(sharedFlags, "-trimpath")
|
||||||
sharedFlags = append(sharedFlags, "-ldflags")
|
sharedFlags = append(sharedFlags, "-ldflags")
|
||||||
|
|
||||||
currentTag, err := build_shared.ReadTag()
|
currentTag, err := build_shared.ReadTag()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
currentTag = "unknown"
|
currentTag = "unknown"
|
||||||
}
|
}
|
||||||
sharedFlags = append(sharedFlags, "-X github.com/sagernet/sing-box/constant.Version="+currentTag+" -s -w -buildid=")
|
sharedFlags = append(sharedFlags, "-X github.com/sagernet/sing-box/constant.Version="+currentTag+" -s -w -buildid=")
|
||||||
debugFlags = append(debugFlags, "-X github.com/sagernet/sing-box/constant.Version="+currentTag)
|
debugFlags = append(debugFlags, "-X github.com/sagernet/sing-box/constant.Version="+currentTag)
|
||||||
|
|
||||||
sharedTags = append(sharedTags, "with_gvisor", "with_quic", "with_wireguard", "with_utls", "with_clash_api")
|
|
||||||
iosTags = append(iosTags, "with_dhcp", "with_low_memory", "with_conntrack")
|
|
||||||
debugTags = append(debugTags, "debug")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildAndroid() {
|
func buildAndroid() {
|
||||||
@@ -77,9 +70,9 @@ func buildAndroid() {
|
|||||||
|
|
||||||
args = append(args, "-tags")
|
args = append(args, "-tags")
|
||||||
if !debugEnabled {
|
if !debugEnabled {
|
||||||
args = append(args, strings.Join(sharedTags, ","))
|
args = append(args, "with_gvisor,with_quic,with_wireguard,with_utls,with_clash_api")
|
||||||
} else {
|
} else {
|
||||||
args = append(args, strings.Join(append(sharedTags, debugTags...), ","))
|
args = append(args, "with_gvisor,with_quic,with_wireguard,with_utls,with_clash_api,debug")
|
||||||
}
|
}
|
||||||
args = append(args, "./experimental/libbox")
|
args = append(args, "./experimental/libbox")
|
||||||
|
|
||||||
@@ -107,7 +100,7 @@ func buildiOS() {
|
|||||||
args := []string{
|
args := []string{
|
||||||
"bind",
|
"bind",
|
||||||
"-v",
|
"-v",
|
||||||
"-target", "ios,iossimulator,tvos,tvossimulator,macos",
|
"-target", "ios,iossimulator,macos",
|
||||||
"-libname=box",
|
"-libname=box",
|
||||||
}
|
}
|
||||||
if !debugEnabled {
|
if !debugEnabled {
|
||||||
@@ -116,12 +109,11 @@ func buildiOS() {
|
|||||||
args = append(args, debugFlags...)
|
args = append(args, debugFlags...)
|
||||||
}
|
}
|
||||||
|
|
||||||
tags := append(sharedTags, iosTags...)
|
|
||||||
args = append(args, "-tags")
|
args = append(args, "-tags")
|
||||||
if !debugEnabled {
|
if !debugEnabled {
|
||||||
args = append(args, strings.Join(tags, ","))
|
args = append(args, "with_gvisor,with_quic,with_utls,with_clash_api,with_low_memory,with_conntrack")
|
||||||
} else {
|
} else {
|
||||||
args = append(args, strings.Join(append(tags, debugTags...), ","))
|
args = append(args, "with_gvisor,with_quic,with_utls,with_clash_api,with_low_memory,with_conntrack,debug")
|
||||||
}
|
}
|
||||||
args = append(args, "./experimental/libbox")
|
args = append(args, "./experimental/libbox")
|
||||||
|
|
||||||
@@ -133,7 +125,7 @@ func buildiOS() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
copyPath := filepath.Join("..", "sing-box-for-apple")
|
copyPath := filepath.Join("..", "sing-box-for-ios")
|
||||||
if rw.FileExists(copyPath) {
|
if rw.FileExists(copyPath) {
|
||||||
targetDir := filepath.Join(copyPath, "Libbox.xcframework")
|
targetDir := filepath.Join(copyPath, "Libbox.xcframework")
|
||||||
targetDir, _ = filepath.Abs(targetDir)
|
targetDir, _ = filepath.Abs(targetDir)
|
||||||
|
|||||||
@@ -31,10 +31,7 @@ func check() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
instance, err := box.New(box.Options{
|
instance, err := box.New(ctx, options, nil)
|
||||||
Context: ctx,
|
|
||||||
Options: options,
|
|
||||||
})
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
instance.Close()
|
instance.Close()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
|
|
||||||
"github.com/sagernet/sing-box/log"
|
"github.com/sagernet/sing-box/log"
|
||||||
|
|
||||||
"github.com/gofrs/uuid/v5"
|
"github.com/gofrs/uuid"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/sagernet/sing-box"
|
"github.com/sagernet/sing-box"
|
||||||
"github.com/sagernet/sing-box/common/badjsonmerge"
|
"github.com/sagernet/sing-box/common/badjsonmerge"
|
||||||
@@ -128,10 +127,7 @@ func create() (*box.Box, context.CancelFunc, error) {
|
|||||||
options.Log.DisableColor = true
|
options.Log.DisableColor = true
|
||||||
}
|
}
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
instance, err := box.New(box.Options{
|
instance, err := box.New(ctx, options, nil)
|
||||||
Context: ctx,
|
|
||||||
Options: options,
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cancel()
|
cancel()
|
||||||
return nil, nil, E.Cause(err, "create service")
|
return nil, nil, E.Cause(err, "create service")
|
||||||
@@ -178,10 +174,7 @@ func run() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
cancel()
|
cancel()
|
||||||
closeCtx, closed := context.WithCancel(context.Background())
|
|
||||||
go closeMonitor(closeCtx)
|
|
||||||
instance.Close()
|
instance.Close()
|
||||||
closed()
|
|
||||||
if osSignal != syscall.SIGHUP {
|
if osSignal != syscall.SIGHUP {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -189,13 +182,3 @@ func run() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func closeMonitor(ctx context.Context) {
|
|
||||||
time.Sleep(3 * time.Second)
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
log.Fatal("sing-box did not close!")
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
"github.com/sagernet/sing-box"
|
"github.com/sagernet/sing-box"
|
||||||
E "github.com/sagernet/sing/common/exceptions"
|
E "github.com/sagernet/sing/common/exceptions"
|
||||||
N "github.com/sagernet/sing/common/network"
|
N "github.com/sagernet/sing/common/network"
|
||||||
@@ -25,7 +27,7 @@ func createPreStartedClient() (*box.Box, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
instance, err := box.New(box.Options{Options: options})
|
instance, err := box.New(context.Background(), options, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, E.Cause(err, "create service")
|
return nil, E.Cause(err, "create service")
|
||||||
}
|
}
|
||||||
|
|||||||
44
cmd/sing-box/debug.go
Normal file
44
cmd/sing-box/debug.go
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
//go:build debug
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
_ "net/http/pprof"
|
||||||
|
"runtime"
|
||||||
|
"runtime/debug"
|
||||||
|
|
||||||
|
"github.com/sagernet/sing-box/common/badjson"
|
||||||
|
"github.com/sagernet/sing-box/log"
|
||||||
|
|
||||||
|
"github.com/dustin/go-humanize"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
http.HandleFunc("/debug/gc", func(writer http.ResponseWriter, request *http.Request) {
|
||||||
|
writer.WriteHeader(http.StatusNoContent)
|
||||||
|
go debug.FreeOSMemory()
|
||||||
|
})
|
||||||
|
http.HandleFunc("/debug/memory", func(writer http.ResponseWriter, request *http.Request) {
|
||||||
|
var memStats runtime.MemStats
|
||||||
|
runtime.ReadMemStats(&memStats)
|
||||||
|
|
||||||
|
var memObject badjson.JSONObject
|
||||||
|
memObject.Put("heap", humanize.IBytes(memStats.HeapInuse))
|
||||||
|
memObject.Put("stack", humanize.IBytes(memStats.StackInuse))
|
||||||
|
memObject.Put("idle", humanize.IBytes(memStats.HeapIdle-memStats.HeapReleased))
|
||||||
|
memObject.Put("goroutines", runtime.NumGoroutine())
|
||||||
|
memObject.Put("rss", rusageMaxRSS())
|
||||||
|
|
||||||
|
encoder := json.NewEncoder(writer)
|
||||||
|
encoder.SetIndent("", " ")
|
||||||
|
encoder.Encode(memObject)
|
||||||
|
})
|
||||||
|
go func() {
|
||||||
|
err := http.ListenAndServe("0.0.0.0:8964", nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Debug(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
package box
|
//go:build debug
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"runtime"
|
"runtime"
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
//go:build !linux
|
//go:build debug && !linux
|
||||||
|
|
||||||
package box
|
package main
|
||||||
|
|
||||||
func rusageMaxRSS() float64 {
|
func rusageMaxRSS() float64 {
|
||||||
return -1
|
return -1
|
||||||
@@ -55,7 +55,7 @@ func WrapQUIC(err error) error {
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if Contains(err, "canceled by local with error code 0") {
|
if Contains(err, "canceled with error code 0") {
|
||||||
return net.ErrClosed
|
return net.ErrClosed
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ func TestMergeJSON(t *testing.T) {
|
|||||||
{
|
{
|
||||||
Type: C.RuleTypeDefault,
|
Type: C.RuleTypeDefault,
|
||||||
DefaultOptions: option.DefaultRule{
|
DefaultOptions: option.DefaultRule{
|
||||||
Network: []string{N.NetworkTCP},
|
Network: N.NetworkTCP,
|
||||||
Outbound: "direct",
|
Outbound: "direct",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -42,7 +42,7 @@ func TestMergeJSON(t *testing.T) {
|
|||||||
{
|
{
|
||||||
Type: C.RuleTypeDefault,
|
Type: C.RuleTypeDefault,
|
||||||
DefaultOptions: option.DefaultRule{
|
DefaultOptions: option.DefaultRule{
|
||||||
Network: []string{N.NetworkUDP},
|
Network: N.NetworkUDP,
|
||||||
Outbound: "direct",
|
Outbound: "direct",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
//go:build go1.20 && !go1.21
|
//go:build go1.19 && !go1.20
|
||||||
|
|
||||||
package badtls
|
package badtls
|
||||||
|
|
||||||
@@ -14,60 +14,39 @@ import (
|
|||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
"github.com/sagernet/sing-box/log"
|
|
||||||
"github.com/sagernet/sing/common"
|
"github.com/sagernet/sing/common"
|
||||||
"github.com/sagernet/sing/common/buf"
|
"github.com/sagernet/sing/common/buf"
|
||||||
"github.com/sagernet/sing/common/bufio"
|
"github.com/sagernet/sing/common/bufio"
|
||||||
E "github.com/sagernet/sing/common/exceptions"
|
E "github.com/sagernet/sing/common/exceptions"
|
||||||
N "github.com/sagernet/sing/common/network"
|
N "github.com/sagernet/sing/common/network"
|
||||||
aTLS "github.com/sagernet/sing/common/tls"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Conn struct {
|
type Conn struct {
|
||||||
*tls.Conn
|
*tls.Conn
|
||||||
writer N.ExtendedWriter
|
writer N.ExtendedWriter
|
||||||
isHandshakeComplete *atomic.Bool
|
activeCall *int32
|
||||||
activeCall *atomic.Int32
|
closeNotifySent *bool
|
||||||
closeNotifySent *bool
|
version *uint16
|
||||||
version *uint16
|
rand io.Reader
|
||||||
rand io.Reader
|
halfAccess *sync.Mutex
|
||||||
halfAccess *sync.Mutex
|
halfError *error
|
||||||
halfError *error
|
cipher cipher.AEAD
|
||||||
cipher cipher.AEAD
|
explicitNonceLen int
|
||||||
explicitNonceLen int
|
halfPtr uintptr
|
||||||
halfPtr uintptr
|
halfSeq []byte
|
||||||
halfSeq []byte
|
halfScratchBuf []byte
|
||||||
halfScratchBuf []byte
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TryCreate(conn aTLS.Conn) aTLS.Conn {
|
func Create(conn *tls.Conn) (TLSConn, error) {
|
||||||
tlsConn, ok := conn.(*tls.Conn)
|
if !handshakeComplete(conn) {
|
||||||
if !ok {
|
|
||||||
return conn
|
|
||||||
}
|
|
||||||
badConn, err := Create(tlsConn)
|
|
||||||
if err != nil {
|
|
||||||
log.Warn("initialize badtls: ", err)
|
|
||||||
return conn
|
|
||||||
}
|
|
||||||
return badConn
|
|
||||||
}
|
|
||||||
|
|
||||||
func Create(conn *tls.Conn) (aTLS.Conn, error) {
|
|
||||||
rawConn := reflect.Indirect(reflect.ValueOf(conn))
|
|
||||||
rawIsHandshakeComplete := rawConn.FieldByName("isHandshakeComplete")
|
|
||||||
if !rawIsHandshakeComplete.IsValid() || rawIsHandshakeComplete.Kind() != reflect.Struct {
|
|
||||||
return nil, E.New("badtls: invalid isHandshakeComplete")
|
|
||||||
}
|
|
||||||
isHandshakeComplete := (*atomic.Bool)(unsafe.Pointer(rawIsHandshakeComplete.UnsafeAddr()))
|
|
||||||
if !isHandshakeComplete.Load() {
|
|
||||||
return nil, E.New("handshake not finished")
|
return nil, E.New("handshake not finished")
|
||||||
}
|
}
|
||||||
|
rawConn := reflect.Indirect(reflect.ValueOf(conn))
|
||||||
rawActiveCall := rawConn.FieldByName("activeCall")
|
rawActiveCall := rawConn.FieldByName("activeCall")
|
||||||
if !rawActiveCall.IsValid() || rawActiveCall.Kind() != reflect.Struct {
|
if !rawActiveCall.IsValid() || rawActiveCall.Kind() != reflect.Int32 {
|
||||||
return nil, E.New("badtls: invalid active call")
|
return nil, E.New("badtls: invalid active call")
|
||||||
}
|
}
|
||||||
activeCall := (*atomic.Int32)(unsafe.Pointer(rawActiveCall.UnsafeAddr()))
|
activeCall := (*int32)(unsafe.Pointer(rawActiveCall.UnsafeAddr()))
|
||||||
rawHalfConn := rawConn.FieldByName("out")
|
rawHalfConn := rawConn.FieldByName("out")
|
||||||
if !rawHalfConn.IsValid() || rawHalfConn.Kind() != reflect.Struct {
|
if !rawHalfConn.IsValid() || rawHalfConn.Kind() != reflect.Struct {
|
||||||
return nil, E.New("badtls: invalid half conn")
|
return nil, E.New("badtls: invalid half conn")
|
||||||
@@ -129,20 +108,19 @@ func Create(conn *tls.Conn) (aTLS.Conn, error) {
|
|||||||
}
|
}
|
||||||
halfScratchBuf := rawHalfScratchBuf.Bytes()
|
halfScratchBuf := rawHalfScratchBuf.Bytes()
|
||||||
return &Conn{
|
return &Conn{
|
||||||
Conn: conn,
|
Conn: conn,
|
||||||
writer: bufio.NewExtendedWriter(conn.NetConn()),
|
writer: bufio.NewExtendedWriter(conn.NetConn()),
|
||||||
isHandshakeComplete: isHandshakeComplete,
|
activeCall: activeCall,
|
||||||
activeCall: activeCall,
|
closeNotifySent: closeNotifySent,
|
||||||
closeNotifySent: closeNotifySent,
|
version: version,
|
||||||
version: version,
|
halfAccess: halfAccess,
|
||||||
halfAccess: halfAccess,
|
halfError: halfError,
|
||||||
halfError: halfError,
|
cipher: aeadCipher,
|
||||||
cipher: aeadCipher,
|
explicitNonceLen: explicitNonceLen,
|
||||||
explicitNonceLen: explicitNonceLen,
|
rand: randReader,
|
||||||
rand: randReader,
|
halfPtr: rawHalfConn.UnsafeAddr(),
|
||||||
halfPtr: rawHalfConn.UnsafeAddr(),
|
halfSeq: halfSeq,
|
||||||
halfSeq: halfSeq,
|
halfScratchBuf: halfScratchBuf,
|
||||||
halfScratchBuf: halfScratchBuf,
|
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -152,15 +130,15 @@ func (c *Conn) WriteBuffer(buffer *buf.Buffer) error {
|
|||||||
return common.Error(c.Write(buffer.Bytes()))
|
return common.Error(c.Write(buffer.Bytes()))
|
||||||
}
|
}
|
||||||
for {
|
for {
|
||||||
x := c.activeCall.Load()
|
x := atomic.LoadInt32(c.activeCall)
|
||||||
if x&1 != 0 {
|
if x&1 != 0 {
|
||||||
return net.ErrClosed
|
return net.ErrClosed
|
||||||
}
|
}
|
||||||
if c.activeCall.CompareAndSwap(x, x+2) {
|
if atomic.CompareAndSwapInt32(c.activeCall, x, x+2) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
defer c.activeCall.Add(-2)
|
defer atomic.AddInt32(c.activeCall, -2)
|
||||||
c.halfAccess.Lock()
|
c.halfAccess.Lock()
|
||||||
defer c.halfAccess.Unlock()
|
defer c.halfAccess.Unlock()
|
||||||
if err := *c.halfError; err != nil {
|
if err := *c.halfError; err != nil {
|
||||||
@@ -208,7 +186,6 @@ func (c *Conn) WriteBuffer(buffer *buf.Buffer) error {
|
|||||||
binary.BigEndian.PutUint16(outBuf[3:], uint16(dataLen+c.explicitNonceLen+c.cipher.Overhead()))
|
binary.BigEndian.PutUint16(outBuf[3:], uint16(dataLen+c.explicitNonceLen+c.cipher.Overhead()))
|
||||||
}
|
}
|
||||||
incSeq(c.halfPtr)
|
incSeq(c.halfPtr)
|
||||||
log.Trace("badtls write ", buffer.Len())
|
|
||||||
return c.writer.WriteBuffer(buffer)
|
return c.writer.WriteBuffer(buffer)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
//go:build !go1.19 || go1.21
|
//go:build !go1.19 || go1.20
|
||||||
|
|
||||||
package badtls
|
package badtls
|
||||||
|
|
||||||
|
|||||||
13
common/badtls/conn.go
Normal file
13
common/badtls/conn.go
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package badtls
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/tls"
|
||||||
|
"net"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TLSConn interface {
|
||||||
|
net.Conn
|
||||||
|
HandshakeContext(ctx context.Context) error
|
||||||
|
ConnectionState() tls.ConnectionState
|
||||||
|
}
|
||||||
@@ -1,8 +1,9 @@
|
|||||||
//go:build go1.20 && !go.1.21
|
//go:build go1.19 && !go.1.20
|
||||||
|
|
||||||
package badtls
|
package badtls
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/tls"
|
||||||
"reflect"
|
"reflect"
|
||||||
_ "unsafe"
|
_ "unsafe"
|
||||||
)
|
)
|
||||||
@@ -15,6 +16,9 @@ const (
|
|||||||
//go:linkname errShutdown crypto/tls.errShutdown
|
//go:linkname errShutdown crypto/tls.errShutdown
|
||||||
var errShutdown error
|
var errShutdown error
|
||||||
|
|
||||||
|
//go:linkname handshakeComplete crypto/tls.(*Conn).handshakeComplete
|
||||||
|
func handshakeComplete(conn *tls.Conn) bool
|
||||||
|
|
||||||
//go:linkname incSeq crypto/tls.(*halfConn).incSeq
|
//go:linkname incSeq crypto/tls.(*halfConn).incSeq
|
||||||
func incSeq(conn uintptr)
|
func incSeq(conn uintptr)
|
||||||
|
|
||||||
|
|||||||
@@ -1,114 +0,0 @@
|
|||||||
package badversion
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
F "github.com/sagernet/sing/common/format"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Version struct {
|
|
||||||
Major int
|
|
||||||
Minor int
|
|
||||||
Patch int
|
|
||||||
PreReleaseIdentifier string
|
|
||||||
PreReleaseVersion int
|
|
||||||
}
|
|
||||||
|
|
||||||
func (v Version) After(anotherVersion Version) bool {
|
|
||||||
if v.Major > anotherVersion.Major {
|
|
||||||
return true
|
|
||||||
} else if v.Major < anotherVersion.Major {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if v.Minor > anotherVersion.Minor {
|
|
||||||
return true
|
|
||||||
} else if v.Minor < anotherVersion.Minor {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if v.Patch > anotherVersion.Patch {
|
|
||||||
return true
|
|
||||||
} else if v.Patch < anotherVersion.Patch {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if v.PreReleaseIdentifier == "" && anotherVersion.PreReleaseIdentifier != "" {
|
|
||||||
return true
|
|
||||||
} else if v.PreReleaseIdentifier != "" && anotherVersion.PreReleaseIdentifier == "" {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if v.PreReleaseIdentifier != "" && anotherVersion.PreReleaseIdentifier != "" {
|
|
||||||
if v.PreReleaseIdentifier == "beta" && anotherVersion.PreReleaseIdentifier == "alpha" {
|
|
||||||
return true
|
|
||||||
} else if v.PreReleaseIdentifier == "alpha" && anotherVersion.PreReleaseIdentifier == "beta" {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if v.PreReleaseVersion > anotherVersion.PreReleaseVersion {
|
|
||||||
return true
|
|
||||||
} else if v.PreReleaseVersion < anotherVersion.PreReleaseVersion {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (v Version) String() string {
|
|
||||||
version := F.ToString(v.Major, ".", v.Minor, ".", v.Patch)
|
|
||||||
if v.PreReleaseIdentifier != "" {
|
|
||||||
version = F.ToString(version, "-", v.PreReleaseIdentifier, ".", v.PreReleaseVersion)
|
|
||||||
}
|
|
||||||
return version
|
|
||||||
}
|
|
||||||
|
|
||||||
func (v Version) BadString() string {
|
|
||||||
version := F.ToString(v.Major, ".", v.Minor)
|
|
||||||
if v.Patch > 0 {
|
|
||||||
version = F.ToString(version, ".", v.Patch)
|
|
||||||
}
|
|
||||||
if v.PreReleaseIdentifier != "" {
|
|
||||||
version = F.ToString(version, "-", v.PreReleaseIdentifier)
|
|
||||||
if v.PreReleaseVersion > 0 {
|
|
||||||
version = F.ToString(version, v.PreReleaseVersion)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return version
|
|
||||||
}
|
|
||||||
|
|
||||||
func Parse(versionName string) (version Version) {
|
|
||||||
if strings.HasPrefix(versionName, "v") {
|
|
||||||
versionName = versionName[1:]
|
|
||||||
}
|
|
||||||
if strings.Contains(versionName, "-") {
|
|
||||||
parts := strings.Split(versionName, "-")
|
|
||||||
versionName = parts[0]
|
|
||||||
identifier := parts[1]
|
|
||||||
if strings.Contains(identifier, ".") {
|
|
||||||
identifierParts := strings.Split(identifier, ".")
|
|
||||||
version.PreReleaseIdentifier = identifierParts[0]
|
|
||||||
if len(identifierParts) >= 2 {
|
|
||||||
version.PreReleaseVersion, _ = strconv.Atoi(identifierParts[1])
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if strings.HasPrefix(identifier, "alpha") {
|
|
||||||
version.PreReleaseIdentifier = "alpha"
|
|
||||||
version.PreReleaseVersion, _ = strconv.Atoi(identifier[5:])
|
|
||||||
} else if strings.HasPrefix(identifier, "beta") {
|
|
||||||
version.PreReleaseIdentifier = "beta"
|
|
||||||
version.PreReleaseVersion, _ = strconv.Atoi(identifier[4:])
|
|
||||||
} else {
|
|
||||||
version.PreReleaseIdentifier = identifier
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
versionElements := strings.Split(versionName, ".")
|
|
||||||
versionLen := len(versionElements)
|
|
||||||
if versionLen >= 1 {
|
|
||||||
version.Major, _ = strconv.Atoi(versionElements[0])
|
|
||||||
}
|
|
||||||
if versionLen >= 2 {
|
|
||||||
version.Minor, _ = strconv.Atoi(versionElements[1])
|
|
||||||
}
|
|
||||||
if versionLen >= 3 {
|
|
||||||
version.Patch, _ = strconv.Atoi(versionElements[2])
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
package badversion
|
|
||||||
|
|
||||||
import "github.com/sagernet/sing-box/common/json"
|
|
||||||
|
|
||||||
func (v Version) MarshalJSON() ([]byte, error) {
|
|
||||||
return json.Marshal(v.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
func (v *Version) UnmarshalJSON(data []byte) error {
|
|
||||||
var version string
|
|
||||||
err := json.Unmarshal(data, &version)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
*v = Parse(version)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
package badversion
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestCompareVersion(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
require.Equal(t, "1.3.0-beta.1", Parse("v1.3.0-beta1").String())
|
|
||||||
require.Equal(t, "1.3-beta1", Parse("v1.3.0-beta.1").BadString())
|
|
||||||
require.True(t, Parse("1.3.0").After(Parse("1.3-beta1")))
|
|
||||||
require.True(t, Parse("1.3.0").After(Parse("1.3.0-beta1")))
|
|
||||||
require.True(t, Parse("1.3.0-beta1").After(Parse("1.3.0-alpha1")))
|
|
||||||
require.True(t, Parse("1.3.1").After(Parse("1.3.0")))
|
|
||||||
require.True(t, Parse("1.4").After(Parse("1.3")))
|
|
||||||
}
|
|
||||||
48
common/canceler/instance.go
Normal file
48
common/canceler/instance.go
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
package canceler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Instance struct {
|
||||||
|
ctx context.Context
|
||||||
|
cancelFunc context.CancelFunc
|
||||||
|
timer *time.Timer
|
||||||
|
timeout time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(ctx context.Context, cancelFunc context.CancelFunc, timeout time.Duration) *Instance {
|
||||||
|
instance := &Instance{
|
||||||
|
ctx,
|
||||||
|
cancelFunc,
|
||||||
|
time.NewTimer(timeout),
|
||||||
|
timeout,
|
||||||
|
}
|
||||||
|
go instance.wait()
|
||||||
|
return instance
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Instance) Update() bool {
|
||||||
|
if !i.timer.Stop() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if !i.timer.Reset(i.timeout) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Instance) wait() {
|
||||||
|
select {
|
||||||
|
case <-i.timer.C:
|
||||||
|
case <-i.ctx.Done():
|
||||||
|
}
|
||||||
|
i.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Instance) Close() error {
|
||||||
|
i.timer.Stop()
|
||||||
|
i.cancelFunc()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
49
common/canceler/packet.go
Normal file
49
common/canceler/packet.go
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
package canceler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/sagernet/sing/common"
|
||||||
|
"github.com/sagernet/sing/common/buf"
|
||||||
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
|
N "github.com/sagernet/sing/common/network"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PacketConn struct {
|
||||||
|
N.PacketConn
|
||||||
|
instance *Instance
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewPacketConn(ctx context.Context, conn N.PacketConn, timeout time.Duration) (context.Context, N.PacketConn) {
|
||||||
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
|
instance := New(ctx, cancel, timeout)
|
||||||
|
return ctx, &PacketConn{conn, instance}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *PacketConn) ReadPacket(buffer *buf.Buffer) (destination M.Socksaddr, err error) {
|
||||||
|
destination, err = c.PacketConn.ReadPacket(buffer)
|
||||||
|
if err == nil {
|
||||||
|
c.instance.Update()
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *PacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error {
|
||||||
|
err := c.PacketConn.WritePacket(buffer, destination)
|
||||||
|
if err == nil {
|
||||||
|
c.instance.Update()
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *PacketConn) Close() error {
|
||||||
|
return common.Close(
|
||||||
|
c.PacketConn,
|
||||||
|
c.instance,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *PacketConn) Upstream() any {
|
||||||
|
return c.PacketConn
|
||||||
|
}
|
||||||
87
common/debugio/log.go
Normal file
87
common/debugio/log.go
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
package debugio
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
|
||||||
|
"github.com/sagernet/sing-box/log"
|
||||||
|
"github.com/sagernet/sing/common/buf"
|
||||||
|
"github.com/sagernet/sing/common/bufio"
|
||||||
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
|
N "github.com/sagernet/sing/common/network"
|
||||||
|
)
|
||||||
|
|
||||||
|
type LogConn struct {
|
||||||
|
N.ExtendedConn
|
||||||
|
logger log.Logger
|
||||||
|
prefix string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewLogConn(conn net.Conn, logger log.Logger, prefix string) N.ExtendedConn {
|
||||||
|
return &LogConn{bufio.NewExtendedConn(conn), logger, prefix}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *LogConn) Read(p []byte) (n int, err error) {
|
||||||
|
n, err = c.ExtendedConn.Read(p)
|
||||||
|
if n > 0 {
|
||||||
|
c.logger.Debug(c.prefix, " read ", buf.EncodeHexString(p[:n]))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *LogConn) Write(p []byte) (n int, err error) {
|
||||||
|
c.logger.Debug(c.prefix, " write ", buf.EncodeHexString(p))
|
||||||
|
return c.ExtendedConn.Write(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *LogConn) ReadBuffer(buffer *buf.Buffer) error {
|
||||||
|
err := c.ExtendedConn.ReadBuffer(buffer)
|
||||||
|
if err == nil {
|
||||||
|
c.logger.Debug(c.prefix, " read buffer ", buf.EncodeHexString(buffer.Bytes()))
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *LogConn) WriteBuffer(buffer *buf.Buffer) error {
|
||||||
|
c.logger.Debug(c.prefix, " write buffer ", buf.EncodeHexString(buffer.Bytes()))
|
||||||
|
return c.ExtendedConn.WriteBuffer(buffer)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *LogConn) Upstream() any {
|
||||||
|
return c.ExtendedConn
|
||||||
|
}
|
||||||
|
|
||||||
|
type LogPacketConn struct {
|
||||||
|
N.NetPacketConn
|
||||||
|
logger log.Logger
|
||||||
|
prefix string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewLogPacketConn(conn net.PacketConn, logger log.Logger, prefix string) N.NetPacketConn {
|
||||||
|
return &LogPacketConn{bufio.NewPacketConn(conn), logger, prefix}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *LogPacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
|
||||||
|
n, addr, err = c.NetPacketConn.ReadFrom(p)
|
||||||
|
if n > 0 {
|
||||||
|
c.logger.Debug(c.prefix, " read from ", addr, " ", buf.EncodeHexString(p[:n]))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *LogPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
|
||||||
|
c.logger.Debug(c.prefix, " write to ", addr, " ", buf.EncodeHexString(p))
|
||||||
|
return c.NetPacketConn.WriteTo(p, addr)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *LogPacketConn) ReadPacket(buffer *buf.Buffer) (destination M.Socksaddr, err error) {
|
||||||
|
destination, err = c.NetPacketConn.ReadPacket(buffer)
|
||||||
|
if err == nil {
|
||||||
|
c.logger.Debug(c.prefix, " read packet from ", destination, " ", buf.EncodeHexString(buffer.Bytes()))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *LogPacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error {
|
||||||
|
c.logger.Debug(c.prefix, " write packet to ", destination, " ", buf.EncodeHexString(buffer.Bytes()))
|
||||||
|
return c.NetPacketConn.WritePacket(buffer, destination)
|
||||||
|
}
|
||||||
19
common/debugio/print.go
Normal file
19
common/debugio/print.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package debugio
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
|
"github.com/sagernet/sing/common"
|
||||||
|
)
|
||||||
|
|
||||||
|
func PrintUpstream(obj any) {
|
||||||
|
for obj != nil {
|
||||||
|
fmt.Println(reflect.TypeOf(obj))
|
||||||
|
if u, ok := obj.(common.WithUpstream); !ok {
|
||||||
|
break
|
||||||
|
} else {
|
||||||
|
obj = u.Upstream()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
48
common/debugio/race.go
Normal file
48
common/debugio/race.go
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
package debugio
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/sagernet/sing/common/buf"
|
||||||
|
"github.com/sagernet/sing/common/bufio"
|
||||||
|
N "github.com/sagernet/sing/common/network"
|
||||||
|
)
|
||||||
|
|
||||||
|
type RaceConn struct {
|
||||||
|
N.ExtendedConn
|
||||||
|
readAccess sync.Mutex
|
||||||
|
writeAccess sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRaceConn(conn net.Conn) N.ExtendedConn {
|
||||||
|
return &RaceConn{ExtendedConn: bufio.NewExtendedConn(conn)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *RaceConn) Read(p []byte) (n int, err error) {
|
||||||
|
c.readAccess.Lock()
|
||||||
|
defer c.readAccess.Unlock()
|
||||||
|
return c.ExtendedConn.Read(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *RaceConn) Write(p []byte) (n int, err error) {
|
||||||
|
c.writeAccess.Lock()
|
||||||
|
defer c.writeAccess.Unlock()
|
||||||
|
return c.ExtendedConn.Write(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *RaceConn) ReadBuffer(buffer *buf.Buffer) error {
|
||||||
|
c.readAccess.Lock()
|
||||||
|
defer c.readAccess.Unlock()
|
||||||
|
return c.ExtendedConn.ReadBuffer(buffer)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *RaceConn) WriteBuffer(buffer *buf.Buffer) error {
|
||||||
|
c.writeAccess.Lock()
|
||||||
|
defer c.writeAccess.Unlock()
|
||||||
|
return c.ExtendedConn.WriteBuffer(buffer)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *RaceConn) Upstream() any {
|
||||||
|
return c.ExtendedConn
|
||||||
|
}
|
||||||
@@ -12,7 +12,7 @@ type Conn struct {
|
|||||||
element *list.Element[io.Closer]
|
element *list.Element[io.Closer]
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConn(conn net.Conn) (net.Conn, error) {
|
func NewConn(conn net.Conn) (*Conn, error) {
|
||||||
connAccess.Lock()
|
connAccess.Lock()
|
||||||
element := openConnection.PushBack(conn)
|
element := openConnection.PushBack(conn)
|
||||||
connAccess.Unlock()
|
connAccess.Unlock()
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/sagernet/sing/common/bufio"
|
|
||||||
"github.com/sagernet/sing/common/x/list"
|
"github.com/sagernet/sing/common/x/list"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -13,7 +12,7 @@ type PacketConn struct {
|
|||||||
element *list.Element[io.Closer]
|
element *list.Element[io.Closer]
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPacketConn(conn net.PacketConn) (net.PacketConn, error) {
|
func NewPacketConn(conn net.PacketConn) (*PacketConn, error) {
|
||||||
connAccess.Lock()
|
connAccess.Lock()
|
||||||
element := openConnection.PushBack(conn)
|
element := openConnection.PushBack(conn)
|
||||||
connAccess.Unlock()
|
connAccess.Unlock()
|
||||||
@@ -43,7 +42,7 @@ func (c *PacketConn) Close() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *PacketConn) Upstream() any {
|
func (c *PacketConn) Upstream() any {
|
||||||
return bufio.NewPacketConn(c.PacketConn)
|
return c.PacketConn
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *PacketConn) ReaderReplaceable() bool {
|
func (c *PacketConn) ReaderReplaceable() bool {
|
||||||
|
|||||||
@@ -14,16 +14,10 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Count() int {
|
func Count() int {
|
||||||
if !Enabled {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
return openConnection.Len()
|
return openConnection.Len()
|
||||||
}
|
}
|
||||||
|
|
||||||
func List() []io.Closer {
|
func List() []io.Closer {
|
||||||
if !Enabled {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
connAccess.RLock()
|
connAccess.RLock()
|
||||||
defer connAccess.RUnlock()
|
defer connAccess.RUnlock()
|
||||||
connList := make([]io.Closer, 0, openConnection.Len())
|
connList := make([]io.Closer, 0, openConnection.Len())
|
||||||
@@ -34,9 +28,6 @@ func List() []io.Closer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Close() {
|
func Close() {
|
||||||
if !Enabled {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
connAccess.Lock()
|
connAccess.Lock()
|
||||||
defer connAccess.Unlock()
|
defer connAccess.Unlock()
|
||||||
for element := openConnection.Front(); element != nil; element = element.Next() {
|
for element := openConnection.Front(); element != nil; element = element.Next() {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
|
|
||||||
"github.com/sagernet/sing-box/adapter"
|
"github.com/sagernet/sing-box/adapter"
|
||||||
"github.com/sagernet/sing-box/common/dialer/conntrack"
|
"github.com/sagernet/sing-box/common/dialer/conntrack"
|
||||||
|
"github.com/sagernet/sing-box/common/warning"
|
||||||
C "github.com/sagernet/sing-box/constant"
|
C "github.com/sagernet/sing-box/constant"
|
||||||
"github.com/sagernet/sing-box/option"
|
"github.com/sagernet/sing-box/option"
|
||||||
"github.com/sagernet/sing/common/control"
|
"github.com/sagernet/sing/common/control"
|
||||||
@@ -16,6 +17,41 @@ import (
|
|||||||
"github.com/sagernet/tfo-go"
|
"github.com/sagernet/tfo-go"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var warnBindInterfaceOnUnsupportedPlatform = warning.New(
|
||||||
|
func() bool {
|
||||||
|
return !(C.IsLinux || C.IsWindows || C.IsDarwin)
|
||||||
|
},
|
||||||
|
"outbound option `bind_interface` is only supported on Linux and Windows",
|
||||||
|
)
|
||||||
|
|
||||||
|
var warnRoutingMarkOnUnsupportedPlatform = warning.New(
|
||||||
|
func() bool {
|
||||||
|
return !C.IsLinux
|
||||||
|
},
|
||||||
|
"outbound option `routing_mark` is only supported on Linux",
|
||||||
|
)
|
||||||
|
|
||||||
|
var warnReuseAdderOnUnsupportedPlatform = warning.New(
|
||||||
|
func() bool {
|
||||||
|
return !(C.IsDarwin || C.IsDragonfly || C.IsFreebsd || C.IsLinux || C.IsNetbsd || C.IsOpenbsd || C.IsSolaris || C.IsWindows)
|
||||||
|
},
|
||||||
|
"outbound option `reuse_addr` is unsupported on current platform",
|
||||||
|
)
|
||||||
|
|
||||||
|
var warnProtectPathOnNonAndroid = warning.New(
|
||||||
|
func() bool {
|
||||||
|
return !C.IsAndroid
|
||||||
|
},
|
||||||
|
"outbound option `protect_path` is only supported on Android",
|
||||||
|
)
|
||||||
|
|
||||||
|
var warnTFOOnUnsupportedPlatform = warning.New(
|
||||||
|
func() bool {
|
||||||
|
return !(C.IsDarwin || C.IsFreebsd || C.IsLinux || C.IsWindows)
|
||||||
|
},
|
||||||
|
"outbound option `tcp_fast_open` is unsupported on current platform",
|
||||||
|
)
|
||||||
|
|
||||||
type DefaultDialer struct {
|
type DefaultDialer struct {
|
||||||
dialer4 tfo.Dialer
|
dialer4 tfo.Dialer
|
||||||
dialer6 tfo.Dialer
|
dialer6 tfo.Dialer
|
||||||
@@ -30,6 +66,7 @@ func NewDefault(router adapter.Router, options option.DialerOptions) *DefaultDia
|
|||||||
var dialer net.Dialer
|
var dialer net.Dialer
|
||||||
var listener net.ListenConfig
|
var listener net.ListenConfig
|
||||||
if options.BindInterface != "" {
|
if options.BindInterface != "" {
|
||||||
|
warnBindInterfaceOnUnsupportedPlatform.Check()
|
||||||
bindFunc := control.BindToInterface(router.InterfaceFinder(), options.BindInterface, -1)
|
bindFunc := control.BindToInterface(router.InterfaceFinder(), options.BindInterface, -1)
|
||||||
dialer.Control = control.Append(dialer.Control, bindFunc)
|
dialer.Control = control.Append(dialer.Control, bindFunc)
|
||||||
listener.Control = control.Append(listener.Control, bindFunc)
|
listener.Control = control.Append(listener.Control, bindFunc)
|
||||||
@@ -43,6 +80,7 @@ func NewDefault(router adapter.Router, options option.DialerOptions) *DefaultDia
|
|||||||
listener.Control = control.Append(listener.Control, bindFunc)
|
listener.Control = control.Append(listener.Control, bindFunc)
|
||||||
}
|
}
|
||||||
if options.RoutingMark != 0 {
|
if options.RoutingMark != 0 {
|
||||||
|
warnRoutingMarkOnUnsupportedPlatform.Check()
|
||||||
dialer.Control = control.Append(dialer.Control, control.RoutingMark(options.RoutingMark))
|
dialer.Control = control.Append(dialer.Control, control.RoutingMark(options.RoutingMark))
|
||||||
listener.Control = control.Append(listener.Control, control.RoutingMark(options.RoutingMark))
|
listener.Control = control.Append(listener.Control, control.RoutingMark(options.RoutingMark))
|
||||||
} else if router.DefaultMark() != 0 {
|
} else if router.DefaultMark() != 0 {
|
||||||
@@ -50,9 +88,11 @@ func NewDefault(router adapter.Router, options option.DialerOptions) *DefaultDia
|
|||||||
listener.Control = control.Append(listener.Control, control.RoutingMark(router.DefaultMark()))
|
listener.Control = control.Append(listener.Control, control.RoutingMark(router.DefaultMark()))
|
||||||
}
|
}
|
||||||
if options.ReuseAddr {
|
if options.ReuseAddr {
|
||||||
|
warnReuseAdderOnUnsupportedPlatform.Check()
|
||||||
listener.Control = control.Append(listener.Control, control.ReuseAddr())
|
listener.Control = control.Append(listener.Control, control.ReuseAddr())
|
||||||
}
|
}
|
||||||
if options.ProtectPath != "" {
|
if options.ProtectPath != "" {
|
||||||
|
warnProtectPathOnNonAndroid.Check()
|
||||||
dialer.Control = control.Append(dialer.Control, control.ProtectPath(options.ProtectPath))
|
dialer.Control = control.Append(dialer.Control, control.ProtectPath(options.ProtectPath))
|
||||||
listener.Control = control.Append(listener.Control, control.ProtectPath(options.ProtectPath))
|
listener.Control = control.Append(listener.Control, control.ProtectPath(options.ProtectPath))
|
||||||
}
|
}
|
||||||
@@ -61,6 +101,9 @@ func NewDefault(router adapter.Router, options option.DialerOptions) *DefaultDia
|
|||||||
} else {
|
} else {
|
||||||
dialer.Timeout = C.TCPTimeout
|
dialer.Timeout = C.TCPTimeout
|
||||||
}
|
}
|
||||||
|
if options.TCPFastOpen {
|
||||||
|
warnTFOOnUnsupportedPlatform.Check()
|
||||||
|
}
|
||||||
var udpFragment bool
|
var udpFragment bool
|
||||||
if options.UDPFragment != nil {
|
if options.UDPFragment != nil {
|
||||||
udpFragment = *options.UDPFragment
|
udpFragment = *options.UDPFragment
|
||||||
@@ -111,9 +154,9 @@ func (d *DefaultDialer) DialContext(ctx context.Context, network string, address
|
|||||||
switch N.NetworkName(network) {
|
switch N.NetworkName(network) {
|
||||||
case N.NetworkUDP:
|
case N.NetworkUDP:
|
||||||
if !address.IsIPv6() {
|
if !address.IsIPv6() {
|
||||||
return trackConn(d.udpDialer4.DialContext(ctx, network, address.String()))
|
return d.udpDialer4.DialContext(ctx, network, address.String())
|
||||||
} else {
|
} else {
|
||||||
return trackConn(d.udpDialer6.DialContext(ctx, network, address.String()))
|
return d.udpDialer6.DialContext(ctx, network, address.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !address.IsIPv6() {
|
if !address.IsIPv6() {
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ import (
|
|||||||
"github.com/sagernet/sing-box/adapter"
|
"github.com/sagernet/sing-box/adapter"
|
||||||
"github.com/sagernet/sing-box/log"
|
"github.com/sagernet/sing-box/log"
|
||||||
"github.com/sagernet/sing-dns"
|
"github.com/sagernet/sing-dns"
|
||||||
"github.com/sagernet/sing/common/bufio"
|
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
N "github.com/sagernet/sing/common/network"
|
N "github.com/sagernet/sing/common/network"
|
||||||
)
|
)
|
||||||
@@ -69,11 +68,11 @@ func (d *ResolveDialer) ListenPacket(ctx context.Context, destination M.Socksadd
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
conn, destinationAddress, err := N.ListenSerial(ctx, d.dialer, destination, addresses)
|
conn, err := N.ListenSerial(ctx, d.dialer, destination, addresses)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return bufio.NewNATPacketConn(bufio.NewPacketConn(conn), M.SocksaddrFrom(destinationAddress, destination.Port), destination), nil
|
return NewResolvePacketConn(ctx, d.router, d.strategy, conn), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *ResolveDialer) Upstream() any {
|
func (d *ResolveDialer) Upstream() any {
|
||||||
|
|||||||
84
common/dialer/resolve_conn.go
Normal file
84
common/dialer/resolve_conn.go
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
package dialer
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net"
|
||||||
|
|
||||||
|
"github.com/sagernet/sing-box/adapter"
|
||||||
|
"github.com/sagernet/sing-dns"
|
||||||
|
"github.com/sagernet/sing/common"
|
||||||
|
"github.com/sagernet/sing/common/buf"
|
||||||
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
|
N "github.com/sagernet/sing/common/network"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewResolvePacketConn(ctx context.Context, router adapter.Router, strategy dns.DomainStrategy, conn net.PacketConn) N.NetPacketConn {
|
||||||
|
if udpConn, ok := conn.(*net.UDPConn); ok {
|
||||||
|
return &ResolveUDPConn{udpConn, ctx, router, strategy}
|
||||||
|
} else {
|
||||||
|
return &ResolvePacketConn{conn, ctx, router, strategy}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type ResolveUDPConn struct {
|
||||||
|
*net.UDPConn
|
||||||
|
ctx context.Context
|
||||||
|
router adapter.Router
|
||||||
|
strategy dns.DomainStrategy
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *ResolveUDPConn) ReadPacket(buffer *buf.Buffer) (M.Socksaddr, error) {
|
||||||
|
n, addr, err := w.ReadFromUDPAddrPort(buffer.FreeBytes())
|
||||||
|
if err != nil {
|
||||||
|
return M.Socksaddr{}, err
|
||||||
|
}
|
||||||
|
buffer.Truncate(n)
|
||||||
|
return M.SocksaddrFromNetIP(addr), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *ResolveUDPConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error {
|
||||||
|
defer buffer.Release()
|
||||||
|
if destination.IsFqdn() {
|
||||||
|
addresses, err := w.router.Lookup(w.ctx, destination.Fqdn, w.strategy)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return common.Error(w.UDPConn.WriteToUDPAddrPort(buffer.Bytes(), M.SocksaddrFrom(addresses[0], destination.Port).AddrPort()))
|
||||||
|
}
|
||||||
|
return common.Error(w.UDPConn.WriteToUDPAddrPort(buffer.Bytes(), destination.AddrPort()))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *ResolveUDPConn) Upstream() any {
|
||||||
|
return w.UDPConn
|
||||||
|
}
|
||||||
|
|
||||||
|
type ResolvePacketConn struct {
|
||||||
|
net.PacketConn
|
||||||
|
ctx context.Context
|
||||||
|
router adapter.Router
|
||||||
|
strategy dns.DomainStrategy
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *ResolvePacketConn) ReadPacket(buffer *buf.Buffer) (M.Socksaddr, error) {
|
||||||
|
_, addr, err := buffer.ReadPacketFrom(w)
|
||||||
|
if err != nil {
|
||||||
|
return M.Socksaddr{}, err
|
||||||
|
}
|
||||||
|
return M.SocksaddrFromNet(addr), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *ResolvePacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error {
|
||||||
|
defer buffer.Release()
|
||||||
|
if destination.IsFqdn() {
|
||||||
|
addresses, err := w.router.Lookup(w.ctx, destination.Fqdn, w.strategy)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return common.Error(w.WriteTo(buffer.Bytes(), M.SocksaddrFrom(addresses[0], destination.Port).UDPAddr()))
|
||||||
|
}
|
||||||
|
return common.Error(w.WriteTo(buffer.Bytes(), destination.UDPAddr()))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *ResolvePacketConn) Upstream() any {
|
||||||
|
return w.PacketConn
|
||||||
|
}
|
||||||
@@ -124,8 +124,11 @@ func (c *slowOpenConn) LazyHeadroom() bool {
|
|||||||
return c.conn == nil
|
return c.conn == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *slowOpenConn) NeedHandshake() bool {
|
func (c *slowOpenConn) ReadFrom(r io.Reader) (n int64, err error) {
|
||||||
return c.conn == nil
|
if c.conn != nil {
|
||||||
|
return bufio.Copy(c.conn, r)
|
||||||
|
}
|
||||||
|
return bufio.ReadFrom0(c, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *slowOpenConn) WriteTo(w io.Writer) (n int64, err error) {
|
func (c *slowOpenConn) WriteTo(w io.Writer) (n int64, err error) {
|
||||||
|
|||||||
@@ -1,21 +1,519 @@
|
|||||||
package mux
|
package mux
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/binary"
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/sagernet/sing-box/option"
|
"github.com/sagernet/sing-box/option"
|
||||||
"github.com/sagernet/sing-mux"
|
"github.com/sagernet/sing/common"
|
||||||
|
"github.com/sagernet/sing/common/buf"
|
||||||
|
"github.com/sagernet/sing/common/bufio"
|
||||||
|
E "github.com/sagernet/sing/common/exceptions"
|
||||||
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
N "github.com/sagernet/sing/common/network"
|
N "github.com/sagernet/sing/common/network"
|
||||||
|
"github.com/sagernet/sing/common/x/list"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewClientWithOptions(dialer N.Dialer, options option.MultiplexOptions) (*Client, error) {
|
var _ N.Dialer = (*Client)(nil)
|
||||||
|
|
||||||
|
type Client struct {
|
||||||
|
access sync.Mutex
|
||||||
|
connections list.List[abstractSession]
|
||||||
|
ctx context.Context
|
||||||
|
dialer N.Dialer
|
||||||
|
protocol Protocol
|
||||||
|
maxConnections int
|
||||||
|
minStreams int
|
||||||
|
maxStreams int
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewClient(ctx context.Context, dialer N.Dialer, protocol Protocol, maxConnections int, minStreams int, maxStreams int) *Client {
|
||||||
|
return &Client{
|
||||||
|
ctx: ctx,
|
||||||
|
dialer: dialer,
|
||||||
|
protocol: protocol,
|
||||||
|
maxConnections: maxConnections,
|
||||||
|
minStreams: minStreams,
|
||||||
|
maxStreams: maxStreams,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewClientWithOptions(ctx context.Context, dialer N.Dialer, options option.MultiplexOptions) (N.Dialer, error) {
|
||||||
if !options.Enabled {
|
if !options.Enabled {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
return mux.NewClient(mux.Options{
|
if options.MaxConnections == 0 && options.MaxStreams == 0 {
|
||||||
Dialer: dialer,
|
options.MinStreams = 8
|
||||||
Protocol: options.Protocol,
|
}
|
||||||
MaxConnections: options.MaxConnections,
|
protocol, err := ParseProtocol(options.Protocol)
|
||||||
MinStreams: options.MinStreams,
|
if err != nil {
|
||||||
MaxStreams: options.MaxStreams,
|
return nil, err
|
||||||
Padding: options.Padding,
|
}
|
||||||
})
|
return NewClient(ctx, dialer, protocol, options.MaxConnections, options.MinStreams, options.MaxStreams), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
|
||||||
|
switch N.NetworkName(network) {
|
||||||
|
case N.NetworkTCP:
|
||||||
|
stream, err := c.openStream()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &ClientConn{Conn: stream, destination: destination}, nil
|
||||||
|
case N.NetworkUDP:
|
||||||
|
stream, err := c.openStream()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return bufio.NewUnbindPacketConn(&ClientPacketConn{ExtendedConn: bufio.NewExtendedConn(stream), destination: destination}), nil
|
||||||
|
default:
|
||||||
|
return nil, E.Extend(N.ErrUnknownNetwork, network)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
|
||||||
|
stream, err := c.openStream()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &ClientPacketAddrConn{ExtendedConn: bufio.NewExtendedConn(stream), destination: destination}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) openStream() (net.Conn, error) {
|
||||||
|
var (
|
||||||
|
session abstractSession
|
||||||
|
stream net.Conn
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
for attempts := 0; attempts < 2; attempts++ {
|
||||||
|
session, err = c.offer()
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
stream, err = session.Open()
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &wrapStream{stream}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) offer() (abstractSession, error) {
|
||||||
|
c.access.Lock()
|
||||||
|
defer c.access.Unlock()
|
||||||
|
|
||||||
|
sessions := make([]abstractSession, 0, c.maxConnections)
|
||||||
|
for element := c.connections.Front(); element != nil; {
|
||||||
|
if element.Value.IsClosed() {
|
||||||
|
nextElement := element.Next()
|
||||||
|
c.connections.Remove(element)
|
||||||
|
element = nextElement
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
sessions = append(sessions, element.Value)
|
||||||
|
element = element.Next()
|
||||||
|
}
|
||||||
|
sLen := len(sessions)
|
||||||
|
if sLen == 0 {
|
||||||
|
return c.offerNew()
|
||||||
|
}
|
||||||
|
session := common.MinBy(sessions, abstractSession.NumStreams)
|
||||||
|
numStreams := session.NumStreams()
|
||||||
|
if numStreams == 0 {
|
||||||
|
return session, nil
|
||||||
|
}
|
||||||
|
if c.maxConnections > 0 {
|
||||||
|
if sLen >= c.maxConnections || numStreams < c.minStreams {
|
||||||
|
return session, nil
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if c.maxStreams > 0 && numStreams < c.maxStreams {
|
||||||
|
return session, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return c.offerNew()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) offerNew() (abstractSession, error) {
|
||||||
|
conn, err := c.dialer.DialContext(c.ctx, N.NetworkTCP, Destination)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if vectorisedWriter, isVectorised := bufio.CreateVectorisedWriter(conn); isVectorised {
|
||||||
|
conn = &vectorisedProtocolConn{protocolConn{Conn: conn, protocol: c.protocol}, vectorisedWriter}
|
||||||
|
} else {
|
||||||
|
conn = &protocolConn{Conn: conn, protocol: c.protocol}
|
||||||
|
}
|
||||||
|
session, err := c.protocol.newClient(conn)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
c.connections.PushBack(session)
|
||||||
|
return session, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) Close() error {
|
||||||
|
c.access.Lock()
|
||||||
|
defer c.access.Unlock()
|
||||||
|
for _, session := range c.connections.Array() {
|
||||||
|
session.Close()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type ClientConn struct {
|
||||||
|
net.Conn
|
||||||
|
destination M.Socksaddr
|
||||||
|
requestWrite bool
|
||||||
|
responseRead bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ClientConn) readResponse() error {
|
||||||
|
response, err := ReadStreamResponse(c.Conn)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if response.Status == statusError {
|
||||||
|
return E.New("remote error: ", response.Message)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ClientConn) Read(b []byte) (n int, err error) {
|
||||||
|
if !c.responseRead {
|
||||||
|
err = c.readResponse()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.responseRead = true
|
||||||
|
}
|
||||||
|
return c.Conn.Read(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ClientConn) Write(b []byte) (n int, err error) {
|
||||||
|
if c.requestWrite {
|
||||||
|
return c.Conn.Write(b)
|
||||||
|
}
|
||||||
|
request := StreamRequest{
|
||||||
|
Network: N.NetworkTCP,
|
||||||
|
Destination: c.destination,
|
||||||
|
}
|
||||||
|
_buffer := buf.StackNewSize(requestLen(request) + len(b))
|
||||||
|
defer common.KeepAlive(_buffer)
|
||||||
|
buffer := common.Dup(_buffer)
|
||||||
|
defer buffer.Release()
|
||||||
|
EncodeStreamRequest(request, buffer)
|
||||||
|
buffer.Write(b)
|
||||||
|
_, err = c.Conn.Write(buffer.Bytes())
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.requestWrite = true
|
||||||
|
return len(b), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ClientConn) ReadFrom(r io.Reader) (n int64, err error) {
|
||||||
|
if !c.requestWrite {
|
||||||
|
return bufio.ReadFrom0(c, r)
|
||||||
|
}
|
||||||
|
return bufio.Copy(c.Conn, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ClientConn) WriteTo(w io.Writer) (n int64, err error) {
|
||||||
|
if !c.responseRead {
|
||||||
|
return bufio.WriteTo0(c, w)
|
||||||
|
}
|
||||||
|
return bufio.Copy(w, c.Conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ClientConn) LocalAddr() net.Addr {
|
||||||
|
return c.Conn.LocalAddr()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ClientConn) RemoteAddr() net.Addr {
|
||||||
|
return c.destination.TCPAddr()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ClientConn) ReaderReplaceable() bool {
|
||||||
|
return c.responseRead
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ClientConn) WriterReplaceable() bool {
|
||||||
|
return c.requestWrite
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ClientConn) Upstream() any {
|
||||||
|
return c.Conn
|
||||||
|
}
|
||||||
|
|
||||||
|
type ClientPacketConn struct {
|
||||||
|
N.ExtendedConn
|
||||||
|
destination M.Socksaddr
|
||||||
|
requestWrite bool
|
||||||
|
responseRead bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ClientPacketConn) readResponse() error {
|
||||||
|
response, err := ReadStreamResponse(c.ExtendedConn)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if response.Status == statusError {
|
||||||
|
return E.New("remote error: ", response.Message)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ClientPacketConn) Read(b []byte) (n int, err error) {
|
||||||
|
if !c.responseRead {
|
||||||
|
err = c.readResponse()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.responseRead = true
|
||||||
|
}
|
||||||
|
var length uint16
|
||||||
|
err = binary.Read(c.ExtendedConn, binary.BigEndian, &length)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if cap(b) < int(length) {
|
||||||
|
return 0, io.ErrShortBuffer
|
||||||
|
}
|
||||||
|
return io.ReadFull(c.ExtendedConn, b[:length])
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ClientPacketConn) writeRequest(payload []byte) (n int, err error) {
|
||||||
|
request := StreamRequest{
|
||||||
|
Network: N.NetworkUDP,
|
||||||
|
Destination: c.destination,
|
||||||
|
}
|
||||||
|
rLen := requestLen(request)
|
||||||
|
if len(payload) > 0 {
|
||||||
|
rLen += 2 + len(payload)
|
||||||
|
}
|
||||||
|
_buffer := buf.StackNewSize(rLen)
|
||||||
|
defer common.KeepAlive(_buffer)
|
||||||
|
buffer := common.Dup(_buffer)
|
||||||
|
defer buffer.Release()
|
||||||
|
EncodeStreamRequest(request, buffer)
|
||||||
|
if len(payload) > 0 {
|
||||||
|
common.Must(
|
||||||
|
binary.Write(buffer, binary.BigEndian, uint16(len(payload))),
|
||||||
|
common.Error(buffer.Write(payload)),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
_, err = c.ExtendedConn.Write(buffer.Bytes())
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.requestWrite = true
|
||||||
|
return len(payload), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ClientPacketConn) Write(b []byte) (n int, err error) {
|
||||||
|
if !c.requestWrite {
|
||||||
|
return c.writeRequest(b)
|
||||||
|
}
|
||||||
|
err = binary.Write(c.ExtendedConn, binary.BigEndian, uint16(len(b)))
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return c.ExtendedConn.Write(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ClientPacketConn) ReadBuffer(buffer *buf.Buffer) (err error) {
|
||||||
|
if !c.responseRead {
|
||||||
|
err = c.readResponse()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.responseRead = true
|
||||||
|
}
|
||||||
|
var length uint16
|
||||||
|
err = binary.Read(c.ExtendedConn, binary.BigEndian, &length)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_, err = buffer.ReadFullFrom(c.ExtendedConn, int(length))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ClientPacketConn) WriteBuffer(buffer *buf.Buffer) error {
|
||||||
|
if !c.requestWrite {
|
||||||
|
defer buffer.Release()
|
||||||
|
return common.Error(c.writeRequest(buffer.Bytes()))
|
||||||
|
}
|
||||||
|
bLen := buffer.Len()
|
||||||
|
binary.BigEndian.PutUint16(buffer.ExtendHeader(2), uint16(bLen))
|
||||||
|
return c.ExtendedConn.WriteBuffer(buffer)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ClientPacketConn) FrontHeadroom() int {
|
||||||
|
return 2
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ClientPacketConn) ReadPacket(buffer *buf.Buffer) (destination M.Socksaddr, err error) {
|
||||||
|
err = c.ReadBuffer(buffer)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ClientPacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error {
|
||||||
|
return c.WriteBuffer(buffer)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ClientPacketConn) LocalAddr() net.Addr {
|
||||||
|
return c.ExtendedConn.LocalAddr()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ClientPacketConn) RemoteAddr() net.Addr {
|
||||||
|
return c.destination.UDPAddr()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ClientPacketConn) Upstream() any {
|
||||||
|
return c.ExtendedConn
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ N.NetPacketConn = (*ClientPacketAddrConn)(nil)
|
||||||
|
|
||||||
|
type ClientPacketAddrConn struct {
|
||||||
|
N.ExtendedConn
|
||||||
|
destination M.Socksaddr
|
||||||
|
requestWrite bool
|
||||||
|
responseRead bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ClientPacketAddrConn) readResponse() error {
|
||||||
|
response, err := ReadStreamResponse(c.ExtendedConn)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if response.Status == statusError {
|
||||||
|
return E.New("remote error: ", response.Message)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ClientPacketAddrConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
|
||||||
|
if !c.responseRead {
|
||||||
|
err = c.readResponse()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.responseRead = true
|
||||||
|
}
|
||||||
|
destination, err := M.SocksaddrSerializer.ReadAddrPort(c.ExtendedConn)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
addr = destination.UDPAddr()
|
||||||
|
var length uint16
|
||||||
|
err = binary.Read(c.ExtendedConn, binary.BigEndian, &length)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if cap(p) < int(length) {
|
||||||
|
return 0, nil, io.ErrShortBuffer
|
||||||
|
}
|
||||||
|
n, err = io.ReadFull(c.ExtendedConn, p[:length])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ClientPacketAddrConn) writeRequest(payload []byte, destination M.Socksaddr) (n int, err error) {
|
||||||
|
request := StreamRequest{
|
||||||
|
Network: N.NetworkUDP,
|
||||||
|
Destination: c.destination,
|
||||||
|
PacketAddr: true,
|
||||||
|
}
|
||||||
|
rLen := requestLen(request)
|
||||||
|
if len(payload) > 0 {
|
||||||
|
rLen += M.SocksaddrSerializer.AddrPortLen(destination) + 2 + len(payload)
|
||||||
|
}
|
||||||
|
_buffer := buf.StackNewSize(rLen)
|
||||||
|
defer common.KeepAlive(_buffer)
|
||||||
|
buffer := common.Dup(_buffer)
|
||||||
|
defer buffer.Release()
|
||||||
|
EncodeStreamRequest(request, buffer)
|
||||||
|
if len(payload) > 0 {
|
||||||
|
common.Must(
|
||||||
|
M.SocksaddrSerializer.WriteAddrPort(buffer, destination),
|
||||||
|
binary.Write(buffer, binary.BigEndian, uint16(len(payload))),
|
||||||
|
common.Error(buffer.Write(payload)),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
_, err = c.ExtendedConn.Write(buffer.Bytes())
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.requestWrite = true
|
||||||
|
return len(payload), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ClientPacketAddrConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
|
||||||
|
if !c.requestWrite {
|
||||||
|
return c.writeRequest(p, M.SocksaddrFromNet(addr))
|
||||||
|
}
|
||||||
|
err = M.SocksaddrSerializer.WriteAddrPort(c.ExtendedConn, M.SocksaddrFromNet(addr))
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = binary.Write(c.ExtendedConn, binary.BigEndian, uint16(len(p)))
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return c.ExtendedConn.Write(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ClientPacketAddrConn) ReadPacket(buffer *buf.Buffer) (destination M.Socksaddr, err error) {
|
||||||
|
if !c.responseRead {
|
||||||
|
err = c.readResponse()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.responseRead = true
|
||||||
|
}
|
||||||
|
destination, err = M.SocksaddrSerializer.ReadAddrPort(c.ExtendedConn)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var length uint16
|
||||||
|
err = binary.Read(c.ExtendedConn, binary.BigEndian, &length)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_, err = buffer.ReadFullFrom(c.ExtendedConn, int(length))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ClientPacketAddrConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error {
|
||||||
|
if !c.requestWrite {
|
||||||
|
defer buffer.Release()
|
||||||
|
return common.Error(c.writeRequest(buffer.Bytes(), destination))
|
||||||
|
}
|
||||||
|
bLen := buffer.Len()
|
||||||
|
header := buf.With(buffer.ExtendHeader(M.SocksaddrSerializer.AddrPortLen(destination) + 2))
|
||||||
|
common.Must(
|
||||||
|
M.SocksaddrSerializer.WriteAddrPort(header, destination),
|
||||||
|
binary.Write(header, binary.BigEndian, uint16(bLen)),
|
||||||
|
)
|
||||||
|
return c.ExtendedConn.WriteBuffer(buffer)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ClientPacketAddrConn) LocalAddr() net.Addr {
|
||||||
|
return c.ExtendedConn.LocalAddr()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ClientPacketAddrConn) FrontHeadroom() int {
|
||||||
|
return 2 + M.MaxSocksaddrLength
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ClientPacketAddrConn) Upstream() any {
|
||||||
|
return c.ExtendedConn
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,240 @@
|
|||||||
package mux
|
package mux
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/sagernet/sing-mux"
|
"encoding/binary"
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
|
||||||
|
C "github.com/sagernet/sing-box/constant"
|
||||||
|
"github.com/sagernet/sing/common"
|
||||||
|
"github.com/sagernet/sing/common/buf"
|
||||||
|
E "github.com/sagernet/sing/common/exceptions"
|
||||||
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
|
N "github.com/sagernet/sing/common/network"
|
||||||
|
"github.com/sagernet/sing/common/rw"
|
||||||
|
"github.com/sagernet/smux"
|
||||||
|
|
||||||
|
"github.com/hashicorp/yamux"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
var Destination = M.Socksaddr{
|
||||||
Client = mux.Client
|
Fqdn: "sp.mux.sing-box.arpa",
|
||||||
|
Port: 444,
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
ProtocolSMux Protocol = iota
|
||||||
|
ProtocolYAMux
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
type Protocol byte
|
||||||
Destination = mux.Destination
|
|
||||||
HandleConnection = mux.HandleConnection
|
func ParseProtocol(name string) (Protocol, error) {
|
||||||
|
switch name {
|
||||||
|
case "", "smux":
|
||||||
|
return ProtocolSMux, nil
|
||||||
|
case "yamux":
|
||||||
|
return ProtocolYAMux, nil
|
||||||
|
default:
|
||||||
|
return ProtocolYAMux, E.New("unknown multiplex protocol: ", name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p Protocol) newServer(conn net.Conn) (abstractSession, error) {
|
||||||
|
switch p {
|
||||||
|
case ProtocolSMux:
|
||||||
|
session, err := smux.Server(conn, smuxConfig())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &smuxSession{session}, nil
|
||||||
|
case ProtocolYAMux:
|
||||||
|
return yamux.Server(conn, yaMuxConfig())
|
||||||
|
default:
|
||||||
|
panic("unknown protocol")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p Protocol) newClient(conn net.Conn) (abstractSession, error) {
|
||||||
|
switch p {
|
||||||
|
case ProtocolSMux:
|
||||||
|
session, err := smux.Client(conn, smuxConfig())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &smuxSession{session}, nil
|
||||||
|
case ProtocolYAMux:
|
||||||
|
return yamux.Client(conn, yaMuxConfig())
|
||||||
|
default:
|
||||||
|
panic("unknown protocol")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func smuxConfig() *smux.Config {
|
||||||
|
config := smux.DefaultConfig()
|
||||||
|
config.KeepAliveDisabled = true
|
||||||
|
return config
|
||||||
|
}
|
||||||
|
|
||||||
|
func yaMuxConfig() *yamux.Config {
|
||||||
|
config := yamux.DefaultConfig()
|
||||||
|
config.LogOutput = io.Discard
|
||||||
|
config.StreamCloseTimeout = C.TCPTimeout
|
||||||
|
config.StreamOpenTimeout = C.TCPTimeout
|
||||||
|
return config
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p Protocol) String() string {
|
||||||
|
switch p {
|
||||||
|
case ProtocolSMux:
|
||||||
|
return "smux"
|
||||||
|
case ProtocolYAMux:
|
||||||
|
return "yamux"
|
||||||
|
default:
|
||||||
|
return "unknown"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
version0 = 0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Request struct {
|
||||||
|
Protocol Protocol
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadRequest(reader io.Reader) (*Request, error) {
|
||||||
|
version, err := rw.ReadByte(reader)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if version != version0 {
|
||||||
|
return nil, E.New("unsupported version: ", version)
|
||||||
|
}
|
||||||
|
protocol, err := rw.ReadByte(reader)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if protocol > byte(ProtocolYAMux) {
|
||||||
|
return nil, E.New("unsupported protocol: ", protocol)
|
||||||
|
}
|
||||||
|
return &Request{Protocol: Protocol(protocol)}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func EncodeRequest(buffer *buf.Buffer, request Request) {
|
||||||
|
buffer.WriteByte(version0)
|
||||||
|
buffer.WriteByte(byte(request.Protocol))
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
flagUDP = 1
|
||||||
|
flagAddr = 2
|
||||||
|
statusSuccess = 0
|
||||||
|
statusError = 1
|
||||||
|
)
|
||||||
|
|
||||||
|
type StreamRequest struct {
|
||||||
|
Network string
|
||||||
|
Destination M.Socksaddr
|
||||||
|
PacketAddr bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadStreamRequest(reader io.Reader) (*StreamRequest, error) {
|
||||||
|
var flags uint16
|
||||||
|
err := binary.Read(reader, binary.BigEndian, &flags)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
destination, err := M.SocksaddrSerializer.ReadAddrPort(reader)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var network string
|
||||||
|
var udpAddr bool
|
||||||
|
if flags&flagUDP == 0 {
|
||||||
|
network = N.NetworkTCP
|
||||||
|
} else {
|
||||||
|
network = N.NetworkUDP
|
||||||
|
udpAddr = flags&flagAddr != 0
|
||||||
|
}
|
||||||
|
return &StreamRequest{network, destination, udpAddr}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func requestLen(request StreamRequest) int {
|
||||||
|
var rLen int
|
||||||
|
rLen += 1 // version
|
||||||
|
rLen += 2 // flags
|
||||||
|
rLen += M.SocksaddrSerializer.AddrPortLen(request.Destination)
|
||||||
|
return rLen
|
||||||
|
}
|
||||||
|
|
||||||
|
func EncodeStreamRequest(request StreamRequest, buffer *buf.Buffer) {
|
||||||
|
destination := request.Destination
|
||||||
|
var flags uint16
|
||||||
|
if request.Network == N.NetworkUDP {
|
||||||
|
flags |= flagUDP
|
||||||
|
}
|
||||||
|
if request.PacketAddr {
|
||||||
|
flags |= flagAddr
|
||||||
|
if !destination.IsValid() {
|
||||||
|
destination = Destination
|
||||||
|
}
|
||||||
|
}
|
||||||
|
common.Must(
|
||||||
|
binary.Write(buffer, binary.BigEndian, flags),
|
||||||
|
M.SocksaddrSerializer.WriteAddrPort(buffer, destination),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
type StreamResponse struct {
|
||||||
|
Status uint8
|
||||||
|
Message string
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadStreamResponse(reader io.Reader) (*StreamResponse, error) {
|
||||||
|
var response StreamResponse
|
||||||
|
status, err := rw.ReadByte(reader)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
response.Status = status
|
||||||
|
if status == statusError {
|
||||||
|
response.Message, err = rw.ReadVString(reader)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return &response, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type wrapStream struct {
|
||||||
|
net.Conn
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *wrapStream) Read(p []byte) (n int, err error) {
|
||||||
|
n, err = w.Conn.Read(p)
|
||||||
|
err = wrapError(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *wrapStream) Write(p []byte) (n int, err error) {
|
||||||
|
n, err = w.Conn.Write(p)
|
||||||
|
err = wrapError(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *wrapStream) WriteIsThreadUnsafe() {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *wrapStream) Upstream() any {
|
||||||
|
return w.Conn
|
||||||
|
}
|
||||||
|
|
||||||
|
func wrapError(err error) error {
|
||||||
|
switch err {
|
||||||
|
case yamux.ErrStreamClosed:
|
||||||
|
return io.EOF
|
||||||
|
default:
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
257
common/mux/service.go
Normal file
257
common/mux/service.go
Normal file
@@ -0,0 +1,257 @@
|
|||||||
|
package mux
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/binary"
|
||||||
|
"net"
|
||||||
|
|
||||||
|
"github.com/sagernet/sing-box/adapter"
|
||||||
|
"github.com/sagernet/sing-box/log"
|
||||||
|
"github.com/sagernet/sing/common"
|
||||||
|
"github.com/sagernet/sing/common/buf"
|
||||||
|
"github.com/sagernet/sing/common/bufio"
|
||||||
|
E "github.com/sagernet/sing/common/exceptions"
|
||||||
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
|
N "github.com/sagernet/sing/common/network"
|
||||||
|
"github.com/sagernet/sing/common/rw"
|
||||||
|
"github.com/sagernet/sing/common/task"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewConnection(ctx context.Context, router adapter.Router, errorHandler E.Handler, logger log.ContextLogger, conn net.Conn, metadata adapter.InboundContext) error {
|
||||||
|
request, err := ReadRequest(conn)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
session, err := request.Protocol.newServer(conn)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
var group task.Group
|
||||||
|
group.Append0(func(ctx context.Context) error {
|
||||||
|
var stream net.Conn
|
||||||
|
for {
|
||||||
|
stream, err = session.Accept()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
go newConnection(ctx, router, errorHandler, logger, stream, metadata)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
group.Cleanup(func() {
|
||||||
|
session.Close()
|
||||||
|
})
|
||||||
|
return group.Run(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
func newConnection(ctx context.Context, router adapter.Router, errorHandler E.Handler, logger log.ContextLogger, stream net.Conn, metadata adapter.InboundContext) {
|
||||||
|
stream = &wrapStream{stream}
|
||||||
|
request, err := ReadStreamRequest(stream)
|
||||||
|
if err != nil {
|
||||||
|
logger.ErrorContext(ctx, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
metadata.Destination = request.Destination
|
||||||
|
if request.Network == N.NetworkTCP {
|
||||||
|
logger.InfoContext(ctx, "inbound multiplex connection to ", metadata.Destination)
|
||||||
|
hErr := router.RouteConnection(ctx, &ServerConn{ExtendedConn: bufio.NewExtendedConn(stream)}, metadata)
|
||||||
|
stream.Close()
|
||||||
|
if hErr != nil {
|
||||||
|
errorHandler.NewError(ctx, hErr)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
var packetConn N.PacketConn
|
||||||
|
if !request.PacketAddr {
|
||||||
|
logger.InfoContext(ctx, "inbound multiplex packet connection to ", metadata.Destination)
|
||||||
|
packetConn = &ServerPacketConn{ExtendedConn: bufio.NewExtendedConn(stream), destination: request.Destination}
|
||||||
|
} else {
|
||||||
|
logger.InfoContext(ctx, "inbound multiplex packet connection")
|
||||||
|
packetConn = &ServerPacketAddrConn{ExtendedConn: bufio.NewExtendedConn(stream)}
|
||||||
|
}
|
||||||
|
hErr := router.RoutePacketConnection(ctx, packetConn, metadata)
|
||||||
|
stream.Close()
|
||||||
|
if hErr != nil {
|
||||||
|
errorHandler.NewError(ctx, hErr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ N.HandshakeConn = (*ServerConn)(nil)
|
||||||
|
|
||||||
|
type ServerConn struct {
|
||||||
|
N.ExtendedConn
|
||||||
|
responseWrite bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ServerConn) HandshakeFailure(err error) error {
|
||||||
|
errMessage := err.Error()
|
||||||
|
_buffer := buf.StackNewSize(1 + rw.UVariantLen(uint64(len(errMessage))) + len(errMessage))
|
||||||
|
defer common.KeepAlive(_buffer)
|
||||||
|
buffer := common.Dup(_buffer)
|
||||||
|
defer buffer.Release()
|
||||||
|
common.Must(
|
||||||
|
buffer.WriteByte(statusError),
|
||||||
|
rw.WriteVString(_buffer, errMessage),
|
||||||
|
)
|
||||||
|
return c.ExtendedConn.WriteBuffer(buffer)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ServerConn) Write(b []byte) (n int, err error) {
|
||||||
|
if c.responseWrite {
|
||||||
|
return c.ExtendedConn.Write(b)
|
||||||
|
}
|
||||||
|
_buffer := buf.StackNewSize(1 + len(b))
|
||||||
|
defer common.KeepAlive(_buffer)
|
||||||
|
buffer := common.Dup(_buffer)
|
||||||
|
defer buffer.Release()
|
||||||
|
common.Must(
|
||||||
|
buffer.WriteByte(statusSuccess),
|
||||||
|
common.Error(buffer.Write(b)),
|
||||||
|
)
|
||||||
|
_, err = c.ExtendedConn.Write(buffer.Bytes())
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.responseWrite = true
|
||||||
|
return len(b), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ServerConn) WriteBuffer(buffer *buf.Buffer) error {
|
||||||
|
if c.responseWrite {
|
||||||
|
return c.ExtendedConn.WriteBuffer(buffer)
|
||||||
|
}
|
||||||
|
buffer.ExtendHeader(1)[0] = statusSuccess
|
||||||
|
c.responseWrite = true
|
||||||
|
return c.ExtendedConn.WriteBuffer(buffer)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ServerConn) FrontHeadroom() int {
|
||||||
|
if !c.responseWrite {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ServerConn) Upstream() any {
|
||||||
|
return c.ExtendedConn
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ N.HandshakeConn = (*ServerPacketConn)(nil)
|
||||||
|
_ N.PacketConn = (*ServerPacketConn)(nil)
|
||||||
|
)
|
||||||
|
|
||||||
|
type ServerPacketConn struct {
|
||||||
|
N.ExtendedConn
|
||||||
|
destination M.Socksaddr
|
||||||
|
responseWrite bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ServerPacketConn) HandshakeFailure(err error) error {
|
||||||
|
errMessage := err.Error()
|
||||||
|
_buffer := buf.StackNewSize(1 + rw.UVariantLen(uint64(len(errMessage))) + len(errMessage))
|
||||||
|
defer common.KeepAlive(_buffer)
|
||||||
|
buffer := common.Dup(_buffer)
|
||||||
|
defer buffer.Release()
|
||||||
|
common.Must(
|
||||||
|
buffer.WriteByte(statusError),
|
||||||
|
rw.WriteVString(_buffer, errMessage),
|
||||||
|
)
|
||||||
|
return c.ExtendedConn.WriteBuffer(buffer)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ServerPacketConn) ReadPacket(buffer *buf.Buffer) (destination M.Socksaddr, err error) {
|
||||||
|
var length uint16
|
||||||
|
err = binary.Read(c.ExtendedConn, binary.BigEndian, &length)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_, err = buffer.ReadFullFrom(c.ExtendedConn, int(length))
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
destination = c.destination
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ServerPacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error {
|
||||||
|
pLen := buffer.Len()
|
||||||
|
common.Must(binary.Write(buf.With(buffer.ExtendHeader(2)), binary.BigEndian, uint16(pLen)))
|
||||||
|
if !c.responseWrite {
|
||||||
|
buffer.ExtendHeader(1)[0] = statusSuccess
|
||||||
|
c.responseWrite = true
|
||||||
|
}
|
||||||
|
return c.ExtendedConn.WriteBuffer(buffer)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ServerPacketConn) Upstream() any {
|
||||||
|
return c.ExtendedConn
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ServerPacketConn) FrontHeadroom() int {
|
||||||
|
if !c.responseWrite {
|
||||||
|
return 3
|
||||||
|
}
|
||||||
|
return 2
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ N.HandshakeConn = (*ServerPacketAddrConn)(nil)
|
||||||
|
_ N.PacketConn = (*ServerPacketAddrConn)(nil)
|
||||||
|
)
|
||||||
|
|
||||||
|
type ServerPacketAddrConn struct {
|
||||||
|
N.ExtendedConn
|
||||||
|
responseWrite bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ServerPacketAddrConn) HandshakeFailure(err error) error {
|
||||||
|
errMessage := err.Error()
|
||||||
|
_buffer := buf.StackNewSize(1 + rw.UVariantLen(uint64(len(errMessage))) + len(errMessage))
|
||||||
|
defer common.KeepAlive(_buffer)
|
||||||
|
buffer := common.Dup(_buffer)
|
||||||
|
defer buffer.Release()
|
||||||
|
common.Must(
|
||||||
|
buffer.WriteByte(statusError),
|
||||||
|
rw.WriteVString(_buffer, errMessage),
|
||||||
|
)
|
||||||
|
return c.ExtendedConn.WriteBuffer(buffer)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ServerPacketAddrConn) ReadPacket(buffer *buf.Buffer) (destination M.Socksaddr, err error) {
|
||||||
|
destination, err = M.SocksaddrSerializer.ReadAddrPort(c.ExtendedConn)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var length uint16
|
||||||
|
err = binary.Read(c.ExtendedConn, binary.BigEndian, &length)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_, err = buffer.ReadFullFrom(c.ExtendedConn, int(length))
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ServerPacketAddrConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error {
|
||||||
|
pLen := buffer.Len()
|
||||||
|
common.Must(binary.Write(buf.With(buffer.ExtendHeader(2)), binary.BigEndian, uint16(pLen)))
|
||||||
|
common.Must(M.SocksaddrSerializer.WriteAddrPort(buf.With(buffer.ExtendHeader(M.SocksaddrSerializer.AddrPortLen(destination))), destination))
|
||||||
|
if !c.responseWrite {
|
||||||
|
buffer.ExtendHeader(1)[0] = statusSuccess
|
||||||
|
c.responseWrite = true
|
||||||
|
}
|
||||||
|
return c.ExtendedConn.WriteBuffer(buffer)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ServerPacketAddrConn) Upstream() any {
|
||||||
|
return c.ExtendedConn
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ServerPacketAddrConn) FrontHeadroom() int {
|
||||||
|
if !c.responseWrite {
|
||||||
|
return 3 + M.MaxSocksaddrLength
|
||||||
|
}
|
||||||
|
return 2 + M.MaxSocksaddrLength
|
||||||
|
}
|
||||||
91
common/mux/session.go
Normal file
91
common/mux/session.go
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
package mux
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
|
||||||
|
"github.com/sagernet/sing/common"
|
||||||
|
"github.com/sagernet/sing/common/buf"
|
||||||
|
"github.com/sagernet/sing/common/bufio"
|
||||||
|
N "github.com/sagernet/sing/common/network"
|
||||||
|
"github.com/sagernet/smux"
|
||||||
|
)
|
||||||
|
|
||||||
|
type abstractSession interface {
|
||||||
|
Open() (net.Conn, error)
|
||||||
|
Accept() (net.Conn, error)
|
||||||
|
NumStreams() int
|
||||||
|
Close() error
|
||||||
|
IsClosed() bool
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ abstractSession = (*smuxSession)(nil)
|
||||||
|
|
||||||
|
type smuxSession struct {
|
||||||
|
*smux.Session
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *smuxSession) Open() (net.Conn, error) {
|
||||||
|
return s.OpenStream()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *smuxSession) Accept() (net.Conn, error) {
|
||||||
|
return s.AcceptStream()
|
||||||
|
}
|
||||||
|
|
||||||
|
type protocolConn struct {
|
||||||
|
net.Conn
|
||||||
|
protocol Protocol
|
||||||
|
protocolWritten bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *protocolConn) Write(p []byte) (n int, err error) {
|
||||||
|
if c.protocolWritten {
|
||||||
|
return c.Conn.Write(p)
|
||||||
|
}
|
||||||
|
_buffer := buf.StackNewSize(2 + len(p))
|
||||||
|
defer common.KeepAlive(_buffer)
|
||||||
|
buffer := common.Dup(_buffer)
|
||||||
|
defer buffer.Release()
|
||||||
|
EncodeRequest(buffer, Request{
|
||||||
|
Protocol: c.protocol,
|
||||||
|
})
|
||||||
|
common.Must(common.Error(buffer.Write(p)))
|
||||||
|
n, err = c.Conn.Write(buffer.Bytes())
|
||||||
|
if err == nil {
|
||||||
|
n--
|
||||||
|
}
|
||||||
|
c.protocolWritten = true
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *protocolConn) ReadFrom(r io.Reader) (n int64, err error) {
|
||||||
|
if !c.protocolWritten {
|
||||||
|
return bufio.ReadFrom0(c, r)
|
||||||
|
}
|
||||||
|
return bufio.Copy(c.Conn, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *protocolConn) Upstream() any {
|
||||||
|
return c.Conn
|
||||||
|
}
|
||||||
|
|
||||||
|
type vectorisedProtocolConn struct {
|
||||||
|
protocolConn
|
||||||
|
N.VectorisedWriter
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *vectorisedProtocolConn) WriteVectorised(buffers []*buf.Buffer) error {
|
||||||
|
if c.protocolWritten {
|
||||||
|
return c.VectorisedWriter.WriteVectorised(buffers)
|
||||||
|
}
|
||||||
|
c.protocolWritten = true
|
||||||
|
_buffer := buf.StackNewSize(2)
|
||||||
|
defer common.KeepAlive(_buffer)
|
||||||
|
buffer := common.Dup(_buffer)
|
||||||
|
defer buffer.Release()
|
||||||
|
EncodeRequest(buffer, Request{
|
||||||
|
Protocol: c.protocol,
|
||||||
|
})
|
||||||
|
return c.VectorisedWriter.WriteVectorised(append([]*buf.Buffer{buffer}, buffers...))
|
||||||
|
}
|
||||||
@@ -3,12 +3,10 @@ package process
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"os/user"
|
|
||||||
|
|
||||||
"github.com/sagernet/sing-box/log"
|
"github.com/sagernet/sing-box/log"
|
||||||
"github.com/sagernet/sing-tun"
|
"github.com/sagernet/sing-tun"
|
||||||
E "github.com/sagernet/sing/common/exceptions"
|
E "github.com/sagernet/sing/common/exceptions"
|
||||||
F "github.com/sagernet/sing/common/format"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Searcher interface {
|
type Searcher interface {
|
||||||
@@ -30,15 +28,5 @@ type Info struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func FindProcessInfo(searcher Searcher, ctx context.Context, network string, source netip.AddrPort, destination netip.AddrPort) (*Info, error) {
|
func FindProcessInfo(searcher Searcher, ctx context.Context, network string, source netip.AddrPort, destination netip.AddrPort) (*Info, error) {
|
||||||
info, err := searcher.FindProcessInfo(ctx, network, source, destination)
|
return findProcessInfo(searcher, ctx, network, source, destination)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if info.UserId != -1 {
|
|
||||||
osUser, _ := user.LookupId(F.ToString(info.UserId))
|
|
||||||
if osUser != nil {
|
|
||||||
info.User = osUser.Username
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return info, nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import (
|
|||||||
"unicode"
|
"unicode"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
|
"github.com/sagernet/sing/common"
|
||||||
"github.com/sagernet/sing/common/buf"
|
"github.com/sagernet/sing/common/buf"
|
||||||
E "github.com/sagernet/sing/common/exceptions"
|
E "github.com/sagernet/sing/common/exceptions"
|
||||||
N "github.com/sagernet/sing/common/network"
|
N "github.com/sagernet/sing/common/network"
|
||||||
@@ -81,7 +82,9 @@ func resolveSocketByNetlink(network string, source netip.AddrPort, destination n
|
|||||||
return 0, 0, E.Cause(err, "write netlink request")
|
return 0, 0, E.Cause(err, "write netlink request")
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer := buf.New()
|
_buffer := buf.StackNew()
|
||||||
|
defer common.KeepAlive(_buffer)
|
||||||
|
buffer := common.Dup(_buffer)
|
||||||
defer buffer.Release()
|
defer buffer.Release()
|
||||||
|
|
||||||
n, err := syscall.Read(socket, buffer.FreeBytes())
|
n, err := syscall.Read(socket, buffer.FreeBytes())
|
||||||
|
|||||||
25
common/process/searcher_with_name.go
Normal file
25
common/process/searcher_with_name.go
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
//go:build linux && !android
|
||||||
|
|
||||||
|
package process
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/netip"
|
||||||
|
"os/user"
|
||||||
|
|
||||||
|
F "github.com/sagernet/sing/common/format"
|
||||||
|
)
|
||||||
|
|
||||||
|
func findProcessInfo(searcher Searcher, ctx context.Context, network string, source netip.AddrPort, destination netip.AddrPort) (*Info, error) {
|
||||||
|
info, err := searcher.FindProcessInfo(ctx, network, source, destination)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if info.UserId != -1 {
|
||||||
|
osUser, _ := user.LookupId(F.ToString(info.UserId))
|
||||||
|
if osUser != nil {
|
||||||
|
info.User = osUser.Username
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return info, nil
|
||||||
|
}
|
||||||
12
common/process/searcher_without_name.go
Normal file
12
common/process/searcher_without_name.go
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
//go:build !linux || android
|
||||||
|
|
||||||
|
package process
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/netip"
|
||||||
|
)
|
||||||
|
|
||||||
|
func findProcessInfo(searcher Searcher, ctx context.Context, network string, source netip.AddrPort, destination netip.AddrPort) (*Info, error) {
|
||||||
|
return searcher.FindProcessInfo(ctx, network, source, destination)
|
||||||
|
}
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
package sleep
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sync"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Manager struct {
|
|
||||||
access sync.Mutex
|
|
||||||
done chan struct{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewManager() *Manager {
|
|
||||||
closedChan := make(chan struct{})
|
|
||||||
close(closedChan)
|
|
||||||
return &Manager{
|
|
||||||
done: closedChan,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Manager) Sleep() {
|
|
||||||
m.access.Lock()
|
|
||||||
defer m.access.Unlock()
|
|
||||||
select {
|
|
||||||
case <-m.done:
|
|
||||||
default:
|
|
||||||
return
|
|
||||||
}
|
|
||||||
m.done = make(chan struct{})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Manager) Wake() {
|
|
||||||
m.access.Lock()
|
|
||||||
defer m.access.Unlock()
|
|
||||||
select {
|
|
||||||
case <-m.done:
|
|
||||||
default:
|
|
||||||
close(m.done)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Manager) Active() <-chan struct{} {
|
|
||||||
return m.done
|
|
||||||
}
|
|
||||||
@@ -26,7 +26,9 @@ func StreamDomainNameQuery(readCtx context.Context, reader io.Reader) (*adapter.
|
|||||||
if length == 0 {
|
if length == 0 {
|
||||||
return nil, os.ErrInvalid
|
return nil, os.ErrInvalid
|
||||||
}
|
}
|
||||||
buffer := buf.NewSize(int(length))
|
_buffer := buf.StackNewSize(int(length))
|
||||||
|
defer common.KeepAlive(_buffer)
|
||||||
|
buffer := common.Dup(_buffer)
|
||||||
defer buffer.Release()
|
defer buffer.Release()
|
||||||
|
|
||||||
readCtx, cancel := context.WithTimeout(readCtx, time.Millisecond*100)
|
readCtx, cancel := context.WithTimeout(readCtx, time.Millisecond*100)
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
|
|
||||||
"github.com/sagernet/sing-box/adapter"
|
"github.com/sagernet/sing-box/adapter"
|
||||||
C "github.com/sagernet/sing-box/constant"
|
C "github.com/sagernet/sing-box/constant"
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
|
||||||
"github.com/sagernet/sing/protocol/http"
|
"github.com/sagernet/sing/protocol/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -16,5 +15,5 @@ func HTTPHost(ctx context.Context, reader io.Reader) (*adapter.InboundContext, e
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &adapter.InboundContext{Protocol: C.ProtocolHTTP, Domain: M.ParseSocksaddr(request.Host).AddrString()}, nil
|
return &adapter.InboundContext{Protocol: C.ProtocolHTTP, Domain: request.Host}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,27 +0,0 @@
|
|||||||
package sniff_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/sagernet/sing-box/common/sniff"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestSniffHTTP1(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
pkt := "GET / HTTP/1.1\r\nHost: www.google.com\r\nAccept: */*\r\n\r\n"
|
|
||||||
metadata, err := sniff.HTTPHost(context.Background(), strings.NewReader(pkt))
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, metadata.Domain, "www.google.com")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSniffHTTP1WithPort(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
pkt := "GET / HTTP/1.1\r\nHost: www.gov.cn:8080\r\nAccept: */*\r\n\r\n"
|
|
||||||
metadata, err := sniff.HTTPHost(context.Background(), strings.NewReader(pkt))
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, metadata.Domain, "www.gov.cn")
|
|
||||||
}
|
|
||||||
@@ -24,12 +24,12 @@ func PeekStream(ctx context.Context, conn net.Conn, buffer *buf.Buffer, timeout
|
|||||||
}
|
}
|
||||||
err := conn.SetReadDeadline(time.Now().Add(timeout))
|
err := conn.SetReadDeadline(time.Now().Add(timeout))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, E.Cause(err, "set read deadline")
|
return nil, err
|
||||||
}
|
}
|
||||||
_, err = buffer.ReadOnceFrom(conn)
|
_, err = buffer.ReadOnceFrom(conn)
|
||||||
err = E.Errors(err, conn.SetReadDeadline(time.Time{}))
|
err = E.Errors(err, conn.SetReadDeadline(time.Time{}))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, E.Cause(err, "read payload")
|
return nil, err
|
||||||
}
|
}
|
||||||
var metadata *adapter.InboundContext
|
var metadata *adapter.InboundContext
|
||||||
var errors []error
|
var errors []error
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ import (
|
|||||||
type acmeWrapper struct {
|
type acmeWrapper struct {
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
cfg *certmagic.Config
|
cfg *certmagic.Config
|
||||||
cache *certmagic.Cache
|
|
||||||
domain []string
|
domain []string
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,7 +29,7 @@ func (w *acmeWrapper) Start() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (w *acmeWrapper) Close() error {
|
func (w *acmeWrapper) Close() error {
|
||||||
w.cache.Stop()
|
w.cfg.Unmanage(w.domain)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,11 +77,10 @@ func startACME(ctx context.Context, options option.InboundACMEOptions) (*tls.Con
|
|||||||
acmeConfig.ExternalAccount = (*acme.EAB)(options.ExternalAccount)
|
acmeConfig.ExternalAccount = (*acme.EAB)(options.ExternalAccount)
|
||||||
}
|
}
|
||||||
config.Issuers = []certmagic.Issuer{certmagic.NewACMEIssuer(config, acmeConfig)}
|
config.Issuers = []certmagic.Issuer{certmagic.NewACMEIssuer(config, acmeConfig)}
|
||||||
cache := certmagic.NewCache(certmagic.CacheOptions{
|
config = certmagic.New(certmagic.NewCache(certmagic.CacheOptions{
|
||||||
GetConfigForCert: func(certificate certmagic.Certificate) (*certmagic.Config, error) {
|
GetConfigForCert: func(certificate certmagic.Certificate) (*certmagic.Config, error) {
|
||||||
return config, nil
|
return config, nil
|
||||||
},
|
},
|
||||||
})
|
}), *config)
|
||||||
config = certmagic.New(cache, *config)
|
return config.TLSConfig(), &acmeWrapper{ctx, config, options.Domain}, nil
|
||||||
return config.TLSConfig(), &acmeWrapper{ctx: ctx, cfg: config, cache: cache, domain: options.Domain}, nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -111,16 +111,6 @@ func (e *RealityClientConfig) ClientHandshake(ctx context.Context, conn net.Conn
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(uConfig.NextProtos) > 0 {
|
|
||||||
for _, extension := range uConn.Extensions {
|
|
||||||
if alpnExtension, isALPN := extension.(*utls.ALPNExtension); isALPN {
|
|
||||||
alpnExtension.AlpnProtocols = uConfig.NextProtos
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
hello := uConn.HandshakeState.Hello
|
hello := uConn.HandshakeState.Hello
|
||||||
hello.SessionId = make([]byte, 32)
|
hello.SessionId = make([]byte, 32)
|
||||||
copy(hello.Raw[39:], hello.SessionId)
|
copy(hello.Raw[39:], hello.SessionId)
|
||||||
@@ -134,9 +124,8 @@ func (e *RealityClientConfig) ClientHandshake(ctx context.Context, conn net.Conn
|
|||||||
binary.BigEndian.PutUint64(hello.SessionId, uint64(nowTime.Unix()))
|
binary.BigEndian.PutUint64(hello.SessionId, uint64(nowTime.Unix()))
|
||||||
|
|
||||||
hello.SessionId[0] = 1
|
hello.SessionId[0] = 1
|
||||||
hello.SessionId[1] = 8
|
hello.SessionId[1] = 7
|
||||||
hello.SessionId[2] = 1
|
hello.SessionId[2] = 5
|
||||||
binary.BigEndian.PutUint32(hello.SessionId[4:], uint32(time.Now().Unix()))
|
|
||||||
copy(hello.SessionId[8:], e.shortID[:])
|
copy(hello.SessionId[8:], e.shortID[:])
|
||||||
|
|
||||||
if debug.Enabled {
|
if debug.Enabled {
|
||||||
|
|||||||
@@ -180,7 +180,7 @@ func NewSTDServer(ctx context.Context, router adapter.Router, logger log.Logger,
|
|||||||
tlsConfig.ServerName = options.ServerName
|
tlsConfig.ServerName = options.ServerName
|
||||||
}
|
}
|
||||||
if len(options.ALPN) > 0 {
|
if len(options.ALPN) > 0 {
|
||||||
tlsConfig.NextProtos = append(options.ALPN, tlsConfig.NextProtos...)
|
tlsConfig.NextProtos = append(tlsConfig.NextProtos, options.ALPN...)
|
||||||
}
|
}
|
||||||
if options.MinVersion != "" {
|
if options.MinVersion != "" {
|
||||||
minVersion, err := ParseTLSVersion(options.MinVersion)
|
minVersion, err := ParseTLSVersion(options.MinVersion)
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
package tls
|
package tls
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
@@ -48,7 +47,7 @@ func (e *UTLSClientConfig) Config() (*STDConfig, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *UTLSClientConfig) Client(conn net.Conn) (Conn, error) {
|
func (e *UTLSClientConfig) Client(conn net.Conn) (Conn, error) {
|
||||||
return &utlsALPNWrapper{utlsConnWrapper{utls.UClient(conn, e.config.Clone(), e.id)}, e.config.NextProtos}, nil
|
return &utlsConnWrapper{utls.UClient(conn, e.config.Clone(), e.id)}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *UTLSClientConfig) SetSessionIDGenerator(generator func(clientHello []byte, sessionID []byte) error) {
|
func (e *UTLSClientConfig) SetSessionIDGenerator(generator func(clientHello []byte, sessionID []byte) error) {
|
||||||
@@ -88,31 +87,6 @@ func (c *utlsConnWrapper) Upstream() any {
|
|||||||
return c.UConn
|
return c.UConn
|
||||||
}
|
}
|
||||||
|
|
||||||
type utlsALPNWrapper struct {
|
|
||||||
utlsConnWrapper
|
|
||||||
nextProtocols []string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *utlsALPNWrapper) HandshakeContext(ctx context.Context) error {
|
|
||||||
if len(c.nextProtocols) > 0 {
|
|
||||||
err := c.BuildHandshakeState()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, extension := range c.Extensions {
|
|
||||||
if alpnExtension, isALPN := extension.(*utls.ALPNExtension); isALPN {
|
|
||||||
alpnExtension.AlpnProtocols = c.nextProtocols
|
|
||||||
err = c.BuildHandshakeState()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return c.UConn.HandshakeContext(ctx)
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewUTLSClient(router adapter.Router, serverAddress string, options option.OutboundTLSOptions) (*UTLSClientConfig, error) {
|
func NewUTLSClient(router adapter.Router, serverAddress string, options option.OutboundTLSOptions) (*UTLSClientConfig, error) {
|
||||||
var serverName string
|
var serverName string
|
||||||
if options.ServerName != "" {
|
if options.ServerName != "" {
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import (
|
|||||||
|
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
N "github.com/sagernet/sing/common/network"
|
N "github.com/sagernet/sing/common/network"
|
||||||
"github.com/sagernet/sing/common/x/list"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type History struct {
|
type History struct {
|
||||||
@@ -21,7 +20,6 @@ type History struct {
|
|||||||
type HistoryStorage struct {
|
type HistoryStorage struct {
|
||||||
access sync.RWMutex
|
access sync.RWMutex
|
||||||
delayHistory map[string]*History
|
delayHistory map[string]*History
|
||||||
callbacks list.List[func()]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHistoryStorage() *HistoryStorage {
|
func NewHistoryStorage() *HistoryStorage {
|
||||||
@@ -30,18 +28,6 @@ func NewHistoryStorage() *HistoryStorage {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *HistoryStorage) AddListener(listener func()) *list.Element[func()] {
|
|
||||||
s.access.Lock()
|
|
||||||
defer s.access.Unlock()
|
|
||||||
return s.callbacks.PushBack(listener)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *HistoryStorage) RemoveListener(element *list.Element[func()]) {
|
|
||||||
s.access.Lock()
|
|
||||||
defer s.access.Unlock()
|
|
||||||
s.callbacks.Remove(element)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *HistoryStorage) LoadURLTestHistory(tag string) *History {
|
func (s *HistoryStorage) LoadURLTestHistory(tag string) *History {
|
||||||
if s == nil {
|
if s == nil {
|
||||||
return nil
|
return nil
|
||||||
@@ -53,30 +39,17 @@ func (s *HistoryStorage) LoadURLTestHistory(tag string) *History {
|
|||||||
|
|
||||||
func (s *HistoryStorage) DeleteURLTestHistory(tag string) {
|
func (s *HistoryStorage) DeleteURLTestHistory(tag string) {
|
||||||
s.access.Lock()
|
s.access.Lock()
|
||||||
|
defer s.access.Unlock()
|
||||||
delete(s.delayHistory, tag)
|
delete(s.delayHistory, tag)
|
||||||
s.access.Unlock()
|
|
||||||
s.notifyUpdated()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *HistoryStorage) StoreURLTestHistory(tag string, history *History) {
|
func (s *HistoryStorage) StoreURLTestHistory(tag string, history *History) {
|
||||||
s.access.Lock()
|
s.access.Lock()
|
||||||
|
defer s.access.Unlock()
|
||||||
s.delayHistory[tag] = history
|
s.delayHistory[tag] = history
|
||||||
s.access.Unlock()
|
|
||||||
s.notifyUpdated()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *HistoryStorage) notifyUpdated() {
|
|
||||||
s.access.RLock()
|
|
||||||
defer s.access.RUnlock()
|
|
||||||
for element := s.callbacks.Front(); element != nil; element = element.Next() {
|
|
||||||
element.Value()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func URLTest(ctx context.Context, link string, detour N.Dialer) (t uint16, err error) {
|
func URLTest(ctx context.Context, link string, detour N.Dialer) (t uint16, err error) {
|
||||||
if link == "" {
|
|
||||||
link = "https://www.gstatic.com/generate_204"
|
|
||||||
}
|
|
||||||
linkURL, err := url.Parse(link)
|
linkURL, err := url.Parse(link)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
|
|||||||
31
common/warning/warning.go
Normal file
31
common/warning/warning.go
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package warning
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/sagernet/sing-box/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Warning struct {
|
||||||
|
logger log.Logger
|
||||||
|
check CheckFunc
|
||||||
|
message string
|
||||||
|
checkOnce sync.Once
|
||||||
|
}
|
||||||
|
|
||||||
|
type CheckFunc = func() bool
|
||||||
|
|
||||||
|
func New(checkFunc CheckFunc, message string) Warning {
|
||||||
|
return Warning{
|
||||||
|
check: checkFunc,
|
||||||
|
message: message,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Warning) Check() {
|
||||||
|
w.checkOnce.Do(func() {
|
||||||
|
if w.check() {
|
||||||
|
log.Warn(w.message)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -3,13 +3,28 @@ package constant
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/sagernet/sing/common/rw"
|
"github.com/sagernet/sing/common/rw"
|
||||||
)
|
)
|
||||||
|
|
||||||
const dirName = "sing-box"
|
const dirName = "sing-box"
|
||||||
|
|
||||||
var resourcePaths []string
|
var (
|
||||||
|
basePath string
|
||||||
|
resourcePaths []string
|
||||||
|
)
|
||||||
|
|
||||||
|
func BasePath(name string) string {
|
||||||
|
if basePath == "" || strings.HasPrefix(name, "/") {
|
||||||
|
return name
|
||||||
|
}
|
||||||
|
return filepath.Join(basePath, name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetBasePath(path string) {
|
||||||
|
basePath = path
|
||||||
|
}
|
||||||
|
|
||||||
func FindPath(name string) (string, bool) {
|
func FindPath(name string) (string, bool) {
|
||||||
name = os.ExpandEnv(name)
|
name = os.ExpandEnv(name)
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ const (
|
|||||||
TypeDirect = "direct"
|
TypeDirect = "direct"
|
||||||
TypeBlock = "block"
|
TypeBlock = "block"
|
||||||
TypeDNS = "dns"
|
TypeDNS = "dns"
|
||||||
TypeSOCKS = "socks"
|
TypeSocks = "socks"
|
||||||
TypeHTTP = "http"
|
TypeHTTP = "http"
|
||||||
TypeMixed = "mixed"
|
TypeMixed = "mixed"
|
||||||
TypeShadowsocks = "shadowsocks"
|
TypeShadowsocks = "shadowsocks"
|
||||||
@@ -27,46 +27,3 @@ const (
|
|||||||
TypeSelector = "selector"
|
TypeSelector = "selector"
|
||||||
TypeURLTest = "urltest"
|
TypeURLTest = "urltest"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ProxyDisplayName(proxyType string) string {
|
|
||||||
switch proxyType {
|
|
||||||
case TypeDirect:
|
|
||||||
return "Direct"
|
|
||||||
case TypeBlock:
|
|
||||||
return "Block"
|
|
||||||
case TypeDNS:
|
|
||||||
return "DNS"
|
|
||||||
case TypeSOCKS:
|
|
||||||
return "SOCKS"
|
|
||||||
case TypeHTTP:
|
|
||||||
return "HTTP"
|
|
||||||
case TypeShadowsocks:
|
|
||||||
return "Shadowsocks"
|
|
||||||
case TypeVMess:
|
|
||||||
return "VMess"
|
|
||||||
case TypeTrojan:
|
|
||||||
return "Trojan"
|
|
||||||
case TypeNaive:
|
|
||||||
return "Naive"
|
|
||||||
case TypeWireGuard:
|
|
||||||
return "WireGuard"
|
|
||||||
case TypeHysteria:
|
|
||||||
return "Hysteria"
|
|
||||||
case TypeTor:
|
|
||||||
return "Tor"
|
|
||||||
case TypeSSH:
|
|
||||||
return "SSH"
|
|
||||||
case TypeShadowTLS:
|
|
||||||
return "ShadowTLS"
|
|
||||||
case TypeShadowsocksR:
|
|
||||||
return "ShadowsocksR"
|
|
||||||
case TypeVLESS:
|
|
||||||
return "VLESS"
|
|
||||||
case TypeSelector:
|
|
||||||
return "Selector"
|
|
||||||
case TypeURLTest:
|
|
||||||
return "URLTest"
|
|
||||||
default:
|
|
||||||
return "Unknown"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func applyDebugOptions(options option.DebugOptions) {
|
func applyDebugOptions(options option.DebugOptions) {
|
||||||
applyDebugListenOption(options)
|
|
||||||
if options.GCPercent != nil {
|
if options.GCPercent != nil {
|
||||||
debug.SetGCPercent(*options.GCPercent)
|
debug.SetGCPercent(*options.GCPercent)
|
||||||
}
|
}
|
||||||
@@ -10,7 +10,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func applyDebugOptions(options option.DebugOptions) {
|
func applyDebugOptions(options option.DebugOptions) {
|
||||||
applyDebugListenOption(options)
|
|
||||||
if options.GCPercent != nil {
|
if options.GCPercent != nil {
|
||||||
debug.SetGCPercent(*options.GCPercent)
|
debug.SetGCPercent(*options.GCPercent)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,67 +0,0 @@
|
|||||||
package box
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
"net/http/pprof"
|
|
||||||
"runtime"
|
|
||||||
"runtime/debug"
|
|
||||||
|
|
||||||
"github.com/sagernet/sing-box/common/badjson"
|
|
||||||
"github.com/sagernet/sing-box/common/json"
|
|
||||||
"github.com/sagernet/sing-box/log"
|
|
||||||
"github.com/sagernet/sing-box/option"
|
|
||||||
E "github.com/sagernet/sing/common/exceptions"
|
|
||||||
|
|
||||||
"github.com/dustin/go-humanize"
|
|
||||||
"github.com/go-chi/chi/v5"
|
|
||||||
)
|
|
||||||
|
|
||||||
var debugHTTPServer *http.Server
|
|
||||||
|
|
||||||
func applyDebugListenOption(options option.DebugOptions) {
|
|
||||||
if debugHTTPServer != nil {
|
|
||||||
debugHTTPServer.Close()
|
|
||||||
debugHTTPServer = nil
|
|
||||||
}
|
|
||||||
if options.Listen == "" {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
r := chi.NewMux()
|
|
||||||
r.Route("/debug", func(r chi.Router) {
|
|
||||||
r.Get("/gc", func(writer http.ResponseWriter, request *http.Request) {
|
|
||||||
writer.WriteHeader(http.StatusNoContent)
|
|
||||||
go debug.FreeOSMemory()
|
|
||||||
})
|
|
||||||
r.Get("/memory", func(writer http.ResponseWriter, request *http.Request) {
|
|
||||||
var memStats runtime.MemStats
|
|
||||||
runtime.ReadMemStats(&memStats)
|
|
||||||
|
|
||||||
var memObject badjson.JSONObject
|
|
||||||
memObject.Put("heap", humanize.IBytes(memStats.HeapInuse))
|
|
||||||
memObject.Put("stack", humanize.IBytes(memStats.StackInuse))
|
|
||||||
memObject.Put("idle", humanize.IBytes(memStats.HeapIdle-memStats.HeapReleased))
|
|
||||||
memObject.Put("goroutines", runtime.NumGoroutine())
|
|
||||||
memObject.Put("rss", rusageMaxRSS())
|
|
||||||
|
|
||||||
encoder := json.NewEncoder(writer)
|
|
||||||
encoder.SetIndent("", " ")
|
|
||||||
encoder.Encode(memObject)
|
|
||||||
})
|
|
||||||
r.HandleFunc("/pprof", pprof.Index)
|
|
||||||
r.HandleFunc("/pprof/*", pprof.Index)
|
|
||||||
r.HandleFunc("/pprof/cmdline", pprof.Cmdline)
|
|
||||||
r.HandleFunc("/pprof/profile", pprof.Profile)
|
|
||||||
r.HandleFunc("/pprof/symbol", pprof.Symbol)
|
|
||||||
r.HandleFunc("/pprof/trace", pprof.Trace)
|
|
||||||
})
|
|
||||||
debugHTTPServer = &http.Server{
|
|
||||||
Addr: options.Listen,
|
|
||||||
Handler: r,
|
|
||||||
}
|
|
||||||
go func() {
|
|
||||||
err := debugHTTPServer.ListenAndServe()
|
|
||||||
if err != nil && !E.IsClosed(err) {
|
|
||||||
log.Error(E.Cause(err, "serve debug HTTP server"))
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
@@ -1,266 +1,3 @@
|
|||||||
#### 1.3.6
|
|
||||||
|
|
||||||
* Fixes and improvements
|
|
||||||
|
|
||||||
#### 1.3.5
|
|
||||||
|
|
||||||
* Fixes and improvements
|
|
||||||
* Introducing our [Apple tvOS](/installation/clients/sft) client applications **1**
|
|
||||||
* Add per app proxy and app installed/updated trigger support for Android client
|
|
||||||
* Add profile sharing support for Android/iOS/macOS clients
|
|
||||||
|
|
||||||
**1**:
|
|
||||||
|
|
||||||
Due to the requirement of tvOS 17, the app cannot be submitted to the App Store for the time being, and can only be downloaded through TestFlight.
|
|
||||||
|
|
||||||
#### 1.3.4
|
|
||||||
|
|
||||||
* Fixes and improvements
|
|
||||||
* We're now on the [App Store](https://apps.apple.com/us/app/sing-box/id6451272673), always free! It should be noted that due to stricter and slower review, the release of Store versions will be delayed.
|
|
||||||
* We've made a standalone version of the macOS client (the original Application Extension relies on App Store distribution), which you can download as SFM-version-universal.zip in the release artifacts.
|
|
||||||
|
|
||||||
#### 1.3.3
|
|
||||||
|
|
||||||
* Fixes and improvements
|
|
||||||
|
|
||||||
#### 1.3.1-rc.1
|
|
||||||
|
|
||||||
* Fix bugs and update dependencies
|
|
||||||
|
|
||||||
#### 1.3.1-beta.3
|
|
||||||
|
|
||||||
* Introducing our [new iOS](/installation/clients/sfi) and [macOS](/installation/clients/sfm) client applications **1**
|
|
||||||
* Fixes and improvements
|
|
||||||
|
|
||||||
**1**:
|
|
||||||
|
|
||||||
The old testflight link and app are no longer valid.
|
|
||||||
|
|
||||||
#### 1.3.1-beta.2
|
|
||||||
|
|
||||||
* Fix bugs and update dependencies
|
|
||||||
|
|
||||||
#### 1.3.1-beta.1
|
|
||||||
|
|
||||||
* Fixes and improvements
|
|
||||||
|
|
||||||
#### 1.3.0
|
|
||||||
|
|
||||||
* Fix bugs and update dependencies
|
|
||||||
|
|
||||||
Important changes since 1.2:
|
|
||||||
|
|
||||||
* Add [FakeIP](/configuration/dns/fakeip) support **1**
|
|
||||||
* Improve multiplex **2**
|
|
||||||
* Add [DNS reverse mapping](/configuration/dns#reverse_mapping) support
|
|
||||||
* Add `rewrite_ttl` DNS rule action
|
|
||||||
* Add `store_fakeip` Clash API option
|
|
||||||
* Add multi-peer support for [WireGuard](/configuration/outbound/wireguard#peers) outbound
|
|
||||||
* Add loopback detect
|
|
||||||
* Add Clash.Meta API compatibility for Clash API
|
|
||||||
* Download Yacd-meta by default if the specified Clash `external_ui` directory is empty
|
|
||||||
* Add path and headers option for HTTP outbound
|
|
||||||
* Perform URLTest recheck after network changes
|
|
||||||
* Fix `system` tun stack for ios
|
|
||||||
* Fix network monitor for android/ios
|
|
||||||
* Update VLESS and XUDP protocol
|
|
||||||
* Make splice work with traffic statistics systems like Clash API
|
|
||||||
* Significantly reduces memory usage of idle connections
|
|
||||||
* Improve DNS caching
|
|
||||||
* Add `independent_cache` [option](/configuration/dns#independent_cache) for DNS
|
|
||||||
* Reimplemented shadowsocks client
|
|
||||||
* Add multiplex support for VLESS outbound
|
|
||||||
* Automatically add Windows firewall rules in order for the system tun stack to work
|
|
||||||
* Fix TLS 1.2 support for shadow-tls client
|
|
||||||
* Add `cache_id` [option](/configuration/experimental#cache_id) for Clash cache file
|
|
||||||
* Fix `local` DNS transport for Android
|
|
||||||
|
|
||||||
*1*:
|
|
||||||
|
|
||||||
See [FAQ](/faq/fakeip) for more information.
|
|
||||||
|
|
||||||
*2*:
|
|
||||||
|
|
||||||
Added new `h2mux` multiplex protocol and `padding` multiplex option, see [Multiplex](/configuration/shared/multiplex).
|
|
||||||
|
|
||||||
#### 1.3-rc2
|
|
||||||
|
|
||||||
* Fix `local` DNS transport for Android
|
|
||||||
* Fix bugs and update dependencies
|
|
||||||
|
|
||||||
#### 1.3-rc1
|
|
||||||
|
|
||||||
* Fix bugs and update dependencies
|
|
||||||
|
|
||||||
#### 1.3-beta14
|
|
||||||
|
|
||||||
* Fixes and improvements
|
|
||||||
|
|
||||||
#### 1.3-beta13
|
|
||||||
|
|
||||||
* Fix resolving fakeip domains **1**
|
|
||||||
* Deprecate L3 routing
|
|
||||||
* Fix bugs and update dependencies
|
|
||||||
|
|
||||||
**1**:
|
|
||||||
|
|
||||||
If the destination address of the connection is obtained from fakeip, dns rules with server type fakeip will be skipped.
|
|
||||||
|
|
||||||
#### 1.3-beta12
|
|
||||||
|
|
||||||
* Automatically add Windows firewall rules in order for the system tun stack to work
|
|
||||||
* Fix TLS 1.2 support for shadow-tls client
|
|
||||||
* Add `cache_id` [option](/configuration/experimental#cache_id) for Clash cache file
|
|
||||||
* Fixes and improvements
|
|
||||||
|
|
||||||
#### 1.3-beta11
|
|
||||||
|
|
||||||
* Fix bugs and update dependencies
|
|
||||||
|
|
||||||
#### 1.3-beta10
|
|
||||||
|
|
||||||
* Improve direct copy **1**
|
|
||||||
* Improve DNS caching
|
|
||||||
* Add `independent_cache` [option](/configuration/dns#independent_cache) for DNS
|
|
||||||
* Reimplemented shadowsocks client **2**
|
|
||||||
* Add multiplex support for VLESS outbound
|
|
||||||
* Set TCP keepalive for WireGuard gVisor TCP connections
|
|
||||||
* Fixes and improvements
|
|
||||||
|
|
||||||
**1**:
|
|
||||||
|
|
||||||
* Make splice work with traffic statistics systems like Clash API
|
|
||||||
* Significantly reduces memory usage of idle connections
|
|
||||||
|
|
||||||
**2**:
|
|
||||||
|
|
||||||
Improved performance and reduced memory usage.
|
|
||||||
|
|
||||||
#### 1.3-beta9
|
|
||||||
|
|
||||||
* Improve multiplex **1**
|
|
||||||
* Fixes and improvements
|
|
||||||
|
|
||||||
*1*:
|
|
||||||
|
|
||||||
Added new `h2mux` multiplex protocol and `padding` multiplex option, see [Multiplex](/configuration/shared/multiplex).
|
|
||||||
|
|
||||||
#### 1.2.6
|
|
||||||
|
|
||||||
* Fix bugs and update dependencies
|
|
||||||
|
|
||||||
#### 1.3-beta8
|
|
||||||
|
|
||||||
* Fix `system` tun stack for ios
|
|
||||||
* Fix network monitor for android/ios
|
|
||||||
* Update VLESS and XUDP protocol **1**
|
|
||||||
* Fixes and improvements
|
|
||||||
|
|
||||||
*1:
|
|
||||||
|
|
||||||
This is an incompatible update for XUDP in VLESS if vision flow is enabled.
|
|
||||||
|
|
||||||
#### 1.3-beta7
|
|
||||||
|
|
||||||
* Add `path` and `headers` options for HTTP outbound
|
|
||||||
* Add multi-user support for Shadowsocks legacy AEAD inbound
|
|
||||||
* Fixes and improvements
|
|
||||||
|
|
||||||
#### 1.2.4
|
|
||||||
|
|
||||||
* Fixes and improvements
|
|
||||||
|
|
||||||
#### 1.3-beta6
|
|
||||||
|
|
||||||
* Fix WireGuard reconnect
|
|
||||||
* Perform URLTest recheck after network changes
|
|
||||||
* Fix bugs and update dependencies
|
|
||||||
|
|
||||||
#### 1.3-beta5
|
|
||||||
|
|
||||||
* Add Clash.Meta API compatibility for Clash API
|
|
||||||
* Download Yacd-meta by default if the specified Clash `external_ui` directory is empty
|
|
||||||
* Add path and headers option for HTTP outbound
|
|
||||||
* Fixes and improvements
|
|
||||||
|
|
||||||
#### 1.3-beta4
|
|
||||||
|
|
||||||
* Fix bugs
|
|
||||||
|
|
||||||
#### 1.3-beta2
|
|
||||||
|
|
||||||
* Download clash-dashboard if the specified Clash `external_ui` directory is empty
|
|
||||||
* Fix bugs and update dependencies
|
|
||||||
|
|
||||||
#### 1.3-beta1
|
|
||||||
|
|
||||||
* Add [DNS reverse mapping](/configuration/dns#reverse_mapping) support
|
|
||||||
* Add [L3 routing](/configuration/route/ip-rule) support **1**
|
|
||||||
* Add `rewrite_ttl` DNS rule action
|
|
||||||
* Add [FakeIP](/configuration/dns/fakeip) support **2**
|
|
||||||
* Add `store_fakeip` Clash API option
|
|
||||||
* Add multi-peer support for [WireGuard](/configuration/outbound/wireguard#peers) outbound
|
|
||||||
* Add loopback detect
|
|
||||||
|
|
||||||
*1*:
|
|
||||||
|
|
||||||
It can currently be used to [route connections directly to WireGuard](/examples/wireguard-direct) or block connections
|
|
||||||
at the IP layer.
|
|
||||||
|
|
||||||
*2*:
|
|
||||||
|
|
||||||
See [FAQ](/faq/fakeip) for more information.
|
|
||||||
|
|
||||||
#### 1.2.3
|
|
||||||
|
|
||||||
* Introducing our [new Android client application](/installation/clients/sfa)
|
|
||||||
* Improve UDP domain destination NAT
|
|
||||||
* Update reality protocol
|
|
||||||
* Fix TTL calculation for DNS response
|
|
||||||
* Fix v2ray HTTP transport compatibility
|
|
||||||
* Fix bugs and update dependencies
|
|
||||||
|
|
||||||
#### 1.2.2
|
|
||||||
|
|
||||||
* Accept `any` outbound in dns rule **1**
|
|
||||||
* Fix bugs and update dependencies
|
|
||||||
|
|
||||||
*1*:
|
|
||||||
|
|
||||||
Now you can use the `any` outbound rule to match server address queries instead of filling in all server domains
|
|
||||||
to `domain` rule.
|
|
||||||
|
|
||||||
#### 1.2.1
|
|
||||||
|
|
||||||
* Fix missing default host in v2ray http transport`s request
|
|
||||||
* Flush DNS cache for macOS when tun start/close
|
|
||||||
* Fix tun's DNS hijacking compatibility with systemd-resolved
|
|
||||||
|
|
||||||
#### 1.2.0
|
|
||||||
|
|
||||||
* Fix bugs and update dependencies
|
|
||||||
|
|
||||||
Important changes since 1.1:
|
|
||||||
|
|
||||||
* Introducing our [new iOS client application](/installation/clients/sfi)
|
|
||||||
* Introducing [UDP over TCP protocol version 2](/configuration/shared/udp-over-tcp)
|
|
||||||
* Add [platform options](/configuration/inbound/tun#platform) for tun inbound
|
|
||||||
* Add [ShadowTLS protocol v3](https://github.com/ihciah/shadow-tls/blob/master/docs/protocol-v3-en.md)
|
|
||||||
* Add [VLESS server](/configuration/inbound/vless) and [vision](/configuration/outbound/vless#flow) support
|
|
||||||
* Add [reality TLS](/configuration/shared/tls) support
|
|
||||||
* Add [NTP service](/configuration/ntp)
|
|
||||||
* Add [DHCP DNS server](/configuration/dns/server) support
|
|
||||||
* Add SSH [host key validation](/configuration/outbound/ssh) support
|
|
||||||
* Add [query_type](/configuration/dns/rule) DNS rule item
|
|
||||||
* Add fallback support for v2ray transport
|
|
||||||
* Add custom TLS server support for http based v2ray transports
|
|
||||||
* Add health check support for http-based v2ray transports
|
|
||||||
* Add multiple configuration support
|
|
||||||
|
|
||||||
#### 1.2-rc1
|
|
||||||
|
|
||||||
* Fix bugs and update dependencies
|
|
||||||
|
|
||||||
#### 1.2-beta10
|
#### 1.2-beta10
|
||||||
|
|
||||||
* Add multiple configuration support **1**
|
* Add multiple configuration support **1**
|
||||||
@@ -268,11 +5,9 @@ Important changes since 1.1:
|
|||||||
|
|
||||||
*1*:
|
*1*:
|
||||||
|
|
||||||
Now you can pass the parameter `--config` or `-c` multiple times, or use the new parameter `--config-directory` or `-C`
|
Now you can pass the parameter `--config` or `-c` multiple times, or use the new parameter `--config-directory` or `-C` to load all configuration files in a directory.
|
||||||
to load all configuration files in a directory.
|
|
||||||
|
|
||||||
Loaded configuration files are sorted by name. If you want to control the merge order, add a numeric prefix to the file
|
Loaded configuration files are sorted by name. If you want to control the merge order, add a numeric prefix to the file name.
|
||||||
name.
|
|
||||||
|
|
||||||
#### 1.1.7
|
#### 1.1.7
|
||||||
|
|
||||||
|
|||||||
@@ -1,25 +0,0 @@
|
|||||||
# FakeIP
|
|
||||||
|
|
||||||
### Structure
|
|
||||||
|
|
||||||
```json
|
|
||||||
{
|
|
||||||
"enabled": true,
|
|
||||||
"inet4_range": "198.18.0.0/15",
|
|
||||||
"inet6_range": "fc00::/18"
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Fields
|
|
||||||
|
|
||||||
#### enabled
|
|
||||||
|
|
||||||
Enable FakeIP service.
|
|
||||||
|
|
||||||
#### inet4_range
|
|
||||||
|
|
||||||
IPv4 address range for FakeIP.
|
|
||||||
|
|
||||||
#### inet6_address
|
|
||||||
|
|
||||||
IPv6 address range for FakeIP.
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
# FakeIP
|
|
||||||
|
|
||||||
### 结构
|
|
||||||
|
|
||||||
```json
|
|
||||||
{
|
|
||||||
"enabled": true,
|
|
||||||
"inet4_range": "198.18.0.0/15",
|
|
||||||
"inet6_range": "fc00::/18"
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### 字段
|
|
||||||
|
|
||||||
#### enabled
|
|
||||||
|
|
||||||
启用 FakeIP 服务。
|
|
||||||
|
|
||||||
#### inet4_range
|
|
||||||
|
|
||||||
用于 FakeIP 的 IPv4 地址范围。
|
|
||||||
|
|
||||||
#### inet6_range
|
|
||||||
|
|
||||||
用于 FakeIP 的 IPv6 地址范围。
|
|
||||||
@@ -10,10 +10,7 @@
|
|||||||
"final": "",
|
"final": "",
|
||||||
"strategy": "",
|
"strategy": "",
|
||||||
"disable_cache": false,
|
"disable_cache": false,
|
||||||
"disable_expire": false,
|
"disable_expire": false
|
||||||
"independent_cache": false,
|
|
||||||
"reverse_mapping": false,
|
|
||||||
"fakeip": {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -25,7 +22,6 @@
|
|||||||
|----------|--------------------------------|
|
|----------|--------------------------------|
|
||||||
| `server` | List of [DNS Server](./server) |
|
| `server` | List of [DNS Server](./server) |
|
||||||
| `rules` | List of [DNS Rule](./rule) |
|
| `rules` | List of [DNS Rule](./rule) |
|
||||||
| `fakeip` | [FakeIP](./fakeip) |
|
|
||||||
|
|
||||||
#### final
|
#### final
|
||||||
|
|
||||||
@@ -47,19 +43,4 @@ Disable dns cache.
|
|||||||
|
|
||||||
#### disable_expire
|
#### disable_expire
|
||||||
|
|
||||||
Disable dns cache expire.
|
Disable dns cache expire.
|
||||||
|
|
||||||
#### independent_cache
|
|
||||||
|
|
||||||
Make each DNS server's cache independent for special purposes. If enabled, will slightly degrade performance.
|
|
||||||
|
|
||||||
#### reverse_mapping
|
|
||||||
|
|
||||||
Stores a reverse mapping of IP addresses after responding to a DNS query in order to provide domain names when routing.
|
|
||||||
|
|
||||||
Since this process relies on the act of resolving domain names by an application before making a request, it can be
|
|
||||||
problematic in environments such as macOS, where DNS is proxied and cached by the system.
|
|
||||||
|
|
||||||
#### fakeip
|
|
||||||
|
|
||||||
[FakeIP](./fakeip) settings.
|
|
||||||
@@ -10,10 +10,7 @@
|
|||||||
"final": "",
|
"final": "",
|
||||||
"strategy": "",
|
"strategy": "",
|
||||||
"disable_cache": false,
|
"disable_cache": false,
|
||||||
"disable_expire": false,
|
"disable_expire": false
|
||||||
"independent_cache": false,
|
|
||||||
"reverse_mapping": false,
|
|
||||||
"fakeip": {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,18 +43,4 @@
|
|||||||
|
|
||||||
#### disable_expire
|
#### disable_expire
|
||||||
|
|
||||||
禁用 DNS 缓存过期。
|
禁用 DNS 缓存过期。
|
||||||
|
|
||||||
#### independent_cache
|
|
||||||
|
|
||||||
使每个 DNS 服务器的缓存独立,以满足特殊目的。如果启用,将轻微降低性能。
|
|
||||||
|
|
||||||
#### reverse_mapping
|
|
||||||
|
|
||||||
在响应 DNS 查询后存储 IP 地址的反向映射以为路由目的提供域名。
|
|
||||||
|
|
||||||
由于此过程依赖于应用程序在发出请求之前解析域名的行为,因此在 macOS 等 DNS 由系统代理和缓存的环境中可能会出现问题。
|
|
||||||
|
|
||||||
#### fakeip
|
|
||||||
|
|
||||||
[FakeIP](./fakeip) 设置。
|
|
||||||
@@ -84,16 +84,14 @@
|
|||||||
"direct"
|
"direct"
|
||||||
],
|
],
|
||||||
"server": "local",
|
"server": "local",
|
||||||
"disable_cache": false,
|
"disable_cache": false
|
||||||
"rewrite_ttl": 100
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"type": "logical",
|
"type": "logical",
|
||||||
"mode": "and",
|
"mode": "and",
|
||||||
"rules": [],
|
"rules": [],
|
||||||
"server": "local",
|
"server": "local",
|
||||||
"disable_cache": false,
|
"disable_cache": false
|
||||||
"rewrite_ttl": 100
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -234,8 +232,6 @@ Invert match result.
|
|||||||
|
|
||||||
Match outbound.
|
Match outbound.
|
||||||
|
|
||||||
`any` can be used as a value to match any outbound.
|
|
||||||
|
|
||||||
#### server
|
#### server
|
||||||
|
|
||||||
==Required==
|
==Required==
|
||||||
@@ -246,10 +242,6 @@ Tag of the target dns server.
|
|||||||
|
|
||||||
Disable cache and save cache in this query.
|
Disable cache and save cache in this query.
|
||||||
|
|
||||||
#### rewrite_ttl
|
|
||||||
|
|
||||||
Rewrite TTL in DNS responses.
|
|
||||||
|
|
||||||
### Logical Fields
|
### Logical Fields
|
||||||
|
|
||||||
#### type
|
#### type
|
||||||
@@ -262,4 +254,18 @@ Rewrite TTL in DNS responses.
|
|||||||
|
|
||||||
#### rules
|
#### rules
|
||||||
|
|
||||||
Included default rules.
|
Included default rules.
|
||||||
|
|
||||||
|
#### invert
|
||||||
|
|
||||||
|
Invert match result.
|
||||||
|
|
||||||
|
#### server
|
||||||
|
|
||||||
|
==Required==
|
||||||
|
|
||||||
|
Tag of the target dns server.
|
||||||
|
|
||||||
|
#### disable_cache
|
||||||
|
|
||||||
|
Disable cache and save cache in this query.
|
||||||
@@ -231,8 +231,6 @@ DNS 查询类型。值可以为整数或者类型名称字符串。
|
|||||||
|
|
||||||
匹配出站。
|
匹配出站。
|
||||||
|
|
||||||
`any` 可作为值用于匹配任意出站。
|
|
||||||
|
|
||||||
#### server
|
#### server
|
||||||
|
|
||||||
==必填==
|
==必填==
|
||||||
@@ -243,10 +241,6 @@ DNS 查询类型。值可以为整数或者类型名称字符串。
|
|||||||
|
|
||||||
在此查询中禁用缓存。
|
在此查询中禁用缓存。
|
||||||
|
|
||||||
#### rewrite_ttl
|
|
||||||
|
|
||||||
重写 DNS 回应中的 TTL。
|
|
||||||
|
|
||||||
### 逻辑字段
|
### 逻辑字段
|
||||||
|
|
||||||
#### type
|
#### type
|
||||||
@@ -259,4 +253,18 @@ DNS 查询类型。值可以为整数或者类型名称字符串。
|
|||||||
|
|
||||||
#### rules
|
#### rules
|
||||||
|
|
||||||
包括的默认规则。
|
包括的默认规则。
|
||||||
|
|
||||||
|
#### invert
|
||||||
|
|
||||||
|
反选匹配结果。
|
||||||
|
|
||||||
|
#### server
|
||||||
|
|
||||||
|
==必填==
|
||||||
|
|
||||||
|
目标 DNS 服务器的标签。
|
||||||
|
|
||||||
|
#### disable_cache
|
||||||
|
|
||||||
|
在此查询中禁用缓存。
|
||||||
@@ -30,18 +30,17 @@ The tag of the dns server.
|
|||||||
|
|
||||||
The address of the dns server.
|
The address of the dns server.
|
||||||
|
|
||||||
| Protocol | Format |
|
| Protocol | Format |
|
||||||
|-------------------------------------|-------------------------------|
|
|----------|-------------------------------|
|
||||||
| `System` | `local` |
|
| `System` | `local` |
|
||||||
| `TCP` | `tcp://1.0.0.1` |
|
| `TCP` | `tcp://1.0.0.1` |
|
||||||
| `UDP` | `8.8.8.8` `udp://8.8.4.4` |
|
| `UDP` | `8.8.8.8` `udp://8.8.4.4` |
|
||||||
| `TLS` | `tls://dns.google` |
|
| `TLS` | `tls://dns.google` |
|
||||||
| `HTTPS` | `https://1.1.1.1/dns-query` |
|
| `HTTPS` | `https://1.1.1.1/dns-query` |
|
||||||
| `QUIC` | `quic://dns.adguard.com` |
|
| `QUIC` | `quic://dns.adguard.com` |
|
||||||
| `HTTP3` | `h3://8.8.8.8/dns-query` |
|
| `HTTP3` | `h3://8.8.8.8/dns-query` |
|
||||||
| `RCode` | `rcode://refused` |
|
| `RCode` | `rcode://refused` |
|
||||||
| `DHCP` | `dhcp://auto` or `dhcp://en0` |
|
| `DHCP` | `dhcp://auto` or `dhcp://en0` |
|
||||||
| [FakeIP](/configuration/dns/fakeip) | `fakeip` |
|
|
||||||
|
|
||||||
!!! warning ""
|
!!! warning ""
|
||||||
|
|
||||||
@@ -94,4 +93,4 @@ Take no effect if override by other settings.
|
|||||||
|
|
||||||
Tag of an outbound for connecting to the dns server.
|
Tag of an outbound for connecting to the dns server.
|
||||||
|
|
||||||
Default outbound will be used if empty.
|
Default outbound will be used if empty.
|
||||||
@@ -30,18 +30,17 @@ DNS 服务器的标签。
|
|||||||
|
|
||||||
DNS 服务器的地址。
|
DNS 服务器的地址。
|
||||||
|
|
||||||
| 协议 | 格式 |
|
| 协议 | 格式 |
|
||||||
|-------------------------------------|------------------------------|
|
|----------|------------------------------|
|
||||||
| `System` | `local` |
|
| `System` | `local` |
|
||||||
| `TCP` | `tcp://1.0.0.1` |
|
| `TCP` | `tcp://1.0.0.1` |
|
||||||
| `UDP` | `8.8.8.8` `udp://8.8.4.4` |
|
| `UDP` | `8.8.8.8` `udp://8.8.4.4` |
|
||||||
| `TLS` | `tls://dns.google` |
|
| `TLS` | `tls://dns.google` |
|
||||||
| `HTTPS` | `https://1.1.1.1/dns-query` |
|
| `HTTPS` | `https://1.1.1.1/dns-query` |
|
||||||
| `QUIC` | `quic://dns.adguard.com` |
|
| `QUIC` | `quic://dns.adguard.com` |
|
||||||
| `HTTP3` | `h3://8.8.8.8/dns-query` |
|
| `HTTP3` | `h3://8.8.8.8/dns-query` |
|
||||||
| `RCode` | `rcode://refused` |
|
| `RCode` | `rcode://refused` |
|
||||||
| `DHCP` | `dhcp://auto` 或 `dhcp://en0` |
|
| `DHCP` | `dhcp://auto` 或 `dhcp://en0` |
|
||||||
| [FakeIP](/configuration/dns/fakeip) | `fakeip` |
|
|
||||||
|
|
||||||
!!! warning ""
|
!!! warning ""
|
||||||
|
|
||||||
|
|||||||
@@ -7,14 +7,11 @@
|
|||||||
"experimental": {
|
"experimental": {
|
||||||
"clash_api": {
|
"clash_api": {
|
||||||
"external_controller": "127.0.0.1:9090",
|
"external_controller": "127.0.0.1:9090",
|
||||||
"external_ui": "",
|
"external_ui": "folder",
|
||||||
"external_ui_download_url": "",
|
|
||||||
"external_ui_download_detour": "",
|
|
||||||
"secret": "",
|
"secret": "",
|
||||||
"default_mode": "",
|
"default_mode": "rule",
|
||||||
"store_selected": false,
|
"store_selected": false,
|
||||||
"cache_file": "",
|
"cache_file": "cache.db"
|
||||||
"cache_id": ""
|
|
||||||
},
|
},
|
||||||
"v2ray_api": {
|
"v2ray_api": {
|
||||||
"listen": "127.0.0.1:8080",
|
"listen": "127.0.0.1:8080",
|
||||||
@@ -56,18 +53,6 @@ A relative path to the configuration directory or an absolute path to a
|
|||||||
directory in which you put some static web resource. sing-box will then
|
directory in which you put some static web resource. sing-box will then
|
||||||
serve it at `http://{{external-controller}}/ui`.
|
serve it at `http://{{external-controller}}/ui`.
|
||||||
|
|
||||||
#### external_ui_download_url
|
|
||||||
|
|
||||||
ZIP download URL for the external UI, will be used if the specified `external_ui` directory is empty.
|
|
||||||
|
|
||||||
`https://github.com/MetaCubeX/Yacd-meta/archive/gh-pages.zip` will be used if empty.
|
|
||||||
|
|
||||||
#### external_ui_download_detour
|
|
||||||
|
|
||||||
The tag of the outbound to download the external UI.
|
|
||||||
|
|
||||||
Default outbound will be used if empty.
|
|
||||||
|
|
||||||
#### secret
|
#### secret
|
||||||
|
|
||||||
Secret for the RESTful API (optional)
|
Secret for the RESTful API (optional)
|
||||||
@@ -92,12 +77,6 @@ Store selected outbound for the `Selector` outbound in cache file.
|
|||||||
|
|
||||||
Cache file path, `cache.db` will be used if empty.
|
Cache file path, `cache.db` will be used if empty.
|
||||||
|
|
||||||
#### cache_id
|
|
||||||
|
|
||||||
Cache ID.
|
|
||||||
|
|
||||||
If not empty, `store_selected` will use a separate store keyed by it.
|
|
||||||
|
|
||||||
### V2Ray API Fields
|
### V2Ray API Fields
|
||||||
|
|
||||||
!!! error ""
|
!!! error ""
|
||||||
|
|||||||
@@ -7,14 +7,11 @@
|
|||||||
"experimental": {
|
"experimental": {
|
||||||
"clash_api": {
|
"clash_api": {
|
||||||
"external_controller": "127.0.0.1:9090",
|
"external_controller": "127.0.0.1:9090",
|
||||||
"external_ui": "",
|
"external_ui": "folder",
|
||||||
"external_ui_download_url": "",
|
|
||||||
"external_ui_download_detour": "",
|
|
||||||
"secret": "",
|
"secret": "",
|
||||||
"default_mode": "",
|
"default_mode": "rule",
|
||||||
"store_selected": false,
|
"store_selected": false,
|
||||||
"cache_file": "",
|
"cache_file": "cache.db"
|
||||||
"cache_id": ""
|
|
||||||
},
|
},
|
||||||
"v2ray_api": {
|
"v2ray_api": {
|
||||||
"listen": "127.0.0.1:8080",
|
"listen": "127.0.0.1:8080",
|
||||||
@@ -54,18 +51,6 @@ RESTful web API 监听地址。如果为空,则禁用 Clash API。
|
|||||||
|
|
||||||
到静态网页资源目录的相对路径或绝对路径。sing-box 会在 `http://{{external-controller}}/ui` 下提供它。
|
到静态网页资源目录的相对路径或绝对路径。sing-box 会在 `http://{{external-controller}}/ui` 下提供它。
|
||||||
|
|
||||||
#### external_ui_download_url
|
|
||||||
|
|
||||||
静态网页资源的 ZIP 下载 URL,如果指定的 `external_ui` 目录为空,将使用。
|
|
||||||
|
|
||||||
默认使用 `https://github.com/MetaCubeX/Yacd-meta/archive/gh-pages.zip`。
|
|
||||||
|
|
||||||
#### external_ui_download_detour
|
|
||||||
|
|
||||||
用于下载静态网页资源的出站的标签。
|
|
||||||
|
|
||||||
如果为空,将使用默认出站。
|
|
||||||
|
|
||||||
#### secret
|
#### secret
|
||||||
|
|
||||||
RESTful API 的密钥(可选)
|
RESTful API 的密钥(可选)
|
||||||
@@ -90,12 +75,6 @@ Clash 中的默认模式,默认使用 `rule`。
|
|||||||
|
|
||||||
缓存文件路径,默认使用`cache.db`。
|
缓存文件路径,默认使用`cache.db`。
|
||||||
|
|
||||||
#### cache_id
|
|
||||||
|
|
||||||
缓存 ID。
|
|
||||||
|
|
||||||
如果不为空,`store_selected` 将会使用以此为键的独立存储。
|
|
||||||
|
|
||||||
### V2Ray API 字段
|
### V2Ray API 字段
|
||||||
|
|
||||||
!!! error ""
|
!!! error ""
|
||||||
|
|||||||
@@ -40,8 +40,4 @@ No authentication required if empty.
|
|||||||
|
|
||||||
Only supported on Linux, Android, Windows, and macOS.
|
Only supported on Linux, Android, Windows, and macOS.
|
||||||
|
|
||||||
!!! warning ""
|
|
||||||
|
|
||||||
To work on Android and iOS without privileges, use tun.platform.http_proxy instead.
|
|
||||||
|
|
||||||
Automatically set system proxy configuration when start and clean up when stop.
|
Automatically set system proxy configuration when start and clean up when stop.
|
||||||
|
|||||||
@@ -40,8 +40,4 @@ HTTP 用户
|
|||||||
|
|
||||||
仅支持 Linux、Android、Windows 和 macOS。
|
仅支持 Linux、Android、Windows 和 macOS。
|
||||||
|
|
||||||
!!! warning ""
|
|
||||||
|
|
||||||
要在无特权的 Android 和 iOS 上工作,请改用 tun.platform.http_proxy。
|
|
||||||
|
|
||||||
启动时自动设置系统代理,停止时自动清理。
|
启动时自动设置系统代理,停止时自动清理。
|
||||||
@@ -37,8 +37,4 @@ No authentication required if empty.
|
|||||||
|
|
||||||
Only supported on Linux, Android, Windows, and macOS.
|
Only supported on Linux, Android, Windows, and macOS.
|
||||||
|
|
||||||
!!! warning ""
|
Automatically set system proxy configuration when start and clean up when stop.
|
||||||
|
|
||||||
To work on Android and iOS without privileges, use tun.platform.http_proxy instead.
|
|
||||||
|
|
||||||
Automatically set system proxy configuration when start and clean up when stop.
|
|
||||||
@@ -37,8 +37,4 @@ SOCKS 和 HTTP 用户
|
|||||||
|
|
||||||
仅支持 Linux、Android、Windows 和 macOS。
|
仅支持 Linux、Android、Windows 和 macOS。
|
||||||
|
|
||||||
!!! warning ""
|
|
||||||
|
|
||||||
要在无特权的 Android 和 iOS 上工作,请改用 tun.platform.http_proxy。
|
|
||||||
|
|
||||||
启动时自动设置系统代理,停止时自动清理。
|
启动时自动设置系统代理,停止时自动清理。
|
||||||
@@ -107,7 +107,8 @@ Enforce strict routing rules when `auto_route` is enabled:
|
|||||||
* Let unsupported network unreachable
|
* Let unsupported network unreachable
|
||||||
* Route all connections to tun
|
* Route all connections to tun
|
||||||
|
|
||||||
It prevents address leaks and makes DNS hijacking work on Android, but your device will not be accessible by others.
|
It prevents address leaks and makes DNS hijacking work on Android and Linux with systemd-resolved, but your device will
|
||||||
|
not be accessible by others.
|
||||||
|
|
||||||
*In Windows*:
|
*In Windows*:
|
||||||
|
|
||||||
|
|||||||
@@ -107,7 +107,7 @@ tun 接口的 IPv6 前缀。
|
|||||||
* 让不支持的网络无法到达
|
* 让不支持的网络无法到达
|
||||||
* 将所有连接路由到 tun
|
* 将所有连接路由到 tun
|
||||||
|
|
||||||
它可以防止地址泄漏,并使 DNS 劫持在 Android 上工作,但你的设备将无法其他设备被访问。
|
它可以防止地址泄漏,并使 DNS 劫持在 Android 和使用 systemd-resolved 的 Linux 上工作,但你的设备将无法其他设备被访问。
|
||||||
|
|
||||||
*在 Windows 中*:
|
*在 Windows 中*:
|
||||||
|
|
||||||
|
|||||||
@@ -11,8 +11,6 @@
|
|||||||
"server_port": 1080,
|
"server_port": 1080,
|
||||||
"username": "sekai",
|
"username": "sekai",
|
||||||
"password": "admin",
|
"password": "admin",
|
||||||
"path": "",
|
|
||||||
"headers": {},
|
|
||||||
"tls": {},
|
"tls": {},
|
||||||
|
|
||||||
... // Dial Fields
|
... // Dial Fields
|
||||||
@@ -41,14 +39,6 @@ Basic authorization username.
|
|||||||
|
|
||||||
Basic authorization password.
|
Basic authorization password.
|
||||||
|
|
||||||
#### path
|
|
||||||
|
|
||||||
Path of HTTP request.
|
|
||||||
|
|
||||||
#### headers
|
|
||||||
|
|
||||||
Extra headers of HTTP request.
|
|
||||||
|
|
||||||
#### tls
|
#### tls
|
||||||
|
|
||||||
TLS configuration, see [TLS](/configuration/shared/tls/#outbound).
|
TLS configuration, see [TLS](/configuration/shared/tls/#outbound).
|
||||||
|
|||||||
@@ -11,8 +11,6 @@
|
|||||||
"server_port": 1080,
|
"server_port": 1080,
|
||||||
"username": "sekai",
|
"username": "sekai",
|
||||||
"password": "admin",
|
"password": "admin",
|
||||||
"path": "",
|
|
||||||
"headers": {},
|
|
||||||
"tls": {},
|
"tls": {},
|
||||||
|
|
||||||
... // 拨号字段
|
... // 拨号字段
|
||||||
@@ -41,14 +39,6 @@ Basic 认证用户名。
|
|||||||
|
|
||||||
Basic 认证密码。
|
Basic 认证密码。
|
||||||
|
|
||||||
#### path
|
|
||||||
|
|
||||||
HTTP 请求路径。
|
|
||||||
|
|
||||||
#### headers
|
|
||||||
|
|
||||||
HTTP 请求的额外标头。
|
|
||||||
|
|
||||||
#### tls
|
#### tls
|
||||||
|
|
||||||
TLS 配置, 参阅 [TLS](/zh/configuration/shared/tls/#outbound)。
|
TLS 配置, 参阅 [TLS](/zh/configuration/shared/tls/#outbound)。
|
||||||
|
|||||||
@@ -37,10 +37,4 @@
|
|||||||
|
|
||||||
#### tag
|
#### tag
|
||||||
|
|
||||||
The tag of the outbound.
|
The tag of the outbound.
|
||||||
|
|
||||||
### Features
|
|
||||||
|
|
||||||
#### Outbounds that support IP connection
|
|
||||||
|
|
||||||
* `WireGuard`
|
|
||||||
@@ -36,10 +36,4 @@
|
|||||||
|
|
||||||
#### tag
|
#### tag
|
||||||
|
|
||||||
出站的标签。
|
出站的标签。
|
||||||
|
|
||||||
### 特性
|
|
||||||
|
|
||||||
#### 支持 IP 连接的出站
|
|
||||||
|
|
||||||
* `WireGuard`
|
|
||||||
@@ -10,7 +10,7 @@
|
|||||||
"proxy-b",
|
"proxy-b",
|
||||||
"proxy-c"
|
"proxy-c"
|
||||||
],
|
],
|
||||||
"url": "https://www.gstatic.com/generate_204",
|
"url": "http://www.gstatic.com/generate_204",
|
||||||
"interval": "1m",
|
"interval": "1m",
|
||||||
"tolerance": 50
|
"tolerance": 50
|
||||||
}
|
}
|
||||||
@@ -26,7 +26,7 @@ List of outbound tags to test.
|
|||||||
|
|
||||||
#### url
|
#### url
|
||||||
|
|
||||||
The URL to test. `https://www.gstatic.com/generate_204` will be used if empty.
|
The URL to test. `http://www.gstatic.com/generate_204` will be used if empty.
|
||||||
|
|
||||||
#### interval
|
#### interval
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@
|
|||||||
"proxy-b",
|
"proxy-b",
|
||||||
"proxy-c"
|
"proxy-c"
|
||||||
],
|
],
|
||||||
"url": "https://www.gstatic.com/generate_204",
|
"url": "http://www.gstatic.com/generate_204",
|
||||||
"interval": "1m",
|
"interval": "1m",
|
||||||
"tolerance": 50
|
"tolerance": 50
|
||||||
}
|
}
|
||||||
@@ -26,7 +26,7 @@
|
|||||||
|
|
||||||
#### url
|
#### url
|
||||||
|
|
||||||
用于测试的链接。默认使用 `https://www.gstatic.com/generate_204`。
|
用于测试的链接。默认使用 `http://www.gstatic.com/generate_204`。
|
||||||
|
|
||||||
#### interval
|
#### interval
|
||||||
|
|
||||||
|
|||||||
@@ -13,18 +13,6 @@
|
|||||||
"10.0.0.2/32"
|
"10.0.0.2/32"
|
||||||
],
|
],
|
||||||
"private_key": "YNXtAzepDqRv9H52osJVDQnznT5AM11eCK3ESpwSt04=",
|
"private_key": "YNXtAzepDqRv9H52osJVDQnznT5AM11eCK3ESpwSt04=",
|
||||||
"peers": [
|
|
||||||
{
|
|
||||||
"server": "127.0.0.1",
|
|
||||||
"server_port": 1080,
|
|
||||||
"public_key": "Z1XXLsKYkYxuiYjJIkRvtIKFepCYHTgON+GwPq7SOV4=",
|
|
||||||
"pre_shared_key": "31aIhAPwktDGpH4JDhA8GNvjFXEf/a6+UaQRyOAiyfM=",
|
|
||||||
"allowed_ips": [
|
|
||||||
"0.0.0.0/0"
|
|
||||||
],
|
|
||||||
"reserved": [0, 0, 0]
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"peer_public_key": "Z1XXLsKYkYxuiYjJIkRvtIKFepCYHTgON+GwPq7SOV4=",
|
"peer_public_key": "Z1XXLsKYkYxuiYjJIkRvtIKFepCYHTgON+GwPq7SOV4=",
|
||||||
"pre_shared_key": "31aIhAPwktDGpH4JDhA8GNvjFXEf/a6+UaQRyOAiyfM=",
|
"pre_shared_key": "31aIhAPwktDGpH4JDhA8GNvjFXEf/a6+UaQRyOAiyfM=",
|
||||||
"reserved": [0, 0, 0],
|
"reserved": [0, 0, 0],
|
||||||
@@ -48,13 +36,13 @@
|
|||||||
|
|
||||||
#### server
|
#### server
|
||||||
|
|
||||||
==Required if multi-peer disabled==
|
==Required==
|
||||||
|
|
||||||
The server address.
|
The server address.
|
||||||
|
|
||||||
#### server_port
|
#### server_port
|
||||||
|
|
||||||
==Required if multi-peer disabled==
|
==Required==
|
||||||
|
|
||||||
The server port.
|
The server port.
|
||||||
|
|
||||||
@@ -87,25 +75,9 @@ wg genkey
|
|||||||
echo "private key" || wg pubkey
|
echo "private key" || wg pubkey
|
||||||
```
|
```
|
||||||
|
|
||||||
#### peers
|
|
||||||
|
|
||||||
Multi-peer support.
|
|
||||||
|
|
||||||
If enabled, `server, server_port, peer_public_key, pre_shared_key` will be ignored.
|
|
||||||
|
|
||||||
#### peers.allowed_ips
|
|
||||||
|
|
||||||
WireGuard allowed IPs.
|
|
||||||
|
|
||||||
#### peers.reserved
|
|
||||||
|
|
||||||
WireGuard reserved field bytes.
|
|
||||||
|
|
||||||
`$outbound.reserved` will be used if empty.
|
|
||||||
|
|
||||||
#### peer_public_key
|
#### peer_public_key
|
||||||
|
|
||||||
==Required if multi-peer disabled==
|
==Required==
|
||||||
|
|
||||||
WireGuard peer public key.
|
WireGuard peer public key.
|
||||||
|
|
||||||
|
|||||||
@@ -19,11 +19,11 @@
|
|||||||
|
|
||||||
### Fields
|
### Fields
|
||||||
|
|
||||||
| Key | Format |
|
| Key | Format |
|
||||||
|------------|------------------------------------|
|
|-----------|------------------------------|
|
||||||
| `geoip` | [GeoIP](./geoip) |
|
| `geoip` | [GeoIP](./geoip) |
|
||||||
| `geosite` | [Geosite](./geosite) |
|
| `geosite` | [Geosite](./geosite) |
|
||||||
| `rules` | List of [Route Rule](./rule) |
|
| `rules` | List of [Route Rule](./rule) |
|
||||||
|
|
||||||
#### final
|
#### final
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,6 @@
|
|||||||
"route": {
|
"route": {
|
||||||
"geoip": {},
|
"geoip": {},
|
||||||
"geosite": {},
|
"geosite": {},
|
||||||
"ip_rules": [],
|
|
||||||
"rules": [],
|
"rules": [],
|
||||||
"final": "",
|
"final": "",
|
||||||
"auto_detect_interface": false,
|
"auto_detect_interface": false,
|
||||||
@@ -20,12 +19,11 @@
|
|||||||
|
|
||||||
### 字段
|
### 字段
|
||||||
|
|
||||||
| 键 | 格式 |
|
| 键 | 格式 |
|
||||||
|------------|-------------------------|
|
|-----------|----------------------|
|
||||||
| `geoip` | [GeoIP](./geoip) |
|
| `geoip` | [GeoIP](./geoip) |
|
||||||
| `geosite` | [GeoSite](./geosite) |
|
| `geosite` | [GeoSite](./geosite) |
|
||||||
| `ip_rules` | 一组 [IP 路由规则](./ip-rule) |
|
| `rules` | 一组 [路由规则](./rule) |
|
||||||
| `rules` | 一组 [路由规则](./rule) |
|
|
||||||
|
|
||||||
#### final
|
#### final
|
||||||
|
|
||||||
@@ -67,4 +65,4 @@
|
|||||||
|
|
||||||
默认为出站连接设置路由标记。
|
默认为出站连接设置路由标记。
|
||||||
|
|
||||||
如果设置了 `outbound.routing_mark` 设置,则不生效。
|
如果设置了 `outbound.routing_mark` 设置,则不生效。
|
||||||
|
|||||||
@@ -1,205 +0,0 @@
|
|||||||
### Structure
|
|
||||||
|
|
||||||
```json
|
|
||||||
{
|
|
||||||
"route": {
|
|
||||||
"ip_rules": [
|
|
||||||
{
|
|
||||||
"inbound": [
|
|
||||||
"mixed-in"
|
|
||||||
],
|
|
||||||
"ip_version": 6,
|
|
||||||
"network": [
|
|
||||||
"tcp"
|
|
||||||
],
|
|
||||||
"domain": [
|
|
||||||
"test.com"
|
|
||||||
],
|
|
||||||
"domain_suffix": [
|
|
||||||
".cn"
|
|
||||||
],
|
|
||||||
"domain_keyword": [
|
|
||||||
"test"
|
|
||||||
],
|
|
||||||
"domain_regex": [
|
|
||||||
"^stun\\..+"
|
|
||||||
],
|
|
||||||
"geosite": [
|
|
||||||
"cn"
|
|
||||||
],
|
|
||||||
"source_geoip": [
|
|
||||||
"private"
|
|
||||||
],
|
|
||||||
"geoip": [
|
|
||||||
"cn"
|
|
||||||
],
|
|
||||||
"source_ip_cidr": [
|
|
||||||
"10.0.0.0/24",
|
|
||||||
"192.168.0.1"
|
|
||||||
],
|
|
||||||
"ip_cidr": [
|
|
||||||
"10.0.0.0/24",
|
|
||||||
"192.168.0.1"
|
|
||||||
],
|
|
||||||
"source_port": [
|
|
||||||
12345
|
|
||||||
],
|
|
||||||
"source_port_range": [
|
|
||||||
"1000:2000",
|
|
||||||
":3000",
|
|
||||||
"4000:"
|
|
||||||
],
|
|
||||||
"port": [
|
|
||||||
80,
|
|
||||||
443
|
|
||||||
],
|
|
||||||
"port_range": [
|
|
||||||
"1000:2000",
|
|
||||||
":3000",
|
|
||||||
"4000:"
|
|
||||||
],
|
|
||||||
"invert": false,
|
|
||||||
"action": "direct",
|
|
||||||
"outbound": "wireguard"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "logical",
|
|
||||||
"mode": "and",
|
|
||||||
"rules": [],
|
|
||||||
"invert": false,
|
|
||||||
"action": "direct",
|
|
||||||
"outbound": "wireguard"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
!!! note ""
|
|
||||||
|
|
||||||
You can ignore the JSON Array [] tag when the content is only one item
|
|
||||||
|
|
||||||
### Default Fields
|
|
||||||
|
|
||||||
!!! note ""
|
|
||||||
|
|
||||||
The default rule uses the following matching logic:
|
|
||||||
(`domain` || `domain_suffix` || `domain_keyword` || `domain_regex` || `geosite` || `geoip` || `ip_cidr`) &&
|
|
||||||
(`port` || `port_range`) &&
|
|
||||||
(`source_geoip` || `source_ip_cidr`) &&
|
|
||||||
(`source_port` || `source_port_range`) &&
|
|
||||||
`other fields`
|
|
||||||
|
|
||||||
#### inbound
|
|
||||||
|
|
||||||
Tags of [Inbound](/configuration/inbound).
|
|
||||||
|
|
||||||
#### ip_version
|
|
||||||
|
|
||||||
4 or 6.
|
|
||||||
|
|
||||||
Not limited if empty.
|
|
||||||
|
|
||||||
#### network
|
|
||||||
|
|
||||||
Match network protocol.
|
|
||||||
|
|
||||||
Available values:
|
|
||||||
|
|
||||||
* `tcp`
|
|
||||||
* `udp`
|
|
||||||
* `icmpv4`
|
|
||||||
* `icmpv6`
|
|
||||||
|
|
||||||
#### domain
|
|
||||||
|
|
||||||
Match full domain.
|
|
||||||
|
|
||||||
#### domain_suffix
|
|
||||||
|
|
||||||
Match domain suffix.
|
|
||||||
|
|
||||||
#### domain_keyword
|
|
||||||
|
|
||||||
Match domain using keyword.
|
|
||||||
|
|
||||||
#### domain_regex
|
|
||||||
|
|
||||||
Match domain using regular expression.
|
|
||||||
|
|
||||||
#### geosite
|
|
||||||
|
|
||||||
Match geosite.
|
|
||||||
|
|
||||||
#### source_geoip
|
|
||||||
|
|
||||||
Match source geoip.
|
|
||||||
|
|
||||||
#### geoip
|
|
||||||
|
|
||||||
Match geoip.
|
|
||||||
|
|
||||||
#### source_ip_cidr
|
|
||||||
|
|
||||||
Match source ip cidr.
|
|
||||||
|
|
||||||
#### ip_cidr
|
|
||||||
|
|
||||||
Match ip cidr.
|
|
||||||
|
|
||||||
#### source_port
|
|
||||||
|
|
||||||
Match source port.
|
|
||||||
|
|
||||||
#### source_port_range
|
|
||||||
|
|
||||||
Match source port range.
|
|
||||||
|
|
||||||
#### port
|
|
||||||
|
|
||||||
Match port.
|
|
||||||
|
|
||||||
#### port_range
|
|
||||||
|
|
||||||
Match port range.
|
|
||||||
|
|
||||||
#### invert
|
|
||||||
|
|
||||||
Invert match result.
|
|
||||||
|
|
||||||
#### action
|
|
||||||
|
|
||||||
==Required==
|
|
||||||
|
|
||||||
| Action | Description |
|
|
||||||
|--------|--------------------------------------------------------------------|
|
|
||||||
| return | Stop IP routing and assemble the connection to the transport layer |
|
|
||||||
| block | Block the connection |
|
|
||||||
| direct | Directly forward the connection |
|
|
||||||
|
|
||||||
#### outbound
|
|
||||||
|
|
||||||
==Required if action is direct==
|
|
||||||
|
|
||||||
Tag of the target outbound.
|
|
||||||
|
|
||||||
Only outbound which supports IP connection can be used, see [Outbounds that support IP connection](/configuration/outbound/#outbounds-that-support-ip-connection).
|
|
||||||
|
|
||||||
### Logical Fields
|
|
||||||
|
|
||||||
#### type
|
|
||||||
|
|
||||||
`logical`
|
|
||||||
|
|
||||||
#### mode
|
|
||||||
|
|
||||||
==Required==
|
|
||||||
|
|
||||||
`and` or `or`
|
|
||||||
|
|
||||||
#### rules
|
|
||||||
|
|
||||||
==Required==
|
|
||||||
|
|
||||||
Included default rules.
|
|
||||||
@@ -1,204 +0,0 @@
|
|||||||
### 结构
|
|
||||||
|
|
||||||
```json
|
|
||||||
{
|
|
||||||
"route": {
|
|
||||||
"ip_rules": [
|
|
||||||
{
|
|
||||||
"inbound": [
|
|
||||||
"mixed-in"
|
|
||||||
],
|
|
||||||
"ip_version": 6,
|
|
||||||
"network": [
|
|
||||||
"tcp"
|
|
||||||
],
|
|
||||||
"domain": [
|
|
||||||
"test.com"
|
|
||||||
],
|
|
||||||
"domain_suffix": [
|
|
||||||
".cn"
|
|
||||||
],
|
|
||||||
"domain_keyword": [
|
|
||||||
"test"
|
|
||||||
],
|
|
||||||
"domain_regex": [
|
|
||||||
"^stun\\..+"
|
|
||||||
],
|
|
||||||
"geosite": [
|
|
||||||
"cn"
|
|
||||||
],
|
|
||||||
"source_geoip": [
|
|
||||||
"private"
|
|
||||||
],
|
|
||||||
"geoip": [
|
|
||||||
"cn"
|
|
||||||
],
|
|
||||||
"source_ip_cidr": [
|
|
||||||
"10.0.0.0/24",
|
|
||||||
"192.168.0.1"
|
|
||||||
],
|
|
||||||
"ip_cidr": [
|
|
||||||
"10.0.0.0/24",
|
|
||||||
"192.168.0.1"
|
|
||||||
],
|
|
||||||
"source_port": [
|
|
||||||
12345
|
|
||||||
],
|
|
||||||
"source_port_range": [
|
|
||||||
"1000:2000",
|
|
||||||
":3000",
|
|
||||||
"4000:"
|
|
||||||
],
|
|
||||||
"port": [
|
|
||||||
80,
|
|
||||||
443
|
|
||||||
],
|
|
||||||
"port_range": [
|
|
||||||
"1000:2000",
|
|
||||||
":3000",
|
|
||||||
"4000:"
|
|
||||||
],
|
|
||||||
"invert": false,
|
|
||||||
"action": "direct",
|
|
||||||
"outbound": "wireguard"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "logical",
|
|
||||||
"mode": "and",
|
|
||||||
"rules": [],
|
|
||||||
"invert": false,
|
|
||||||
"action": "direct",
|
|
||||||
"outbound": "wireguard"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
!!! note ""
|
|
||||||
|
|
||||||
当内容只有一项时,可以忽略 JSON 数组 [] 标签。
|
|
||||||
|
|
||||||
### Default Fields
|
|
||||||
|
|
||||||
!!! note ""
|
|
||||||
|
|
||||||
默认规则使用以下匹配逻辑:
|
|
||||||
(`domain` || `domain_suffix` || `domain_keyword` || `domain_regex` || `geosite` || `geoip` || `ip_cidr`) &&
|
|
||||||
(`port` || `port_range`) &&
|
|
||||||
(`source_geoip` || `source_ip_cidr`) &&
|
|
||||||
(`source_port` || `source_port_range`) &&
|
|
||||||
`other fields`
|
|
||||||
|
|
||||||
#### inbound
|
|
||||||
|
|
||||||
[入站](/zh/configuration/inbound) 标签。
|
|
||||||
|
|
||||||
#### ip_version
|
|
||||||
|
|
||||||
4 或 6。
|
|
||||||
|
|
||||||
默认不限制。
|
|
||||||
|
|
||||||
#### network
|
|
||||||
|
|
||||||
匹配网络协议。
|
|
||||||
|
|
||||||
可用值:
|
|
||||||
|
|
||||||
* `tcp`
|
|
||||||
* `udp`
|
|
||||||
* `icmpv4`
|
|
||||||
* `icmpv6`
|
|
||||||
|
|
||||||
#### domain
|
|
||||||
|
|
||||||
匹配完整域名。
|
|
||||||
|
|
||||||
#### domain_suffix
|
|
||||||
|
|
||||||
匹配域名后缀。
|
|
||||||
|
|
||||||
#### domain_keyword
|
|
||||||
|
|
||||||
匹配域名关键字。
|
|
||||||
|
|
||||||
#### domain_regex
|
|
||||||
|
|
||||||
匹配域名正则表达式。
|
|
||||||
|
|
||||||
#### geosite
|
|
||||||
|
|
||||||
匹配 GeoSite。
|
|
||||||
|
|
||||||
#### source_geoip
|
|
||||||
|
|
||||||
匹配源 GeoIP。
|
|
||||||
|
|
||||||
#### geoip
|
|
||||||
|
|
||||||
匹配 GeoIP。
|
|
||||||
|
|
||||||
#### source_ip_cidr
|
|
||||||
|
|
||||||
匹配源 IP CIDR。
|
|
||||||
|
|
||||||
#### ip_cidr
|
|
||||||
|
|
||||||
匹配 IP CIDR。
|
|
||||||
|
|
||||||
#### source_port
|
|
||||||
|
|
||||||
匹配源端口。
|
|
||||||
|
|
||||||
#### source_port_range
|
|
||||||
|
|
||||||
匹配源端口范围。
|
|
||||||
|
|
||||||
#### port
|
|
||||||
|
|
||||||
匹配端口。
|
|
||||||
|
|
||||||
#### port_range
|
|
||||||
|
|
||||||
匹配端口范围。
|
|
||||||
|
|
||||||
#### invert
|
|
||||||
|
|
||||||
反选匹配结果。
|
|
||||||
|
|
||||||
#### action
|
|
||||||
|
|
||||||
==必填==
|
|
||||||
|
|
||||||
| Action | 描述 |
|
|
||||||
|--------|---------------------|
|
|
||||||
| return | 停止 IP 路由并将该连接组装到传输层 |
|
|
||||||
| block | 屏蔽该连接 |
|
|
||||||
| direct | 直接转发该连接 |
|
|
||||||
|
|
||||||
|
|
||||||
#### outbound
|
|
||||||
|
|
||||||
==action 为 direct 则必填==
|
|
||||||
|
|
||||||
目标出站的标签。
|
|
||||||
|
|
||||||
### 逻辑字段
|
|
||||||
|
|
||||||
#### type
|
|
||||||
|
|
||||||
`logical`
|
|
||||||
|
|
||||||
#### mode
|
|
||||||
|
|
||||||
==必填==
|
|
||||||
|
|
||||||
`and` 或 `or`
|
|
||||||
|
|
||||||
#### rules
|
|
||||||
|
|
||||||
==必填==
|
|
||||||
|
|
||||||
包括的默认规则。
|
|
||||||
@@ -9,9 +9,7 @@
|
|||||||
"mixed-in"
|
"mixed-in"
|
||||||
],
|
],
|
||||||
"ip_version": 6,
|
"ip_version": 6,
|
||||||
"network": [
|
"network": "tcp",
|
||||||
"tcp"
|
|
||||||
],
|
|
||||||
"auth_user": [
|
"auth_user": [
|
||||||
"usera",
|
"usera",
|
||||||
"userb"
|
"userb"
|
||||||
@@ -246,12 +244,18 @@ Tag of the target outbound.
|
|||||||
|
|
||||||
#### mode
|
#### mode
|
||||||
|
|
||||||
==Required==
|
|
||||||
|
|
||||||
`and` or `or`
|
`and` or `or`
|
||||||
|
|
||||||
#### rules
|
#### rules
|
||||||
|
|
||||||
|
Included default rules.
|
||||||
|
|
||||||
|
#### invert
|
||||||
|
|
||||||
|
Invert match result.
|
||||||
|
|
||||||
|
#### outbound
|
||||||
|
|
||||||
==Required==
|
==Required==
|
||||||
|
|
||||||
Included default rules.
|
Tag of the target outbound.
|
||||||
|
|||||||
@@ -9,9 +9,7 @@
|
|||||||
"mixed-in"
|
"mixed-in"
|
||||||
],
|
],
|
||||||
"ip_version": 6,
|
"ip_version": 6,
|
||||||
"network": [
|
"network": "tcp",
|
||||||
"tcp"
|
|
||||||
],
|
|
||||||
"auth_user": [
|
"auth_user": [
|
||||||
"usera",
|
"usera",
|
||||||
"userb"
|
"userb"
|
||||||
@@ -244,12 +242,18 @@
|
|||||||
|
|
||||||
#### mode
|
#### mode
|
||||||
|
|
||||||
==必填==
|
|
||||||
|
|
||||||
`and` 或 `or`
|
`and` 或 `or`
|
||||||
|
|
||||||
#### rules
|
#### rules
|
||||||
|
|
||||||
|
包括的默认规则。
|
||||||
|
|
||||||
|
#### invert
|
||||||
|
|
||||||
|
反选匹配结果。
|
||||||
|
|
||||||
|
#### outbound
|
||||||
|
|
||||||
==必填==
|
==必填==
|
||||||
|
|
||||||
包括的默认规则。
|
目标出站的标签。
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user