github.com/kelleygo/clashcore@v1.0.2/transport/vmess/http.go (about)

     1  package vmess
     2  
     3  import (
     4  	"bufio"
     5  	"bytes"
     6  	"errors"
     7  	"fmt"
     8  	"net"
     9  	"net/http"
    10  	"net/textproto"
    11  
    12  	"github.com/kelleygo/clashcore/common/utils"
    13  
    14  	"github.com/zhangyunhao116/fastrand"
    15  )
    16  
    17  type httpConn struct {
    18  	net.Conn
    19  	cfg        *HTTPConfig
    20  	reader     *bufio.Reader
    21  	whandshake bool
    22  }
    23  
    24  type HTTPConfig struct {
    25  	Method  string
    26  	Host    string
    27  	Path    []string
    28  	Headers map[string][]string
    29  }
    30  
    31  // Read implements net.Conn.Read()
    32  func (hc *httpConn) Read(b []byte) (int, error) {
    33  	if hc.reader != nil {
    34  		n, err := hc.reader.Read(b)
    35  		return n, err
    36  	}
    37  
    38  	reader := textproto.NewConn(hc.Conn)
    39  	// First line: GET /index.html HTTP/1.0
    40  	if _, err := reader.ReadLine(); err != nil {
    41  		return 0, err
    42  	}
    43  
    44  	if _, err := reader.ReadMIMEHeader(); err != nil {
    45  		return 0, err
    46  	}
    47  
    48  	hc.reader = reader.R
    49  	return reader.R.Read(b)
    50  }
    51  
    52  // Write implements io.Writer.
    53  func (hc *httpConn) Write(b []byte) (int, error) {
    54  	if hc.whandshake {
    55  		return hc.Conn.Write(b)
    56  	}
    57  
    58  	if len(hc.cfg.Path) == 0 {
    59  		return -1, errors.New("path is empty")
    60  	}
    61  
    62  	path := hc.cfg.Path[fastrand.Intn(len(hc.cfg.Path))]
    63  	host := hc.cfg.Host
    64  	if header := hc.cfg.Headers["Host"]; len(header) != 0 {
    65  		host = header[fastrand.Intn(len(header))]
    66  	}
    67  
    68  	u := fmt.Sprintf("http://%s%s", net.JoinHostPort(host, "80"), path)
    69  	req, _ := http.NewRequest(utils.EmptyOr(hc.cfg.Method, http.MethodGet), u, bytes.NewBuffer(b))
    70  	for key, list := range hc.cfg.Headers {
    71  		req.Header.Set(key, list[fastrand.Intn(len(list))])
    72  	}
    73  	req.ContentLength = int64(len(b))
    74  	if err := req.Write(hc.Conn); err != nil {
    75  		return 0, err
    76  	}
    77  	hc.whandshake = true
    78  	return len(b), nil
    79  }
    80  
    81  func (hc *httpConn) Close() error {
    82  	return hc.Conn.Close()
    83  }
    84  
    85  func StreamHTTPConn(conn net.Conn, cfg *HTTPConfig) net.Conn {
    86  	return &httpConn{
    87  		Conn: conn,
    88  		cfg:  cfg,
    89  	}
    90  }