github.com/chwjbn/xclash@v0.2.0/transport/v2ray-plugin/websocket.go (about)

     1  package obfs
     2  
     3  import (
     4  	"crypto/tls"
     5  	"net"
     6  	"net/http"
     7  
     8  	"github.com/chwjbn/xclash/transport/vmess"
     9  )
    10  
    11  // Option is options of websocket obfs
    12  type Option struct {
    13  	Host           string
    14  	Port           string
    15  	Path           string
    16  	Headers        map[string]string
    17  	TLS            bool
    18  	SkipCertVerify bool
    19  	Mux            bool
    20  }
    21  
    22  // NewV2rayObfs return a HTTPObfs
    23  func NewV2rayObfs(conn net.Conn, option *Option) (net.Conn, error) {
    24  	header := http.Header{}
    25  	for k, v := range option.Headers {
    26  		header.Add(k, v)
    27  	}
    28  
    29  	config := &vmess.WebsocketConfig{
    30  		Host:    option.Host,
    31  		Port:    option.Port,
    32  		Path:    option.Path,
    33  		Headers: header,
    34  	}
    35  
    36  	if option.TLS {
    37  		config.TLS = true
    38  		config.TLSConfig = &tls.Config{
    39  			ServerName:         option.Host,
    40  			InsecureSkipVerify: option.SkipCertVerify,
    41  			NextProtos:         []string{"http/1.1"},
    42  		}
    43  		if host := config.Headers.Get("Host"); host != "" {
    44  			config.TLSConfig.ServerName = host
    45  		}
    46  	}
    47  
    48  	var err error
    49  	conn, err = vmess.StreamWebsocketConn(conn, config)
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  
    54  	if option.Mux {
    55  		conn = NewMux(conn, MuxOption{
    56  			ID:   [2]byte{0, 0},
    57  			Host: "127.0.0.1",
    58  			Port: 0,
    59  		})
    60  	}
    61  	return conn, nil
    62  }