dial.go 1.1 KB

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