github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/pkg/net/proxy/shadowsocksr/protocol/auth_chain_b.go (about) 1 package protocol 2 3 import ( 4 "sort" 5 6 ssr "github.com/Asutorufa/yuhaiin/pkg/net/proxy/shadowsocksr/utils" 7 ) 8 9 func NewAuthChainB(info Protocol) protocol { 10 a := newAuthChain(info, authChainBGetRandLen) 11 a.authChainBInitDataSize() 12 return a 13 } 14 15 func (a *authChainA) authChainBInitDataSize() { 16 if len(a.Key()) == 0 { 17 return 18 } 19 // libev version 20 random := &a.randomServer 21 random.InitFromBin(a.Key()) 22 length := random.Next()%8 + 4 23 a.dataSizeList = make([]int, length) 24 for i := 0; i < int(length); i++ { 25 a.dataSizeList[i] = int(random.Next() % 2340 % 2040 % 1440) 26 } 27 sort.Ints(a.dataSizeList) 28 29 length = random.Next()%16 + 8 30 a.dataSizeList2 = make([]int, length) 31 for i := 0; i < int(length); i++ { 32 a.dataSizeList2[i] = int(random.Next() % 2340 % 2040 % 1440) 33 } 34 sort.Ints(a.dataSizeList2) 35 } 36 37 func authChainBGetRandLen(dataLength int, random *ssr.Shift128plusContext, lastHash []byte, dataSizeList, dataSizeList2 []int, overhead int) int { 38 if dataLength > 1440 { 39 return 0 40 } 41 random.InitFromBinDatalen(lastHash[:16], dataLength) 42 // libev version, upper_bound 43 pos := sort.Search(len(dataSizeList), func(i int) bool { return dataSizeList[i] > dataLength+overhead }) 44 finalPos := uint64(pos) + random.Next()%uint64(len(dataSizeList)) 45 if finalPos < uint64(len(dataSizeList)) { 46 return dataSizeList[finalPos] - dataLength - overhead 47 } 48 // libev version, upper_bound 49 pos = sort.Search(len(dataSizeList2), func(i int) bool { return dataSizeList2[i] > dataLength+overhead }) 50 finalPos = uint64(pos) + random.Next()%uint64(len(dataSizeList2)) 51 if finalPos < uint64(len(dataSizeList2)) { 52 return dataSizeList2[finalPos] - dataLength - overhead 53 } 54 if finalPos < uint64(pos+len(dataSizeList2)-1) { 55 return 0 56 } 57 58 if dataLength > 1300 { 59 return int(random.Next() % 31) 60 } 61 if dataLength > 900 { 62 return int(random.Next() % 127) 63 } 64 if dataLength > 400 { 65 return int(random.Next() % 521) 66 } 67 return int(random.Next() % 1021) 68 }