Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TLS Client Authentication #1151

Open
wants to merge 117 commits into
base: dev-next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
117 commits
Select commit Hold shift + click to select a range
41452ef
Migrate to gobwas/ws
nekohasekai Nov 9, 2023
a19fbba
Add udp_disable_domain_unmapping inbound listen option
nekohasekai Oct 30, 2023
39517da
Add exclude route support for tun &
nekohasekai Oct 30, 2023
c9aa599
Add support for v2ray http upgrade transport
nekohasekai Nov 2, 2023
615e93b
Migrate multiplex and UoT server to inbound & Add tcp-brutal support …
nekohasekai Nov 8, 2023
4fcf29d
Update quic-go to v0.40.0
nekohasekai Nov 14, 2023
a5cea60
Add `wifi_ssid` and `wifi_bssid` route and DNS rules
nekohasekai Nov 24, 2023
0f643bf
documentation: Bump version & Refactor docs
nekohasekai Nov 9, 2023
737f478
feat: add TLS mutual authentication
kwfcfc Nov 25, 2023
20088dc
docs: add documentation for TLS client authentication settings
kwfcfc Nov 25, 2023
1c1eb01
fix dhcp 2
nekohasekai Nov 26, 2023
edadbba
docs: fix typo
jose-C2OaWi Nov 27, 2023
bbea3aa
Migrate to independent cache file
nekohasekai Nov 28, 2023
1febc0c
Merge branch 'dev-next' into origin/tls-client-auth
jose-C2OaWi Nov 28, 2023
0f2e73f
Migrate to independent cache file
nekohasekai Nov 28, 2023
927865e
Allow nested logical rules
nekohasekai Nov 28, 2023
aab482f
Merge branch 'dev-next' into origin/tls-client-auth
jose-C2OaWi Nov 29, 2023
5fd9422
Migrate to independent cache file
nekohasekai Nov 28, 2023
d26cbfb
Allow nested logical rules
nekohasekai Nov 28, 2023
1bb4093
Add rule set
nekohasekai Nov 29, 2023
65c9254
documentation: Add rule set
nekohasekai Nov 29, 2023
49fc4c6
Merge branch 'dev-next' into origin/tls-client-auth
jose-C2OaWi Nov 29, 2023
40e364f
Migrate to independent cache file
nekohasekai Nov 28, 2023
554f994
Allow nested logical rules
nekohasekai Nov 28, 2023
92b904d
Add rule set
nekohasekai Nov 29, 2023
4bb8497
documentation: Add rule set
nekohasekai Nov 29, 2023
1450538
Merge branch 'dev-next' into origin/tls-client-auth
jose-C2OaWi Nov 30, 2023
4d6b7ff
Migrate to independent cache file
nekohasekai Nov 28, 2023
33881eb
Allow nested logical rules
nekohasekai Nov 28, 2023
4e4c082
Add rule set
nekohasekai Nov 29, 2023
249e501
documentation: Add rule set
nekohasekai Nov 29, 2023
bbc1d12
documentation: Bump version
nekohasekai Nov 29, 2023
31ea920
Merge branch 'dev-next' into origin/tls-client-auth
jose-C2OaWi Nov 30, 2023
cb28aba
documentation: Update rule-set example
nekohasekai Nov 30, 2023
7f36e08
Merge branch 'dev-next' into origin/tls-client-auth
jose-C2OaWi Nov 30, 2023
b256e54
Add rule set
nekohasekai Nov 29, 2023
d8fd51f
documentation: Add rule set
nekohasekai Nov 29, 2023
ff26fb0
documentation: Bump version
nekohasekai Nov 29, 2023
ee086ea
Make rule-set initialization parallel
nekohasekai Nov 30, 2023
f61ea0d
Independent `source_ip_is_private` and `ip_is_private` rules
nekohasekai Nov 30, 2023
6441003
Fix router start order
nekohasekai Nov 30, 2023
6218594
Fix URLTest group early start
nekohasekai Nov 30, 2023
3bb709d
documentation: Update rule-set example
nekohasekai Nov 30, 2023
efdb34d
documentation: Bump version
nekohasekai Nov 30, 2023
b191b28
Merge branch 'dev-next' into origin/tls-client-auth
jose-C2OaWi Nov 30, 2023
1bf48d7
Migrate to independent cache file
nekohasekai Nov 28, 2023
6c6af93
Allow nested logical rules
nekohasekai Nov 28, 2023
4ae9b09
Update buffer usage
nekohasekai Nov 29, 2023
5d22545
Add rule-set
nekohasekai Dec 1, 2023
aa72c6e
Independent `source_ip_is_private` and `ip_is_private` rules
nekohasekai Dec 1, 2023
8d58e88
Update documentation
nekohasekai Dec 1, 2023
b78a74e
contextjson: Import json
nekohasekai Dec 1, 2023
53c270c
contextjson: Add context to decode error message
nekohasekai Dec 1, 2023
2f3beff
documentation: Bump version
nekohasekai Dec 1, 2023
b5675e1
Use contextjson
nekohasekai Dec 1, 2023
fbd3b23
Merge branch 'dev-next' into origin/tls-client-auth
jose-C2OaWi Dec 1, 2023
6fbcac3
Skip internal fake-ip queries
nekohasekai Dec 1, 2023
292d63d
documentation: Bump version
nekohasekai Dec 1, 2023
10681a7
Merge branch 'dev-next' into origin/tls-client-auth
jose-C2OaWi Dec 2, 2023
3e62aaa
documentation: Bump version
nekohasekai Dec 2, 2023
6b817e3
Migrate to independent cache file
nekohasekai Nov 28, 2023
d5ff6ec
Allow nested logical rules
nekohasekai Nov 28, 2023
71af66a
Update buffer usage
nekohasekai Nov 29, 2023
185f272
Add rule-set
nekohasekai Dec 1, 2023
ec17e1c
Independent `source_ip_is_private` and `ip_is_private` rules
nekohasekai Dec 1, 2023
830aff4
Update documentation
nekohasekai Dec 1, 2023
b8551de
contextjson: Import json
nekohasekai Dec 1, 2023
c0ddc35
contextjson: Add context to decode error message
nekohasekai Dec 1, 2023
c7446c4
Use contextjson
nekohasekai Dec 1, 2023
9d1ce04
Skip internal fake-ip queries
nekohasekai Dec 1, 2023
7f43e7a
Add `idle_timeout` for URLTest outbound
nekohasekai Dec 3, 2023
b1142de
documentation: Bump version
nekohasekai Dec 1, 2023
df3ff30
Merge branch 'dev-next' into origin/tls-client-auth
jose-C2OaWi Dec 3, 2023
53da5fd
Update documentation
nekohasekai Dec 3, 2023
d0b2e92
Merge branch 'dev-next' into origin/tls-client-auth
jose-C2OaWi Dec 3, 2023
29e6884
Migrate to independent cache file
nekohasekai Nov 28, 2023
ce99c4a
Allow nested logical rules
nekohasekai Nov 28, 2023
7674ec8
Update buffer usage
nekohasekai Nov 29, 2023
c34ca78
Add rule-set
nekohasekai Dec 1, 2023
1bd1066
Independent `source_ip_is_private` and `ip_is_private` rules
nekohasekai Dec 1, 2023
9d778a3
Update documentation
nekohasekai Dec 1, 2023
e573625
contextjson: Import json
nekohasekai Dec 1, 2023
1c503fe
contextjson: Add context to decode error message
nekohasekai Dec 1, 2023
02e148e
Use contextjson
nekohasekai Dec 1, 2023
aa5df43
Skip internal fake-ip queries
nekohasekai Dec 1, 2023
f2ce9fb
Add `idle_timeout` for URLTest outbound
nekohasekai Dec 3, 2023
43df5dc
Update documentation
nekohasekai Dec 3, 2023
379065e
documentation: Bump version
nekohasekai Dec 1, 2023
075f229
Merge branch 'dev-next' into origin/tls-client-auth
jose-C2OaWi Dec 3, 2023
88e3a4c
Add rule-set
nekohasekai Dec 1, 2023
868664d
Independent `source_ip_is_private` and `ip_is_private` rules
nekohasekai Dec 1, 2023
1d6c3c6
Update documentation
nekohasekai Dec 1, 2023
a9e6608
contextjson: Import json
nekohasekai Dec 1, 2023
40ca0a3
contextjson: Add context to decode error message
nekohasekai Dec 1, 2023
bc5c601
Use contextjson
nekohasekai Dec 1, 2023
9184cbf
Skip internal fake-ip queries
nekohasekai Dec 1, 2023
949bb61
Add `idle_timeout` for URLTest outbound
nekohasekai Dec 3, 2023
8736ff9
Update documentation
nekohasekai Dec 3, 2023
263b6c8
Avoid opening log output before start &
nekohasekai Dec 4, 2023
656b43a
documentation: Bump version
nekohasekai Dec 1, 2023
5b23ad1
Merge branch 'dev-next' into origin/tls-client-auth
jose-C2OaWi Dec 4, 2023
895ce42
Migrate to independent cache file
nekohasekai Nov 28, 2023
9eeaf26
Allow nested logical rules
nekohasekai Nov 28, 2023
488a446
Update buffer usage
nekohasekai Nov 29, 2023
98326e4
Add rule-set
nekohasekai Dec 1, 2023
d486fbc
Independent `source_ip_is_private` and `ip_is_private` rules
nekohasekai Dec 1, 2023
2d04e0b
Update documentation
nekohasekai Dec 1, 2023
b4b4771
contextjson: Import json
nekohasekai Dec 1, 2023
c593d5e
contextjson: Add context to decode error message
nekohasekai Dec 1, 2023
c7a13de
Use contextjson
nekohasekai Dec 1, 2023
509a714
Skip internal fake-ip queries
nekohasekai Dec 1, 2023
6ab33e8
Add `idle_timeout` for URLTest outbound
nekohasekai Dec 3, 2023
311a2f0
Update documentation
nekohasekai Dec 3, 2023
1637aeb
Avoid opening log output before start &
nekohasekai Dec 4, 2023
67fd53b
Remove comparable limit for Listable
nekohasekai Dec 4, 2023
a947f3a
documentation: Bump version
nekohasekai Dec 1, 2023
a6c7c73
Merge branch 'dev-next' into origin/tls-client-auth
jose-C2OaWi Dec 4, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/.idea/
/vendor/
/*.json
/*.srs
/*.db
/site/
/bin/
Expand Down
76 changes: 71 additions & 5 deletions adapter/experimental.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,100 @@
package adapter

import (
"bytes"
"context"
"encoding/binary"
"io"
"net"
"time"

"github.com/sagernet/sing-box/common/urltest"
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing/common/rw"
)

type ClashServer interface {
Service
PreStarter
Mode() string
ModeList() []string
StoreSelected() bool
StoreFakeIP() bool
CacheFile() ClashCacheFile
HistoryStorage() *urltest.HistoryStorage
RoutedConnection(ctx context.Context, conn net.Conn, metadata InboundContext, matchedRule Rule) (net.Conn, Tracker)
RoutedPacketConnection(ctx context.Context, conn N.PacketConn, metadata InboundContext, matchedRule Rule) (N.PacketConn, Tracker)
}

type ClashCacheFile interface {
type CacheFile interface {
Service
PreStarter

StoreFakeIP() bool
FakeIPStorage

LoadMode() string
StoreMode(mode string) error
LoadSelected(group string) string
StoreSelected(group string, selected string) error
LoadGroupExpand(group string) (isExpand bool, loaded bool)
StoreGroupExpand(group string, expand bool) error
FakeIPStorage
LoadRuleSet(tag string) *SavedRuleSet
SaveRuleSet(tag string, set *SavedRuleSet) error
}

type SavedRuleSet struct {
Content []byte
LastUpdated time.Time
LastEtag string
}

func (s *SavedRuleSet) MarshalBinary() ([]byte, error) {
var buffer bytes.Buffer
err := binary.Write(&buffer, binary.BigEndian, uint8(1))
if err != nil {
return nil, err
}
err = rw.WriteUVariant(&buffer, uint64(len(s.Content)))
if err != nil {
return nil, err
}
buffer.Write(s.Content)
err = binary.Write(&buffer, binary.BigEndian, s.LastUpdated.Unix())
if err != nil {
return nil, err
}
err = rw.WriteVString(&buffer, s.LastEtag)
if err != nil {
return nil, err
}
return buffer.Bytes(), nil
}

func (s *SavedRuleSet) UnmarshalBinary(data []byte) error {
reader := bytes.NewReader(data)
var version uint8
err := binary.Read(reader, binary.BigEndian, &version)
if err != nil {
return err
}
contentLen, err := rw.ReadUVariant(reader)
if err != nil {
return err
}
s.Content = make([]byte, contentLen)
_, err = io.ReadFull(reader, s.Content)
if err != nil {
return err
}
var lastUpdated int64
err = binary.Read(reader, binary.BigEndian, &lastUpdated)
if err != nil {
return err
}
s.LastUpdated = time.Unix(lastUpdated, 0)
s.LastEtag, err = rw.ReadVString(reader)
if err != nil {
return err
}
return nil
}

type Tracker interface {
Expand Down
18 changes: 16 additions & 2 deletions adapter/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,25 @@ type InboundContext struct {
SourceGeoIPCode string
GeoIPCode string
ProcessInfo *process.Info
QueryType uint16
FakeIP bool
IPCIDRMatchSource bool

// dns cache
// rule cache

QueryType uint16
IPCIDRMatchSource bool
SourceAddressMatch bool
SourcePortMatch bool
DestinationAddressMatch bool
DestinationPortMatch bool
}

func (c *InboundContext) ResetRuleCache() {
c.IPCIDRMatchSource = false
c.SourceAddressMatch = false
c.SourcePortMatch = false
c.DestinationAddressMatch = false
c.DestinationPortMatch = false
}

type inboundContextKey struct{}
Expand Down
30 changes: 28 additions & 2 deletions adapter/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package adapter

import (
"context"
"net/http"
"net/netip"

"github.com/sagernet/sing-box/common/geoip"
"github.com/sagernet/sing-dns"
"github.com/sagernet/sing-tun"
"github.com/sagernet/sing/common/control"
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing/service"

mdns "github.com/miekg/dns"
Expand All @@ -19,7 +21,7 @@ type Router interface {

Outbounds() []Outbound
Outbound(tag string) (Outbound, bool)
DefaultOutbound(network string) Outbound
DefaultOutbound(network string) (Outbound, error)

FakeIPStore() FakeIPStore

Expand All @@ -28,6 +30,8 @@ type Router interface {
GeoIPReader() *geoip.Reader
LoadGeosite(code string) (Rule, error)

RuleSet(tag string) (RuleSet, bool)

Exchange(ctx context.Context, message *mdns.Msg) (*mdns.Msg, error)
Lookup(ctx context.Context, domain string, strategy dns.DomainStrategy) ([]netip.Addr, error)
LookupDefault(ctx context.Context, domain string) ([]netip.Addr, error)
Expand Down Expand Up @@ -62,11 +66,15 @@ func RouterFromContext(ctx context.Context) Router {
return service.FromContext[Router](ctx)
}

type HeadlessRule interface {
Match(metadata *InboundContext) bool
}

type Rule interface {
HeadlessRule
Service
Type() string
UpdateGeosite() error
Match(metadata *InboundContext) bool
Outbound() string
String() string
}
Expand All @@ -77,6 +85,24 @@ type DNSRule interface {
RewriteTTL() *uint32
}

type RuleSet interface {
StartContext(ctx context.Context, startContext RuleSetStartContext) error
PostStart() error
Metadata() RuleSetMetadata
Close() error
HeadlessRule
}

type RuleSetMetadata struct {
ContainsProcessRule bool
ContainsWIFIRule bool
}

type RuleSetStartContext interface {
HTTPClient(detour string, dialer N.Dialer) *http.Client
Close()
}

type InterfaceUpdateListener interface {
InterfaceUpdated()
}
Expand Down
Loading