-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ec0fa47
commit e033ed3
Showing
34 changed files
with
4,182 additions
and
49 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
//go:build with_quic | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
package inbound | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"time" | ||
|
||
"github.com/sagernet/sing-box/adapter" | ||
"github.com/sagernet/sing-box/common/tls" | ||
C "github.com/sagernet/sing-box/constant" | ||
"github.com/sagernet/sing-box/log" | ||
"github.com/sagernet/sing-box/option" | ||
"github.com/sagernet/sing-box/transport/tuic" | ||
"github.com/sagernet/sing/common" | ||
"github.com/sagernet/sing/common/auth" | ||
E "github.com/sagernet/sing/common/exceptions" | ||
N "github.com/sagernet/sing/common/network" | ||
|
||
"github.com/gofrs/uuid/v5" | ||
) | ||
|
||
var _ adapter.Inbound = (*TUIC)(nil) | ||
|
||
type TUIC struct { | ||
myInboundAdapter | ||
server *tuic.Server | ||
tlsConfig tls.ServerConfig | ||
} | ||
|
||
func NewTUIC(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.TUICInboundOptions) (*TUIC, error) { | ||
options.UDPFragmentDefault = true | ||
if options.TLS == nil || !options.TLS.Enabled { | ||
return nil, C.ErrTLSRequired | ||
} | ||
tlsConfig, err := tls.NewServer(ctx, router, logger, common.PtrValueOrDefault(options.TLS)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
rawConfig, err := tlsConfig.Config() | ||
if err != nil { | ||
return nil, err | ||
} | ||
var users []tuic.User | ||
for index, user := range options.Users { | ||
if user.UUID == "" { | ||
return nil, E.New("missing uuid for user ", index) | ||
} | ||
userUUID, err := uuid.FromString(user.UUID) | ||
if err != nil { | ||
return nil, E.Cause(err, "invalid uuid for user ", index) | ||
} | ||
users = append(users, tuic.User{Name: user.Name, UUID: userUUID, Password: user.Password}) | ||
} | ||
inbound := &TUIC{ | ||
myInboundAdapter: myInboundAdapter{ | ||
protocol: C.TypeTUIC, | ||
network: []string{N.NetworkUDP}, | ||
ctx: ctx, | ||
router: router, | ||
logger: logger, | ||
tag: tag, | ||
listenOptions: options.ListenOptions, | ||
}, | ||
} | ||
server, err := tuic.NewServer(tuic.ServerOptions{ | ||
Context: ctx, | ||
Logger: logger, | ||
TLSConfig: rawConfig, | ||
Users: users, | ||
CongestionControl: options.CongestionControl, | ||
AuthTimeout: time.Duration(options.AuthTimeout), | ||
ZeroRTTHandshake: options.ZeroRTTHandshake, | ||
Heartbeat: time.Duration(options.Heartbeat), | ||
Handler: adapter.NewUpstreamHandler(adapter.InboundContext{}, inbound.newConnection, inbound.newPacketConnection, nil), | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
inbound.server = server | ||
return inbound, nil | ||
} | ||
|
||
func (h *TUIC) newConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error { | ||
ctx = log.ContextWithNewID(ctx) | ||
h.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination) | ||
metadata = h.createMetadata(conn, metadata) | ||
metadata.User, _ = auth.UserFromContext[string](ctx) | ||
return h.router.RouteConnection(ctx, conn, metadata) | ||
} | ||
|
||
func (h *TUIC) newPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error { | ||
ctx = log.ContextWithNewID(ctx) | ||
metadata = h.createPacketMetadata(conn, metadata) | ||
metadata.User, _ = auth.UserFromContext[string](ctx) | ||
h.logger.InfoContext(ctx, "inbound packet connection to ", metadata.Destination) | ||
return h.router.RoutePacketConnection(ctx, conn, metadata) | ||
} | ||
|
||
func (h *TUIC) Start() error { | ||
packetConn, err := h.myInboundAdapter.ListenUDP() | ||
if err != nil { | ||
return err | ||
} | ||
return h.server.Start(packetConn) | ||
} | ||
|
||
func (h *TUIC) Close() error { | ||
return common.Close( | ||
&h.myInboundAdapter, | ||
common.PtrOrNil(h.server), | ||
) | ||
} |
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,16 @@ | ||
//go:build !with_quic | ||
|
||
package inbound | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/sagernet/sing-box/adapter" | ||
C "github.com/sagernet/sing-box/constant" | ||
"github.com/sagernet/sing-box/log" | ||
"github.com/sagernet/sing-box/option" | ||
) | ||
|
||
func NewTUIC(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.TUICInboundOptions) (adapter.Inbound, error) { | ||
return nil, C.ErrQUICNotIncluded | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package option | ||
|
||
type TUICInboundOptions struct { | ||
ListenOptions | ||
Users []TUICUser `json:"users,omitempty"` | ||
CongestionControl string `json:"congestion_control,omitempty"` | ||
AuthTimeout Duration `json:"auth_timeout,omitempty"` | ||
ZeroRTTHandshake bool `json:"zero_rtt_handshake,omitempty"` | ||
Heartbeat Duration `json:"heartbeat,omitempty"` | ||
TLS *InboundTLSOptions `json:"tls,omitempty"` | ||
} | ||
|
||
type TUICUser struct { | ||
Name string `json:"name,omitempty"` | ||
UUID string `json:"uuid,omitempty"` | ||
Password string `json:"password,omitempty"` | ||
} | ||
|
||
type TUICOutboundOptions struct { | ||
DialerOptions | ||
ServerOptions | ||
UUID string `json:"uuid,omitempty"` | ||
Password string `json:"password,omitempty"` | ||
CongestionControl string `json:"congestion_control,omitempty"` | ||
UDPRelayMode string `json:"udp_relay_mode,omitempty"` | ||
ZeroRTTHandshake bool `json:"zero_rtt_handshake,omitempty"` | ||
Heartbeat Duration `json:"heartbeat,omitempty"` | ||
Network NetworkList `json:"network,omitempty"` | ||
TLS *OutboundTLSOptions `json:"tls,omitempty"` | ||
} |
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.
with_tuic ?