mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-07-17 21:31:04 +03:00
Update sing-box core, refactor MASQUE, update XHTTP
This commit is contained in:
@@ -136,7 +136,7 @@ func (c *ClientBind) receive(packets [][]byte, sizes []int, eps []conn.Endpoint)
|
||||
sizes[0] = n
|
||||
if n > 3 {
|
||||
b := packets[0]
|
||||
common.ClearArray(b[1:4])
|
||||
clear(b[1:4])
|
||||
}
|
||||
eps[0] = remoteEndpoint(M.SocksaddrFromNet(addr).Unwrap().AddrPort())
|
||||
count = 1
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"net"
|
||||
"net/netip"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/sagernet/gvisor/pkg/buffer"
|
||||
@@ -42,6 +43,7 @@ type stackDevice struct {
|
||||
outbound chan *stack.PacketBuffer
|
||||
packetOutbound chan *buf.Buffer
|
||||
done chan struct{}
|
||||
closeOnce sync.Once
|
||||
dispatcher stack.NetworkDispatcher
|
||||
inet4Address netip.Addr
|
||||
inet6Address netip.Addr
|
||||
@@ -146,11 +148,17 @@ func (w *stackDevice) ListenPacket(ctx context.Context, destination M.Socksaddr)
|
||||
}
|
||||
var networkProtocol tcpip.NetworkProtocolNumber
|
||||
if destination.IsIPv4() {
|
||||
if !w.inet4Address.IsValid() {
|
||||
return nil, E.New("missing IPv4 local address")
|
||||
}
|
||||
networkProtocol = header.IPv4ProtocolNumber
|
||||
bind.Addr = tun.AddressFromAddr(w.inet4Address)
|
||||
} else {
|
||||
if !w.inet6Address.IsValid() {
|
||||
return nil, E.New("missing IPv6 local address")
|
||||
}
|
||||
networkProtocol = header.IPv6ProtocolNumber
|
||||
bind.Addr = tun.AddressFromAddr(w.inet4Address)
|
||||
bind.Addr = tun.AddressFromAddr(w.inet6Address)
|
||||
}
|
||||
udpConn, err := gonet.DialUDP(w.stack, &bind, nil, networkProtocol)
|
||||
if err != nil {
|
||||
@@ -244,13 +252,15 @@ func (w *stackDevice) Events() <-chan wgTun.Event {
|
||||
}
|
||||
|
||||
func (w *stackDevice) Close() error {
|
||||
close(w.done)
|
||||
close(w.events)
|
||||
w.stack.Close()
|
||||
for _, endpoint := range w.stack.CleanupEndpoints() {
|
||||
endpoint.Abort()
|
||||
}
|
||||
w.stack.Wait()
|
||||
w.closeOnce.Do(func() {
|
||||
close(w.done)
|
||||
close(w.events)
|
||||
w.stack.Close()
|
||||
for _, endpoint := range w.stack.CleanupEndpoints() {
|
||||
endpoint.Abort()
|
||||
}
|
||||
w.stack.Wait()
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -111,6 +111,7 @@ func (w *systemDevice) Start() error {
|
||||
}
|
||||
err = tunInterface.Start()
|
||||
if err != nil {
|
||||
tunInterface.Close()
|
||||
return err
|
||||
}
|
||||
w.options.Logger.Info("started at ", w.options.Name)
|
||||
@@ -147,7 +148,7 @@ func (w *systemDevice) Write(bufs [][]byte, offset int) (count int, err error) {
|
||||
} else {
|
||||
for _, packet := range bufs {
|
||||
if tun.PacketOffset > 0 {
|
||||
common.ClearArray(packet[offset-tun.PacketOffset : offset])
|
||||
clear(packet[offset-tun.PacketOffset : offset])
|
||||
tun.PacketFillHeader(packet[offset-tun.PacketOffset:], tun.PacketIPVersion(packet[offset:]))
|
||||
}
|
||||
_, err = w.device.Write(packet[offset-tun.PacketOffset:])
|
||||
@@ -177,8 +178,14 @@ func (w *systemDevice) Events() <-chan wgTun.Event {
|
||||
}
|
||||
|
||||
func (w *systemDevice) Close() error {
|
||||
close(w.events)
|
||||
return w.device.Close()
|
||||
var err error
|
||||
w.closeOnce.Do(func() {
|
||||
close(w.events)
|
||||
if w.device != nil {
|
||||
err = w.device.Close()
|
||||
}
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (w *systemDevice) BatchSize() int {
|
||||
|
||||
@@ -5,6 +5,7 @@ package wireguard
|
||||
import (
|
||||
"context"
|
||||
"net/netip"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/sagernet/gvisor/pkg/buffer"
|
||||
@@ -20,7 +21,6 @@ import (
|
||||
"github.com/sagernet/sing-box/log"
|
||||
"github.com/sagernet/sing-tun"
|
||||
"github.com/sagernet/sing-tun/ping"
|
||||
"github.com/sagernet/sing/common"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
"github.com/sagernet/sing/common/logger"
|
||||
"github.com/sagernet/wireguard-go/device"
|
||||
@@ -35,6 +35,7 @@ type systemStackDevice struct {
|
||||
stack *stack.Stack
|
||||
endpoint *deviceEndpoint
|
||||
writeBufs [][]byte
|
||||
closeOnce sync.Once
|
||||
}
|
||||
|
||||
func newSystemStackDevice(options DeviceOptions) (*systemStackDevice, error) {
|
||||
@@ -104,13 +105,13 @@ func (w *systemStackDevice) Write(bufs [][]byte, offset int) (count int, err err
|
||||
}
|
||||
}
|
||||
if len(w.writeBufs) > 0 {
|
||||
return w.batchDevice.BatchWrite(bufs, offset)
|
||||
return w.batchDevice.BatchWrite(w.writeBufs, offset)
|
||||
}
|
||||
} else {
|
||||
for _, packet := range bufs {
|
||||
if !w.writeStack(packet[offset:]) {
|
||||
if tun.PacketOffset > 0 {
|
||||
common.ClearArray(packet[offset-tun.PacketOffset : offset])
|
||||
clear(packet[offset-tun.PacketOffset : offset])
|
||||
tun.PacketFillHeader(packet[offset-tun.PacketOffset:], tun.PacketIPVersion(packet[offset:]))
|
||||
}
|
||||
_, err = w.device.Write(packet[offset-tun.PacketOffset:])
|
||||
@@ -125,13 +126,17 @@ func (w *systemStackDevice) Write(bufs [][]byte, offset int) (count int, err err
|
||||
}
|
||||
|
||||
func (w *systemStackDevice) Close() error {
|
||||
close(w.endpoint.done)
|
||||
w.stack.Close()
|
||||
for _, endpoint := range w.stack.CleanupEndpoints() {
|
||||
endpoint.Abort()
|
||||
}
|
||||
w.stack.Wait()
|
||||
return w.systemDevice.Close()
|
||||
var err error
|
||||
w.closeOnce.Do(func() {
|
||||
close(w.endpoint.done)
|
||||
w.stack.Close()
|
||||
for _, endpoint := range w.stack.CleanupEndpoints() {
|
||||
endpoint.Abort()
|
||||
}
|
||||
w.stack.Wait()
|
||||
err = w.systemDevice.Close()
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (w *systemStackDevice) writeStack(packet []byte) bool {
|
||||
|
||||
@@ -183,10 +183,10 @@ func (e *Endpoint) Start(resolve bool) error {
|
||||
return err
|
||||
}
|
||||
logger := &device.Logger{
|
||||
Verbosef: func(format string, args ...interface{}) {
|
||||
Verbosef: func(format string, args ...any) {
|
||||
e.options.Logger.Debug(fmt.Sprintf(strings.ToLower(format), args...))
|
||||
},
|
||||
Errorf: func(format string, args ...interface{}) {
|
||||
Errorf: func(format string, args ...any) {
|
||||
e.options.Logger.Error(fmt.Sprintf(strings.ToLower(format), args...))
|
||||
},
|
||||
}
|
||||
@@ -198,75 +198,77 @@ func (e *Endpoint) Start(resolve bool) error {
|
||||
}
|
||||
wgDevice := device.NewDevice(e.options.Context, deviceInput, bind, logger, e.options.Workers, e.options.PreallocatedBuffersPerPool, e.options.DisablePauses)
|
||||
e.tunDevice.SetDevice(wgDevice)
|
||||
ipcConf := e.ipcConf
|
||||
var ipcConf strings.Builder
|
||||
ipcConf.WriteString(e.ipcConf)
|
||||
if e.options.Amnezia != nil {
|
||||
if e.options.Amnezia.JC > 0 {
|
||||
ipcConf += "\njc=" + strconv.Itoa(e.options.Amnezia.JC)
|
||||
ipcConf.WriteString("\njc=" + strconv.Itoa(e.options.Amnezia.JC))
|
||||
}
|
||||
if e.options.Amnezia.JMin > 0 {
|
||||
ipcConf += "\njmin=" + strconv.Itoa(e.options.Amnezia.JMin)
|
||||
ipcConf.WriteString("\njmin=" + strconv.Itoa(e.options.Amnezia.JMin))
|
||||
}
|
||||
if e.options.Amnezia.JMax > 0 {
|
||||
ipcConf += "\njmax=" + strconv.Itoa(e.options.Amnezia.JMax)
|
||||
ipcConf.WriteString("\njmax=" + strconv.Itoa(e.options.Amnezia.JMax))
|
||||
}
|
||||
if e.options.Amnezia.S1 > 0 {
|
||||
ipcConf += "\ns1=" + strconv.Itoa(e.options.Amnezia.S1)
|
||||
ipcConf.WriteString("\ns1=" + strconv.Itoa(e.options.Amnezia.S1))
|
||||
}
|
||||
if e.options.Amnezia.S2 > 0 {
|
||||
ipcConf += "\ns2=" + strconv.Itoa(e.options.Amnezia.S2)
|
||||
ipcConf.WriteString("\ns2=" + strconv.Itoa(e.options.Amnezia.S2))
|
||||
}
|
||||
if e.options.Amnezia.S3 > 0 {
|
||||
ipcConf += "\ns3=" + strconv.Itoa(e.options.Amnezia.S3)
|
||||
ipcConf.WriteString("\ns3=" + strconv.Itoa(e.options.Amnezia.S3))
|
||||
}
|
||||
if e.options.Amnezia.S4 > 0 {
|
||||
ipcConf += "\ns4=" + strconv.Itoa(e.options.Amnezia.S4)
|
||||
ipcConf.WriteString("\ns4=" + strconv.Itoa(e.options.Amnezia.S4))
|
||||
}
|
||||
if e.options.Amnezia.H1 != nil {
|
||||
ipcConf += "\nh1=" + e.options.Amnezia.H1.String()
|
||||
ipcConf.WriteString("\nh1=" + e.options.Amnezia.H1.String())
|
||||
}
|
||||
if e.options.Amnezia.H2 != nil {
|
||||
ipcConf += "\nh2=" + e.options.Amnezia.H2.String()
|
||||
ipcConf.WriteString("\nh2=" + e.options.Amnezia.H2.String())
|
||||
}
|
||||
if e.options.Amnezia.H3 != nil {
|
||||
ipcConf += "\nh3=" + e.options.Amnezia.H3.String()
|
||||
ipcConf.WriteString("\nh3=" + e.options.Amnezia.H3.String())
|
||||
}
|
||||
if e.options.Amnezia.H4 != nil {
|
||||
ipcConf += "\nh4=" + e.options.Amnezia.H4.String()
|
||||
ipcConf.WriteString("\nh4=" + e.options.Amnezia.H4.String())
|
||||
}
|
||||
if e.options.Amnezia.I1 != "" {
|
||||
ipcConf += "\ni1=" + e.options.Amnezia.I1
|
||||
ipcConf.WriteString("\ni1=" + e.options.Amnezia.I1)
|
||||
}
|
||||
if e.options.Amnezia.I2 != "" {
|
||||
ipcConf += "\ni2=" + e.options.Amnezia.I2
|
||||
ipcConf.WriteString("\ni2=" + e.options.Amnezia.I2)
|
||||
}
|
||||
if e.options.Amnezia.I3 != "" {
|
||||
ipcConf += "\ni3=" + e.options.Amnezia.I3
|
||||
ipcConf.WriteString("\ni3=" + e.options.Amnezia.I3)
|
||||
}
|
||||
if e.options.Amnezia.I4 != "" {
|
||||
ipcConf += "\ni4=" + e.options.Amnezia.I4
|
||||
ipcConf.WriteString("\ni4=" + e.options.Amnezia.I4)
|
||||
}
|
||||
if e.options.Amnezia.I5 != "" {
|
||||
ipcConf += "\ni5=" + e.options.Amnezia.I5
|
||||
ipcConf.WriteString("\ni5=" + e.options.Amnezia.I5)
|
||||
}
|
||||
if e.options.Amnezia.J1 != "" {
|
||||
ipcConf += "\nj1=" + e.options.Amnezia.J1
|
||||
ipcConf.WriteString("\nj1=" + e.options.Amnezia.J1)
|
||||
}
|
||||
if e.options.Amnezia.J2 != "" {
|
||||
ipcConf += "\nj2=" + e.options.Amnezia.J2
|
||||
ipcConf.WriteString("\nj2=" + e.options.Amnezia.J2)
|
||||
}
|
||||
if e.options.Amnezia.J3 != "" {
|
||||
ipcConf += "\nj3=" + e.options.Amnezia.J3
|
||||
ipcConf.WriteString("\nj3=" + e.options.Amnezia.J3)
|
||||
}
|
||||
if e.options.Amnezia.ITime > 0 {
|
||||
ipcConf += "\nitime=" + strconv.FormatInt(e.options.Amnezia.ITime, 10)
|
||||
ipcConf.WriteString("\nitime=" + strconv.FormatInt(e.options.Amnezia.ITime, 10))
|
||||
}
|
||||
}
|
||||
for _, peer := range e.peers {
|
||||
ipcConf += peer.GenerateIpcLines()
|
||||
ipcConf.WriteString(peer.GenerateIpcLines())
|
||||
}
|
||||
err = wgDevice.IpcSet(ipcConf)
|
||||
err = wgDevice.IpcSet(ipcConf.String())
|
||||
if err != nil {
|
||||
return E.Cause(err, "setup wireguard: \n", ipcConf)
|
||||
wgDevice.Close()
|
||||
return E.Cause(err, "setup wireguard: \n", ipcConf.String())
|
||||
}
|
||||
e.device = wgDevice
|
||||
e.pause = service.FromContext[pause.Manager](e.options.Context)
|
||||
@@ -294,10 +296,12 @@ func (e *Endpoint) ListenPacket(ctx context.Context, destination M.Socksaddr) (n
|
||||
func (e *Endpoint) Close() error {
|
||||
if e.pauseCallback != nil {
|
||||
e.pause.UnregisterCallback(e.pauseCallback)
|
||||
e.pauseCallback = nil
|
||||
}
|
||||
if e.device != nil {
|
||||
e.device.Down()
|
||||
e.device.Close()
|
||||
e.device = nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -336,18 +340,19 @@ type peerConfig struct {
|
||||
}
|
||||
|
||||
func (c peerConfig) GenerateIpcLines() string {
|
||||
ipcLines := "\npublic_key=" + c.publicKeyHex
|
||||
var ipcLines strings.Builder
|
||||
ipcLines.WriteString("\npublic_key=" + c.publicKeyHex)
|
||||
if c.endpoint.IsValid() {
|
||||
ipcLines += "\nendpoint=" + c.endpoint.String()
|
||||
ipcLines.WriteString("\nendpoint=" + c.endpoint.String())
|
||||
}
|
||||
if c.preSharedKeyHex != "" {
|
||||
ipcLines += "\npreshared_key=" + c.preSharedKeyHex
|
||||
ipcLines.WriteString("\npreshared_key=" + c.preSharedKeyHex)
|
||||
}
|
||||
for _, allowedIP := range c.allowedIPs {
|
||||
ipcLines += "\nallowed_ip=" + allowedIP.String()
|
||||
ipcLines.WriteString("\nallowed_ip=" + allowedIP.String())
|
||||
}
|
||||
if c.keepalive > 0 {
|
||||
ipcLines += "\npersistent_keepalive_interval=" + F.ToString(c.keepalive)
|
||||
ipcLines.WriteString("\npersistent_keepalive_interval=" + F.ToString(c.keepalive))
|
||||
}
|
||||
return ipcLines
|
||||
return ipcLines.String()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user