mihomo/transport/v2ray-plugin/websocket.go

76 lines
1.7 KiB
Go
Raw Normal View History

2019-02-11 15:25:10 +08:00
package obfs
import (
"context"
2021-10-16 20:19:59 +08:00
"crypto/tls"
2019-02-11 15:25:10 +08:00
"net"
"net/http"
2019-02-11 15:25:10 +08:00
2023-11-03 21:01:45 +08:00
"github.com/metacubex/mihomo/component/ca"
"github.com/metacubex/mihomo/transport/vmess"
2019-02-11 15:25:10 +08:00
)
// Option is options of websocket obfs
type Option struct {
Host string
Port string
Path string
Headers map[string]string
TLS bool
SkipCertVerify bool
Fingerprint string
Mux bool
V2rayHttpUpgrade bool
V2rayHttpUpgradeFastOpen bool
2019-02-11 15:25:10 +08:00
}
// NewV2rayObfs return a HTTPObfs
func NewV2rayObfs(ctx context.Context, conn net.Conn, option *Option) (net.Conn, error) {
header := http.Header{}
for k, v := range option.Headers {
header.Add(k, v)
}
2019-02-11 15:25:10 +08:00
config := &vmess.WebsocketConfig{
Host: option.Host,
Port: option.Port,
Path: option.Path,
V2rayHttpUpgrade: option.V2rayHttpUpgrade,
V2rayHttpUpgradeFastOpen: option.V2rayHttpUpgradeFastOpen,
Headers: header,
2021-10-16 20:19:59 +08:00
}
if option.TLS {
config.TLS = true
2022-07-11 13:42:28 +08:00
tlsConfig := &tls.Config{
2021-10-16 20:19:59 +08:00
ServerName: option.Host,
InsecureSkipVerify: option.SkipCertVerify,
NextProtos: []string{"http/1.1"},
}
2023-09-22 14:45:34 +08:00
var err error
config.TLSConfig, err = ca.GetSpecifiedFingerprintTLSConfig(tlsConfig, option.Fingerprint)
if err != nil {
return nil, err
2022-07-11 13:42:28 +08:00
}
2021-10-16 20:19:59 +08:00
if host := config.Headers.Get("Host"); host != "" {
config.TLSConfig.ServerName = host
}
2019-02-11 15:25:10 +08:00
}
var err error
conn, err = vmess.StreamWebsocketConn(ctx, conn, config)
2019-02-11 15:25:10 +08:00
if err != nil {
return nil, err
}
if option.Mux {
conn = NewMux(conn, MuxOption{
ID: [2]byte{0, 0},
Host: "127.0.0.1",
Port: 0,
})
}
2019-02-11 15:25:10 +08:00
return conn, nil
}