Skip to content

Commit

Permalink
fix: simple-obfs wrong host
Browse files Browse the repository at this point in the history
  • Loading branch information
mzz2017 committed Feb 13, 2023
1 parent 91e0bc0 commit 7315537
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
8 changes: 7 additions & 1 deletion dialer/transport/simpleobfs/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"net"
"net/http"
"strings"
"sync"
)

// HTTPObfs is shadowsocks http simple-obfs implementation
Expand All @@ -24,9 +25,13 @@ type HTTPObfs struct {
offset int
firstRequest bool
firstResponse bool
rMu sync.Mutex
wMu sync.Mutex
}

func (ho *HTTPObfs) Read(b []byte) (int, error) {
ho.rMu.Lock()
defer ho.rMu.Unlock()
if ho.buf != nil {
n := copy(b, ho.buf[ho.offset:])
ho.offset += n
Expand Down Expand Up @@ -64,14 +69,15 @@ func (ho *HTTPObfs) Read(b []byte) (int, error) {
}

func (ho *HTTPObfs) Write(b []byte) (int, error) {
ho.wMu.Lock()
defer ho.wMu.Unlock()
if ho.firstRequest {
randBytes := make([]byte, 16)
rand.Read(randBytes)
req, _ := http.NewRequest("GET", fmt.Sprintf("http://%s%s", ho.host, ho.path), bytes.NewBuffer(b[:]))
req.Header.Set("User-Agent", fmt.Sprintf("curl/7.%d.%d", rand.Int()%54, rand.Int()%2))
req.Header.Set("Upgrade", "websocket")
req.Header.Set("Connection", "Upgrade")
req.Host = ho.host
if ho.port != "80" {
req.Host = fmt.Sprintf("%s:%s", ho.host, ho.port)
}
Expand Down
19 changes: 10 additions & 9 deletions dialer/transport/simpleobfs/simpleobfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,19 @@ func (s *SimpleObfs) Dial(network, addr string) (c net.Conn, err error) {
if err != nil {
return nil, fmt.Errorf("[simpleobfs]: dial to %s: %w", s.addr, err)
}

host, port, err := net.SplitHostPort(s.addr)
if err != nil {
return nil, err
}
if s.host != "" {
host = s.host
}
switch s.obfstype {
case HTTP:
rs := strings.Split(s.addr, ":")
var port string
if len(rs) == 1 {
port = "80"
} else {
port = rs[1]
}
c = NewHTTPObfs(rc, rs[0], port, s.path)
c = NewHTTPObfs(rc, host, port, s.path)
case TLS:
c = NewTLSObfs(rc, s.host)
c = NewTLSObfs(rc, host)
}
return c, err
}
7 changes: 7 additions & 0 deletions dialer/transport/simpleobfs/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/mzz2017/softwind/pkg/fastrand"
"io"
"net"
"sync"
"time"
)

Expand All @@ -22,6 +23,8 @@ type TLSObfs struct {
remain int
firstRequest bool
firstResponse bool
rMu sync.Mutex
wMu sync.Mutex
}

func (to *TLSObfs) read(b []byte, discardN int) (int, error) {
Expand Down Expand Up @@ -50,6 +53,8 @@ func (to *TLSObfs) read(b []byte, discardN int) (int, error) {
}

func (to *TLSObfs) Read(b []byte) (int, error) {
to.rMu.Lock()
defer to.rMu.Unlock()
if to.remain > 0 {
length := to.remain
if length > len(b) {
Expand All @@ -73,6 +78,8 @@ func (to *TLSObfs) Read(b []byte) (int, error) {
return to.read(b, 3)
}
func (to *TLSObfs) Write(b []byte) (int, error) {
to.wMu.Lock()
defer to.wMu.Unlock()
length := len(b)
for i := 0; i < length; i += chunkSize {
end := i + chunkSize
Expand Down

0 comments on commit 7315537

Please sign in to comment.