mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-04-30 09:18:34 +00:00
Refactor log (#3446)
* Refactor log * Add new log methods * Fix logger test * Change all logging code * Clean up pathObj * Rebase to latest main * Remove invoking method name after the dot
This commit is contained in:
parent
8320732743
commit
079d0bd8a9
291 changed files with 1837 additions and 2368 deletions
|
@ -202,7 +202,7 @@ func (bind *netBindServer) Send(buff [][]byte, endpoint conn.Endpoint) error {
|
|||
}
|
||||
|
||||
if nend.conn == nil {
|
||||
return newError("connection not open yet")
|
||||
return errors.New("connection not open yet")
|
||||
}
|
||||
|
||||
for _, buff := range buff {
|
||||
|
|
|
@ -29,6 +29,7 @@ import (
|
|||
|
||||
"github.com/xtls/xray-core/common"
|
||||
"github.com/xtls/xray-core/common/buf"
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
"github.com/xtls/xray-core/common/dice"
|
||||
"github.com/xtls/xray-core/common/log"
|
||||
"github.com/xtls/xray-core/common/net"
|
||||
|
@ -120,7 +121,7 @@ func (h *Handler) processWireGuard(ctx context.Context, dialer internet.Dialer)
|
|||
|
||||
h.net, err = h.makeVirtualTun(bind)
|
||||
if err != nil {
|
||||
return newError("failed to create virtual tun interface").Base(err)
|
||||
return errors.New("failed to create virtual tun interface").Base(err)
|
||||
}
|
||||
h.bind = bind
|
||||
return nil
|
||||
|
@ -129,9 +130,9 @@ func (h *Handler) processWireGuard(ctx context.Context, dialer internet.Dialer)
|
|||
// Process implements OutboundHandler.Dispatch().
|
||||
func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer internet.Dialer) error {
|
||||
outbounds := session.OutboundsFromContext(ctx)
|
||||
ob := outbounds[len(outbounds) - 1]
|
||||
ob := outbounds[len(outbounds)-1]
|
||||
if !ob.Target.IsValid() {
|
||||
return newError("target not specified")
|
||||
return errors.New("target not specified")
|
||||
}
|
||||
ob.Name = "wireguard"
|
||||
ob.CanSpliceCopy = 3
|
||||
|
@ -163,7 +164,7 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte
|
|||
}
|
||||
}
|
||||
if err != nil {
|
||||
return newError("failed to lookup DNS").Base(err)
|
||||
return errors.New("failed to lookup DNS").Base(err)
|
||||
} else if len(ips) == 0 {
|
||||
return dns.ErrEmptyResponse
|
||||
}
|
||||
|
@ -193,7 +194,7 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte
|
|||
if command == protocol.RequestCommandTCP {
|
||||
conn, err := h.net.DialContextTCPAddrPort(ctx, addrPort)
|
||||
if err != nil {
|
||||
return newError("failed to create TCP connection").Base(err)
|
||||
return errors.New("failed to create TCP connection").Base(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
|
@ -208,7 +209,7 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte
|
|||
} 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)
|
||||
return errors.New("failed to create UDP connection").Base(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
|
@ -230,7 +231,7 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte
|
|||
if err := task.Run(ctx, requestFunc, responseDonePost); err != nil {
|
||||
common.Interrupt(link.Reader)
|
||||
common.Interrupt(link.Writer)
|
||||
return newError("connection ends").Base(err)
|
||||
return errors.New("connection ends").Base(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -275,14 +276,14 @@ func (h *Handler) createIPCRequest(bind *netBindClient, conf *DeviceConfig) stri
|
|||
|
||||
address, port, err := net.SplitHostPort(peer.Endpoint)
|
||||
if err != nil {
|
||||
newError("failed to split endpoint ", peer.Endpoint, " into address and port").AtError().WriteToLog()
|
||||
errors.LogError(h.bind.ctx, "failed to split endpoint ", peer.Endpoint, " into address and port")
|
||||
}
|
||||
addr := net.ParseAddress(address)
|
||||
if addr.Family().IsDomain() {
|
||||
dialerIp := bind.dialer.DestIpAddress()
|
||||
if dialerIp != nil {
|
||||
addr = net.ParseAddress(dialerIp.String())
|
||||
newError("createIPCRequest use dialer dest ip: ", addr).WriteToLog()
|
||||
errors.LogInfo(h.bind.ctx, "createIPCRequest use dialer dest ip: ", addr)
|
||||
} else {
|
||||
ips, err := h.dns.LookupIP(addr.Domain(), dns.IPOption{
|
||||
IPv4Enable: h.hasIPv4 && h.conf.preferIP4(),
|
||||
|
@ -297,9 +298,9 @@ func (h *Handler) createIPCRequest(bind *netBindClient, conf *DeviceConfig) stri
|
|||
}
|
||||
}
|
||||
if err != nil {
|
||||
newError("createIPCRequest failed to lookup DNS").Base(err).WriteToLog()
|
||||
errors.LogInfoInner(h.bind.ctx, err, "createIPCRequest failed to lookup DNS")
|
||||
} else if len(ips) == 0 {
|
||||
newError("createIPCRequest empty lookup DNS").WriteToLog()
|
||||
errors.LogInfo(h.bind.ctx, "createIPCRequest empty lookup DNS")
|
||||
} else {
|
||||
addr = net.IPAddress(ips[dice.Roll(len(ips))])
|
||||
}
|
||||
|
|
|
@ -1,9 +0,0 @@
|
|||
package wireguard
|
||||
|
||||
import "github.com/xtls/xray-core/common/errors"
|
||||
|
||||
type errPathObjHolder struct{}
|
||||
|
||||
func newError(values ...interface{}) *errors.Error {
|
||||
return errors.New(values...).WithPathObj(errPathObjHolder{})
|
||||
}
|
|
@ -2,11 +2,12 @@ package wireguard
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
goerrors "errors"
|
||||
"io"
|
||||
|
||||
"github.com/xtls/xray-core/common"
|
||||
"github.com/xtls/xray-core/common/buf"
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
"github.com/xtls/xray-core/common/log"
|
||||
"github.com/xtls/xray-core/common/net"
|
||||
"github.com/xtls/xray-core/common/session"
|
||||
|
@ -81,7 +82,7 @@ func (s *Server) Process(ctx context.Context, network net.Network, conn stat.Con
|
|||
inbound.Name = "wireguard"
|
||||
inbound.CanSpliceCopy = 3
|
||||
outbounds := session.OutboundsFromContext(ctx)
|
||||
ob := outbounds[len(outbounds) - 1]
|
||||
ob := outbounds[len(outbounds)-1]
|
||||
|
||||
s.info = routingInfo{
|
||||
ctx: core.ToBackgroundDetachedContext(ctx),
|
||||
|
@ -117,7 +118,7 @@ func (s *Server) Process(ctx context.Context, network net.Network, conn stat.Con
|
|||
v.endpoint = nep
|
||||
v.err = err
|
||||
v.waiter.Done()
|
||||
if err != nil && errors.Is(err, io.EOF) {
|
||||
if err != nil && goerrors.Is(err, io.EOF) {
|
||||
nep.conn = nil
|
||||
return nil
|
||||
}
|
||||
|
@ -127,7 +128,7 @@ func (s *Server) Process(ctx context.Context, network net.Network, conn stat.Con
|
|||
|
||||
func (s *Server) forwardConnection(dest net.Destination, conn net.Conn) {
|
||||
if s.info.dispatcher == nil {
|
||||
newError("unexpected: dispatcher == nil").AtError().WriteToLog()
|
||||
errors.LogError(s.info.ctx, "unexpected: dispatcher == nil")
|
||||
return
|
||||
}
|
||||
defer conn.Close()
|
||||
|
@ -155,14 +156,14 @@ func (s *Server) forwardConnection(dest net.Destination, conn net.Conn) {
|
|||
|
||||
link, err := s.info.dispatcher.Dispatch(ctx, dest)
|
||||
if err != nil {
|
||||
newError("dispatch connection").Base(err).AtError().WriteToLog()
|
||||
errors.LogErrorInner(s.info.ctx, err, "dispatch connection")
|
||||
}
|
||||
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 {
|
||||
return newError("failed to transport all TCP request").Base(err)
|
||||
return errors.New("failed to transport all TCP request").Base(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -171,7 +172,7 @@ func (s *Server) forwardConnection(dest net.Destination, conn net.Conn) {
|
|||
responseDone := func() error {
|
||||
defer timer.SetTimeout(plcy.Timeouts.UplinkOnly)
|
||||
if err := buf.Copy(link.Reader, buf.NewWriter(conn), buf.UpdateActivity(timer)); err != nil {
|
||||
return newError("failed to transport all TCP response").Base(err)
|
||||
return errors.New("failed to transport all TCP response").Base(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -181,7 +182,7 @@ func (s *Server) forwardConnection(dest net.Destination, conn net.Conn) {
|
|||
if err := task.Run(ctx, requestDonePost, responseDone); err != nil {
|
||||
common.Interrupt(link.Reader)
|
||||
common.Interrupt(link.Writer)
|
||||
newError("connection ends").Base(err).AtDebug().WriteToLog()
|
||||
errors.LogDebugInner(s.info.ctx, err, "connection ends")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ package wireguard
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/netip"
|
||||
|
@ -12,6 +11,7 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
"github.com/xtls/xray-core/common/log"
|
||||
xnet "github.com/xtls/xray-core/common/net"
|
||||
"github.com/xtls/xray-core/proxy/wireguard/gvisortun"
|
||||
|
@ -158,7 +158,7 @@ func createGVisorTun(localAddresses []netip.Addr, mtu int, handler promiscuousMo
|
|||
// Perform a TCP three-way handshake.
|
||||
ep, err := r.CreateEndpoint(&wq)
|
||||
if err != nil {
|
||||
newError(err.String()).AtError().WriteToLog()
|
||||
errors.LogError(context.Background(), err.String())
|
||||
r.Complete(true)
|
||||
return
|
||||
}
|
||||
|
@ -183,7 +183,7 @@ func createGVisorTun(localAddresses []netip.Addr, mtu int, handler promiscuousMo
|
|||
|
||||
ep, err := r.CreateEndpoint(&wq)
|
||||
if err != nil {
|
||||
newError(err.String()).AtError().WriteToLog()
|
||||
errors.LogError(context.Background(), err.String())
|
||||
return
|
||||
}
|
||||
defer ep.Close()
|
||||
|
|
|
@ -73,7 +73,7 @@ func (d *deviceNet) Close() (err error) {
|
|||
|
||||
func createKernelTun(localAddresses []netip.Addr, mtu int, handler promiscuousModeHandler) (t Tunnel, err error) {
|
||||
if handler != nil {
|
||||
return nil, newError("TODO: support promiscuous mode")
|
||||
return nil, errors.New("TODO: support promiscuous mode")
|
||||
}
|
||||
|
||||
var v4, v6 *netip.Addr
|
||||
|
|
|
@ -2,6 +2,7 @@ package wireguard
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/netip"
|
||||
"strings"
|
||||
|
@ -53,7 +54,7 @@ func parseEndpoints(conf *DeviceConfig) ([]netip.Addr, bool, bool, error) {
|
|||
}
|
||||
addr = prefix.Addr()
|
||||
if prefix.Bits() != addr.BitLen() {
|
||||
return nil, false, false, newError("interface address subnet should be /32 for IPv4 and /128 for IPv6")
|
||||
return nil, false, false, errors.New("interface address subnet should be /32 for IPv4 and /128 for IPv6")
|
||||
}
|
||||
} else {
|
||||
var err error
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue