diff --git a/adapter/outbound/sudoku.go b/adapter/outbound/sudoku.go index 1db9ad2d..5fcc0800 100644 --- a/adapter/outbound/sudoku.go +++ b/adapter/outbound/sudoku.go @@ -6,11 +6,14 @@ import ( "encoding/binary" "fmt" "io" + "math/rand" "net" "strconv" "strings" "time" + "github.com/metacubex/mihomo/log" + "github.com/saba-futai/sudoku/apis" "github.com/saba-futai/sudoku/pkg/crypto" "github.com/saba-futai/sudoku/pkg/obfs/httpmask" @@ -38,8 +41,8 @@ type SudokuOption struct { AEADMethod string `proxy:"aead-method,omitempty"` PaddingMin *int `proxy:"padding-min,omitempty"` PaddingMax *int `proxy:"padding-max,omitempty"` - Seed string `proxy:"seed,omitempty"` TableType string `proxy:"table-type,omitempty"` // "prefer_ascii" or "prefer_entropy" + HTTPMask bool `proxy:"http-mask,omitempty"` } // DialContext implements C.ProxyAdapter @@ -120,8 +123,10 @@ func (s *Sudoku) buildConfig(metadata *C.Metadata) (*apis.ProtocolConfig, error) } func (s *Sudoku) streamConn(rawConn net.Conn, cfg *apis.ProtocolConfig) (_ net.Conn, err error) { - if err = httpmask.WriteRandomRequestHeader(rawConn, cfg.ServerAddress); err != nil { - return nil, fmt.Errorf("write http mask failed: %w", err) + if !cfg.DisableHTTPMask { + if err = httpmask.WriteRandomRequestHeader(rawConn, cfg.ServerAddress); err != nil { + return nil, fmt.Errorf("write http mask failed: %w", err) + } } obfsConn := sudoku.NewConn(rawConn, cfg.Table, cfg.PaddingMin, cfg.PaddingMax, false) @@ -163,12 +168,13 @@ func NewSudoku(option SudokuOption) (*Sudoku, error) { return nil, fmt.Errorf("table-type must be prefer_ascii or prefer_entropy") } - seed := option.Seed - if seed == "" { - seed = option.Key + seed := option.Key + if recoveredFromKey, err := crypto.RecoverPublicKey(option.Key); err == nil { + seed = crypto.EncodePoint(recoveredFromKey) } - table := sudoku.NewTable(seed, tableType) + // Use local initTable instead of sudoku.NewTable to control logging + table := initTable(seed, tableType) defaultConf := apis.DefaultConfig() paddingMin := defaultConf.PaddingMin @@ -194,6 +200,7 @@ func NewSudoku(option SudokuOption) (*Sudoku, error) { PaddingMin: paddingMin, PaddingMax: paddingMax, HandshakeTimeoutSeconds: defaultConf.HandshakeTimeoutSeconds, + DisableHTTPMask: !option.HTTPMask, } if option.AEADMethod != "" { baseConf.AEADMethod = option.AEADMethod @@ -261,3 +268,147 @@ func writeTargetAddress(w io.Writer, rawAddr string) error { _, err = w.Write(buf) return err } + +// initTable initializes the obfuscation tables with Mihomo logging +// mode: "prefer_ascii" or "prefer_entropy" +func initTable(key string, mode string) *sudoku.Table { + start := time.Now() + t := &sudoku.Table{ + DecodeMap: make(map[uint32]byte), + IsASCII: mode == "prefer_ascii", + } + + // Initialize padding pool and encoding logic + if t.IsASCII { + // === ASCII Mode (0x20 - 0x7F) === + // Payload (Hint): 01vvpppp (Bit 6 is 1) -> 0x40 | ... + // Padding: 001xxxxx (Bit 6 is 0) -> 0x20 | rand(0-31) + // Range: Padding [32, 63], Payload [64, 127] + + t.PaddingPool = make([]byte, 0, 32) + for i := 0; i < 32; i++ { + // 001xxxxx -> 0x20 + i + t.PaddingPool = append(t.PaddingPool, byte(0x20+i)) + } + } else { + // === Entropy Mode (Legacy) === + // Padding: 0x80 (10000xxx) & 0x10 (00010xxx) + // Payload: Avoids bits 7 and 4 being strictly defined like padding + t.PaddingPool = make([]byte, 0, 16) + for i := 0; i < 8; i++ { + t.PaddingPool = append(t.PaddingPool, byte(0x80+i)) + t.PaddingPool = append(t.PaddingPool, byte(0x10+i)) + } + } + + // Generate sudoku grids + allGrids := sudoku.GenerateAllGrids() + h := sha256.New() + h.Write([]byte(key)) + seed := int64(binary.BigEndian.Uint64(h.Sum(nil)[:8])) + rng := rand.New(rand.NewSource(seed)) + + shuffledGrids := make([]sudoku.Grid, 288) + copy(shuffledGrids, allGrids) + rng.Shuffle(len(shuffledGrids), func(i, j int) { + shuffledGrids[i], shuffledGrids[j] = shuffledGrids[j], shuffledGrids[i] + }) + + // Pre-calculate combinations + var combinations [][]int + var combine func(int, int, []int) + combine = func(s, k int, c []int) { + if k == 0 { + tmp := make([]int, len(c)) + copy(tmp, c) + combinations = append(combinations, tmp) + return + } + for i := s; i <= 16-k; i++ { + c = append(c, i) + combine(i+1, k-1, c) + c = c[:len(c)-1] + } + } + combine(0, 4, []int{}) + + // Build mapping table + for byteVal := 0; byteVal < 256; byteVal++ { + targetGrid := shuffledGrids[byteVal] + for _, positions := range combinations { + var currentHints [4]byte + + // 1. Calculate Abstract Hints + var rawParts [4]struct{ val, pos byte } + + for i, pos := range positions { + val := targetGrid[pos] // 1..4 + rawParts[i] = struct{ val, pos byte }{val, uint8(pos)} + } + + // Check uniqueness (Sudoku logic) + matchCount := 0 + for _, g := range allGrids { + match := true + for _, p := range rawParts { + if g[p.pos] != p.val { + match = false + break + } + } + if match { + matchCount++ + if matchCount > 1 { + break + } + } + } + + if matchCount == 1 { + // Unique, generate final encoding bytes + for i, p := range rawParts { + if t.IsASCII { + // ASCII Encoding: 01vvpppp + // vv = val-1 (0..3), pppp = pos (0..15) + // 0x40 | (vv << 4) | pppp + currentHints[i] = 0x40 | ((p.val - 1) << 4) | (p.pos & 0x0F) + } else { + // Entropy Encoding (Legacy) + // 0vv0pppp + // Format: ((val-1) << 5) | pos + currentHints[i] = ((p.val - 1) << 5) | (p.pos & 0x0F) + } + } + + t.EncodeTable[byteVal] = append(t.EncodeTable[byteVal], currentHints) + // Generate decode key + key := packHintsToKey(currentHints) + t.DecodeMap[key] = byte(byteVal) + } + } + } + log.Infoln("[Sudoku] Tables initialized (%s) in %v", mode, time.Since(start)) + return t +} + +func packHintsToKey(hints [4]byte) uint32 { + // Sorting network for 4 elements (Bubble sort unrolled) + // Swap if a > b + if hints[0] > hints[1] { + hints[0], hints[1] = hints[1], hints[0] + } + if hints[2] > hints[3] { + hints[2], hints[3] = hints[3], hints[2] + } + if hints[0] > hints[2] { + hints[0], hints[2] = hints[2], hints[0] + } + if hints[1] > hints[3] { + hints[1], hints[3] = hints[3], hints[1] + } + if hints[1] > hints[2] { + hints[1], hints[2] = hints[2], hints[1] + } + + return uint32(hints[0])<<24 | uint32(hints[1])<<16 | uint32(hints[2])<<8 | uint32(hints[3]) +} diff --git a/docs/config.yaml b/docs/config.yaml index 0d5fb75e..bef2c659 100644 --- a/docs/config.yaml +++ b/docs/config.yaml @@ -1047,8 +1047,8 @@ proxies: # socks5 aead-method: chacha20-poly1305 # 可选值:chacha20-poly1305、aes-128-gcm、none 我们保证在none的情况下sudoku混淆层仍然确保安全 padding-min: 2 # 最小填充字节数 padding-max: 7 # 最大填充字节数 - seed: "" # 如果使用sudoku生成的ED25519密钥对,请填写密钥对中的公钥(如果你有安全焦虑,填入私钥也可以,只是私钥长度比较长不好看而已),否则填入和服务端相同的uuid table-type: prefer_ascii # 可选值:prefer_ascii、prefer_entropy 前者全ascii映射,后者保证熵值(汉明1)低于3 + http-mask: true # 是否启用http掩码 # anytls - name: anytls @@ -1587,7 +1587,7 @@ listeners: aead-method: chacha20-poly1305 # 支持chacha20-poly1305或者aes-128-gcm以及none,sudoku的混淆层可以确保none情况下数据安全 padding-min: 1 # 填充最小长度 padding-max: 15 # 填充最大长度,均不建议过大 - seed: "" # 如果你不使用ED25519密钥对,就请填入客户端的key,否则仍然是公钥 + seed: "" # 如果你不使用ED25519密钥对,就请填入uuid,否则仍然是公钥 table-type: prefer_ascii # 可选值:prefer_ascii、prefer_entropy 前者全ascii映射,后者保证熵值(汉明1)低于3 handshake-timeout: 5 # optional diff --git a/go.mod b/go.mod index 67db2286..86232a15 100644 --- a/go.mod +++ b/go.mod @@ -43,7 +43,7 @@ require ( github.com/mroth/weightedrand/v2 v2.1.0 github.com/openacid/low v0.1.21 github.com/oschwald/maxminddb-golang v1.12.0 // lastest version compatible with golang1.20 - github.com/saba-futai/sudoku v0.0.1-e + github.com/saba-futai/sudoku v0.0.1-f github.com/sagernet/cors v1.2.1 github.com/sagernet/netlink v0.0.0-20240612041022-b9a21c07ac6a github.com/samber/lo v1.52.0 diff --git a/go.sum b/go.sum index c135867e..bc5bf78a 100644 --- a/go.sum +++ b/go.sum @@ -171,8 +171,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/prashantv/gostub v1.1.0 h1:BTyx3RfQjRHnUWaGF9oQos79AlQ5k8WNktv7VGvVH4g= github.com/quic-go/qpack v0.4.0 h1:Cr9BXA1sQS2SmDUWjSofMPNKmvF6IiIfDRmgU0w1ZCo= github.com/quic-go/qpack v0.4.0/go.mod h1:UZVnYIfi5GRk+zI9UMaCPsmZ2xKJP7XBUvVyT1Knj9A= -github.com/saba-futai/sudoku v0.0.1-e h1:PetJcOdoybBWGT1k65puNv+kt6Cmger6i/TSfuu6CdM= -github.com/saba-futai/sudoku v0.0.1-e/go.mod h1:2ZRzRwz93cS2K/o2yOG4CPJEltcvk5y6vbvUmjftGU0= +github.com/saba-futai/sudoku v0.0.1-f h1:DXTiCwzcM49KYDTwucG0KrsCbS93M0/b5gQon7gaChg= +github.com/saba-futai/sudoku v0.0.1-f/go.mod h1:2ZRzRwz93cS2K/o2yOG4CPJEltcvk5y6vbvUmjftGU0= github.com/sagernet/cors v1.2.1 h1:Cv5Z8y9YSD6Gm+qSpNrL3LO4lD3eQVvbFYJSG7JCMHQ= github.com/sagernet/cors v1.2.1/go.mod h1:O64VyOjjhrkLmQIjF4KGRrJO/5dVXFdpEmCW/eISRAI= github.com/sagernet/netlink v0.0.0-20240612041022-b9a21c07ac6a h1:ObwtHN2VpqE0ZNjr6sGeT00J8uU7JF4cNUdb44/Duis=