mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-05-14 00:51:12 +03:00
Resolve conflicts
This commit is contained in:
@@ -1,280 +0,0 @@
|
||||
package dhcp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/netip"
|
||||
"net/url"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
"github.com/sagernet/sing-box/common/dialer"
|
||||
C "github.com/sagernet/sing-box/constant"
|
||||
"github.com/sagernet/sing-box/option"
|
||||
"github.com/sagernet/sing-dns"
|
||||
"github.com/sagernet/sing-tun"
|
||||
"github.com/sagernet/sing/common"
|
||||
"github.com/sagernet/sing/common/buf"
|
||||
"github.com/sagernet/sing/common/control"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
"github.com/sagernet/sing/common/task"
|
||||
"github.com/sagernet/sing/common/x/list"
|
||||
"github.com/sagernet/sing/service"
|
||||
|
||||
"github.com/insomniacslk/dhcp/dhcpv4"
|
||||
mDNS "github.com/miekg/dns"
|
||||
)
|
||||
|
||||
func init() {
|
||||
dns.RegisterTransport([]string{"dhcp"}, func(options dns.TransportOptions) (dns.Transport, error) {
|
||||
return NewTransport(options)
|
||||
})
|
||||
}
|
||||
|
||||
type Transport struct {
|
||||
options dns.TransportOptions
|
||||
router adapter.Router
|
||||
networkManager adapter.NetworkManager
|
||||
interfaceName string
|
||||
autoInterface bool
|
||||
interfaceCallback *list.Element[tun.DefaultInterfaceUpdateCallback]
|
||||
transports []dns.Transport
|
||||
updateAccess sync.Mutex
|
||||
updatedAt time.Time
|
||||
}
|
||||
|
||||
func NewTransport(options dns.TransportOptions) (*Transport, error) {
|
||||
linkURL, err := url.Parse(options.Address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if linkURL.Host == "" {
|
||||
return nil, E.New("missing interface name for DHCP")
|
||||
}
|
||||
transport := &Transport{
|
||||
options: options,
|
||||
networkManager: service.FromContext[adapter.NetworkManager](options.Context),
|
||||
interfaceName: linkURL.Host,
|
||||
autoInterface: linkURL.Host == "auto",
|
||||
}
|
||||
return transport, nil
|
||||
}
|
||||
|
||||
func (t *Transport) Name() string {
|
||||
return t.options.Name
|
||||
}
|
||||
|
||||
func (t *Transport) Start() error {
|
||||
err := t.fetchServers()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if t.autoInterface {
|
||||
t.interfaceCallback = t.networkManager.InterfaceMonitor().RegisterCallback(t.interfaceUpdated)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Transport) Reset() {
|
||||
for _, transport := range t.transports {
|
||||
transport.Reset()
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Transport) Close() error {
|
||||
for _, transport := range t.transports {
|
||||
transport.Close()
|
||||
}
|
||||
if t.interfaceCallback != nil {
|
||||
t.networkManager.InterfaceMonitor().UnregisterCallback(t.interfaceCallback)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Transport) Raw() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (t *Transport) Exchange(ctx context.Context, message *mDNS.Msg) (*mDNS.Msg, error) {
|
||||
err := t.fetchServers()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(t.transports) == 0 {
|
||||
return nil, E.New("dhcp: empty DNS servers from response")
|
||||
}
|
||||
|
||||
var response *mDNS.Msg
|
||||
for _, transport := range t.transports {
|
||||
response, err = transport.Exchange(ctx, message)
|
||||
if err == nil {
|
||||
return response, nil
|
||||
}
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func (t *Transport) fetchInterface() (*control.Interface, error) {
|
||||
if t.autoInterface {
|
||||
if t.networkManager.InterfaceMonitor() == nil {
|
||||
return nil, E.New("missing monitor for auto DHCP, set route.auto_detect_interface")
|
||||
}
|
||||
defaultInterface := t.networkManager.InterfaceMonitor().DefaultInterface()
|
||||
if defaultInterface == nil {
|
||||
return nil, E.New("missing default interface")
|
||||
}
|
||||
return defaultInterface, nil
|
||||
} else {
|
||||
return t.networkManager.InterfaceFinder().ByName(t.interfaceName)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Transport) fetchServers() error {
|
||||
if time.Since(t.updatedAt) < C.DHCPTTL {
|
||||
return nil
|
||||
}
|
||||
t.updateAccess.Lock()
|
||||
defer t.updateAccess.Unlock()
|
||||
if time.Since(t.updatedAt) < C.DHCPTTL {
|
||||
return nil
|
||||
}
|
||||
return t.updateServers()
|
||||
}
|
||||
|
||||
func (t *Transport) updateServers() error {
|
||||
iface, err := t.fetchInterface()
|
||||
if err != nil {
|
||||
return E.Cause(err, "dhcp: prepare interface")
|
||||
}
|
||||
|
||||
t.options.Logger.Info("dhcp: query DNS servers on ", iface.Name)
|
||||
fetchCtx, cancel := context.WithTimeout(t.options.Context, C.DHCPTimeout)
|
||||
err = t.fetchServers0(fetchCtx, iface)
|
||||
cancel()
|
||||
if err != nil {
|
||||
return err
|
||||
} else if len(t.transports) == 0 {
|
||||
return E.New("dhcp: empty DNS servers response")
|
||||
} else {
|
||||
t.updatedAt = time.Now()
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Transport) interfaceUpdated(defaultInterface *control.Interface, flags int) {
|
||||
err := t.updateServers()
|
||||
if err != nil {
|
||||
t.options.Logger.Error("update servers: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Transport) fetchServers0(ctx context.Context, iface *control.Interface) error {
|
||||
var listener net.ListenConfig
|
||||
listener.Control = control.Append(listener.Control, control.BindToInterface(t.networkManager.InterfaceFinder(), iface.Name, iface.Index))
|
||||
listener.Control = control.Append(listener.Control, control.ReuseAddr())
|
||||
listenAddr := "0.0.0.0:68"
|
||||
if runtime.GOOS == "linux" || runtime.GOOS == "android" {
|
||||
listenAddr = "255.255.255.255:68"
|
||||
}
|
||||
packetConn, err := listener.ListenPacket(t.options.Context, "udp4", listenAddr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer packetConn.Close()
|
||||
|
||||
discovery, err := dhcpv4.NewDiscovery(iface.HardwareAddr, dhcpv4.WithBroadcast(true), dhcpv4.WithRequestedOptions(dhcpv4.OptionDomainNameServer))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = packetConn.WriteTo(discovery.ToBytes(), &net.UDPAddr{IP: net.IPv4bcast, Port: 67})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var group task.Group
|
||||
group.Append0(func(ctx context.Context) error {
|
||||
return t.fetchServersResponse(iface, packetConn, discovery.TransactionID)
|
||||
})
|
||||
group.Cleanup(func() {
|
||||
packetConn.Close()
|
||||
})
|
||||
return group.Run(ctx)
|
||||
}
|
||||
|
||||
func (t *Transport) fetchServersResponse(iface *control.Interface, packetConn net.PacketConn, transactionID dhcpv4.TransactionID) error {
|
||||
buffer := buf.NewSize(dhcpv4.MaxMessageSize)
|
||||
defer buffer.Release()
|
||||
|
||||
for {
|
||||
_, _, err := buffer.ReadPacketFrom(packetConn)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dhcpPacket, err := dhcpv4.FromBytes(buffer.Bytes())
|
||||
if err != nil {
|
||||
t.options.Logger.Trace("dhcp: parse DHCP response: ", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if dhcpPacket.MessageType() != dhcpv4.MessageTypeOffer {
|
||||
t.options.Logger.Trace("dhcp: expected OFFER response, but got ", dhcpPacket.MessageType())
|
||||
continue
|
||||
}
|
||||
|
||||
if dhcpPacket.TransactionID != transactionID {
|
||||
t.options.Logger.Trace("dhcp: expected transaction ID ", transactionID, ", but got ", dhcpPacket.TransactionID)
|
||||
continue
|
||||
}
|
||||
|
||||
dns := dhcpPacket.DNS()
|
||||
if len(dns) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
var addrs []netip.Addr
|
||||
for _, ip := range dns {
|
||||
addr, _ := netip.AddrFromSlice(ip)
|
||||
addrs = append(addrs, addr.Unmap())
|
||||
}
|
||||
return t.recreateServers(iface, addrs)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Transport) recreateServers(iface *control.Interface, serverAddrs []netip.Addr) error {
|
||||
if len(serverAddrs) > 0 {
|
||||
t.options.Logger.Info("dhcp: updated DNS servers from ", iface.Name, ": [", strings.Join(common.Map(serverAddrs, func(it netip.Addr) string {
|
||||
return it.String()
|
||||
}), ","), "]")
|
||||
}
|
||||
serverDialer := common.Must1(dialer.NewDefault(t.options.Context, option.DialerOptions{
|
||||
BindInterface: iface.Name,
|
||||
UDPFragmentDefault: true,
|
||||
}))
|
||||
var transports []dns.Transport
|
||||
for _, serverAddr := range serverAddrs {
|
||||
newOptions := t.options
|
||||
newOptions.Address = serverAddr.String()
|
||||
newOptions.Dialer = serverDialer
|
||||
serverTransport, err := dns.NewUDPTransport(newOptions)
|
||||
if err != nil {
|
||||
return E.Cause(err, "create UDP transport from DHCP result: ", serverAddr)
|
||||
}
|
||||
transports = append(transports, serverTransport)
|
||||
}
|
||||
for _, transport := range t.transports {
|
||||
transport.Close()
|
||||
}
|
||||
t.transports = transports
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Transport) Lookup(ctx context.Context, domain string, strategy dns.DomainStrategy) ([]netip.Addr, error) {
|
||||
return nil, os.ErrInvalid
|
||||
}
|
||||
@@ -1,89 +0,0 @@
|
||||
package fakeip
|
||||
|
||||
import (
|
||||
"net/netip"
|
||||
"sync"
|
||||
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
"github.com/sagernet/sing/common/logger"
|
||||
)
|
||||
|
||||
var _ adapter.FakeIPStorage = (*MemoryStorage)(nil)
|
||||
|
||||
type MemoryStorage struct {
|
||||
addressAccess sync.RWMutex
|
||||
domainAccess sync.RWMutex
|
||||
addressCache map[netip.Addr]string
|
||||
domainCache4 map[string]netip.Addr
|
||||
domainCache6 map[string]netip.Addr
|
||||
}
|
||||
|
||||
func NewMemoryStorage() *MemoryStorage {
|
||||
return &MemoryStorage{
|
||||
addressCache: make(map[netip.Addr]string),
|
||||
domainCache4: make(map[string]netip.Addr),
|
||||
domainCache6: make(map[string]netip.Addr),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *MemoryStorage) FakeIPMetadata() *adapter.FakeIPMetadata {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *MemoryStorage) FakeIPSaveMetadata(metadata *adapter.FakeIPMetadata) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *MemoryStorage) FakeIPSaveMetadataAsync(metadata *adapter.FakeIPMetadata) {
|
||||
}
|
||||
|
||||
func (s *MemoryStorage) FakeIPStore(address netip.Addr, domain string) error {
|
||||
s.addressAccess.Lock()
|
||||
s.domainAccess.Lock()
|
||||
if oldDomain, loaded := s.addressCache[address]; loaded {
|
||||
if address.Is4() {
|
||||
delete(s.domainCache4, oldDomain)
|
||||
} else {
|
||||
delete(s.domainCache6, oldDomain)
|
||||
}
|
||||
}
|
||||
s.addressCache[address] = domain
|
||||
if address.Is4() {
|
||||
s.domainCache4[domain] = address
|
||||
} else {
|
||||
s.domainCache6[domain] = address
|
||||
}
|
||||
s.domainAccess.Unlock()
|
||||
s.addressAccess.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *MemoryStorage) FakeIPStoreAsync(address netip.Addr, domain string, logger logger.Logger) {
|
||||
_ = s.FakeIPStore(address, domain)
|
||||
}
|
||||
|
||||
func (s *MemoryStorage) FakeIPLoad(address netip.Addr) (string, bool) {
|
||||
s.addressAccess.RLock()
|
||||
defer s.addressAccess.RUnlock()
|
||||
domain, loaded := s.addressCache[address]
|
||||
return domain, loaded
|
||||
}
|
||||
|
||||
func (s *MemoryStorage) FakeIPLoadDomain(domain string, isIPv6 bool) (netip.Addr, bool) {
|
||||
s.domainAccess.RLock()
|
||||
defer s.domainAccess.RUnlock()
|
||||
if !isIPv6 {
|
||||
address, loaded := s.domainCache4[domain]
|
||||
return address, loaded
|
||||
} else {
|
||||
address, loaded := s.domainCache6[domain]
|
||||
return address, loaded
|
||||
}
|
||||
}
|
||||
|
||||
func (s *MemoryStorage) FakeIPReset() error {
|
||||
s.addressCache = make(map[netip.Addr]string)
|
||||
s.domainCache4 = make(map[string]netip.Addr)
|
||||
s.domainCache6 = make(map[string]netip.Addr)
|
||||
return nil
|
||||
}
|
||||
@@ -1,95 +0,0 @@
|
||||
package fakeip
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/netip"
|
||||
"os"
|
||||
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
"github.com/sagernet/sing-dns"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
"github.com/sagernet/sing/common/logger"
|
||||
"github.com/sagernet/sing/service"
|
||||
|
||||
mDNS "github.com/miekg/dns"
|
||||
)
|
||||
|
||||
var (
|
||||
_ dns.Transport = (*Transport)(nil)
|
||||
_ adapter.FakeIPTransport = (*Transport)(nil)
|
||||
)
|
||||
|
||||
func init() {
|
||||
dns.RegisterTransport([]string{"fakeip"}, func(options dns.TransportOptions) (dns.Transport, error) {
|
||||
return NewTransport(options)
|
||||
})
|
||||
}
|
||||
|
||||
type Transport struct {
|
||||
name string
|
||||
router adapter.Router
|
||||
store adapter.FakeIPStore
|
||||
logger logger.ContextLogger
|
||||
}
|
||||
|
||||
func NewTransport(options dns.TransportOptions) (*Transport, error) {
|
||||
router := service.FromContext[adapter.Router](options.Context)
|
||||
if router == nil {
|
||||
return nil, E.New("missing router in context")
|
||||
}
|
||||
return &Transport{
|
||||
name: options.Name,
|
||||
router: router,
|
||||
logger: options.Logger,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Transport) Name() string {
|
||||
return s.name
|
||||
}
|
||||
|
||||
func (s *Transport) Start() error {
|
||||
s.store = s.router.FakeIPStore()
|
||||
if s.store == nil {
|
||||
return E.New("fakeip not enabled")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Transport) Reset() {
|
||||
}
|
||||
|
||||
func (s *Transport) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Transport) Raw() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *Transport) Exchange(ctx context.Context, message *mDNS.Msg) (*mDNS.Msg, error) {
|
||||
return nil, os.ErrInvalid
|
||||
}
|
||||
|
||||
func (s *Transport) Lookup(ctx context.Context, domain string, strategy dns.DomainStrategy) ([]netip.Addr, error) {
|
||||
var addresses []netip.Addr
|
||||
if strategy != dns.DomainStrategyUseIPv6 {
|
||||
inet4Address, err := s.store.Create(domain, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
addresses = append(addresses, inet4Address)
|
||||
}
|
||||
if strategy != dns.DomainStrategyUseIPv4 {
|
||||
inet6Address, err := s.store.Create(domain, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
addresses = append(addresses, inet6Address)
|
||||
}
|
||||
return addresses, nil
|
||||
}
|
||||
|
||||
func (s *Transport) Store() adapter.FakeIPStore {
|
||||
return s.store
|
||||
}
|
||||
@@ -1,118 +0,0 @@
|
||||
package fakeip
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/netip"
|
||||
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
"github.com/sagernet/sing/common/logger"
|
||||
"github.com/sagernet/sing/service"
|
||||
)
|
||||
|
||||
var _ adapter.FakeIPStore = (*Store)(nil)
|
||||
|
||||
type Store struct {
|
||||
ctx context.Context
|
||||
logger logger.Logger
|
||||
inet4Range netip.Prefix
|
||||
inet6Range netip.Prefix
|
||||
storage adapter.FakeIPStorage
|
||||
inet4Current netip.Addr
|
||||
inet6Current netip.Addr
|
||||
}
|
||||
|
||||
func NewStore(ctx context.Context, logger logger.Logger, inet4Range netip.Prefix, inet6Range netip.Prefix) *Store {
|
||||
return &Store{
|
||||
ctx: ctx,
|
||||
logger: logger,
|
||||
inet4Range: inet4Range,
|
||||
inet6Range: inet6Range,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Store) Start() error {
|
||||
var storage adapter.FakeIPStorage
|
||||
cacheFile := service.FromContext[adapter.CacheFile](s.ctx)
|
||||
if cacheFile != nil && cacheFile.StoreFakeIP() {
|
||||
storage = cacheFile
|
||||
}
|
||||
if storage == nil {
|
||||
storage = NewMemoryStorage()
|
||||
}
|
||||
metadata := storage.FakeIPMetadata()
|
||||
if metadata != nil && metadata.Inet4Range == s.inet4Range && metadata.Inet6Range == s.inet6Range {
|
||||
s.inet4Current = metadata.Inet4Current
|
||||
s.inet6Current = metadata.Inet6Current
|
||||
} else {
|
||||
if s.inet4Range.IsValid() {
|
||||
s.inet4Current = s.inet4Range.Addr().Next().Next()
|
||||
}
|
||||
if s.inet6Range.IsValid() {
|
||||
s.inet6Current = s.inet6Range.Addr().Next().Next()
|
||||
}
|
||||
_ = storage.FakeIPReset()
|
||||
}
|
||||
s.storage = storage
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) Contains(address netip.Addr) bool {
|
||||
return s.inet4Range.Contains(address) || s.inet6Range.Contains(address)
|
||||
}
|
||||
|
||||
func (s *Store) Close() error {
|
||||
if s.storage == nil {
|
||||
return nil
|
||||
}
|
||||
return s.storage.FakeIPSaveMetadata(&adapter.FakeIPMetadata{
|
||||
Inet4Range: s.inet4Range,
|
||||
Inet6Range: s.inet6Range,
|
||||
Inet4Current: s.inet4Current,
|
||||
Inet6Current: s.inet6Current,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Store) Create(domain string, isIPv6 bool) (netip.Addr, error) {
|
||||
if address, loaded := s.storage.FakeIPLoadDomain(domain, isIPv6); loaded {
|
||||
return address, nil
|
||||
}
|
||||
var address netip.Addr
|
||||
if !isIPv6 {
|
||||
if !s.inet4Current.IsValid() {
|
||||
return netip.Addr{}, E.New("missing IPv4 fakeip address range")
|
||||
}
|
||||
nextAddress := s.inet4Current.Next()
|
||||
if !s.inet4Range.Contains(nextAddress) {
|
||||
nextAddress = s.inet4Range.Addr().Next().Next()
|
||||
}
|
||||
s.inet4Current = nextAddress
|
||||
address = nextAddress
|
||||
} else {
|
||||
if !s.inet6Current.IsValid() {
|
||||
return netip.Addr{}, E.New("missing IPv6 fakeip address range")
|
||||
}
|
||||
nextAddress := s.inet6Current.Next()
|
||||
if !s.inet6Range.Contains(nextAddress) {
|
||||
nextAddress = s.inet6Range.Addr().Next().Next()
|
||||
}
|
||||
s.inet6Current = nextAddress
|
||||
address = nextAddress
|
||||
}
|
||||
s.storage.FakeIPStoreAsync(address, domain, s.logger)
|
||||
s.storage.FakeIPSaveMetadataAsync(&adapter.FakeIPMetadata{
|
||||
Inet4Range: s.inet4Range,
|
||||
Inet6Range: s.inet6Range,
|
||||
Inet4Current: s.inet4Current,
|
||||
Inet6Current: s.inet6Current,
|
||||
})
|
||||
return address, nil
|
||||
}
|
||||
|
||||
func (s *Store) Lookup(address netip.Addr) (string, bool) {
|
||||
return s.storage.FakeIPLoad(address)
|
||||
}
|
||||
|
||||
func (s *Store) Reset() error {
|
||||
return s.storage.FakeIPReset()
|
||||
}
|
||||
@@ -82,6 +82,10 @@ func (ho *HTTPObfs) Write(b []byte) (int, error) {
|
||||
return ho.Conn.Write(b)
|
||||
}
|
||||
|
||||
func (ho *HTTPObfs) Upstream() any {
|
||||
return ho.Conn
|
||||
}
|
||||
|
||||
// NewHTTPObfs return a HTTPObfs
|
||||
func NewHTTPObfs(conn net.Conn, host string, port string) net.Conn {
|
||||
return &HTTPObfs{
|
||||
|
||||
@@ -113,6 +113,10 @@ func (to *TLSObfs) write(b []byte) (int, error) {
|
||||
return len(b), err
|
||||
}
|
||||
|
||||
func (to *TLSObfs) Upstream() any {
|
||||
return to.Conn
|
||||
}
|
||||
|
||||
// NewTLSObfs return a SimpleObfs
|
||||
func NewTLSObfs(conn net.Conn, server string) net.Conn {
|
||||
return &TLSObfs{
|
||||
|
||||
@@ -62,6 +62,7 @@ func NewClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, opt
|
||||
dialOptions = append(dialOptions, grpc.WithContextDialer(func(ctx context.Context, server string) (net.Conn, error) {
|
||||
return dialer.DialContext(ctx, N.NetworkTCP, M.ParseSocksaddr(server))
|
||||
}))
|
||||
//nolint:staticcheck
|
||||
dialOptions = append(dialOptions, grpc.WithReturnConnectionError())
|
||||
return &Client{
|
||||
ctx: ctx,
|
||||
@@ -84,7 +85,6 @@ func (c *Client) connect() (*grpc.ClientConn, error) {
|
||||
return conn, nil
|
||||
}
|
||||
//nolint:staticcheck
|
||||
//goland:noinspection GoDeprecation
|
||||
conn, err := grpc.DialContext(c.ctx, c.serverAddr, c.dialOptions...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -18,6 +18,7 @@ type GRPCConn struct {
|
||||
}
|
||||
|
||||
func NewGRPCConn(service GunService) *GRPCConn {
|
||||
//nolint:staticcheck
|
||||
if client, isClient := service.(GunService_TunClient); isClient {
|
||||
service = &clientConnWrapper{client}
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ func (c *gunServiceClient) TunCustomName(ctx context.Context, name string, opts
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
x := &gunServiceTunClient{stream}
|
||||
x := &grpc.GenericClientStream[Hunk, Hunk]{ClientStream: stream}
|
||||
return x, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package v2raygrpc
|
||||
import (
|
||||
reflect "reflect"
|
||||
sync "sync"
|
||||
unsafe "unsafe"
|
||||
|
||||
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
|
||||
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
|
||||
@@ -16,20 +17,17 @@ const (
|
||||
)
|
||||
|
||||
type Hunk struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
Data []byte `protobuf:"bytes,1,opt,name=data,proto3" json:"data,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
Data []byte `protobuf:"bytes,1,opt,name=data,proto3" json:"data,omitempty"`
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *Hunk) Reset() {
|
||||
*x = Hunk{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_transport_v2raygrpc_stream_proto_msgTypes[0]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
mi := &file_transport_v2raygrpc_stream_proto_msgTypes[0]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *Hunk) String() string {
|
||||
@@ -40,7 +38,7 @@ func (*Hunk) ProtoMessage() {}
|
||||
|
||||
func (x *Hunk) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_transport_v2raygrpc_stream_proto_msgTypes[0]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
@@ -64,38 +62,30 @@ func (x *Hunk) GetData() []byte {
|
||||
|
||||
var File_transport_v2raygrpc_stream_proto protoreflect.FileDescriptor
|
||||
|
||||
var file_transport_v2raygrpc_stream_proto_rawDesc = []byte{
|
||||
0x0a, 0x20, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x70, 0x6f, 0x72, 0x74, 0x2f, 0x76, 0x32, 0x72, 0x61,
|
||||
0x79, 0x67, 0x72, 0x70, 0x63, 0x2f, 0x73, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x2e, 0x70, 0x72, 0x6f,
|
||||
0x74, 0x6f, 0x12, 0x13, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x70, 0x6f, 0x72, 0x74, 0x2e, 0x76, 0x32,
|
||||
0x72, 0x61, 0x79, 0x67, 0x72, 0x70, 0x63, 0x22, 0x1a, 0x0a, 0x04, 0x48, 0x75, 0x6e, 0x6b, 0x12,
|
||||
0x12, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x64,
|
||||
0x61, 0x74, 0x61, 0x32, 0x4d, 0x0a, 0x0a, 0x47, 0x75, 0x6e, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63,
|
||||
0x65, 0x12, 0x3f, 0x0a, 0x03, 0x54, 0x75, 0x6e, 0x12, 0x19, 0x2e, 0x74, 0x72, 0x61, 0x6e, 0x73,
|
||||
0x70, 0x6f, 0x72, 0x74, 0x2e, 0x76, 0x32, 0x72, 0x61, 0x79, 0x67, 0x72, 0x70, 0x63, 0x2e, 0x48,
|
||||
0x75, 0x6e, 0x6b, 0x1a, 0x19, 0x2e, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x70, 0x6f, 0x72, 0x74, 0x2e,
|
||||
0x76, 0x32, 0x72, 0x61, 0x79, 0x67, 0x72, 0x70, 0x63, 0x2e, 0x48, 0x75, 0x6e, 0x6b, 0x28, 0x01,
|
||||
0x30, 0x01, 0x42, 0x32, 0x5a, 0x30, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d,
|
||||
0x2f, 0x73, 0x61, 0x67, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x2f, 0x73, 0x69, 0x6e, 0x67, 0x2d, 0x62,
|
||||
0x6f, 0x78, 0x2f, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x70, 0x6f, 0x72, 0x74, 0x2f, 0x76, 0x32, 0x72,
|
||||
0x61, 0x79, 0x67, 0x72, 0x70, 0x63, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
}
|
||||
const file_transport_v2raygrpc_stream_proto_rawDesc = "" +
|
||||
"\n" +
|
||||
" transport/v2raygrpc/stream.proto\x12\x13transport.v2raygrpc\"\x1a\n" +
|
||||
"\x04Hunk\x12\x12\n" +
|
||||
"\x04data\x18\x01 \x01(\fR\x04data2M\n" +
|
||||
"\n" +
|
||||
"GunService\x12?\n" +
|
||||
"\x03Tun\x12\x19.transport.v2raygrpc.Hunk\x1a\x19.transport.v2raygrpc.Hunk(\x010\x01B2Z0github.com/sagernet/sing-box/transport/v2raygrpcb\x06proto3"
|
||||
|
||||
var (
|
||||
file_transport_v2raygrpc_stream_proto_rawDescOnce sync.Once
|
||||
file_transport_v2raygrpc_stream_proto_rawDescData = file_transport_v2raygrpc_stream_proto_rawDesc
|
||||
file_transport_v2raygrpc_stream_proto_rawDescData []byte
|
||||
)
|
||||
|
||||
func file_transport_v2raygrpc_stream_proto_rawDescGZIP() []byte {
|
||||
file_transport_v2raygrpc_stream_proto_rawDescOnce.Do(func() {
|
||||
file_transport_v2raygrpc_stream_proto_rawDescData = protoimpl.X.CompressGZIP(file_transport_v2raygrpc_stream_proto_rawDescData)
|
||||
file_transport_v2raygrpc_stream_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_transport_v2raygrpc_stream_proto_rawDesc), len(file_transport_v2raygrpc_stream_proto_rawDesc)))
|
||||
})
|
||||
return file_transport_v2raygrpc_stream_proto_rawDescData
|
||||
}
|
||||
|
||||
var (
|
||||
file_transport_v2raygrpc_stream_proto_msgTypes = make([]protoimpl.MessageInfo, 1)
|
||||
file_transport_v2raygrpc_stream_proto_goTypes = []interface{}{
|
||||
file_transport_v2raygrpc_stream_proto_goTypes = []any{
|
||||
(*Hunk)(nil), // 0: transport.v2raygrpc.Hunk
|
||||
}
|
||||
)
|
||||
@@ -115,25 +105,11 @@ func file_transport_v2raygrpc_stream_proto_init() {
|
||||
if File_transport_v2raygrpc_stream_proto != nil {
|
||||
return
|
||||
}
|
||||
if !protoimpl.UnsafeEnabled {
|
||||
file_transport_v2raygrpc_stream_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*Hunk); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
type x struct{}
|
||||
out := protoimpl.TypeBuilder{
|
||||
File: protoimpl.DescBuilder{
|
||||
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
||||
RawDescriptor: file_transport_v2raygrpc_stream_proto_rawDesc,
|
||||
RawDescriptor: unsafe.Slice(unsafe.StringData(file_transport_v2raygrpc_stream_proto_rawDesc), len(file_transport_v2raygrpc_stream_proto_rawDesc)),
|
||||
NumEnums: 0,
|
||||
NumMessages: 1,
|
||||
NumExtensions: 0,
|
||||
@@ -144,7 +120,6 @@ func file_transport_v2raygrpc_stream_proto_init() {
|
||||
MessageInfos: file_transport_v2raygrpc_stream_proto_msgTypes,
|
||||
}.Build()
|
||||
File_transport_v2raygrpc_stream_proto = out.File
|
||||
file_transport_v2raygrpc_stream_proto_rawDesc = nil
|
||||
file_transport_v2raygrpc_stream_proto_goTypes = nil
|
||||
file_transport_v2raygrpc_stream_proto_depIdxs = nil
|
||||
}
|
||||
|
||||
@@ -10,8 +10,8 @@ import (
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the grpc package it is being compiled against.
|
||||
// Requires gRPC-Go v1.32.0 or later.
|
||||
const _ = grpc.SupportPackageIsVersion7
|
||||
// Requires gRPC-Go v1.64.0 or later.
|
||||
const _ = grpc.SupportPackageIsVersion9
|
||||
|
||||
const (
|
||||
GunService_Tun_FullMethodName = "/transport.v2raygrpc.GunService/Tun"
|
||||
@@ -21,7 +21,7 @@ const (
|
||||
//
|
||||
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
|
||||
type GunServiceClient interface {
|
||||
Tun(ctx context.Context, opts ...grpc.CallOption) (GunService_TunClient, error)
|
||||
Tun(ctx context.Context, opts ...grpc.CallOption) (grpc.BidiStreamingClient[Hunk, Hunk], error)
|
||||
}
|
||||
|
||||
type gunServiceClient struct {
|
||||
@@ -32,52 +32,39 @@ func NewGunServiceClient(cc grpc.ClientConnInterface) GunServiceClient {
|
||||
return &gunServiceClient{cc}
|
||||
}
|
||||
|
||||
func (c *gunServiceClient) Tun(ctx context.Context, opts ...grpc.CallOption) (GunService_TunClient, error) {
|
||||
stream, err := c.cc.NewStream(ctx, &GunService_ServiceDesc.Streams[0], GunService_Tun_FullMethodName, opts...)
|
||||
func (c *gunServiceClient) Tun(ctx context.Context, opts ...grpc.CallOption) (grpc.BidiStreamingClient[Hunk, Hunk], error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
stream, err := c.cc.NewStream(ctx, &GunService_ServiceDesc.Streams[0], GunService_Tun_FullMethodName, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
x := &gunServiceTunClient{stream}
|
||||
x := &grpc.GenericClientStream[Hunk, Hunk]{ClientStream: stream}
|
||||
return x, nil
|
||||
}
|
||||
|
||||
type GunService_TunClient interface {
|
||||
Send(*Hunk) error
|
||||
Recv() (*Hunk, error)
|
||||
grpc.ClientStream
|
||||
}
|
||||
|
||||
type gunServiceTunClient struct {
|
||||
grpc.ClientStream
|
||||
}
|
||||
|
||||
func (x *gunServiceTunClient) Send(m *Hunk) error {
|
||||
return x.ClientStream.SendMsg(m)
|
||||
}
|
||||
|
||||
func (x *gunServiceTunClient) Recv() (*Hunk, error) {
|
||||
m := new(Hunk)
|
||||
if err := x.ClientStream.RecvMsg(m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name.
|
||||
type GunService_TunClient = grpc.BidiStreamingClient[Hunk, Hunk]
|
||||
|
||||
// GunServiceServer is the server API for GunService service.
|
||||
// All implementations must embed UnimplementedGunServiceServer
|
||||
// for forward compatibility
|
||||
// for forward compatibility.
|
||||
type GunServiceServer interface {
|
||||
Tun(GunService_TunServer) error
|
||||
Tun(grpc.BidiStreamingServer[Hunk, Hunk]) error
|
||||
mustEmbedUnimplementedGunServiceServer()
|
||||
}
|
||||
|
||||
// UnimplementedGunServiceServer must be embedded to have forward compatible implementations.
|
||||
// UnimplementedGunServiceServer must be embedded to have
|
||||
// forward compatible implementations.
|
||||
//
|
||||
// NOTE: this should be embedded by value instead of pointer to avoid a nil
|
||||
// pointer dereference when methods are called.
|
||||
type UnimplementedGunServiceServer struct{}
|
||||
|
||||
func (UnimplementedGunServiceServer) Tun(GunService_TunServer) error {
|
||||
func (UnimplementedGunServiceServer) Tun(grpc.BidiStreamingServer[Hunk, Hunk]) error {
|
||||
return status.Errorf(codes.Unimplemented, "method Tun not implemented")
|
||||
}
|
||||
func (UnimplementedGunServiceServer) mustEmbedUnimplementedGunServiceServer() {}
|
||||
func (UnimplementedGunServiceServer) testEmbeddedByValue() {}
|
||||
|
||||
// UnsafeGunServiceServer may be embedded to opt out of forward compatibility for this service.
|
||||
// Use of this interface is not recommended, as added methods to GunServiceServer will
|
||||
@@ -87,34 +74,22 @@ type UnsafeGunServiceServer interface {
|
||||
}
|
||||
|
||||
func RegisterGunServiceServer(s grpc.ServiceRegistrar, srv GunServiceServer) {
|
||||
// If the following call pancis, it indicates UnimplementedGunServiceServer was
|
||||
// embedded by pointer and is nil. This will cause panics if an
|
||||
// unimplemented method is ever invoked, so we test this at initialization
|
||||
// time to prevent it from happening at runtime later due to I/O.
|
||||
if t, ok := srv.(interface{ testEmbeddedByValue() }); ok {
|
||||
t.testEmbeddedByValue()
|
||||
}
|
||||
s.RegisterService(&GunService_ServiceDesc, srv)
|
||||
}
|
||||
|
||||
func _GunService_Tun_Handler(srv interface{}, stream grpc.ServerStream) error {
|
||||
return srv.(GunServiceServer).Tun(&gunServiceTunServer{stream})
|
||||
return srv.(GunServiceServer).Tun(&grpc.GenericServerStream[Hunk, Hunk]{ServerStream: stream})
|
||||
}
|
||||
|
||||
type GunService_TunServer interface {
|
||||
Send(*Hunk) error
|
||||
Recv() (*Hunk, error)
|
||||
grpc.ServerStream
|
||||
}
|
||||
|
||||
type gunServiceTunServer struct {
|
||||
grpc.ServerStream
|
||||
}
|
||||
|
||||
func (x *gunServiceTunServer) Send(m *Hunk) error {
|
||||
return x.ServerStream.SendMsg(m)
|
||||
}
|
||||
|
||||
func (x *gunServiceTunServer) Recv() (*Hunk, error) {
|
||||
m := new(Hunk)
|
||||
if err := x.ServerStream.RecvMsg(m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name.
|
||||
type GunService_TunServer = grpc.BidiStreamingServer[Hunk, Hunk]
|
||||
|
||||
// GunService_ServiceDesc is the grpc.ServiceDesc for GunService service.
|
||||
// It's only intended for direct use with grpc.RegisterService,
|
||||
|
||||
@@ -165,7 +165,7 @@ func (s *Server) Serve(listener net.Listener) error {
|
||||
if len(s.tlsConfig.NextProtos()) == 0 {
|
||||
s.tlsConfig.SetNextProtos([]string{http2.NextProtoTLS, "http/1.1"})
|
||||
} else if !common.Contains(s.tlsConfig.NextProtos(), http2.NextProtoTLS) {
|
||||
s.tlsConfig.SetNextProtos(append([]string{"h2"}, s.tlsConfig.NextProtos()...))
|
||||
s.tlsConfig.SetNextProtos(append([]string{http2.NextProtoTLS}, s.tlsConfig.NextProtos()...))
|
||||
}
|
||||
listener = aTLS.NewListener(listener, s.tlsConfig)
|
||||
}
|
||||
|
||||
@@ -23,7 +23,6 @@ import (
|
||||
"github.com/sagernet/sing-box/common/xray/signal/done"
|
||||
"github.com/sagernet/sing-box/common/xray/uuid"
|
||||
"github.com/sagernet/sing-box/option"
|
||||
dns "github.com/sagernet/sing-dns"
|
||||
"github.com/sagernet/sing/common"
|
||||
"github.com/sagernet/sing/common/bufio"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
@@ -47,7 +46,6 @@ func NewClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, opt
|
||||
if options.Mode == "" {
|
||||
return nil, E.New("mode is not set")
|
||||
}
|
||||
router := service.FromContext[adapter.Router](ctx)
|
||||
dest := serverAddr
|
||||
var gotlsConfig *gotls.Config
|
||||
if tlsConfig != nil {
|
||||
@@ -68,13 +66,6 @@ func NewClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, opt
|
||||
requestURL.Path += sessionId
|
||||
return requestURL
|
||||
}
|
||||
if dest.IsFqdn() {
|
||||
addresses, err := router.Lookup(ctx, dest.Fqdn, dns.DomainStrategy(options.DomainStrategy))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dest.Addr = addresses[0]
|
||||
}
|
||||
var xmuxOptions option.V2RayXHTTPXmuxOptions
|
||||
if options.Xmux != nil {
|
||||
xmuxOptions = *options.Xmux
|
||||
@@ -120,13 +111,6 @@ func NewClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, opt
|
||||
requestURL2.Path += sessionId
|
||||
return requestURL2
|
||||
}
|
||||
if dest2.IsFqdn() {
|
||||
addresses2, err := router.Lookup(ctx, dest2.Fqdn, dns.DomainStrategy(options2.DomainStrategy))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dest2.Addr = addresses2[0]
|
||||
}
|
||||
var xmuxOptions2 option.V2RayXHTTPXmuxOptions
|
||||
if options2.Xmux != nil {
|
||||
xmuxOptions2 = *options2.Xmux
|
||||
@@ -350,7 +334,7 @@ func createHTTPClient(dest M.Socksaddr, dialer N.Dialer, options *option.V2RayXH
|
||||
transport = &http3.Transport{
|
||||
QUICConfig: quicConfig,
|
||||
TLSClientConfig: gotlsConfig.Clone(),
|
||||
Dial: func(ctx context.Context, addr string, tlsCfg *gotls.Config, cfg *quic.Config) (quic.EarlyConnection, error) {
|
||||
Dial: func(ctx context.Context, addr string, tlsCfg *gotls.Config, cfg *quic.Config) (*quic.Conn, error) {
|
||||
udpConn, dErr := dialer.DialContext(ctx, N.NetworkUDP, dest)
|
||||
if dErr != nil {
|
||||
return nil, dErr
|
||||
|
||||
Reference in New Issue
Block a user