mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2024-11-16 06:01:21 +08:00
46 lines
890 B
Go
46 lines
890 B
Go
package obfs
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
|
|
"github.com/zhangyunhao116/fastrand"
|
|
)
|
|
|
|
// [salt][obfuscated payload]
|
|
|
|
const saltLen = 16
|
|
|
|
type XPlusObfuscator struct {
|
|
Key []byte
|
|
}
|
|
|
|
func NewXPlusObfuscator(key []byte) *XPlusObfuscator {
|
|
return &XPlusObfuscator{
|
|
Key: key,
|
|
}
|
|
}
|
|
|
|
func (x *XPlusObfuscator) Deobfuscate(in []byte, out []byte) int {
|
|
pLen := len(in) - saltLen
|
|
if pLen <= 0 || len(out) < pLen {
|
|
// Invalid
|
|
return 0
|
|
}
|
|
key := sha256.Sum256(append(x.Key, in[:saltLen]...))
|
|
// Deobfuscate the payload
|
|
for i, c := range in[saltLen:] {
|
|
out[i] = c ^ key[i%sha256.Size]
|
|
}
|
|
return pLen
|
|
}
|
|
|
|
func (x *XPlusObfuscator) Obfuscate(in []byte, out []byte) int {
|
|
_, _ = fastrand.Read(out[:saltLen]) // salt
|
|
// Obfuscate the payload
|
|
key := sha256.Sum256(append(x.Key, out[:saltLen]...))
|
|
for i, c := range in {
|
|
out[i+saltLen] = c ^ key[i%sha256.Size]
|
|
}
|
|
return len(in) + saltLen
|
|
}
|