Feature: refactor vmess & add http network
This commit is contained in:
@ -10,11 +10,14 @@ import (
|
||||
|
||||
// Option is options of websocket obfs
|
||||
type Option struct {
|
||||
Host string
|
||||
Path string
|
||||
Headers map[string]string
|
||||
TLSConfig *tls.Config
|
||||
Mux bool
|
||||
Host string
|
||||
Port string
|
||||
Path string
|
||||
Headers map[string]string
|
||||
TLS bool
|
||||
SkipCertVerify bool
|
||||
SessionCache tls.ClientSessionCache
|
||||
Mux bool
|
||||
}
|
||||
|
||||
// NewV2rayObfs return a HTTPObfs
|
||||
@ -25,15 +28,17 @@ func NewV2rayObfs(conn net.Conn, option *Option) (net.Conn, error) {
|
||||
}
|
||||
|
||||
config := &vmess.WebsocketConfig{
|
||||
Host: option.Host,
|
||||
Path: option.Path,
|
||||
TLS: option.TLSConfig != nil,
|
||||
Headers: header,
|
||||
TLSConfig: option.TLSConfig,
|
||||
Host: option.Host,
|
||||
Port: option.Port,
|
||||
Path: option.Path,
|
||||
TLS: option.TLS,
|
||||
Headers: header,
|
||||
SkipCertVerify: option.SkipCertVerify,
|
||||
SessionCache: option.SessionCache,
|
||||
}
|
||||
|
||||
var err error
|
||||
conn, err = vmess.NewWebsocketConn(conn, config)
|
||||
conn, err = vmess.StreamWebsocketConn(conn, config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
76
component/vmess/http.go
Normal file
76
component/vmess/http.go
Normal file
@ -0,0 +1,76 @@
|
||||
package vmess
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/textproto"
|
||||
)
|
||||
|
||||
type httpConn struct {
|
||||
net.Conn
|
||||
cfg *HTTPConfig
|
||||
rhandshake bool
|
||||
whandshake bool
|
||||
}
|
||||
|
||||
type HTTPConfig struct {
|
||||
Method string
|
||||
Host string
|
||||
Path []string
|
||||
Headers map[string][]string
|
||||
}
|
||||
|
||||
// Read implements net.Conn.Read()
|
||||
func (hc *httpConn) Read(b []byte) (int, error) {
|
||||
if hc.rhandshake {
|
||||
n, err := hc.Conn.Read(b)
|
||||
return n, err
|
||||
}
|
||||
|
||||
reader := textproto.NewConn(hc.Conn)
|
||||
// First line: GET /index.html HTTP/1.0
|
||||
if _, err := reader.ReadLine(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if _, err := reader.ReadMIMEHeader(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
hc.rhandshake = true
|
||||
return hc.Conn.Read(b)
|
||||
}
|
||||
|
||||
// Write implements io.Writer.
|
||||
func (hc *httpConn) Write(b []byte) (int, error) {
|
||||
if hc.whandshake {
|
||||
return hc.Conn.Write(b)
|
||||
}
|
||||
|
||||
path := hc.cfg.Path[rand.Intn(len(hc.cfg.Path))]
|
||||
u := fmt.Sprintf("http://%s%s", hc.cfg.Host, path)
|
||||
req, _ := http.NewRequest("GET", u, bytes.NewBuffer(b))
|
||||
for key, list := range hc.cfg.Headers {
|
||||
req.Header.Set(key, list[rand.Intn(len(list))])
|
||||
}
|
||||
req.ContentLength = int64(len(b))
|
||||
if err := req.Write(hc.Conn); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
hc.whandshake = true
|
||||
return len(b), nil
|
||||
}
|
||||
|
||||
func (hc *httpConn) Close() error {
|
||||
return hc.Conn.Close()
|
||||
}
|
||||
|
||||
func StreamHTTPConn(conn net.Conn, cfg *HTTPConfig) net.Conn {
|
||||
return &httpConn{
|
||||
Conn: conn,
|
||||
cfg: cfg,
|
||||
}
|
||||
}
|
@ -5,7 +5,6 @@ import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net"
|
||||
"net/http"
|
||||
"runtime"
|
||||
"sync"
|
||||
|
||||
@ -66,42 +65,23 @@ type DstAddr struct {
|
||||
|
||||
// Client is vmess connection generator
|
||||
type Client struct {
|
||||
user []*ID
|
||||
uuid *uuid.UUID
|
||||
security Security
|
||||
tls bool
|
||||
host string
|
||||
wsConfig *WebsocketConfig
|
||||
tlsConfig *tls.Config
|
||||
user []*ID
|
||||
uuid *uuid.UUID
|
||||
security Security
|
||||
}
|
||||
|
||||
// Config of vmess
|
||||
type Config struct {
|
||||
UUID string
|
||||
AlterID uint16
|
||||
Security string
|
||||
TLS bool
|
||||
HostName string
|
||||
Port string
|
||||
NetWork string
|
||||
WebSocketPath string
|
||||
WebSocketHeaders map[string]string
|
||||
SkipCertVerify bool
|
||||
SessionCache tls.ClientSessionCache
|
||||
UUID string
|
||||
AlterID uint16
|
||||
Security string
|
||||
Port string
|
||||
HostName string
|
||||
}
|
||||
|
||||
// New return a Conn with net.Conn and DstAddr
|
||||
func (c *Client) New(conn net.Conn, dst *DstAddr) (net.Conn, error) {
|
||||
var err error
|
||||
// StreamConn return a Conn with net.Conn and DstAddr
|
||||
func (c *Client) StreamConn(conn net.Conn, dst *DstAddr) (net.Conn, error) {
|
||||
r := rand.Intn(len(c.user))
|
||||
if c.wsConfig != nil {
|
||||
conn, err = NewWebsocketConn(conn, c.wsConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else if c.tls {
|
||||
conn = tls.Client(conn, c.tlsConfig)
|
||||
}
|
||||
return newConn(conn, c.user[r], dst, c.security)
|
||||
}
|
||||
|
||||
@ -129,57 +109,9 @@ func NewClient(config Config) (*Client, error) {
|
||||
return nil, fmt.Errorf("Unknown security type: %s", config.Security)
|
||||
}
|
||||
|
||||
if config.NetWork != "" && config.NetWork != "ws" {
|
||||
return nil, fmt.Errorf("Unknown network type: %s", config.NetWork)
|
||||
}
|
||||
|
||||
header := http.Header{}
|
||||
for k, v := range config.WebSocketHeaders {
|
||||
header.Add(k, v)
|
||||
}
|
||||
|
||||
host := net.JoinHostPort(config.HostName, config.Port)
|
||||
|
||||
var tlsConfig *tls.Config
|
||||
if config.TLS {
|
||||
tlsConfig = &tls.Config{
|
||||
ServerName: config.HostName,
|
||||
InsecureSkipVerify: config.SkipCertVerify,
|
||||
ClientSessionCache: config.SessionCache,
|
||||
}
|
||||
if tlsConfig.ClientSessionCache == nil {
|
||||
tlsConfig.ClientSessionCache = getClientSessionCache()
|
||||
}
|
||||
if host := header.Get("Host"); host != "" {
|
||||
tlsConfig.ServerName = host
|
||||
}
|
||||
}
|
||||
|
||||
var wsConfig *WebsocketConfig
|
||||
if config.NetWork == "ws" {
|
||||
wsConfig = &WebsocketConfig{
|
||||
Host: host,
|
||||
Path: config.WebSocketPath,
|
||||
Headers: header,
|
||||
TLS: config.TLS,
|
||||
TLSConfig: tlsConfig,
|
||||
}
|
||||
}
|
||||
|
||||
return &Client{
|
||||
user: newAlterIDs(newID(&uid), config.AlterID),
|
||||
uuid: &uid,
|
||||
security: security,
|
||||
tls: config.TLS,
|
||||
host: host,
|
||||
wsConfig: wsConfig,
|
||||
tlsConfig: tlsConfig,
|
||||
user: newAlterIDs(newID(&uid), config.AlterID),
|
||||
uuid: &uid,
|
||||
security: security,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func getClientSessionCache() tls.ClientSessionCache {
|
||||
once.Do(func() {
|
||||
clientSessionCache = tls.NewLRUClientSessionCache(128)
|
||||
})
|
||||
return clientSessionCache
|
||||
}
|
||||
|
@ -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{
|
||||
|
Reference in New Issue
Block a user