2022-11-22 04:05:54 +03:00
|
|
|
/*
|
|
|
|
|
|
|
|
Some of codes are copied from https://github.com/octeep/wireproxy, license below.
|
|
|
|
|
|
|
|
Copyright (c) 2022 Wind T.F. Wong <octeep@pm.me>
|
|
|
|
|
|
|
|
Permission to use, copy, modify, and distribute this software for any
|
|
|
|
purpose with or without fee is hereby granted, provided that the above
|
|
|
|
copyright notice and this permission notice appear in all copies.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
package wireguard
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2023-11-12 23:10:01 +03:00
|
|
|
stdnet "net"
|
2022-11-22 04:05:54 +03:00
|
|
|
"net/netip"
|
|
|
|
"strings"
|
2023-11-12 23:10:01 +03:00
|
|
|
"sync"
|
2022-11-22 04:05:54 +03:00
|
|
|
|
|
|
|
"github.com/xtls/xray-core/common"
|
|
|
|
"github.com/xtls/xray-core/common/buf"
|
|
|
|
"github.com/xtls/xray-core/common/log"
|
|
|
|
"github.com/xtls/xray-core/common/net"
|
|
|
|
"github.com/xtls/xray-core/common/protocol"
|
|
|
|
"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/transport"
|
|
|
|
"github.com/xtls/xray-core/transport/internet"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Handler is an outbound connection that silently swallow the entire payload.
|
|
|
|
type Handler struct {
|
|
|
|
conf *DeviceConfig
|
2023-11-12 23:10:01 +03:00
|
|
|
net Tunnel
|
2022-11-22 04:05:54 +03:00
|
|
|
bind *netBindClient
|
|
|
|
policyManager policy.Manager
|
|
|
|
dns dns.Client
|
|
|
|
// cached configuration
|
2023-11-12 23:10:01 +03:00
|
|
|
ipc string
|
|
|
|
endpoints []netip.Addr
|
|
|
|
hasIPv4, hasIPv6 bool
|
|
|
|
wgLock sync.Mutex
|
2022-11-22 04:05:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new wireguard handler.
|
|
|
|
func New(ctx context.Context, conf *DeviceConfig) (*Handler, error) {
|
|
|
|
v := core.MustFromContext(ctx)
|
|
|
|
|
|
|
|
endpoints, err := parseEndpoints(conf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-11-12 23:10:01 +03:00
|
|
|
hasIPv4, hasIPv6 := false, false
|
|
|
|
for _, e := range endpoints {
|
|
|
|
if e.Is4() {
|
|
|
|
hasIPv4 = true
|
|
|
|
}
|
|
|
|
if e.Is6() {
|
|
|
|
hasIPv6 = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d := v.GetFeature(dns.ClientType()).(dns.Client)
|
2022-11-22 04:05:54 +03:00
|
|
|
return &Handler{
|
|
|
|
conf: conf,
|
|
|
|
policyManager: v.GetFeature(policy.ManagerType()).(policy.Manager),
|
2023-11-12 23:10:01 +03:00
|
|
|
dns: d,
|
|
|
|
ipc: createIPCRequest(conf, d, hasIPv6),
|
2022-11-22 04:05:54 +03:00
|
|
|
endpoints: endpoints,
|
2023-11-12 23:10:01 +03:00
|
|
|
hasIPv4: hasIPv4,
|
|
|
|
hasIPv6: hasIPv6,
|
2022-11-22 04:05:54 +03:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-11-12 23:10:01 +03:00
|
|
|
func (h *Handler) processWireGuard(dialer internet.Dialer) (err error) {
|
|
|
|
h.wgLock.Lock()
|
|
|
|
defer h.wgLock.Unlock()
|
|
|
|
|
|
|
|
if h.bind != nil && h.bind.dialer == dialer && h.net != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Record(&log.GeneralMessage{
|
|
|
|
Severity: log.Severity_Info,
|
|
|
|
Content: "switching dialer",
|
|
|
|
})
|
|
|
|
|
|
|
|
if h.net != nil {
|
|
|
|
_ = h.net.Close()
|
|
|
|
h.net = nil
|
|
|
|
}
|
|
|
|
if h.bind != nil {
|
|
|
|
_ = h.bind.Close()
|
|
|
|
h.bind = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// bind := conn.NewStdNetBind() // TODO: conn.Bind wrapper for dialer
|
|
|
|
bind := &netBindClient{
|
|
|
|
dialer: dialer,
|
|
|
|
workers: int(h.conf.NumWorkers),
|
|
|
|
dns: h.dns,
|
|
|
|
reserved: h.conf.Reserved,
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
_ = bind.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
h.net, err = h.makeVirtualTun(bind)
|
|
|
|
if err != nil {
|
|
|
|
return newError("failed to create virtual tun interface").Base(err)
|
|
|
|
}
|
|
|
|
h.bind = bind
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-22 04:05:54 +03:00
|
|
|
// Process implements OutboundHandler.Dispatch().
|
|
|
|
func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer internet.Dialer) error {
|
2023-05-04 05:21:45 +03:00
|
|
|
outbound := session.OutboundFromContext(ctx)
|
|
|
|
if outbound == nil || !outbound.Target.IsValid() {
|
|
|
|
return newError("target not specified")
|
|
|
|
}
|
|
|
|
outbound.Name = "wireguard"
|
|
|
|
inbound := session.InboundFromContext(ctx)
|
|
|
|
if inbound != nil {
|
|
|
|
inbound.SetCanSpliceCopy(3)
|
|
|
|
}
|
|
|
|
|
2023-11-12 23:10:01 +03:00
|
|
|
if err := h.processWireGuard(dialer); err != nil {
|
|
|
|
return err
|
2022-11-22 04:05:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Destination of the inner request.
|
|
|
|
destination := outbound.Target
|
|
|
|
command := protocol.RequestCommandTCP
|
|
|
|
if destination.Network == net.Network_UDP {
|
|
|
|
command = protocol.RequestCommandUDP
|
|
|
|
}
|
|
|
|
|
|
|
|
// resolve dns
|
|
|
|
addr := destination.Address
|
|
|
|
if addr.Family().IsDomain() {
|
|
|
|
ips, err := h.dns.LookupIP(addr.Domain(), dns.IPOption{
|
2023-11-12 23:10:01 +03:00
|
|
|
IPv4Enable: h.hasIPv4,
|
|
|
|
IPv6Enable: h.hasIPv6,
|
2022-11-22 04:05:54 +03:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return newError("failed to lookup DNS").Base(err)
|
|
|
|
} else if len(ips) == 0 {
|
|
|
|
return dns.ErrEmptyResponse
|
|
|
|
}
|
|
|
|
addr = net.IPAddress(ips[0])
|
|
|
|
}
|
|
|
|
|
2023-04-06 13:21:35 +03:00
|
|
|
var newCtx context.Context
|
|
|
|
var newCancel context.CancelFunc
|
|
|
|
if session.TimeoutOnlyFromContext(ctx) {
|
|
|
|
newCtx, newCancel = context.WithCancel(context.Background())
|
|
|
|
}
|
|
|
|
|
2022-11-22 04:05:54 +03:00
|
|
|
p := h.policyManager.ForLevel(0)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2023-04-06 13:21:35 +03:00
|
|
|
timer := signal.CancelAfterInactivity(ctx, func() {
|
|
|
|
cancel()
|
|
|
|
if newCancel != nil {
|
|
|
|
newCancel()
|
|
|
|
}
|
|
|
|
}, p.Timeouts.ConnectionIdle)
|
2022-11-22 04:05:54 +03:00
|
|
|
addrPort := netip.AddrPortFrom(toNetIpAddr(addr), destination.Port.Value())
|
|
|
|
|
|
|
|
var requestFunc func() error
|
|
|
|
var responseFunc func() error
|
|
|
|
|
|
|
|
if command == protocol.RequestCommandTCP {
|
|
|
|
conn, err := h.net.DialContextTCPAddrPort(ctx, addrPort)
|
|
|
|
if err != nil {
|
|
|
|
return newError("failed to create TCP connection").Base(err)
|
|
|
|
}
|
2023-05-12 15:11:58 +03:00
|
|
|
defer conn.Close()
|
2022-11-22 04:05:54 +03:00
|
|
|
|
|
|
|
requestFunc = func() error {
|
|
|
|
defer timer.SetTimeout(p.Timeouts.DownlinkOnly)
|
|
|
|
return buf.Copy(link.Reader, buf.NewWriter(conn), buf.UpdateActivity(timer))
|
|
|
|
}
|
|
|
|
responseFunc = func() error {
|
|
|
|
defer timer.SetTimeout(p.Timeouts.UplinkOnly)
|
|
|
|
return buf.Copy(buf.NewReader(conn), link.Writer, buf.UpdateActivity(timer))
|
|
|
|
}
|
|
|
|
} else if command == protocol.RequestCommandUDP {
|
|
|
|
conn, err := h.net.DialUDPAddrPort(netip.AddrPort{}, addrPort)
|
|
|
|
if err != nil {
|
|
|
|
return newError("failed to create UDP connection").Base(err)
|
|
|
|
}
|
2023-05-12 15:11:58 +03:00
|
|
|
defer conn.Close()
|
2022-11-22 04:05:54 +03:00
|
|
|
|
|
|
|
requestFunc = func() error {
|
|
|
|
defer timer.SetTimeout(p.Timeouts.DownlinkOnly)
|
|
|
|
return buf.Copy(link.Reader, buf.NewWriter(conn), buf.UpdateActivity(timer))
|
|
|
|
}
|
|
|
|
responseFunc = func() error {
|
|
|
|
defer timer.SetTimeout(p.Timeouts.UplinkOnly)
|
|
|
|
return buf.Copy(buf.NewReader(conn), link.Writer, buf.UpdateActivity(timer))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-06 13:21:35 +03:00
|
|
|
if newCtx != nil {
|
|
|
|
ctx = newCtx
|
|
|
|
}
|
|
|
|
|
2022-11-22 04:05:54 +03:00
|
|
|
responseDonePost := task.OnSuccess(responseFunc, task.Close(link.Writer))
|
|
|
|
if err := task.Run(ctx, requestFunc, responseDonePost); err != nil {
|
2023-05-12 15:11:58 +03:00
|
|
|
common.Interrupt(link.Reader)
|
|
|
|
common.Interrupt(link.Writer)
|
2022-11-22 04:05:54 +03:00
|
|
|
return newError("connection ends").Base(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// serialize the config into an IPC request
|
2023-11-12 23:10:01 +03:00
|
|
|
func createIPCRequest(conf *DeviceConfig, d dns.Client, resolveEndPointToV4 bool) string {
|
2022-11-22 04:05:54 +03:00
|
|
|
var request bytes.Buffer
|
|
|
|
|
|
|
|
request.WriteString(fmt.Sprintf("private_key=%s\n", conf.SecretKey))
|
|
|
|
|
|
|
|
for _, peer := range conf.Peers {
|
2023-11-12 23:10:01 +03:00
|
|
|
endpoint := peer.Endpoint
|
|
|
|
host, port, err := net.SplitHostPort(endpoint)
|
|
|
|
if resolveEndPointToV4 && err == nil {
|
|
|
|
_, err = netip.ParseAddr(host)
|
|
|
|
if err != nil {
|
|
|
|
ipList, err := d.LookupIP(host, dns.IPOption{IPv4Enable: true, IPv6Enable: false})
|
|
|
|
if err == nil && len(ipList) > 0 {
|
|
|
|
endpoint = stdnet.JoinHostPort(ipList[0].String(), port)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-22 04:05:54 +03:00
|
|
|
request.WriteString(fmt.Sprintf("public_key=%s\nendpoint=%s\npersistent_keepalive_interval=%d\npreshared_key=%s\n",
|
2023-11-12 23:10:01 +03:00
|
|
|
peer.PublicKey, endpoint, peer.KeepAlive, peer.PreSharedKey))
|
2022-11-22 04:05:54 +03:00
|
|
|
|
|
|
|
for _, ip := range peer.AllowedIps {
|
|
|
|
request.WriteString(fmt.Sprintf("allowed_ip=%s\n", ip))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return request.String()[:request.Len()]
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert endpoint string to netip.Addr
|
|
|
|
func parseEndpoints(conf *DeviceConfig) ([]netip.Addr, error) {
|
|
|
|
endpoints := make([]netip.Addr, len(conf.Endpoint))
|
|
|
|
for i, str := range conf.Endpoint {
|
|
|
|
var addr netip.Addr
|
|
|
|
if strings.Contains(str, "/") {
|
|
|
|
prefix, err := netip.ParsePrefix(str)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
addr = prefix.Addr()
|
|
|
|
if prefix.Bits() != addr.BitLen() {
|
|
|
|
return nil, newError("interface address subnet should be /32 for IPv4 and /128 for IPv6")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var err error
|
|
|
|
addr, err = netip.ParseAddr(str)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
endpoints[i] = addr
|
|
|
|
}
|
|
|
|
|
|
|
|
return endpoints, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// creates a tun interface on netstack given a configuration
|
2023-11-12 23:10:01 +03:00
|
|
|
func (h *Handler) makeVirtualTun(bind *netBindClient) (Tunnel, error) {
|
|
|
|
t, err := CreateTun(h.endpoints, int(h.conf.Mtu))
|
2022-11-22 04:05:54 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-11-12 23:10:01 +03:00
|
|
|
bind.dnsOption.IPv4Enable = h.hasIPv4
|
|
|
|
bind.dnsOption.IPv6Enable = h.hasIPv6
|
2022-11-22 04:05:54 +03:00
|
|
|
|
2023-11-12 23:10:01 +03:00
|
|
|
if err = t.BuildDevice(h.ipc, bind); err != nil {
|
|
|
|
_ = t.Close()
|
2022-11-22 04:05:54 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-11-12 23:10:01 +03:00
|
|
|
return t, nil
|
2022-11-22 04:05:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
common.Must(common.RegisterConfig((*DeviceConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
|
|
|
return New(ctx, config.(*DeviceConfig))
|
|
|
|
}))
|
|
|
|
}
|