After the week I've had, I deserve to make a commit like this lmao
This commit is contained in:
50
dial.go
50
dial.go
@@ -1,7 +1,9 @@
|
||||
package hopp
|
||||
|
||||
import "net"
|
||||
import "context"
|
||||
import "crypto/tls"
|
||||
import "github.com/quic-go/quic-go"
|
||||
|
||||
// TODO: dial should be super simple like it is now, and there should be a
|
||||
// "dialer" which the dial function dial configures automaticaly, but the dialer
|
||||
@@ -29,24 +31,52 @@ func (diale Dialer) Dial(ctx context.Context, network, address string) (Conn, er
|
||||
}
|
||||
|
||||
func (diale Dialer) dialQUIC(ctx context.Context, network, address string) (Conn, error) {
|
||||
// TODO: dial a QUIC connection and return METADAPT-B wrapping it
|
||||
udpNetwork, err := quicNetworkToUDPNetwork(network)
|
||||
if err != nil { return nil, err }
|
||||
addr, err := net.ResolveUDPAddr(udpNetwork, address)
|
||||
if err != nil { return nil, err }
|
||||
udpConn, err := net.DialUDP(udpNetwork, nil, addr)
|
||||
if err != nil { return nil, err }
|
||||
conn, err := quic.Dial(ctx, udpConn, addr, diale.tlsConfig(), diale.quicConfig())
|
||||
if err != nil { return nil, err }
|
||||
return AdaptB(quicMultiConn { underlying: conn }), nil
|
||||
}
|
||||
|
||||
func (diale Dialer) dialUnix(ctx context.Context, network, address string) (Conn, error) {
|
||||
if network != "unix" { return nil, ErrUnknownNetwork }
|
||||
// TODO: dial a unix stream connection and return METADAPT-A wrapping it
|
||||
addr, err := net.ResolveUnixAddr(network, address)
|
||||
if err != nil { return nil, err }
|
||||
conn, err := net.DialUnix(network, nil, addr)
|
||||
if err != nil { return nil, err }
|
||||
// REMEMBER - THIS IS VERY IMPORTANT:
|
||||
// WHEN YOU INEVITABLY COPY PASTE THIS FOR THE SERVER-SIDE, CHANGE THE
|
||||
// PARTY CONSTANT TO ServerSide! OTHERWISE THERE WILL BE COLLISIONS!
|
||||
return AdaptA(conn, ClientSide), nil
|
||||
}
|
||||
|
||||
// addrStrs implements net.Addr
|
||||
type addrStrs struct {
|
||||
net string
|
||||
addr string
|
||||
func (diale Dialer) tlsConfig() *tls.Config {
|
||||
conf := diale.TLSConfig.Clone()
|
||||
conf.NextProtos = []string {
|
||||
"HOPP/0",
|
||||
}
|
||||
return conf
|
||||
}
|
||||
|
||||
func (addr addrStrs) Network() string {
|
||||
return addr.net
|
||||
func (diale Dialer) quicConfig() *quic.Config {
|
||||
return &quic.Config {
|
||||
// TODO: perhaps we might want to put something here
|
||||
// the quic config shouldn't be exported, just set up
|
||||
// automatically. we can't have that strangely built quic-go
|
||||
// package be part of the API, or any third-party packages for
|
||||
// that matter. it must all be abstracted away.
|
||||
}
|
||||
}
|
||||
|
||||
func (addr addrStrs) String() string {
|
||||
return addr.addr
|
||||
func quicNetworkToUDPNetwork(network string) (string, error) {
|
||||
switch network {
|
||||
case "quic4": return "udp4", nil
|
||||
case "quic6": return "udp6", nil
|
||||
case "quic": return "udp", nil
|
||||
default: return "", ErrUnknownNetwork
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user