2021-12-28 21:14:57 +08:00
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2022-01-20 20:03:07 +08:00
|
|
|
"context"
|
2021-12-28 21:14:57 +08:00
|
|
|
"net"
|
2022-01-20 20:03:07 +08:00
|
|
|
"net/url"
|
2021-12-28 21:14:57 +08:00
|
|
|
|
2022-01-20 20:03:07 +08:00
|
|
|
libdial "github.com/fatedier/golib/net/dial"
|
|
|
|
"golang.org/x/net/websocket"
|
|
|
|
)
|
2021-12-28 21:14:57 +08:00
|
|
|
|
2022-01-20 20:03:07 +08:00
|
|
|
func DialHookCustomTLSHeadByte(enableTLS bool, disableCustomTLSHeadByte bool) libdial.AfterHookFunc {
|
|
|
|
return func(ctx context.Context, c net.Conn, addr string) (context.Context, net.Conn, error) {
|
|
|
|
if enableTLS && !disableCustomTLSHeadByte {
|
|
|
|
_, err := c.Write([]byte{byte(FRPTLSHeadByte)})
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ctx, c, nil
|
2021-12-28 21:14:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-29 11:20:45 +08:00
|
|
|
func DialHookWebsocket(protocol string, host string) libdial.AfterHookFunc {
|
2022-01-20 20:03:07 +08:00
|
|
|
return func(ctx context.Context, c net.Conn, addr string) (context.Context, net.Conn, error) {
|
2023-06-29 11:20:45 +08:00
|
|
|
if protocol != "wss" {
|
|
|
|
protocol = "ws"
|
|
|
|
}
|
|
|
|
if host == "" {
|
|
|
|
host = addr
|
|
|
|
}
|
|
|
|
addr = protocol + "://" + host + FrpWebsocketPath
|
2022-01-20 20:03:07 +08:00
|
|
|
uri, err := url.Parse(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
origin := "http://" + uri.Host
|
|
|
|
cfg, err := websocket.NewConfig(addr, origin)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := websocket.NewClient(cfg, c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return ctx, conn, nil
|
2021-12-28 21:14:57 +08:00
|
|
|
}
|
|
|
|
}
|