Skip to content

Commit

Permalink
fix: don't ignore http.NewRequest's error
Browse files Browse the repository at this point in the history
  • Loading branch information
wwqgtxx committed May 15, 2024
1 parent 6d42bf6 commit 5c37598
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
10 changes: 6 additions & 4 deletions transport/simple-obfs/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package obfs

import (
"bytes"
"crypto/rand"
"encoding/base64"
"fmt"
"io"
Expand Down Expand Up @@ -65,8 +64,11 @@ func (ho *HTTPObfs) Read(b []byte) (int, error) {
func (ho *HTTPObfs) Write(b []byte) (int, error) {
if ho.firstRequest {
randBytes := make([]byte, 16)
rand.Read(randBytes)
req, _ := http.NewRequest("GET", fmt.Sprintf("http://%s/", ho.host), bytes.NewBuffer(b[:]))
fastrand.Read(randBytes)
req, err := http.NewRequest("GET", fmt.Sprintf("http://%s/", ho.host), bytes.NewBuffer(b[:]))
if err != nil {
return 0, err
}
req.Header.Set("User-Agent", fmt.Sprintf("curl/7.%d.%d", fastrand.Int()%54, fastrand.Int()%2))
req.Header.Set("Upgrade", "websocket")
req.Header.Set("Connection", "Upgrade")
Expand All @@ -76,7 +78,7 @@ func (ho *HTTPObfs) Write(b []byte) (int, error) {
}
req.Header.Set("Sec-WebSocket-Key", base64.URLEncoding.EncodeToString(randBytes))
req.ContentLength = int64(len(b))
err := req.Write(ho.Conn)
err = req.Write(ho.Conn)
ho.firstRequest = false
return len(b), err
}
Expand Down
5 changes: 4 additions & 1 deletion transport/vmess/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ func (hc *httpConn) Write(b []byte) (int, error) {
}

u := fmt.Sprintf("http://%s%s", host, path)
req, _ := http.NewRequest(utils.EmptyOr(hc.cfg.Method, http.MethodGet), u, bytes.NewBuffer(b))
req, err := http.NewRequest(utils.EmptyOr(hc.cfg.Method, http.MethodGet), u, bytes.NewBuffer(b))
if err != nil {
return 0, err
}
for key, list := range hc.cfg.Headers {
req.Header.Set(key, list[fastrand.Intn(len(list))])
}
Expand Down

0 comments on commit 5c37598

Please sign in to comment.