dial.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // +build !windows
  2. // +build !wasm
  3. package domainsocket
  4. import (
  5. "context"
  6. "v2ray.com/core/common"
  7. "v2ray.com/core/common/net"
  8. "v2ray.com/core/transport/internet"
  9. "v2ray.com/core/transport/internet/tls"
  10. )
  11. func getSettingsFromContext(ctx context.Context) *Config {
  12. rawSettings := internet.StreamSettingsFromContext(ctx)
  13. if rawSettings == nil {
  14. return nil
  15. }
  16. return rawSettings.ProtocolSettings.(*Config)
  17. }
  18. func Dial(ctx context.Context, dest net.Destination) (internet.Connection, error) {
  19. settings := getSettingsFromContext(ctx)
  20. if settings == nil {
  21. return nil, newError("domain socket settings is not specified.").AtError()
  22. }
  23. addr, err := settings.GetUnixAddr()
  24. if err != nil {
  25. return nil, err
  26. }
  27. conn, err := net.DialUnix("unix", nil, addr)
  28. if err != nil {
  29. return nil, newError("failed to dial unix: ", settings.Path).Base(err).AtWarning()
  30. }
  31. if config := tls.ConfigFromContext(ctx); config != nil {
  32. return tls.Client(conn, config.GetTLSConfig(tls.WithDestination(dest))), nil
  33. }
  34. return conn, nil
  35. }
  36. func init() {
  37. common.Must(internet.RegisterTransportDialer(protocolName, Dial))
  38. }