chore: embed hysteria, clean irrelevant codes, code from https://github.com/HyNetwork/hysteria

This commit is contained in:
Skyxim
2022-07-03 18:22:56 +08:00
parent 8ce9737f3d
commit 3cc1870aee
28 changed files with 3251 additions and 375 deletions

View File

@ -0,0 +1,6 @@
package obfs
type Obfuscator interface {
Deobfuscate(in []byte, out []byte) int
Obfuscate(in []byte, out []byte) int
}

View File

@ -0,0 +1,52 @@
package obfs
import (
"crypto/sha256"
"math/rand"
"sync"
"time"
)
// [salt][obfuscated payload]
const saltLen = 16
type XPlusObfuscator struct {
Key []byte
RandSrc *rand.Rand
lk sync.Mutex
}
func NewXPlusObfuscator(key []byte) *XPlusObfuscator {
return &XPlusObfuscator{
Key: key,
RandSrc: rand.New(rand.NewSource(time.Now().UnixNano())),
}
}
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 {
x.lk.Lock()
_, _ = x.RandSrc.Read(out[:saltLen]) // salt
x.lk.Unlock()
// 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
}

View File

@ -0,0 +1,31 @@
package obfs
import (
"bytes"
"testing"
)
func TestXPlusObfuscator(t *testing.T) {
x := NewXPlusObfuscator([]byte("Vaundy"))
tests := []struct {
name string
p []byte
}{
{name: "1", p: []byte("HelloWorld")},
{name: "2", p: []byte("Regret is just a horrible attempt at time travel that ends with you feeling like crap")},
{name: "3", p: []byte("To be, or not to be, that is the question:\nWhether 'tis nobler in the mind to suffer\n" +
"The slings and arrows of outrageous fortune,\nOr to take arms against a sea of troubles\n" +
"And by opposing end them. To die—to sleep,\nNo more; and by a sleep to say we end")},
{name: "empty", p: []byte("")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
buf := make([]byte, 10240)
n := x.Obfuscate(tt.p, buf)
n2 := x.Deobfuscate(buf[:n], buf[n:])
if !bytes.Equal(tt.p, buf[n:n+n2]) {
t.Errorf("Inconsistent deobfuscate result: got %v, want %v", buf[n:n+n2], tt.p)
}
})
}
}