2018-04-12 11:44:23 -04:00
|
|
|
// +build !windows
|
|
|
|
|
2017-11-03 08:02:05 -04:00
|
|
|
package domainsocket
|
|
|
|
|
2017-11-22 08:53:22 -05:00
|
|
|
import (
|
|
|
|
"context"
|
2018-04-09 11:09:24 -04:00
|
|
|
|
|
|
|
"v2ray.com/core/common"
|
|
|
|
"v2ray.com/core/common/net"
|
|
|
|
"v2ray.com/core/transport/internet"
|
|
|
|
"v2ray.com/core/transport/internet/tls"
|
2017-11-22 08:53:22 -05:00
|
|
|
)
|
2017-11-03 08:02:05 -04:00
|
|
|
|
2018-04-09 11:09:24 -04:00
|
|
|
func getSettingsFromContext(ctx context.Context) *Config {
|
|
|
|
rawSettings := internet.TransportSettingsFromContext(ctx)
|
|
|
|
if rawSettings == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return rawSettings.(*Config)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Dial(ctx context.Context, dest net.Destination) (internet.Connection, error) {
|
|
|
|
settings := getSettingsFromContext(ctx)
|
|
|
|
if settings == nil {
|
|
|
|
return nil, newError("domain socket settings is not specified.").AtError()
|
|
|
|
}
|
|
|
|
|
|
|
|
addr, err := settings.GetUnixAddr()
|
2017-11-22 08:53:22 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-09 11:09:24 -04:00
|
|
|
|
|
|
|
conn, err := net.DialUnix("unix", nil, addr)
|
2017-11-22 08:53:22 -05:00
|
|
|
if err != nil {
|
2018-04-09 11:09:24 -04:00
|
|
|
return nil, newError("failed to dial unix: ", settings.Path).Base(err).AtWarning()
|
|
|
|
}
|
|
|
|
|
|
|
|
if config := tls.ConfigFromContext(ctx); config != nil {
|
|
|
|
return tls.Client(conn, config.GetTLSConfig(tls.WithDestination(dest))), nil
|
2017-11-22 08:53:22 -05:00
|
|
|
}
|
2018-04-09 11:09:24 -04:00
|
|
|
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
common.Must(internet.RegisterTransportDialer(internet.TransportProtocol_DomainSocket, Dial))
|
2017-11-03 08:02:05 -04:00
|
|
|
}
|