Skip to content

Commit

Permalink
optimize: compatible with Cloudflare CDN
Browse files Browse the repository at this point in the history
  • Loading branch information
nange committed Apr 22, 2024
1 parent 43aabfb commit 3c20797
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
1 change: 1 addition & 0 deletions easyss.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,7 @@ func (ss *Easyss) initHTTPOutboundClient() error {
})
}

client.SetProxy(nil)
ss.httpOutboundClient = client
return nil
}
Expand Down
42 changes: 27 additions & 15 deletions httptunnel/local_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,27 +144,39 @@ func (l *LocalConn) pull() error {
}

func (l *LocalConn) push() error {
resp, err := l.client.R().
r := l.client.R().
SetHeader("Content-Type", "application/json").
SetHeader("Transfer-Encoding", "chunked").
SetHeader("Accept-Encoding", "gzip").
SetHeader("Content-Encoding", "gzip").
SetQueryParam(RequestIDQuery, l.uuid).
SetBody(l).
Post(l.serverAddr + "/push")
if err != nil {
return err
}
SetQueryParam(RequestIDQuery, l.uuid)

body, err := resp.ToBytes()
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("status code:%v, body:%v", resp.StatusCode, string(body))
}
buf := bytespool.Get(cipherstream.MaxPayloadSize)
defer bytespool.MustPut(buf)

return nil
for {
var resp *req.Response
n, err1 := l.Read(buf)
if n > 0 {
var err2 error
resp, err2 = r.SetBody(buf[:n]).Post(l.serverAddr + "/push")
if err2 != nil {
return errors.Join(err1, err2)
}
}
if resp != nil {
body, err3 := resp.ToBytes()
if err3 != nil {
return err3
}
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("status code:%v, body:%v", resp.StatusCode, string(body))
}
}
if err1 != nil {
return err1
}
}
}

// Read implements io.Reader
Expand Down
3 changes: 2 additions & 1 deletion httptunnel/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,9 @@ func (s *Server) push(w http.ResponseWriter, r *http.Request) {
break
}
}
if err != nil {
if err != nil && !errors.Is(err, io.EOF) {
_ = conns[0].Close()
return
}

writeSuccess(w)
Expand Down
4 changes: 2 additions & 2 deletions local.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (ss *Easyss) validateAddr(addr string) error {
serverPort := strconv.FormatInt(int64(ss.ServerPort()), 10)
if !util.IsIP(host) {
if host == ss.Server() && port == serverPort {
return fmt.Errorf("target host,port equals to server host,port, which may caused infinite-loop")
return fmt.Errorf("target %s:%s equals to server host,port, which may caused infinite-loop", host, port)
}
return nil
}
Expand All @@ -149,7 +149,7 @@ func (ss *Easyss) validateAddr(addr string) error {
return fmt.Errorf("target %s is ipv6, but ipv6 network is disabled", host)
}
if (host == ss.ServerIP() || host == ss.ServerIPV6()) && port == serverPort {
return fmt.Errorf("target host:%v equals server host ip, which may caused infinite-loop", host)
return fmt.Errorf("target %s:%s equals server host,port, which may caused infinite-loop", host, port)
}

return nil
Expand Down

0 comments on commit 3c20797

Please sign in to comment.