Commit 06ed63e2 authored by David Fifield's avatar David Fifield
Browse files

Add a DialOrWithDialer function.

The intention of this is to permit setting a source address to avoid
ephemeral port exhaustion on servers with many connections.

https://bugs.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/40198#note_2839304
https://bugs.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/40201#note_2839433
parent 7075f159
Loading
Loading
Loading
Loading
+20 −7
Original line number Diff line number Diff line
@@ -1000,20 +1000,20 @@ func extOrPortSetup(s net.Conn, timeout time.Duration,
	return nil
}

// Dial info.ExtendedOrAddr if defined, or else info.OrAddr, and return an open
// *net.TCPConn. If connecting to the extended OR port, extended OR port
// authentication à la 217-ext-orport-auth.txt is done before returning; an
// error is returned if authentication fails.
// Dial (using the given net.Dialer) info.ExtendedOrAddr if defined, or else
// info.OrAddr, and return an open net.Conn. If connecting to the extended
// OR port, extended OR port authentication à la 217-ext-orport-auth.txt is done
// before returning; an error is returned if authentication fails.
//
// The addr and methodName arguments are put in USERADDR and TRANSPORT ExtOrPort
// commands, respectively. If either is "", the corresponding command is not
// sent.
func DialOr(info *ServerInfo, addr, methodName string) (*net.TCPConn, error) {
func DialOrWithDialer(dialer *net.Dialer, info *ServerInfo, addr, methodName string) (net.Conn, error) {
	if info.ExtendedOrAddr == nil || info.AuthCookiePath == "" {
		return net.DialTCP("tcp", nil, info.OrAddr)
		return dialer.Dial("tcp", info.OrAddr.String())
	}

	s, err := net.DialTCP("tcp", nil, info.ExtendedOrAddr)
	s, err := dialer.Dial("tcp", info.ExtendedOrAddr.String())
	if err != nil {
		return nil, err
	}
@@ -1025,3 +1025,16 @@ func DialOr(info *ServerInfo, addr, methodName string) (*net.TCPConn, error) {

	return s, nil
}

// Dial info.ExtendedOrAddr if defined, or else info.OrAddr, and return an open
// *net.TCPConn. If connecting to the extended OR port, extended OR port
// authentication à la 217-ext-orport-auth.txt is done before returning; an
// error is returned if authentication fails.
//
// The addr and methodName arguments are put in USERADDR and TRANSPORT ExtOrPort
// commands, respectively. If either is "", the corresponding command is not
// sent.
func DialOr(info *ServerInfo, addr, methodName string) (*net.TCPConn, error) {
	c, err := DialOrWithDialer(&net.Dialer{}, info, addr, methodName)
	return c.(*net.TCPConn), err
}