github.com/database64128/shadowsocks-go@v1.10.2-0.20240315062903-143a773533f1/ss2022/padding.go (about) 1 package ss2022 2 3 import ( 4 "fmt" 5 6 "github.com/database64128/shadowsocks-go/conn" 7 ) 8 9 // PaddingPolicy is a function that takes the target address and 10 // returns whether padding should be added. 11 type PaddingPolicy func(targetAddr conn.Addr) (shouldPad bool) 12 13 // NoPadding is a PaddingPolicy that never adds padding. 14 func NoPadding(_ conn.Addr) bool { 15 return false 16 } 17 18 // PadAll is a PaddingPolicy that adds padding to all traffic. 19 func PadAll(_ conn.Addr) bool { 20 return true 21 } 22 23 // PadPlainDNS is a PaddingPolicy that adds padding to plain DNS traffic. 24 func PadPlainDNS(targetAddr conn.Addr) bool { 25 return targetAddr.Port() == 53 26 } 27 28 // ParsePaddingPolicy parses a string representation of a PaddingPolicy. 29 func ParsePaddingPolicy(paddingPolicy string) (PaddingPolicy, error) { 30 switch paddingPolicy { 31 case "NoPadding": 32 return NoPadding, nil 33 case "PadAll": 34 return PadAll, nil 35 case "PadPlainDNS", "": 36 return PadPlainDNS, nil 37 default: 38 return nil, fmt.Errorf("invalid padding policy: %s", paddingPolicy) 39 } 40 }