mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2025-02-23 01:53:14 +08:00
Chore: optimize code structure in vmess websocket (#28)
* Chore: move conn process of ws to websocket.go * Chore: some routine adjustment
This commit is contained in:
parent
10e0231bc1
commit
cc6d496143
@ -5,13 +5,10 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
"net/url"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/gofrs/uuid"
|
"github.com/gofrs/uuid"
|
||||||
"github.com/gorilla/websocket"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Version of vmess
|
// Version of vmess
|
||||||
@ -67,14 +64,13 @@ type DstAddr struct {
|
|||||||
|
|
||||||
// Client is vmess connection generator
|
// Client is vmess connection generator
|
||||||
type Client struct {
|
type Client struct {
|
||||||
user []*ID
|
user []*ID
|
||||||
uuid *uuid.UUID
|
uuid *uuid.UUID
|
||||||
security Security
|
security Security
|
||||||
tls bool
|
tls bool
|
||||||
host string
|
host string
|
||||||
websocket bool
|
wsConfig *websocketConfig
|
||||||
websocketPath string
|
tlsConfig *tls.Config
|
||||||
tlsConfig *tls.Config
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Config of vmess
|
// Config of vmess
|
||||||
@ -92,43 +88,13 @@ type Config struct {
|
|||||||
|
|
||||||
// New return a Conn with net.Conn and DstAddr
|
// New return a Conn with net.Conn and DstAddr
|
||||||
func (c *Client) New(conn net.Conn, dst *DstAddr) (net.Conn, error) {
|
func (c *Client) New(conn net.Conn, dst *DstAddr) (net.Conn, error) {
|
||||||
|
var err error
|
||||||
r := rand.Intn(len(c.user))
|
r := rand.Intn(len(c.user))
|
||||||
if c.websocket {
|
if c.wsConfig != nil {
|
||||||
dialer := &websocket.Dialer{
|
conn, err = newWebsocketConn(conn, c.wsConfig)
|
||||||
NetDial: func(network, addr string) (net.Conn, error) {
|
|
||||||
return conn, nil
|
|
||||||
},
|
|
||||||
ReadBufferSize: 4 * 1024,
|
|
||||||
WriteBufferSize: 4 * 1024,
|
|
||||||
HandshakeTimeout: time.Second * 8,
|
|
||||||
}
|
|
||||||
scheme := "ws"
|
|
||||||
if c.tls {
|
|
||||||
scheme = "wss"
|
|
||||||
dialer.TLSClientConfig = c.tlsConfig
|
|
||||||
}
|
|
||||||
|
|
||||||
host, port, err := net.SplitHostPort(c.host)
|
|
||||||
if (scheme == "ws" && port != "80") || (scheme == "wss" && port != "443") {
|
|
||||||
host = c.host
|
|
||||||
}
|
|
||||||
|
|
||||||
uri := url.URL{
|
|
||||||
Scheme: scheme,
|
|
||||||
Host: host,
|
|
||||||
Path: c.websocketPath,
|
|
||||||
}
|
|
||||||
|
|
||||||
wsConn, resp, err := dialer.Dial(uri.String(), nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
var reason string
|
return nil, err
|
||||||
if resp != nil {
|
|
||||||
reason = resp.Status
|
|
||||||
}
|
|
||||||
return nil, fmt.Errorf("Dial %s error: %s", host, reason)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
conn = newWebsocketConn(wsConn, conn.RemoteAddr())
|
|
||||||
} else if c.tls {
|
} else if c.tls {
|
||||||
conn = tls.Client(conn, c.tlsConfig)
|
conn = tls.Client(conn, c.tlsConfig)
|
||||||
}
|
}
|
||||||
@ -174,15 +140,24 @@ func NewClient(config Config) (*Client, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var wsConfig *websocketConfig
|
||||||
|
if config.NetWork == "ws" {
|
||||||
|
wsConfig = &websocketConfig{
|
||||||
|
host: config.Host,
|
||||||
|
path: config.WebSocketPath,
|
||||||
|
tls: config.TLS,
|
||||||
|
tlsConfig: tlsConfig,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return &Client{
|
return &Client{
|
||||||
user: newAlterIDs(newID(&uid), config.AlterID),
|
user: newAlterIDs(newID(&uid), config.AlterID),
|
||||||
uuid: &uid,
|
uuid: &uid,
|
||||||
security: security,
|
security: security,
|
||||||
tls: config.TLS,
|
tls: config.TLS,
|
||||||
host: config.Host,
|
host: config.Host,
|
||||||
websocket: config.NetWork == "ws",
|
wsConfig: wsConfig,
|
||||||
websocketPath: config.WebSocketPath,
|
tlsConfig: tlsConfig,
|
||||||
tlsConfig: tlsConfig,
|
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
package vmess
|
package vmess
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -16,6 +18,13 @@ type websocketConn struct {
|
|||||||
remoteAddr net.Addr
|
remoteAddr net.Addr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type websocketConfig struct {
|
||||||
|
host string
|
||||||
|
path string
|
||||||
|
tls bool
|
||||||
|
tlsConfig *tls.Config
|
||||||
|
}
|
||||||
|
|
||||||
// Read implements net.Conn.Read()
|
// Read implements net.Conn.Read()
|
||||||
func (wsc *websocketConn) Read(b []byte) (int, error) {
|
func (wsc *websocketConn) Read(b []byte) (int, error) {
|
||||||
for {
|
for {
|
||||||
@ -91,9 +100,44 @@ func (wsc *websocketConn) SetWriteDeadline(t time.Time) error {
|
|||||||
return wsc.conn.SetWriteDeadline(t)
|
return wsc.conn.SetWriteDeadline(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func newWebsocketConn(conn *websocket.Conn, remoteAddr net.Addr) net.Conn {
|
func newWebsocketConn(conn net.Conn, c *websocketConfig) (net.Conn, error) {
|
||||||
return &websocketConn{
|
dialer := &websocket.Dialer{
|
||||||
conn: conn,
|
NetDial: func(network, addr string) (net.Conn, error) {
|
||||||
remoteAddr: remoteAddr,
|
return conn, nil
|
||||||
|
},
|
||||||
|
ReadBufferSize: 4 * 1024,
|
||||||
|
WriteBufferSize: 4 * 1024,
|
||||||
|
HandshakeTimeout: time.Second * 8,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
scheme := "ws"
|
||||||
|
if c.tls {
|
||||||
|
scheme = "wss"
|
||||||
|
dialer.TLSClientConfig = c.tlsConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
host, port, err := net.SplitHostPort(c.host)
|
||||||
|
if (scheme == "ws" && port != "80") || (scheme == "wss" && port != "443") {
|
||||||
|
host = c.host
|
||||||
|
}
|
||||||
|
|
||||||
|
uri := url.URL{
|
||||||
|
Scheme: scheme,
|
||||||
|
Host: host,
|
||||||
|
Path: c.path,
|
||||||
|
}
|
||||||
|
|
||||||
|
wsConn, resp, err := dialer.Dial(uri.String(), nil)
|
||||||
|
if err != nil {
|
||||||
|
var reason string
|
||||||
|
if resp != nil {
|
||||||
|
reason = resp.Status
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("Dial %s error: %s", host, reason)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &websocketConn{
|
||||||
|
conn: wsConn,
|
||||||
|
remoteAddr: conn.RemoteAddr(),
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user