func init() { common.Must(internet.RegisterTransportDialer(internet.TransportProtocol_UDP, func(ctx context.Context, dest v2net.Destination) (internet.Connection, error) { src := internet.DialerSourceFromContext(ctx) conn, err := internet.DialSystem(src, dest) if err != nil { return nil, err } // TODO: handle dialer options return internal.NewConnection(internal.NewConnectionID(src, dest), conn, internal.NoOpConnectionRecyler{}, internal.ReuseConnection(false)), nil })) }
func (v *WSListener) Accept() (internet.Connection, error) { for v.acccepting { select { case connErr, open := <-v.awaitingConns: if !open { return nil, ErrClosedListener } if connErr.err != nil { return nil, connErr.err } return internal.NewConnection(internal.ConnectionID{}, connErr.conn.(*wsconn), v, internal.ReuseConnection(v.config.IsConnectionReuse())), nil case <-time.After(time.Second * 2): } } return nil, ErrClosedListener }
func Dial(ctx context.Context, dest v2net.Destination) (internet.Connection, error) { log.Info("Internet|TCP: Dailing TCP to ", dest) src := internet.DialerSourceFromContext(ctx) tcpSettings := internet.TransportSettingsFromContext(ctx).(*Config) id := internal.NewConnectionID(src, dest) var conn net.Conn if dest.Network == v2net.Network_TCP && tcpSettings.IsConnectionReuse() { conn = globalCache.Get(id) } if conn == nil { var err error conn, err = internet.DialSystem(src, dest) if err != nil { return nil, err } if securitySettings := internet.SecuritySettingsFromContext(ctx); securitySettings != nil { tlsConfig, ok := securitySettings.(*v2tls.Config) if ok { config := tlsConfig.GetTLSConfig() if dest.Address.Family().IsDomain() { config.ServerName = dest.Address.Domain() } conn = tls.Client(conn, config) } } if tcpSettings.HeaderSettings != nil { headerConfig, err := tcpSettings.HeaderSettings.GetInstance() if err != nil { return nil, errors.Base(err).Message("Interent|TCP: Failed to get header settings.") } auth, err := internet.CreateConnectionAuthenticator(headerConfig) if err != nil { return nil, errors.Base(err).Message("Internet|TCP: Failed to create header authenticator.") } conn = auth.Client(conn) } } return internal.NewConnection(id, conn, globalCache, internal.ReuseConnection(tcpSettings.IsConnectionReuse())), nil }
func Dial(ctx context.Context, dest v2net.Destination) (internet.Connection, error) { log.Info("WebSocket|Dialer: Creating connection to ", dest) src := internet.DialerSourceFromContext(ctx) wsSettings := internet.TransportSettingsFromContext(ctx).(*Config) id := internal.NewConnectionID(src, dest) var conn *wsconn if dest.Network == v2net.Network_TCP && wsSettings.IsConnectionReuse() { connt := globalCache.Get(id) if connt != nil { conn = connt.(*wsconn) } } if conn == nil { var err error conn, err = wsDial(ctx, dest) if err != nil { log.Warning("WebSocket|Dialer: Dial failed: ", err) return nil, err } } return internal.NewConnection(id, conn, globalCache, internal.ReuseConnection(wsSettings.IsConnectionReuse())), nil }