2023-11-18 03:27:17 +00:00
|
|
|
package wireguard
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-06-29 18:32:57 +00:00
|
|
|
goerrors "errors"
|
2023-11-18 03:27:17 +00:00
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/xtls/xray-core/common"
|
|
|
|
"github.com/xtls/xray-core/common/buf"
|
2024-06-29 18:32:57 +00:00
|
|
|
"github.com/xtls/xray-core/common/errors"
|
2023-11-18 03:27:17 +00:00
|
|
|
"github.com/xtls/xray-core/common/log"
|
|
|
|
"github.com/xtls/xray-core/common/net"
|
|
|
|
"github.com/xtls/xray-core/common/session"
|
|
|
|
"github.com/xtls/xray-core/common/signal"
|
|
|
|
"github.com/xtls/xray-core/common/task"
|
|
|
|
"github.com/xtls/xray-core/core"
|
|
|
|
"github.com/xtls/xray-core/features/dns"
|
|
|
|
"github.com/xtls/xray-core/features/policy"
|
|
|
|
"github.com/xtls/xray-core/features/routing"
|
|
|
|
"github.com/xtls/xray-core/transport/internet/stat"
|
|
|
|
)
|
|
|
|
|
|
|
|
var nullDestination = net.TCPDestination(net.AnyIP, 0)
|
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
bindServer *netBindServer
|
|
|
|
|
|
|
|
info routingInfo
|
|
|
|
policyManager policy.Manager
|
|
|
|
}
|
|
|
|
|
|
|
|
type routingInfo struct {
|
|
|
|
ctx context.Context
|
|
|
|
dispatcher routing.Dispatcher
|
|
|
|
inboundTag *session.Inbound
|
|
|
|
outboundTag *session.Outbound
|
|
|
|
contentTag *session.Content
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewServer(ctx context.Context, conf *DeviceConfig) (*Server, error) {
|
|
|
|
v := core.MustFromContext(ctx)
|
|
|
|
|
|
|
|
endpoints, hasIPv4, hasIPv6, err := parseEndpoints(conf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
server := &Server{
|
|
|
|
bindServer: &netBindServer{
|
|
|
|
netBind: netBind{
|
|
|
|
dns: v.GetFeature(dns.ClientType()).(dns.Client),
|
|
|
|
dnsOption: dns.IPOption{
|
|
|
|
IPv4Enable: hasIPv4,
|
|
|
|
IPv6Enable: hasIPv6,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
policyManager: v.GetFeature(policy.ManagerType()).(policy.Manager),
|
|
|
|
}
|
|
|
|
|
|
|
|
tun, err := conf.createTun()(endpoints, int(conf.Mtu), server.forwardConnection)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = tun.BuildDevice(createIPCRequest(conf), server.bindServer); err != nil {
|
|
|
|
_ = tun.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return server, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Network implements proxy.Inbound.
|
|
|
|
func (*Server) Network() []net.Network {
|
|
|
|
return []net.Network{net.Network_UDP}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process implements proxy.Inbound.
|
|
|
|
func (s *Server) Process(ctx context.Context, network net.Network, conn stat.Connection, dispatcher routing.Dispatcher) error {
|
2023-11-18 01:10:46 +00:00
|
|
|
inbound := session.InboundFromContext(ctx)
|
|
|
|
inbound.Name = "wireguard"
|
2024-05-14 01:52:24 +00:00
|
|
|
inbound.CanSpliceCopy = 3
|
|
|
|
outbounds := session.OutboundsFromContext(ctx)
|
2024-06-29 18:32:57 +00:00
|
|
|
ob := outbounds[len(outbounds)-1]
|
2023-11-18 01:10:46 +00:00
|
|
|
|
2023-11-18 03:27:17 +00:00
|
|
|
s.info = routingInfo{
|
|
|
|
ctx: core.ToBackgroundDetachedContext(ctx),
|
|
|
|
dispatcher: dispatcher,
|
|
|
|
inboundTag: session.InboundFromContext(ctx),
|
2024-05-14 01:52:24 +00:00
|
|
|
outboundTag: ob,
|
2023-11-18 03:27:17 +00:00
|
|
|
contentTag: session.ContentFromContext(ctx),
|
|
|
|
}
|
|
|
|
|
|
|
|
ep, err := s.bindServer.ParseEndpoint(conn.RemoteAddr().String())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
nep := ep.(*netEndpoint)
|
|
|
|
nep.conn = conn
|
|
|
|
|
|
|
|
reader := buf.NewPacketReader(conn)
|
|
|
|
for {
|
|
|
|
mpayload, err := reader.ReadMultiBuffer()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, payload := range mpayload {
|
|
|
|
v, ok := <-s.bindServer.readQueue
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
i, err := payload.Read(v.buff)
|
|
|
|
|
|
|
|
v.bytes = i
|
|
|
|
v.endpoint = nep
|
|
|
|
v.err = err
|
|
|
|
v.waiter.Done()
|
2024-06-29 18:32:57 +00:00
|
|
|
if err != nil && goerrors.Is(err, io.EOF) {
|
2023-11-18 03:27:17 +00:00
|
|
|
nep.conn = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) forwardConnection(dest net.Destination, conn net.Conn) {
|
|
|
|
if s.info.dispatcher == nil {
|
2024-06-29 18:32:57 +00:00
|
|
|
errors.LogError(s.info.ctx, "unexpected: dispatcher == nil")
|
2023-11-18 03:27:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(core.ToBackgroundDetachedContext(s.info.ctx))
|
|
|
|
plcy := s.policyManager.ForLevel(0)
|
|
|
|
timer := signal.CancelAfterInactivity(ctx, cancel, plcy.Timeouts.ConnectionIdle)
|
|
|
|
|
|
|
|
ctx = log.ContextWithAccessMessage(ctx, &log.AccessMessage{
|
|
|
|
From: nullDestination,
|
|
|
|
To: dest,
|
|
|
|
Status: log.AccessAccepted,
|
|
|
|
Reason: "",
|
|
|
|
})
|
|
|
|
|
2024-12-06 14:10:50 +00:00
|
|
|
if s.info.inboundTag != nil {
|
|
|
|
ctx = session.ContextWithInbound(ctx, s.info.inboundTag)
|
|
|
|
}
|
|
|
|
|
2024-11-20 05:00:40 +00:00
|
|
|
// what's this?
|
|
|
|
// Session information should not be shared between different connections
|
|
|
|
// why reuse them in server level? This will cause incorrect destoverride and unexpected routing behavior.
|
|
|
|
// Disable it temporarily. Maybe s.info should be removed.
|
|
|
|
|
|
|
|
// if s.info.outboundTag != nil {
|
|
|
|
// ctx = session.ContextWithOutbounds(ctx, []*session.Outbound{s.info.outboundTag})
|
|
|
|
// }
|
|
|
|
// if s.info.contentTag != nil {
|
|
|
|
// ctx = session.ContextWithContent(ctx, s.info.contentTag)
|
|
|
|
// }
|
2023-11-18 03:27:17 +00:00
|
|
|
|
|
|
|
link, err := s.info.dispatcher.Dispatch(ctx, dest)
|
|
|
|
if err != nil {
|
2024-06-29 18:32:57 +00:00
|
|
|
errors.LogErrorInner(s.info.ctx, err, "dispatch connection")
|
2023-11-18 03:27:17 +00:00
|
|
|
}
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
requestDone := func() error {
|
|
|
|
defer timer.SetTimeout(plcy.Timeouts.DownlinkOnly)
|
|
|
|
if err := buf.Copy(buf.NewReader(conn), link.Writer, buf.UpdateActivity(timer)); err != nil {
|
2024-06-29 18:32:57 +00:00
|
|
|
return errors.New("failed to transport all TCP request").Base(err)
|
2023-11-18 03:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
responseDone := func() error {
|
|
|
|
defer timer.SetTimeout(plcy.Timeouts.UplinkOnly)
|
|
|
|
if err := buf.Copy(link.Reader, buf.NewWriter(conn), buf.UpdateActivity(timer)); err != nil {
|
2024-06-29 18:32:57 +00:00
|
|
|
return errors.New("failed to transport all TCP response").Base(err)
|
2023-11-18 03:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
requestDonePost := task.OnSuccess(requestDone, task.Close(link.Writer))
|
|
|
|
if err := task.Run(ctx, requestDonePost, responseDone); err != nil {
|
|
|
|
common.Interrupt(link.Reader)
|
|
|
|
common.Interrupt(link.Writer)
|
2024-06-29 18:32:57 +00:00
|
|
|
errors.LogDebugInner(s.info.ctx, err, "connection ends")
|
2023-11-18 03:27:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|