add core
This commit is contained in:
@ -71,17 +71,7 @@ func DialWithRawAddr(rawConn *net.Conn, rawaddr []byte, server string, cipher *C
|
|||||||
} else {
|
} else {
|
||||||
c = NewConn(conn, cipher)
|
c = NewConn(conn, cipher)
|
||||||
}
|
}
|
||||||
if cipher.ota {
|
|
||||||
if c.enc == nil {
|
|
||||||
if _, err = c.initEncrypt(); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// since we have initEncrypt, we must send iv manually
|
|
||||||
conn.Write(cipher.iv)
|
|
||||||
rawaddr[0] |= OneTimeAuthMask
|
|
||||||
rawaddr = otaConnectAuth(cipher.iv, cipher.key, rawaddr)
|
|
||||||
}
|
|
||||||
if _, err = c.write(rawaddr); err != nil {
|
if _, err = c.write(rawaddr); err != nil {
|
||||||
c.Close()
|
c.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -150,10 +140,7 @@ func (c *Conn) Read(b []byte) (n int, err error) {
|
|||||||
|
|
||||||
func (c *Conn) Write(b []byte) (n int, err error) {
|
func (c *Conn) Write(b []byte) (n int, err error) {
|
||||||
nn := len(b)
|
nn := len(b)
|
||||||
if c.ota {
|
|
||||||
chunkId := c.GetAndIncrChunkId()
|
|
||||||
b = otaReqChunkAuth(c.iv, chunkId, b)
|
|
||||||
}
|
|
||||||
headerLen := len(b) - nn
|
headerLen := len(b) - nn
|
||||||
|
|
||||||
n, err = c.write(b)
|
n, err = c.write(b)
|
||||||
|
|||||||
103
utils/ss/util.go
103
utils/ss/util.go
@ -1,15 +1,7 @@
|
|||||||
package ss
|
package ss
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/hmac"
|
|
||||||
"crypto/sha1"
|
|
||||||
"encoding/binary"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"net"
|
"net"
|
||||||
"os"
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/snail007/goproxy/utils"
|
"github.com/snail007/goproxy/utils"
|
||||||
)
|
)
|
||||||
@ -19,39 +11,6 @@ const maxNBuf = 2048
|
|||||||
|
|
||||||
var leakyBuf = utils.NewLeakyBuf(maxNBuf, leakyBufSize)
|
var leakyBuf = utils.NewLeakyBuf(maxNBuf, leakyBufSize)
|
||||||
|
|
||||||
func IsFileExists(path string) (bool, error) {
|
|
||||||
stat, err := os.Stat(path)
|
|
||||||
if err == nil {
|
|
||||||
if stat.Mode()&os.ModeType == 0 {
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
return false, errors.New(path + " exists but is not regular file")
|
|
||||||
}
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func HmacSha1(key []byte, data []byte) []byte {
|
|
||||||
hmacSha1 := hmac.New(sha1.New, key)
|
|
||||||
hmacSha1.Write(data)
|
|
||||||
return hmacSha1.Sum(nil)[:10]
|
|
||||||
}
|
|
||||||
|
|
||||||
func otaConnectAuth(iv, key, data []byte) []byte {
|
|
||||||
return append(data, HmacSha1(append(iv, key...), data)...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func otaReqChunkAuth(iv []byte, 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, HmacSha1(append(iv, chunkIdBytes...), data)...)
|
|
||||||
return append(header, data...)
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
idType = 0 // address type index
|
idType = 0 // address type index
|
||||||
idIP0 = 1 // ip addres start index
|
idIP0 = 1 // ip addres start index
|
||||||
@ -67,65 +26,3 @@ const (
|
|||||||
lenDmBase = 2 // 1addrLen + 2port, plus addrLen
|
lenDmBase = 2 // 1addrLen + 2port, plus addrLen
|
||||||
lenHmacSha1 = 10
|
lenHmacSha1 = 10
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetRequest(conn *Conn) (host string, err error) {
|
|
||||||
|
|
||||||
// buf size should at least have the same size with the largest possible
|
|
||||||
// request size (when addrType is 3, domain name has at most 256 bytes)
|
|
||||||
// 1(addrType) + 1(lenByte) + 255(max length address) + 2(port) + 10(hmac-sha1)
|
|
||||||
buf := make([]byte, 269)
|
|
||||||
// read till we get possible domain length field
|
|
||||||
if _, err = io.ReadFull(conn, buf[:idType+1]); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var reqStart, reqEnd int
|
|
||||||
addrType := buf[idType]
|
|
||||||
switch addrType & AddrMask {
|
|
||||||
case typeIPv4:
|
|
||||||
reqStart, reqEnd = idIP0, idIP0+lenIPv4
|
|
||||||
case typeIPv6:
|
|
||||||
reqStart, reqEnd = idIP0, idIP0+lenIPv6
|
|
||||||
case typeDm:
|
|
||||||
if _, err = io.ReadFull(conn, buf[idType+1:idDmLen+1]); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
reqStart, reqEnd = idDm0, idDm0+int(buf[idDmLen])+lenDmBase
|
|
||||||
default:
|
|
||||||
err = fmt.Errorf("addr type %d not supported", addrType&AddrMask)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err = io.ReadFull(conn, buf[reqStart:reqEnd]); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return string for typeIP is not most efficient, but browsers (Chrome,
|
|
||||||
// Safari, Firefox) all seems using typeDm exclusively. So this is not a
|
|
||||||
// big problem.
|
|
||||||
switch addrType & AddrMask {
|
|
||||||
case typeIPv4:
|
|
||||||
host = net.IP(buf[idIP0 : idIP0+net.IPv4len]).String()
|
|
||||||
case typeIPv6:
|
|
||||||
host = net.IP(buf[idIP0 : idIP0+net.IPv6len]).String()
|
|
||||||
case typeDm:
|
|
||||||
host = string(buf[idDm0 : idDm0+int(buf[idDmLen])])
|
|
||||||
}
|
|
||||||
// parse port
|
|
||||||
port := binary.BigEndian.Uint16(buf[reqEnd-2 : reqEnd])
|
|
||||||
host = net.JoinHostPort(host, strconv.Itoa(int(port)))
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
type ClosedFlag struct {
|
|
||||||
flag bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func (flag *ClosedFlag) SetClosed() {
|
|
||||||
flag.flag = true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (flag *ClosedFlag) IsClosed() bool {
|
|
||||||
return flag.flag
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user