Feature: refactor vmess & add http network

This commit is contained in:
Dreamacro
2020-03-31 16:07:21 +08:00
parent 206767247e
commit 19f809b1c8
8 changed files with 214 additions and 134 deletions

View File

@ -25,11 +25,13 @@ type websocketConn struct {
}
type WebsocketConfig struct {
Host string
Path string
Headers http.Header
TLS bool
TLSConfig *tls.Config
Host string
Port string
Path string
Headers http.Header
TLS bool
SkipCertVerify bool
SessionCache tls.ClientSessionCache
}
// Read implements net.Conn.Read()
@ -111,7 +113,7 @@ func (wsc *websocketConn) SetWriteDeadline(t time.Time) error {
return wsc.conn.SetWriteDeadline(t)
}
func NewWebsocketConn(conn net.Conn, c *WebsocketConfig) (net.Conn, error) {
func StreamWebsocketConn(conn net.Conn, c *WebsocketConfig) (net.Conn, error) {
dialer := &websocket.Dialer{
NetDial: func(network, addr string) (net.Conn, error) {
return conn, nil
@ -124,17 +126,20 @@ func NewWebsocketConn(conn net.Conn, c *WebsocketConfig) (net.Conn, error) {
scheme := "ws"
if c.TLS {
scheme = "wss"
dialer.TLSClientConfig = c.TLSConfig
}
dialer.TLSClientConfig = &tls.Config{
ServerName: c.Host,
InsecureSkipVerify: c.SkipCertVerify,
ClientSessionCache: c.SessionCache,
}
host, port, _ := net.SplitHostPort(c.Host)
if (scheme == "ws" && port != "80") || (scheme == "wss" && port != "443") {
host = c.Host
if host := c.Headers.Get("Host"); host != "" {
dialer.TLSClientConfig.ServerName = host
}
}
uri := url.URL{
Scheme: scheme,
Host: host,
Host: net.JoinHostPort(c.Host, c.Port),
Path: c.Path,
}
@ -151,7 +156,7 @@ func NewWebsocketConn(conn net.Conn, c *WebsocketConfig) (net.Conn, error) {
if resp != nil {
reason = resp.Status
}
return nil, fmt.Errorf("Dial %s error: %s", host, reason)
return nil, fmt.Errorf("Dial %s error: %s", uri.Host, reason)
}
return &websocketConn{