mirror of
https://github.com/nadoo/glider.git
synced 2026-11-19 07:09:42 +08:00
ssr: move 3rd party ssr pkg to internal
This commit is contained in:
1 parent
7c92aca331
commit
c15c55fe05
26 files changed
+2098
-90
No files matched your search
@@ -0,0 +1,280 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"encoding/base64"
|
||||
"encoding/binary"
|
||||
"math/rand"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/nadoo/glider/proxy/ssr/internal/ssr"
|
||||
"github.com/nadoo/glider/proxy/ssr/internal/tools"
|
||||
)
|
||||
|
||||
func init() {
|
||||
register("auth_aes128_md5", NewAuthAES128MD5)
|
||||
}
|
||||
|
||||
func NewAuthAES128MD5() IProtocol {
|
||||
a := &authAES128{
|
||||
salt: "auth_aes128_md5",
|
||||
hmac: tools.HmacMD5,
|
||||
hashDigest: tools.MD5Sum,
|
||||
packID: 1,
|
||||
recvInfo: recvInfo{
|
||||
recvID: 1,
|
||||
buffer: bytes.NewBuffer(nil),
|
||||
},
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
type recvInfo struct {
|
||||
recvID uint32
|
||||
buffer *bytes.Buffer
|
||||
}
|
||||
|
||||
type authAES128 struct {
|
||||
ssr.ServerInfo
|
||||
recvInfo
|
||||
data *AuthData
|
||||
hasSentHeader bool
|
||||
packID uint32
|
||||
userKey []byte
|
||||
salt string
|
||||
hmac hmacMethod
|
||||
hashDigest hashDigestMethod
|
||||
}
|
||||
|
||||
func (a *authAES128) SetServerInfo(s *ssr.ServerInfo) {
|
||||
a.ServerInfo = *s
|
||||
}
|
||||
|
||||
func (a *authAES128) GetServerInfo() (s *ssr.ServerInfo) {
|
||||
return &a.ServerInfo
|
||||
}
|
||||
|
||||
func (a *authAES128) SetData(data interface{}) {
|
||||
if auth, ok := data.(*AuthData); ok {
|
||||
a.data = auth
|
||||
}
|
||||
}
|
||||
|
||||
func (a *authAES128) GetData() interface{} {
|
||||
if a.data == nil {
|
||||
a.data = &AuthData{}
|
||||
}
|
||||
return a.data
|
||||
}
|
||||
|
||||
func (a *authAES128) packData(data []byte) (outData []byte) {
|
||||
dataLength := len(data)
|
||||
randLength := 1
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
if dataLength <= 1200 {
|
||||
if a.packID > 4 {
|
||||
randLength += rand.Intn(32)
|
||||
} else {
|
||||
if dataLength > 900 {
|
||||
randLength += rand.Intn(128)
|
||||
} else {
|
||||
randLength += rand.Intn(512)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
outLength := randLength + dataLength + 8
|
||||
outData = make([]byte, outLength)
|
||||
// 0~1, out length
|
||||
binary.LittleEndian.PutUint16(outData[0:], uint16(outLength&0xFFFF))
|
||||
// 2~3, hmac
|
||||
key := make([]byte, len(a.userKey)+4)
|
||||
copy(key, a.userKey)
|
||||
binary.LittleEndian.PutUint32(key[len(key)-4:], a.packID)
|
||||
h := a.hmac(key, outData[0:2])
|
||||
copy(outData[2:4], h[:2])
|
||||
// 4~rand length+4, rand number
|
||||
rand.Read(outData[4 : 4+randLength])
|
||||
// 4, rand length
|
||||
if randLength < 128 {
|
||||
outData[4] = byte(randLength & 0xFF)
|
||||
} else {
|
||||
// 4, magic number 0xFF
|
||||
outData[4] = 0xFF
|
||||
// 5~6, rand length
|
||||
binary.LittleEndian.PutUint16(outData[5:], uint16(randLength&0xFFFF))
|
||||
}
|
||||
// rand length+4~out length-4, data
|
||||
if dataLength > 0 {
|
||||
copy(outData[randLength+4:], data)
|
||||
}
|
||||
a.packID++
|
||||
h = a.hmac(key, outData[:outLength-4])
|
||||
copy(outData[outLength-4:], h[:4])
|
||||
return
|
||||
}
|
||||
|
||||
func (a *authAES128) packAuthData(data []byte) (outData []byte) {
|
||||
dataLength := len(data)
|
||||
var randLength int
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
if dataLength > 400 {
|
||||
randLength = rand.Intn(512)
|
||||
} else {
|
||||
randLength = rand.Intn(1024)
|
||||
}
|
||||
|
||||
dataOffset := randLength + 16 + 4 + 4 + 7
|
||||
outLength := dataOffset + dataLength + 4
|
||||
outData = make([]byte, outLength)
|
||||
encrypt := make([]byte, 24)
|
||||
key := make([]byte, a.IVLen+a.KeyLen)
|
||||
copy(key, a.IV)
|
||||
copy(key[a.IVLen:], a.Key)
|
||||
|
||||
rand.Read(outData[dataOffset-randLength:])
|
||||
a.data.mutex.Lock()
|
||||
a.data.connectionID++
|
||||
if a.data.connectionID > 0xFF000000 {
|
||||
a.data.clientID = nil
|
||||
}
|
||||
if len(a.data.clientID) == 0 {
|
||||
a.data.clientID = make([]byte, 8)
|
||||
rand.Read(a.data.clientID)
|
||||
b := make([]byte, 4)
|
||||
rand.Read(b)
|
||||
a.data.connectionID = binary.LittleEndian.Uint32(b) & 0xFFFFFF
|
||||
}
|
||||
copy(encrypt[4:], a.data.clientID)
|
||||
binary.LittleEndian.PutUint32(encrypt[8:], a.data.connectionID)
|
||||
a.data.mutex.Unlock()
|
||||
|
||||
now := time.Now().Unix()
|
||||
binary.LittleEndian.PutUint32(encrypt[0:4], uint32(now))
|
||||
|
||||
binary.LittleEndian.PutUint16(encrypt[12:], uint16(outLength&0xFFFF))
|
||||
binary.LittleEndian.PutUint16(encrypt[14:], uint16(randLength&0xFFFF))
|
||||
|
||||
params := strings.Split(a.Param, ":")
|
||||
uid := make([]byte, 4)
|
||||
if len(params) >= 2 {
|
||||
if userID, err := strconv.ParseUint(params[0], 10, 32); err != nil {
|
||||
rand.Read(uid)
|
||||
} else {
|
||||
binary.LittleEndian.PutUint32(uid, uint32(userID))
|
||||
a.userKey = a.hashDigest([]byte(params[1]))
|
||||
}
|
||||
} else {
|
||||
rand.Read(uid)
|
||||
}
|
||||
|
||||
if a.userKey == nil {
|
||||
a.userKey = make([]byte, a.KeyLen)
|
||||
copy(a.userKey, a.Key)
|
||||
}
|
||||
|
||||
encryptKey := make([]byte, len(a.userKey))
|
||||
copy(encryptKey, a.userKey)
|
||||
|
||||
aesCipherKey := tools.EVPBytesToKey(base64.StdEncoding.EncodeToString(encryptKey)+a.salt, 16)
|
||||
block, err := aes.NewCipher(aesCipherKey)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
encryptData := make([]byte, 16)
|
||||
iv := make([]byte, aes.BlockSize)
|
||||
cbc := cipher.NewCBCEncrypter(block, iv)
|
||||
cbc.CryptBlocks(encryptData, encrypt[0:16])
|
||||
copy(encrypt[4:4+16], encryptData)
|
||||
copy(encrypt[0:4], uid)
|
||||
|
||||
h := a.hmac(key, encrypt[0:20])
|
||||
copy(encrypt[20:], h[:4])
|
||||
|
||||
rand.Read(outData[0:1])
|
||||
h = a.hmac(key, outData[0:1])
|
||||
copy(outData[1:], h[0:7-1])
|
||||
|
||||
copy(outData[7:], encrypt)
|
||||
copy(outData[dataOffset:], data)
|
||||
|
||||
h = a.hmac(a.userKey, outData[0:outLength-4])
|
||||
copy(outData[outLength-4:], h[:4])
|
||||
|
||||
//log.Println("clientID:", a.data.clientID, "connectionID:", a.data.connectionID)
|
||||
return
|
||||
}
|
||||
|
||||
func (a *authAES128) PreEncrypt(plainData []byte) (outData []byte, err error) {
|
||||
dataLength := len(plainData)
|
||||
offset := 0
|
||||
if dataLength > 0 && !a.hasSentHeader {
|
||||
authLength := dataLength
|
||||
if authLength > 1200 {
|
||||
authLength = 1200
|
||||
}
|
||||
packData := a.packAuthData(plainData[:authLength])
|
||||
a.hasSentHeader = true
|
||||
outData = append(outData, packData...)
|
||||
dataLength -= authLength
|
||||
offset += authLength
|
||||
}
|
||||
const blockSize = 4096
|
||||
for dataLength > blockSize {
|
||||
packData := a.packData(plainData[offset : offset+blockSize])
|
||||
outData = append(outData, packData...)
|
||||
dataLength -= blockSize
|
||||
offset += blockSize
|
||||
}
|
||||
if dataLength > 0 {
|
||||
packData := a.packData(plainData[offset:])
|
||||
outData = append(outData, packData...)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (a *authAES128) PostDecrypt(plainData []byte) ([]byte, int, error) {
|
||||
a.buffer.Reset()
|
||||
plainLength := len(plainData)
|
||||
readlenth := 0
|
||||
key := make([]byte, len(a.userKey)+4)
|
||||
copy(key, a.userKey)
|
||||
for plainLength > 4 {
|
||||
binary.LittleEndian.PutUint32(key[len(key)-4:], a.recvID)
|
||||
|
||||
h := a.hmac(key, plainData[0:2])
|
||||
if h[0] != plainData[2] || h[1] != plainData[3] {
|
||||
return nil, 0, ssr.ErrAuthAES128IncorrectHMAC
|
||||
}
|
||||
length := int(binary.LittleEndian.Uint16(plainData[0:2]))
|
||||
if length >= 8192 || length < 8 {
|
||||
return nil, 0, ssr.ErrAuthAES128DataLengthError
|
||||
}
|
||||
if length > plainLength {
|
||||
break
|
||||
}
|
||||
a.recvID++
|
||||
pos := int(plainData[4])
|
||||
if pos < 255 {
|
||||
pos += 4
|
||||
} else {
|
||||
pos = int(binary.LittleEndian.Uint16(plainData[5:7])) + 4
|
||||
}
|
||||
|
||||
a.buffer.Write(plainData[pos : length-4])
|
||||
plainData = plainData[length:]
|
||||
plainLength -= length
|
||||
readlenth += length
|
||||
}
|
||||
return a.buffer.Bytes(), readlenth, nil
|
||||
}
|
||||
|
||||
func (a *authAES128) GetOverhead() int {
|
||||
return 9
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"github.com/nadoo/glider/proxy/ssr/internal/tools"
|
||||
)
|
||||
|
||||
func init() {
|
||||
register("auth_aes128_sha1", NewAuthAES128SHA1)
|
||||
}
|
||||
|
||||
func NewAuthAES128SHA1() IProtocol {
|
||||
a := &authAES128{
|
||||
salt: "auth_aes128_sha1",
|
||||
hmac: tools.HmacSHA1,
|
||||
hashDigest: tools.SHA1Sum,
|
||||
packID: 1,
|
||||
recvInfo: recvInfo{
|
||||
recvID: 1,
|
||||
buffer: bytes.NewBuffer(nil),
|
||||
},
|
||||
}
|
||||
return a
|
||||
}
|
||||
@@ -0,0 +1,320 @@
|
||||
// https://github.com/shadowsocksr-backup/shadowsocks-rss/blob/master/doc/auth_chain_a.md
|
||||
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/aes"
|
||||
stdCipher "crypto/cipher"
|
||||
"encoding/base64"
|
||||
"encoding/binary"
|
||||
"math/rand"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/nadoo/glider/proxy/ssr/internal/cipher"
|
||||
"github.com/nadoo/glider/proxy/ssr/internal/ssr"
|
||||
"github.com/nadoo/glider/proxy/ssr/internal/tools"
|
||||
)
|
||||
|
||||
func init() {
|
||||
register("auth_chain_a", NewAuthChainA)
|
||||
}
|
||||
|
||||
type authChainA struct {
|
||||
ssr.ServerInfo
|
||||
randomClient tools.Shift128plusContext
|
||||
randomServer tools.Shift128plusContext
|
||||
recvInfo
|
||||
cipher *cipher.StreamCipher
|
||||
hasSentHeader bool
|
||||
lastClientHash []byte
|
||||
lastServerHash []byte
|
||||
userKey []byte
|
||||
uid [4]byte
|
||||
salt string
|
||||
data *AuthData
|
||||
hmac hmacMethod
|
||||
hashDigest hashDigestMethod
|
||||
rnd rndMethod
|
||||
dataSizeList []int
|
||||
dataSizeList2 []int
|
||||
chunkID uint32
|
||||
}
|
||||
|
||||
func NewAuthChainA() IProtocol {
|
||||
a := &authChainA{
|
||||
salt: "auth_chain_a",
|
||||
hmac: tools.HmacMD5,
|
||||
hashDigest: tools.SHA1Sum,
|
||||
rnd: authChainAGetRandLen,
|
||||
recvInfo: recvInfo{
|
||||
recvID: 1,
|
||||
buffer: new(bytes.Buffer),
|
||||
},
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *authChainA) SetServerInfo(s *ssr.ServerInfo) {
|
||||
a.ServerInfo = *s
|
||||
if a.salt == "auth_chain_b" {
|
||||
a.authChainBInitDataSize()
|
||||
}
|
||||
}
|
||||
|
||||
func (a *authChainA) GetServerInfo() (s *ssr.ServerInfo) {
|
||||
return &a.ServerInfo
|
||||
}
|
||||
|
||||
func (a *authChainA) SetData(data interface{}) {
|
||||
if auth, ok := data.(*AuthData); ok {
|
||||
a.data = auth
|
||||
}
|
||||
}
|
||||
|
||||
func (a *authChainA) GetData() interface{} {
|
||||
if a.data == nil {
|
||||
a.data = &AuthData{}
|
||||
}
|
||||
return a.data
|
||||
}
|
||||
|
||||
func authChainAGetRandLen(dataLength int, random *tools.Shift128plusContext, lastHash []byte, dataSizeList, dataSizeList2 []int, overhead int) int {
|
||||
if dataLength > 1440 {
|
||||
return 0
|
||||
}
|
||||
random.InitFromBinDatalen(lastHash[:16], dataLength)
|
||||
if dataLength > 1300 {
|
||||
return int(random.Next() % 31)
|
||||
}
|
||||
if dataLength > 900 {
|
||||
return int(random.Next() % 127)
|
||||
}
|
||||
if dataLength > 400 {
|
||||
return int(random.Next() % 521)
|
||||
}
|
||||
return int(random.Next() % 1021)
|
||||
}
|
||||
|
||||
func getRandStartPos(random *tools.Shift128plusContext, randLength int) int {
|
||||
if randLength > 0 {
|
||||
return int(random.Next() % 8589934609 % uint64(randLength))
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (a *authChainA) getClientRandLen(dataLength int, overhead int) int {
|
||||
return a.rnd(dataLength, &a.randomClient, a.lastClientHash, a.dataSizeList, a.dataSizeList2, overhead)
|
||||
}
|
||||
|
||||
func (a *authChainA) getServerRandLen(dataLength int, overhead int) int {
|
||||
return a.rnd(dataLength, &a.randomServer, a.lastServerHash, a.dataSizeList, a.dataSizeList2, overhead)
|
||||
}
|
||||
|
||||
func (a *authChainA) packedDataLen(data []byte) (chunkLength, randLength int) {
|
||||
dataLength := len(data)
|
||||
randLength = a.getClientRandLen(dataLength, a.Overhead)
|
||||
chunkLength = randLength + dataLength + 2 + 2
|
||||
return
|
||||
}
|
||||
|
||||
func (a *authChainA) packData(outData []byte, data []byte, randLength int) {
|
||||
dataLength := len(data)
|
||||
outLength := randLength + dataLength + 2
|
||||
outData[0] = byte(dataLength) ^ a.lastClientHash[14]
|
||||
outData[1] = byte(dataLength>>8) ^ a.lastClientHash[15]
|
||||
|
||||
{
|
||||
if dataLength > 0 {
|
||||
randPart1Length := getRandStartPos(&a.randomClient, randLength)
|
||||
rand.Read(outData[2 : 2+randPart1Length])
|
||||
a.cipher.Encrypt(outData[2+randPart1Length:], data)
|
||||
rand.Read(outData[2+randPart1Length+dataLength : outLength])
|
||||
} else {
|
||||
rand.Read(outData[2 : 2+randLength])
|
||||
}
|
||||
}
|
||||
|
||||
userKeyLen := uint8(len(a.userKey))
|
||||
key := make([]byte, userKeyLen+4)
|
||||
copy(key, a.userKey)
|
||||
a.chunkID++
|
||||
binary.LittleEndian.PutUint32(key[userKeyLen:], a.chunkID)
|
||||
a.lastClientHash = a.hmac(key, outData[:outLength])
|
||||
copy(outData[outLength:], a.lastClientHash[:2])
|
||||
return
|
||||
}
|
||||
|
||||
const authheadLength = 4 + 8 + 4 + 16 + 4
|
||||
|
||||
func (a *authChainA) packAuthData(data []byte) (outData []byte) {
|
||||
outData = make([]byte, authheadLength, authheadLength+1500)
|
||||
a.data.connectionID++
|
||||
if a.data.connectionID > 0xFF000000 {
|
||||
rand.Read(a.data.clientID)
|
||||
b := make([]byte, 4)
|
||||
rand.Read(b)
|
||||
a.data.connectionID = binary.LittleEndian.Uint32(b) & 0xFFFFFF
|
||||
}
|
||||
var key = make([]byte, a.IVLen+a.KeyLen)
|
||||
copy(key, a.IV)
|
||||
copy(key[a.IVLen:], a.Key)
|
||||
|
||||
encrypt := make([]byte, 20)
|
||||
t := time.Now().Unix()
|
||||
binary.LittleEndian.PutUint32(encrypt[:4], uint32(t))
|
||||
copy(encrypt[4:8], a.data.clientID)
|
||||
binary.LittleEndian.PutUint32(encrypt[8:], a.data.connectionID)
|
||||
binary.LittleEndian.PutUint16(encrypt[12:], uint16(a.Overhead))
|
||||
//binary.LittleEndian.PutUint16(encrypt[14:], 0)
|
||||
|
||||
// first 12 bytes
|
||||
{
|
||||
rand.Read(outData[:4])
|
||||
a.lastClientHash = a.hmac(key, outData[:4])
|
||||
copy(outData[4:], a.lastClientHash[:8])
|
||||
}
|
||||
var base64UserKey string
|
||||
// uid & 16 bytes auth data
|
||||
{
|
||||
uid := make([]byte, 4)
|
||||
if a.userKey == nil {
|
||||
params := strings.Split(a.ServerInfo.Param, ":")
|
||||
if len(params) >= 2 {
|
||||
if userID, err := strconv.ParseUint(params[0], 10, 32); err == nil {
|
||||
binary.LittleEndian.PutUint32(a.uid[:], uint32(userID))
|
||||
a.userKey = a.hashDigest([]byte(params[1]))
|
||||
}
|
||||
}
|
||||
if a.userKey == nil {
|
||||
rand.Read(a.uid[:])
|
||||
a.userKey = make([]byte, a.KeyLen)
|
||||
copy(a.userKey, a.Key)
|
||||
}
|
||||
}
|
||||
for i := 0; i < 4; i++ {
|
||||
uid[i] = a.uid[i] ^ a.lastClientHash[8+i]
|
||||
}
|
||||
base64UserKey = base64.StdEncoding.EncodeToString(a.userKey)
|
||||
aesCipherKey := tools.EVPBytesToKey(base64UserKey+a.salt, 16)
|
||||
block, err := aes.NewCipher(aesCipherKey)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
encryptData := make([]byte, 16)
|
||||
iv := make([]byte, aes.BlockSize)
|
||||
cbc := stdCipher.NewCBCEncrypter(block, iv)
|
||||
cbc.CryptBlocks(encryptData, encrypt[:16])
|
||||
copy(encrypt[:4], uid[:])
|
||||
copy(encrypt[4:4+16], encryptData)
|
||||
}
|
||||
// final HMAC
|
||||
{
|
||||
a.lastServerHash = a.hmac(a.userKey, encrypt[0:20])
|
||||
|
||||
copy(outData[12:], encrypt)
|
||||
copy(outData[12+20:], a.lastServerHash[:4])
|
||||
}
|
||||
|
||||
// init cipher
|
||||
password := make([]byte, len(base64UserKey)+base64.StdEncoding.EncodedLen(16))
|
||||
copy(password, base64UserKey)
|
||||
base64.StdEncoding.Encode(password[len(base64UserKey):], a.lastClientHash[:16])
|
||||
a.cipher, _ = cipher.NewStreamCipher("rc4", string(password))
|
||||
_, _ = a.cipher.InitEncrypt()
|
||||
_ = a.cipher.InitDecrypt(nil)
|
||||
|
||||
// data
|
||||
chunkLength, randLength := a.packedDataLen(data)
|
||||
if chunkLength <= 1500 {
|
||||
outData = outData[:authheadLength+chunkLength]
|
||||
} else {
|
||||
newOutData := make([]byte, authheadLength+chunkLength)
|
||||
copy(newOutData, outData[:authheadLength])
|
||||
outData = newOutData
|
||||
}
|
||||
a.packData(outData[authheadLength:], data, randLength)
|
||||
return
|
||||
}
|
||||
|
||||
func (a *authChainA) PreEncrypt(plainData []byte) (outData []byte, err error) {
|
||||
a.buffer.Reset()
|
||||
dataLength := len(plainData)
|
||||
length := dataLength
|
||||
offset := 0
|
||||
if length > 0 && !a.hasSentHeader {
|
||||
headSize := 1200
|
||||
if headSize > dataLength {
|
||||
headSize = dataLength
|
||||
}
|
||||
a.buffer.Write(a.packAuthData(plainData[:headSize]))
|
||||
offset += headSize
|
||||
dataLength -= headSize
|
||||
a.hasSentHeader = true
|
||||
}
|
||||
var unitSize = a.TcpMss - a.Overhead
|
||||
for dataLength > unitSize {
|
||||
dataLen, randLength := a.packedDataLen(plainData[offset : offset+unitSize])
|
||||
b := make([]byte, dataLen)
|
||||
a.packData(b, plainData[offset:offset+unitSize], randLength)
|
||||
a.buffer.Write(b)
|
||||
dataLength -= unitSize
|
||||
offset += unitSize
|
||||
}
|
||||
if dataLength > 0 {
|
||||
dataLen, randLength := a.packedDataLen(plainData[offset:])
|
||||
b := make([]byte, dataLen)
|
||||
a.packData(b, plainData[offset:], randLength)
|
||||
a.buffer.Write(b)
|
||||
}
|
||||
return a.buffer.Bytes(), nil
|
||||
}
|
||||
|
||||
func (a *authChainA) PostDecrypt(plainData []byte) (outData []byte, n int, err error) {
|
||||
a.buffer.Reset()
|
||||
key := make([]byte, len(a.userKey)+4)
|
||||
readlenth := 0
|
||||
copy(key, a.userKey)
|
||||
for len(plainData) > 4 {
|
||||
binary.LittleEndian.PutUint32(key[len(a.userKey):], a.recvID)
|
||||
dataLen := (int)((uint(plainData[1]^a.lastServerHash[15]) << 8) + uint(plainData[0]^a.lastServerHash[14]))
|
||||
randLen := a.getServerRandLen(dataLen, a.Overhead)
|
||||
length := randLen + dataLen
|
||||
if length >= 4096 {
|
||||
return nil, 0, ssr.ErrAuthChainDataLengthError
|
||||
}
|
||||
length += 4
|
||||
if length > len(plainData) {
|
||||
break
|
||||
}
|
||||
|
||||
hash := a.hmac(key, plainData[:length-2])
|
||||
if !bytes.Equal(hash[:2], plainData[length-2:length]) {
|
||||
return nil, 0, ssr.ErrAuthChainIncorrectHMAC
|
||||
}
|
||||
var dataPos int
|
||||
if dataLen > 0 && randLen > 0 {
|
||||
dataPos = 2 + getRandStartPos(&a.randomServer, randLen)
|
||||
} else {
|
||||
dataPos = 2
|
||||
}
|
||||
b := make([]byte, dataLen)
|
||||
a.cipher.Decrypt(b, plainData[dataPos:dataPos+dataLen])
|
||||
a.buffer.Write(b)
|
||||
if a.recvID == 1 {
|
||||
a.TcpMss = int(binary.LittleEndian.Uint16(a.buffer.Next(2)))
|
||||
}
|
||||
a.lastServerHash = hash
|
||||
a.recvID++
|
||||
plainData = plainData[length:]
|
||||
readlenth += length
|
||||
|
||||
}
|
||||
return a.buffer.Bytes(), readlenth, nil
|
||||
}
|
||||
|
||||
func (a *authChainA) GetOverhead() int {
|
||||
return 4
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"sort"
|
||||
|
||||
"github.com/nadoo/glider/proxy/ssr/internal/tools"
|
||||
)
|
||||
|
||||
func init() {
|
||||
register("auth_chain_b", NewAuthChainB)
|
||||
}
|
||||
|
||||
func NewAuthChainB() IProtocol {
|
||||
a := &authChainA{
|
||||
salt: "auth_chain_b",
|
||||
hmac: tools.HmacMD5,
|
||||
hashDigest: tools.SHA1Sum,
|
||||
rnd: authChainBGetRandLen,
|
||||
recvInfo: recvInfo{
|
||||
recvID: 1,
|
||||
buffer: new(bytes.Buffer),
|
||||
},
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *authChainA) authChainBInitDataSize() {
|
||||
if len(a.Key) == 0 {
|
||||
return
|
||||
}
|
||||
// libev version
|
||||
random := &a.randomServer
|
||||
random.InitFromBin(a.Key)
|
||||
length := random.Next()%8 + 4
|
||||
a.dataSizeList = make([]int, length)
|
||||
for i := 0; i < int(length); i++ {
|
||||
a.dataSizeList[i] = int(random.Next() % 2340 % 2040 % 1440)
|
||||
}
|
||||
sort.Ints(a.dataSizeList)
|
||||
|
||||
length = random.Next()%16 + 8
|
||||
a.dataSizeList2 = make([]int, length)
|
||||
for i := 0; i < int(length); i++ {
|
||||
a.dataSizeList2[i] = int(random.Next() % 2340 % 2040 % 1440)
|
||||
}
|
||||
sort.Ints(a.dataSizeList2)
|
||||
}
|
||||
|
||||
func authChainBGetRandLen(dataLength int, random *tools.Shift128plusContext, lastHash []byte, dataSizeList, dataSizeList2 []int, overhead int) int {
|
||||
if dataLength > 1440 {
|
||||
return 0
|
||||
}
|
||||
random.InitFromBinDatalen(lastHash[:16], dataLength)
|
||||
// libev version, upper_bound
|
||||
pos := sort.Search(len(dataSizeList), func(i int) bool { return dataSizeList[i] > dataLength+overhead })
|
||||
finalPos := uint64(pos) + random.Next()%uint64(len(dataSizeList))
|
||||
if finalPos < uint64(len(dataSizeList)) {
|
||||
return dataSizeList[finalPos] - dataLength - overhead
|
||||
}
|
||||
// libev version, upper_bound
|
||||
pos = sort.Search(len(dataSizeList2), func(i int) bool { return dataSizeList2[i] > dataLength+overhead })
|
||||
finalPos = uint64(pos) + random.Next()%uint64(len(dataSizeList2))
|
||||
if finalPos < uint64(len(dataSizeList2)) {
|
||||
return dataSizeList2[finalPos] - dataLength - overhead
|
||||
}
|
||||
if finalPos < uint64(pos+len(dataSizeList2)-1) {
|
||||
return 0
|
||||
}
|
||||
|
||||
if dataLength > 1300 {
|
||||
return int(random.Next() % 31)
|
||||
}
|
||||
if dataLength > 900 {
|
||||
return int(random.Next() % 127)
|
||||
}
|
||||
if dataLength > 400 {
|
||||
return int(random.Next() % 521)
|
||||
}
|
||||
return int(random.Next() % 1021)
|
||||
}
|
||||
@@ -0,0 +1,226 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/nadoo/glider/proxy/ssr/internal/ssr"
|
||||
"github.com/nadoo/glider/proxy/ssr/internal/tools"
|
||||
)
|
||||
|
||||
func init() {
|
||||
register("auth_sha1_v4", NewAuthSHA1v4)
|
||||
}
|
||||
|
||||
type authSHA1v4 struct {
|
||||
ssr.ServerInfo
|
||||
data *AuthData
|
||||
hasSentHeader bool
|
||||
buffer bytes.Buffer
|
||||
}
|
||||
|
||||
func NewAuthSHA1v4() IProtocol {
|
||||
a := &authSHA1v4{}
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *authSHA1v4) SetServerInfo(s *ssr.ServerInfo) {
|
||||
a.ServerInfo = *s
|
||||
}
|
||||
|
||||
func (a *authSHA1v4) GetServerInfo() (s *ssr.ServerInfo) {
|
||||
return &a.ServerInfo
|
||||
}
|
||||
|
||||
func (a *authSHA1v4) SetData(data interface{}) {
|
||||
if auth, ok := data.(*AuthData); ok {
|
||||
a.data = auth
|
||||
}
|
||||
}
|
||||
|
||||
func (a *authSHA1v4) GetData() interface{} {
|
||||
if a.data == nil {
|
||||
a.data = &AuthData{}
|
||||
}
|
||||
return a.data
|
||||
}
|
||||
|
||||
func (a *authSHA1v4) packData(data []byte) (outData []byte) {
|
||||
dataLength := len(data)
|
||||
randLength := 1
|
||||
|
||||
if dataLength <= 1300 {
|
||||
if dataLength > 400 {
|
||||
randLength += rand.Intn(128)
|
||||
} else {
|
||||
randLength += rand.Intn(1024)
|
||||
}
|
||||
}
|
||||
|
||||
outLength := randLength + dataLength + 8
|
||||
outData = make([]byte, outLength)
|
||||
// 0~1, out length
|
||||
binary.BigEndian.PutUint16(outData[0:2], uint16(outLength&0xFFFF))
|
||||
// 2~3, crc of out length
|
||||
crc32 := ssr.CalcCRC32(outData, 2, 0xFFFFFFFF)
|
||||
binary.LittleEndian.PutUint16(outData[2:4], uint16(crc32&0xFFFF))
|
||||
// 4, rand length
|
||||
if randLength < 128 {
|
||||
outData[4] = uint8(randLength & 0xFF)
|
||||
} else {
|
||||
outData[4] = uint8(0xFF)
|
||||
binary.BigEndian.PutUint16(outData[5:7], uint16(randLength&0xFFFF))
|
||||
}
|
||||
// rand length+4~out length-4, data
|
||||
if dataLength > 0 {
|
||||
copy(outData[randLength+4:], data)
|
||||
}
|
||||
// out length-4~end, adler32 of full data
|
||||
adler := ssr.CalcAdler32(outData[:outLength-4])
|
||||
binary.LittleEndian.PutUint32(outData[outLength-4:], adler)
|
||||
|
||||
return outData
|
||||
}
|
||||
|
||||
func (a *authSHA1v4) packAuthData(data []byte) (outData []byte) {
|
||||
|
||||
dataLength := len(data)
|
||||
randLength := 1
|
||||
if dataLength <= 1300 {
|
||||
if dataLength > 400 {
|
||||
randLength += rand.Intn(128)
|
||||
} else {
|
||||
randLength += rand.Intn(1024)
|
||||
}
|
||||
}
|
||||
dataOffset := randLength + 4 + 2
|
||||
outLength := dataOffset + dataLength + 12 + ssr.ObfsHMACSHA1Len
|
||||
outData = make([]byte, outLength)
|
||||
a.data.connectionID++
|
||||
if a.data.connectionID > 0xFF000000 {
|
||||
a.data.clientID = nil
|
||||
}
|
||||
if len(a.data.clientID) == 0 {
|
||||
a.data.clientID = make([]byte, 8)
|
||||
rand.Read(a.data.clientID)
|
||||
b := make([]byte, 4)
|
||||
rand.Read(b)
|
||||
a.data.connectionID = binary.LittleEndian.Uint32(b) & 0xFFFFFF
|
||||
}
|
||||
// 0-1, out length
|
||||
binary.BigEndian.PutUint16(outData[0:2], uint16(outLength&0xFFFF))
|
||||
|
||||
// 2~6, crc of out length+salt+key
|
||||
salt := []byte("auth_sha1_v4")
|
||||
crcData := make([]byte, len(salt)+a.KeyLen+2)
|
||||
copy(crcData[0:2], outData[0:2])
|
||||
copy(crcData[2:], salt)
|
||||
copy(crcData[2+len(salt):], a.Key)
|
||||
crc32 := ssr.CalcCRC32(crcData, len(crcData), 0xFFFFFFFF)
|
||||
// 2~6, crc of out length+salt+key
|
||||
binary.LittleEndian.PutUint32(outData[2:], crc32)
|
||||
// 6~rand length+6, rand numbers
|
||||
rand.Read(outData[dataOffset-randLength : dataOffset])
|
||||
// 6, rand length
|
||||
if randLength < 128 {
|
||||
outData[6] = byte(randLength & 0xFF)
|
||||
} else {
|
||||
// 6, magic number 0xFF
|
||||
outData[6] = 0xFF
|
||||
// 7-8, rand length
|
||||
binary.BigEndian.PutUint16(outData[7:9], uint16(randLength&0xFFFF))
|
||||
}
|
||||
// rand length+6~rand length+10, time stamp
|
||||
now := time.Now().Unix()
|
||||
binary.LittleEndian.PutUint32(outData[dataOffset:dataOffset+4], uint32(now))
|
||||
// rand length+10~rand length+14, client ID
|
||||
copy(outData[dataOffset+4:dataOffset+4+4], a.data.clientID[0:4])
|
||||
// rand length+14~rand length+18, connection ID
|
||||
binary.LittleEndian.PutUint32(outData[dataOffset+8:dataOffset+8+4], a.data.connectionID)
|
||||
// rand length+18~rand length+18+data length, data
|
||||
copy(outData[dataOffset+12:], data)
|
||||
|
||||
key := make([]byte, a.IVLen+a.KeyLen)
|
||||
copy(key, a.IV)
|
||||
copy(key[a.IVLen:], a.Key)
|
||||
|
||||
h := tools.HmacSHA1(key, outData[:outLength-ssr.ObfsHMACSHA1Len])
|
||||
// out length-10~out length/rand length+18+data length~end, hmac
|
||||
copy(outData[outLength-ssr.ObfsHMACSHA1Len:], h[0:ssr.ObfsHMACSHA1Len])
|
||||
return outData
|
||||
}
|
||||
|
||||
func (a *authSHA1v4) PreEncrypt(plainData []byte) (outData []byte, err error) {
|
||||
a.buffer.Reset()
|
||||
dataLength := len(plainData)
|
||||
offset := 0
|
||||
if !a.hasSentHeader && dataLength > 0 {
|
||||
headSize := ssr.GetHeadSize(plainData, 30)
|
||||
if headSize > dataLength {
|
||||
headSize = dataLength
|
||||
}
|
||||
a.buffer.Write(a.packAuthData(plainData[:headSize]))
|
||||
offset += headSize
|
||||
dataLength -= headSize
|
||||
a.hasSentHeader = true
|
||||
}
|
||||
const blockSize = 4096
|
||||
for dataLength > blockSize {
|
||||
a.buffer.Write(a.packData(plainData[offset : offset+blockSize]))
|
||||
offset += blockSize
|
||||
dataLength -= blockSize
|
||||
}
|
||||
if dataLength > 0 {
|
||||
a.buffer.Write(a.packData(plainData[offset:]))
|
||||
}
|
||||
|
||||
return a.buffer.Bytes(), nil
|
||||
}
|
||||
|
||||
func (a *authSHA1v4) PostDecrypt(plainData []byte) (outData []byte, n int, err error) {
|
||||
a.buffer.Reset()
|
||||
dataLength := len(plainData)
|
||||
plainLength := dataLength
|
||||
for dataLength > 4 {
|
||||
crc32 := ssr.CalcCRC32(plainData, 2, 0xFFFFFFFF)
|
||||
if binary.LittleEndian.Uint16(plainData[2:4]) != uint16(crc32&0xFFFF) {
|
||||
//common.Error("auth_sha1_v4 post decrypt data crc32 error")
|
||||
return nil, 0, ssr.ErrAuthSHA1v4CRC32Error
|
||||
}
|
||||
length := int(binary.BigEndian.Uint16(plainData[0:2]))
|
||||
if length >= 8192 || length < 8 {
|
||||
//common.Error("auth_sha1_v4 post decrypt data length error")
|
||||
dataLength = 0
|
||||
plainData = nil
|
||||
return nil, 0, ssr.ErrAuthSHA1v4DataLengthError
|
||||
}
|
||||
if length > dataLength {
|
||||
break
|
||||
}
|
||||
|
||||
if ssr.CheckAdler32(plainData, length) {
|
||||
pos := int(plainData[4])
|
||||
if pos != 0xFF {
|
||||
pos += 4
|
||||
} else {
|
||||
pos = int(binary.BigEndian.Uint16(plainData[5:5+2])) + 4
|
||||
}
|
||||
outLength := length - pos - 4
|
||||
a.buffer.Write(plainData[pos : pos+outLength])
|
||||
dataLength -= length
|
||||
plainData = plainData[length:]
|
||||
} else {
|
||||
//common.Error("auth_sha1_v4 post decrypt incorrect checksum")
|
||||
dataLength = 0
|
||||
plainData = nil
|
||||
return nil, 0, ssr.ErrAuthSHA1v4IncorrectChecksum
|
||||
}
|
||||
}
|
||||
return a.buffer.Bytes(), plainLength - dataLength, nil
|
||||
}
|
||||
|
||||
func (a *authSHA1v4) GetOverhead() int {
|
||||
return 7
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/nadoo/glider/proxy/ssr/internal/ssr"
|
||||
"github.com/nadoo/glider/proxy/ssr/internal/tools"
|
||||
)
|
||||
|
||||
type creator func() IProtocol
|
||||
|
||||
var (
|
||||
creatorMap = make(map[string]creator)
|
||||
)
|
||||
|
||||
type hmacMethod func(key []byte, data []byte) []byte
|
||||
type hashDigestMethod func(data []byte) []byte
|
||||
type rndMethod func(dataLength int, random *tools.Shift128plusContext, lastHash []byte, dataSizeList, dataSizeList2 []int, overhead int) int
|
||||
|
||||
type IProtocol interface {
|
||||
SetServerInfo(s *ssr.ServerInfo)
|
||||
GetServerInfo() *ssr.ServerInfo
|
||||
PreEncrypt(data []byte) ([]byte, error)
|
||||
PostDecrypt(data []byte) ([]byte, int, error)
|
||||
SetData(data interface{})
|
||||
GetData() interface{}
|
||||
GetOverhead() int
|
||||
}
|
||||
|
||||
type AuthData struct {
|
||||
clientID []byte
|
||||
connectionID uint32
|
||||
mutex sync.Mutex
|
||||
}
|
||||
|
||||
func register(name string, c creator) {
|
||||
creatorMap[name] = c
|
||||
}
|
||||
|
||||
func NewProtocol(name string) IProtocol {
|
||||
c, ok := creatorMap[strings.ToLower(name)]
|
||||
if ok {
|
||||
return c()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"github.com/nadoo/glider/proxy/ssr/internal/ssr"
|
||||
)
|
||||
|
||||
func init() {
|
||||
register("origin", NewOrigin)
|
||||
}
|
||||
|
||||
type origin struct {
|
||||
ssr.ServerInfo
|
||||
}
|
||||
|
||||
func NewOrigin() IProtocol {
|
||||
a := &origin{}
|
||||
return a
|
||||
}
|
||||
|
||||
func (o *origin) SetServerInfo(s *ssr.ServerInfo) {
|
||||
o.ServerInfo = *s
|
||||
}
|
||||
|
||||
func (o *origin) GetServerInfo() (s *ssr.ServerInfo) {
|
||||
return &o.ServerInfo
|
||||
}
|
||||
|
||||
func (o *origin) PreEncrypt(data []byte) (encryptedData []byte, err error) {
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (o *origin) PostDecrypt(data []byte) ([]byte, int, error) {
|
||||
return data, len(data), nil
|
||||
}
|
||||
|
||||
func (o *origin) SetData(data interface{}) {
|
||||
|
||||
}
|
||||
|
||||
func (o *origin) GetData() interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (o *origin) GetOverhead() int {
|
||||
return 0
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
|
||||
"github.com/nadoo/glider/proxy/ssr/internal/ssr"
|
||||
"github.com/nadoo/glider/proxy/ssr/internal/tools"
|
||||
)
|
||||
|
||||
func init() {
|
||||
register("verify_sha1", NewVerifySHA1)
|
||||
register("ota", NewVerifySHA1)
|
||||
}
|
||||
|
||||
type verifySHA1 struct {
|
||||
ssr.ServerInfo
|
||||
hasSentHeader bool
|
||||
buffer bytes.Buffer
|
||||
chunkId uint32
|
||||
}
|
||||
|
||||
const (
|
||||
oneTimeAuthMask byte = 0x10
|
||||
)
|
||||
|
||||
func NewVerifySHA1() IProtocol {
|
||||
a := &verifySHA1{}
|
||||
return a
|
||||
}
|
||||
|
||||
func (v *verifySHA1) otaConnectAuth(data []byte) []byte {
|
||||
return append(data, tools.HmacSHA1(append(v.IV, v.Key...), data)...)
|
||||
}
|
||||
|
||||
func (v *verifySHA1) otaReqChunkAuth(chunkId uint32, data []byte) []byte {
|
||||
nb := make([]byte, 2)
|
||||
binary.BigEndian.PutUint16(nb, uint16(len(data)))
|
||||
chunkIdBytes := make([]byte, 4)
|
||||
binary.BigEndian.PutUint32(chunkIdBytes, chunkId)
|
||||
header := append(nb, tools.HmacSHA1(append(v.IV, chunkIdBytes...), data)...)
|
||||
return append(header, data...)
|
||||
}
|
||||
|
||||
func (v *verifySHA1) otaVerifyAuth(iv []byte, chunkId uint32, data []byte, expectedHmacSha1 []byte) bool {
|
||||
chunkIdBytes := make([]byte, 4)
|
||||
binary.BigEndian.PutUint32(chunkIdBytes, chunkId)
|
||||
actualHmacSha1 := tools.HmacSHA1(append(iv, chunkIdBytes...), data)
|
||||
return bytes.Equal(expectedHmacSha1, actualHmacSha1)
|
||||
}
|
||||
|
||||
func (v *verifySHA1) getAndIncreaseChunkId() (chunkId uint32) {
|
||||
chunkId = v.chunkId
|
||||
v.chunkId += 1
|
||||
return
|
||||
}
|
||||
|
||||
func (v *verifySHA1) SetServerInfo(s *ssr.ServerInfo) {
|
||||
v.ServerInfo = *s
|
||||
}
|
||||
|
||||
func (v *verifySHA1) GetServerInfo() (s *ssr.ServerInfo) {
|
||||
return &v.ServerInfo
|
||||
}
|
||||
|
||||
func (v *verifySHA1) SetData(data interface{}) {
|
||||
|
||||
}
|
||||
|
||||
func (v *verifySHA1) GetData() interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *verifySHA1) PreEncrypt(data []byte) (encryptedData []byte, err error) {
|
||||
v.buffer.Reset()
|
||||
dataLength := len(data)
|
||||
offset := 0
|
||||
if !v.hasSentHeader {
|
||||
data[0] |= oneTimeAuthMask
|
||||
v.buffer.Write(v.otaConnectAuth(data[:v.HeadLen]))
|
||||
v.hasSentHeader = true
|
||||
dataLength -= v.HeadLen
|
||||
offset += v.HeadLen
|
||||
}
|
||||
const blockSize = 4096
|
||||
for dataLength > blockSize {
|
||||
chunkId := v.getAndIncreaseChunkId()
|
||||
v.buffer.Write(v.otaReqChunkAuth(chunkId, data[offset:offset+blockSize]))
|
||||
dataLength -= blockSize
|
||||
offset += blockSize
|
||||
}
|
||||
if dataLength > 0 {
|
||||
chunkId := v.getAndIncreaseChunkId()
|
||||
v.buffer.Write(v.otaReqChunkAuth(chunkId, data[offset:]))
|
||||
}
|
||||
return v.buffer.Bytes(), nil
|
||||
}
|
||||
|
||||
func (v *verifySHA1) PostDecrypt(data []byte) ([]byte, int, error) {
|
||||
return data, len(data), nil
|
||||
}
|
||||
|
||||
func (v *verifySHA1) GetOverhead() int {
|
||||
return 0
|
||||
}
|
||||
Reference in new issue
Block a user