add obfs dialer
This commit is contained in:
@ -6,23 +6,26 @@ import (
|
||||
"crypto/sha1"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/gost/pkg/logger"
|
||||
)
|
||||
|
||||
type conn struct {
|
||||
type obfsHTTPConn struct {
|
||||
net.Conn
|
||||
rbuf bytes.Buffer
|
||||
wbuf bytes.Buffer
|
||||
handshaked bool
|
||||
handshakeMutex sync.Mutex
|
||||
logger logger.Logger
|
||||
}
|
||||
|
||||
func (c *conn) Handshake() (err error) {
|
||||
func (c *obfsHTTPConn) Handshake() (err error) {
|
||||
c.handshakeMutex.Lock()
|
||||
defer c.handshakeMutex.Unlock()
|
||||
|
||||
@ -38,18 +41,17 @@ func (c *conn) Handshake() (err error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *conn) handshake() (err error) {
|
||||
func (c *obfsHTTPConn) handshake() (err error) {
|
||||
br := bufio.NewReader(c.Conn)
|
||||
r, err := http.ReadRequest(br)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
/*
|
||||
if Debug {
|
||||
dump, _ := httputil.DumpRequest(r, false)
|
||||
log.Logf("[ohttp] %s -> %s\n%s", c.RemoteAddr(), c.LocalAddr(), string(dump))
|
||||
}
|
||||
*/
|
||||
|
||||
if c.logger.IsLevelEnabled(logger.DebugLevel) {
|
||||
dump, _ := httputil.DumpRequest(r, false)
|
||||
c.logger.Debug(string(dump))
|
||||
}
|
||||
|
||||
if r.ContentLength > 0 {
|
||||
_, err = io.Copy(&c.rbuf, r.Body)
|
||||
@ -61,52 +63,52 @@ func (c *conn) handshake() (err error) {
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
// log.Logf("[ohttp] %s -> %s : %v", c.Conn.RemoteAddr(), c.Conn.LocalAddr(), err)
|
||||
c.logger.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
b := bytes.Buffer{}
|
||||
resp := http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
ProtoMajor: 1,
|
||||
ProtoMinor: 1,
|
||||
Header: make(http.Header),
|
||||
}
|
||||
resp.Header.Set("Server", "nginx/1.18.0")
|
||||
resp.Header.Set("Date", time.Now().Format(time.RFC1123))
|
||||
|
||||
if r.Method != http.MethodGet || r.Header.Get("Upgrade") != "websocket" {
|
||||
b.WriteString("HTTP/1.1 503 Service Unavailable\r\n")
|
||||
b.WriteString("Content-Length: 0\r\n")
|
||||
b.WriteString("Date: " + time.Now().Format(time.RFC1123) + "\r\n")
|
||||
b.WriteString("\r\n")
|
||||
resp.StatusCode = http.StatusBadRequest
|
||||
|
||||
/*
|
||||
if Debug {
|
||||
log.Logf("[ohttp] %s <- %s\n%s", c.RemoteAddr(), c.LocalAddr(), b.String())
|
||||
}
|
||||
*/
|
||||
if c.logger.IsLevelEnabled(logger.DebugLevel) {
|
||||
dump, _ := httputil.DumpResponse(&resp, false)
|
||||
c.logger.Debug(string(dump))
|
||||
}
|
||||
|
||||
b.WriteTo(c.Conn)
|
||||
resp.Write(c.Conn)
|
||||
return errors.New("bad request")
|
||||
}
|
||||
|
||||
b.WriteString("HTTP/1.1 101 Switching Protocols\r\n")
|
||||
b.WriteString("Server: nginx/1.10.0\r\n")
|
||||
b.WriteString("Date: " + time.Now().Format(time.RFC1123) + "\r\n")
|
||||
b.WriteString("Connection: Upgrade\r\n")
|
||||
b.WriteString("Upgrade: websocket\r\n")
|
||||
b.WriteString(fmt.Sprintf("Sec-WebSocket-Accept: %s\r\n", computeAcceptKey(r.Header.Get("Sec-WebSocket-Key"))))
|
||||
b.WriteString("\r\n")
|
||||
resp.StatusCode = http.StatusSwitchingProtocols
|
||||
resp.Header.Set("Connection", "Upgrade")
|
||||
resp.Header.Set("Upgrade", "websocket")
|
||||
resp.Header.Set("Sec-WebSocket-Accept", c.computeAcceptKey(r.Header.Get("Sec-WebSocket-Key")))
|
||||
|
||||
/*
|
||||
if Debug {
|
||||
log.Logf("[ohttp] %s <- %s\n%s", c.RemoteAddr(), c.LocalAddr(), b.String())
|
||||
}
|
||||
*/
|
||||
if c.logger.IsLevelEnabled(logger.DebugLevel) {
|
||||
dump, _ := httputil.DumpResponse(&resp, false)
|
||||
c.logger.Debug(string(dump))
|
||||
}
|
||||
|
||||
if c.rbuf.Len() > 0 {
|
||||
c.wbuf = b // cache the response header if there are extra data in the request body.
|
||||
// cache the response header if there are extra data in the request body.
|
||||
resp.Write(&c.wbuf)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = b.WriteTo(c.Conn)
|
||||
err = resp.Write(c.Conn)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *conn) Read(b []byte) (n int, err error) {
|
||||
func (c *obfsHTTPConn) Read(b []byte) (n int, err error) {
|
||||
if err = c.Handshake(); err != nil {
|
||||
return
|
||||
}
|
||||
@ -117,7 +119,7 @@ func (c *conn) Read(b []byte) (n int, err error) {
|
||||
return c.Conn.Read(b)
|
||||
}
|
||||
|
||||
func (c *conn) Write(b []byte) (n int, err error) {
|
||||
func (c *obfsHTTPConn) Write(b []byte) (n int, err error) {
|
||||
if err = c.Handshake(); err != nil {
|
||||
return
|
||||
}
|
||||
@ -132,7 +134,7 @@ func (c *conn) Write(b []byte) (n int, err error) {
|
||||
|
||||
var keyGUID = []byte("258EAFA5-E914-47DA-95CA-C5AB0DC85B11")
|
||||
|
||||
func computeAcceptKey(challengeKey string) string {
|
||||
func (c *obfsHTTPConn) computeAcceptKey(challengeKey string) string {
|
||||
h := sha1.New()
|
||||
h.Write([]byte(challengeKey))
|
||||
h.Write(keyGUID)
|
||||
|
@ -3,7 +3,6 @@ package http
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/go-gost/gost/pkg/common/util"
|
||||
"github.com/go-gost/gost/pkg/listener"
|
||||
"github.com/go-gost/gost/pkg/logger"
|
||||
md "github.com/go-gost/gost/pkg/metadata"
|
||||
@ -46,14 +45,6 @@ func (l *obfsListener) Init(md md.Metadata) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
if l.md.keepAlive {
|
||||
l.Listener = &util.TCPKeepAliveListener{
|
||||
TCPListener: ln,
|
||||
KeepAlivePeriod: l.md.keepAlivePeriod,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
l.Listener = ln
|
||||
return
|
||||
}
|
||||
@ -64,12 +55,8 @@ func (l *obfsListener) Accept() (net.Conn, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &conn{Conn: c}, nil
|
||||
}
|
||||
|
||||
func (l *obfsListener) parseMetadata(md md.Metadata) (err error) {
|
||||
l.md.keepAlive = md.GetBool(keepAlive)
|
||||
l.md.keepAlivePeriod = md.GetDuration(keepAlivePeriod)
|
||||
|
||||
return
|
||||
return &obfsHTTPConn{
|
||||
Conn: c,
|
||||
logger: l.logger,
|
||||
}, nil
|
||||
}
|
||||
|
@ -1,17 +1,17 @@
|
||||
package http
|
||||
|
||||
import "time"
|
||||
import (
|
||||
md "github.com/go-gost/gost/pkg/metadata"
|
||||
)
|
||||
|
||||
const (
|
||||
keepAlive = "keepAlive"
|
||||
keepAlivePeriod = "keepAlivePeriod"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultKeepAlivePeriod = 180 * time.Second
|
||||
)
|
||||
|
||||
type metadata struct {
|
||||
keepAlive bool
|
||||
keepAlivePeriod time.Duration
|
||||
}
|
||||
|
||||
func (l *obfsListener) parseMetadata(md md.Metadata) (err error) {
|
||||
return
|
||||
}
|
||||
|
@ -4,169 +4,30 @@ import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
dissector "github.com/ginuerzh/tls-dissector"
|
||||
dissector "github.com/go-gost/tls-dissector"
|
||||
)
|
||||
|
||||
const (
|
||||
maxTLSDataLen = 16384
|
||||
)
|
||||
|
||||
var (
|
||||
cipherSuites = []uint16{
|
||||
0xc02c, 0xc030, 0x009f, 0xcca9, 0xcca8, 0xccaa, 0xc02b, 0xc02f,
|
||||
0x009e, 0xc024, 0xc028, 0x006b, 0xc023, 0xc027, 0x0067, 0xc00a,
|
||||
0xc014, 0x0039, 0xc009, 0xc013, 0x0033, 0x009d, 0x009c, 0x003d,
|
||||
0x003c, 0x0035, 0x002f, 0x00ff,
|
||||
}
|
||||
|
||||
compressionMethods = []uint8{0x00}
|
||||
|
||||
algorithms = []uint16{
|
||||
0x0601, 0x0602, 0x0603, 0x0501, 0x0502, 0x0503, 0x0401, 0x0402,
|
||||
0x0403, 0x0301, 0x0302, 0x0303, 0x0201, 0x0202, 0x0203,
|
||||
}
|
||||
|
||||
tlsRecordTypes = []uint8{0x16, 0x14, 0x16, 0x17}
|
||||
tlsVersionMinors = []uint8{0x01, 0x03, 0x03, 0x03}
|
||||
|
||||
ErrBadType = errors.New("bad type")
|
||||
ErrBadMajorVersion = errors.New("bad major version")
|
||||
ErrBadMinorVersion = errors.New("bad minor version")
|
||||
ErrMaxDataLen = errors.New("bad tls data len")
|
||||
)
|
||||
|
||||
const (
|
||||
tlsRecordStateType = iota
|
||||
tlsRecordStateVersion0
|
||||
tlsRecordStateVersion1
|
||||
tlsRecordStateLength0
|
||||
tlsRecordStateLength1
|
||||
tlsRecordStateData
|
||||
)
|
||||
|
||||
type obfsTLSParser struct {
|
||||
step uint8
|
||||
state uint8
|
||||
length uint16
|
||||
}
|
||||
|
||||
func (r *obfsTLSParser) Parse(b []byte) (int, error) {
|
||||
i := 0
|
||||
last := 0
|
||||
length := len(b)
|
||||
|
||||
for i < length {
|
||||
ch := b[i]
|
||||
switch r.state {
|
||||
case tlsRecordStateType:
|
||||
if tlsRecordTypes[r.step] != ch {
|
||||
return 0, ErrBadType
|
||||
}
|
||||
r.state = tlsRecordStateVersion0
|
||||
i++
|
||||
case tlsRecordStateVersion0:
|
||||
if ch != 0x03 {
|
||||
return 0, ErrBadMajorVersion
|
||||
}
|
||||
r.state = tlsRecordStateVersion1
|
||||
i++
|
||||
case tlsRecordStateVersion1:
|
||||
if ch != tlsVersionMinors[r.step] {
|
||||
return 0, ErrBadMinorVersion
|
||||
}
|
||||
r.state = tlsRecordStateLength0
|
||||
i++
|
||||
case tlsRecordStateLength0:
|
||||
r.length = uint16(ch) << 8
|
||||
r.state = tlsRecordStateLength1
|
||||
i++
|
||||
case tlsRecordStateLength1:
|
||||
r.length |= uint16(ch)
|
||||
if r.step == 0 {
|
||||
r.length = 91
|
||||
} else if r.step == 1 {
|
||||
r.length = 1
|
||||
} else if r.length > maxTLSDataLen {
|
||||
return 0, ErrMaxDataLen
|
||||
}
|
||||
if r.length > 0 {
|
||||
r.state = tlsRecordStateData
|
||||
} else {
|
||||
r.state = tlsRecordStateType
|
||||
r.step++
|
||||
}
|
||||
i++
|
||||
case tlsRecordStateData:
|
||||
left := uint16(length - i)
|
||||
if left > r.length {
|
||||
left = r.length
|
||||
}
|
||||
if r.step >= 2 {
|
||||
skip := i - last
|
||||
copy(b[last:], b[i:length])
|
||||
length -= int(skip)
|
||||
last += int(left)
|
||||
i = last
|
||||
} else {
|
||||
i += int(left)
|
||||
}
|
||||
r.length -= left
|
||||
if r.length == 0 {
|
||||
if r.step < 3 {
|
||||
r.step++
|
||||
}
|
||||
r.state = tlsRecordStateType
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if last == 0 {
|
||||
return 0, nil
|
||||
} else if last < length {
|
||||
length -= last
|
||||
}
|
||||
|
||||
return length, nil
|
||||
}
|
||||
|
||||
type conn struct {
|
||||
type obfsTLSConn struct {
|
||||
net.Conn
|
||||
rbuf bytes.Buffer
|
||||
wbuf bytes.Buffer
|
||||
host string
|
||||
handshaked chan struct{}
|
||||
parser *obfsTLSParser
|
||||
handshaked bool
|
||||
handshakeMutex sync.Mutex
|
||||
}
|
||||
|
||||
// newConn creates a connection for obfs-tls server.
|
||||
func newConn(c net.Conn, host string) net.Conn {
|
||||
return &conn{
|
||||
Conn: c,
|
||||
host: host,
|
||||
handshaked: make(chan struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *conn) Handshaked() bool {
|
||||
select {
|
||||
case <-c.handshaked:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (c *conn) Handshake(payload []byte) (err error) {
|
||||
func (c *obfsTLSConn) Handshake() (err error) {
|
||||
c.handshakeMutex.Lock()
|
||||
defer c.handshakeMutex.Unlock()
|
||||
|
||||
if c.Handshaked() {
|
||||
if c.handshaked {
|
||||
return
|
||||
}
|
||||
|
||||
@ -174,11 +35,11 @@ func (c *conn) Handshake(payload []byte) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
close(c.handshaked)
|
||||
c.handshaked = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *conn) handshake() error {
|
||||
func (c *obfsTLSConn) handshake() error {
|
||||
record := &dissector.Record{}
|
||||
if _, err := record.ReadFrom(c.Conn); err != nil {
|
||||
// log.Log(err)
|
||||
@ -248,15 +109,11 @@ func (c *conn) handshake() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *conn) Read(b []byte) (n int, err error) {
|
||||
if err = c.Handshake(nil); err != nil {
|
||||
func (c *obfsTLSConn) Read(b []byte) (n int, err error) {
|
||||
if err = c.Handshake(); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
select {
|
||||
case <-c.handshaked:
|
||||
}
|
||||
|
||||
if c.rbuf.Len() > 0 {
|
||||
return c.rbuf.Read(b)
|
||||
}
|
||||
@ -269,13 +126,11 @@ func (c *conn) Read(b []byte) (n int, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (c *conn) Write(b []byte) (n int, err error) {
|
||||
n = len(b)
|
||||
if !c.Handshaked() {
|
||||
if err = c.Handshake(b); err != nil {
|
||||
return
|
||||
}
|
||||
func (c *obfsTLSConn) Write(b []byte) (n int, err error) {
|
||||
if err = c.Handshake(); err != nil {
|
||||
return
|
||||
}
|
||||
n = len(b)
|
||||
|
||||
for len(b) > 0 {
|
||||
data := b
|
||||
|
@ -3,7 +3,6 @@ package tls
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/go-gost/gost/pkg/common/util"
|
||||
"github.com/go-gost/gost/pkg/listener"
|
||||
"github.com/go-gost/gost/pkg/logger"
|
||||
md "github.com/go-gost/gost/pkg/metadata"
|
||||
@ -46,14 +45,6 @@ func (l *obfsListener) Init(md md.Metadata) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
if l.md.keepAlive {
|
||||
l.Listener = &util.TCPKeepAliveListener{
|
||||
TCPListener: ln,
|
||||
KeepAlivePeriod: l.md.keepAlivePeriod,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
l.Listener = ln
|
||||
return
|
||||
}
|
||||
@ -64,12 +55,7 @@ func (l *obfsListener) Accept() (net.Conn, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &conn{Conn: c}, nil
|
||||
}
|
||||
|
||||
func (l *obfsListener) parseMetadata(md md.Metadata) (err error) {
|
||||
l.md.keepAlive = md.GetBool(keepAlive)
|
||||
l.md.keepAlivePeriod = md.GetDuration(keepAlivePeriod)
|
||||
|
||||
return
|
||||
return &obfsTLSConn{
|
||||
Conn: c,
|
||||
}, nil
|
||||
}
|
||||
|
@ -1,17 +1,12 @@
|
||||
package tls
|
||||
|
||||
import "time"
|
||||
|
||||
const (
|
||||
keepAlive = "keepAlive"
|
||||
keepAlivePeriod = "keepAlivePeriod"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultKeepAlivePeriod = 180 * time.Second
|
||||
import (
|
||||
md "github.com/go-gost/gost/pkg/metadata"
|
||||
)
|
||||
|
||||
type metadata struct {
|
||||
keepAlive bool
|
||||
keepAlivePeriod time.Duration
|
||||
}
|
||||
|
||||
func (l *obfsListener) parseMetadata(md md.Metadata) (err error) {
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user