-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ECH support for QUIC based protocols
- Loading branch information
1 parent
42df2ef
commit f913090
Showing
19 changed files
with
265 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package qtls | ||
|
||
import ( | ||
"context" | ||
"net" | ||
|
||
"github.com/sagernet/quic-go" | ||
aTLS "github.com/sagernet/sing/common/tls" | ||
) | ||
|
||
type QUICConfig interface { | ||
Dial(ctx context.Context, conn net.PacketConn, addr net.Addr, config *quic.Config) (quic.Connection, error) | ||
DialEarly(ctx context.Context, conn net.PacketConn, addr net.Addr, config *quic.Config) (quic.EarlyConnection, error) | ||
} | ||
|
||
type QUICServerConfig interface { | ||
Listen(conn net.PacketConn, config *quic.Config) (QUICListener, error) | ||
ListenEarly(conn net.PacketConn, config *quic.Config) (QUICEarlyListener, error) | ||
} | ||
|
||
type QUICListener interface { | ||
Accept(ctx context.Context) (quic.Connection, error) | ||
Close() error | ||
Addr() net.Addr | ||
} | ||
|
||
type QUICEarlyListener interface { | ||
Accept(ctx context.Context) (quic.EarlyConnection, error) | ||
Close() error | ||
Addr() net.Addr | ||
} | ||
|
||
func Dial(ctx context.Context, conn net.PacketConn, addr net.Addr, config aTLS.Config, quicConfig *quic.Config) (quic.Connection, error) { | ||
if quicTLSConfig, isQUICConfig := config.(QUICConfig); isQUICConfig { | ||
return quicTLSConfig.Dial(ctx, conn, addr, quicConfig) | ||
} | ||
tlsConfig, err := config.Config() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return quic.Dial(ctx, conn, addr, tlsConfig, quicConfig) | ||
} | ||
|
||
func DialEarly(ctx context.Context, conn net.PacketConn, addr net.Addr, config aTLS.Config, quicConfig *quic.Config) (quic.EarlyConnection, error) { | ||
if quicTLSConfig, isQUICConfig := config.(QUICConfig); isQUICConfig { | ||
return quicTLSConfig.DialEarly(ctx, conn, addr, quicConfig) | ||
} | ||
tlsConfig, err := config.Config() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return quic.DialEarly(ctx, conn, addr, tlsConfig, quicConfig) | ||
} | ||
|
||
func Listen(conn net.PacketConn, config aTLS.ServerConfig, quicConfig *quic.Config) (QUICListener, error) { | ||
if quicTLSConfig, isQUICConfig := config.(QUICServerConfig); isQUICConfig { | ||
return quicTLSConfig.Listen(conn, quicConfig) | ||
} | ||
tlsConfig, err := config.Config() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return quic.Listen(conn, tlsConfig, quicConfig) | ||
} | ||
|
||
func ListenEarly(conn net.PacketConn, config aTLS.ServerConfig, quicConfig *quic.Config) (QUICEarlyListener, error) { | ||
if quicTLSConfig, isQUICConfig := config.(QUICServerConfig); isQUICConfig { | ||
return quicTLSConfig.ListenEarly(conn, quicConfig) | ||
} | ||
tlsConfig, err := config.Config() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return quic.ListenEarly(conn, tlsConfig, quicConfig) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
//go:build with_quic && with_ech | ||
|
||
package tls | ||
|
||
import ( | ||
"context" | ||
"net" | ||
|
||
"github.com/sagernet/quic-go/ech" | ||
"github.com/sagernet/sing-box/common/qtls" | ||
) | ||
|
||
var ( | ||
_ qtls.QUICConfig = (*echClientConfig)(nil) | ||
_ qtls.QUICServerConfig = (*echServerConfig)(nil) | ||
) | ||
|
||
func (c *echClientConfig) Dial(ctx context.Context, conn net.PacketConn, addr net.Addr, config *quic.Config) (quic.Connection, error) { | ||
return quic.Dial(ctx, conn, addr, c.config, config) | ||
} | ||
|
||
func (c *echClientConfig) DialEarly(ctx context.Context, conn net.PacketConn, addr net.Addr, config *quic.Config) (quic.EarlyConnection, error) { | ||
return quic.DialEarly(ctx, conn, addr, c.config, config) | ||
} | ||
|
||
func (c *echServerConfig) Listen(conn net.PacketConn, config *quic.Config) (qtls.QUICListener, error) { | ||
return quic.Listen(conn, c.config, config) | ||
} | ||
|
||
func (c *echServerConfig) ListenEarly(conn net.PacketConn, config *quic.Config) (qtls.QUICEarlyListener, error) { | ||
return quic.ListenEarly(conn, c.config, config) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.