dial.go 1017 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package net
  2. import (
  3. "context"
  4. "net"
  5. "net/url"
  6. libdial "github.com/fatedier/golib/net/dial"
  7. "golang.org/x/net/websocket"
  8. )
  9. func DialHookCustomTLSHeadByte(enableTLS bool, disableCustomTLSHeadByte bool) libdial.AfterHookFunc {
  10. return func(ctx context.Context, c net.Conn, addr string) (context.Context, net.Conn, error) {
  11. if enableTLS && !disableCustomTLSHeadByte {
  12. _, err := c.Write([]byte{byte(FRPTLSHeadByte)})
  13. if err != nil {
  14. return nil, nil, err
  15. }
  16. }
  17. return ctx, c, nil
  18. }
  19. }
  20. func DialHookWebsocket() libdial.AfterHookFunc {
  21. return func(ctx context.Context, c net.Conn, addr string) (context.Context, net.Conn, error) {
  22. addr = "ws://" + addr + FrpWebsocketPath
  23. uri, err := url.Parse(addr)
  24. if err != nil {
  25. return nil, nil, err
  26. }
  27. origin := "http://" + uri.Host
  28. cfg, err := websocket.NewConfig(addr, origin)
  29. if err != nil {
  30. return nil, nil, err
  31. }
  32. conn, err := websocket.NewClient(cfg, c)
  33. if err != nil {
  34. return nil, nil, err
  35. }
  36. return ctx, conn, nil
  37. }
  38. }