mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-04-30 17:38:41 +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
|
@ -74,15 +74,15 @@ func (c *connection) WriteMultiBuffer(mb buf.MultiBuffer) error {
|
|||
}
|
||||
|
||||
func (c *connection) Close() error {
|
||||
var errors []interface{}
|
||||
var errs []interface{}
|
||||
if err := c.conn.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""), time.Now().Add(time.Second*5)); err != nil {
|
||||
errors = append(errors, err)
|
||||
errs = append(errs, err)
|
||||
}
|
||||
if err := c.conn.Close(); err != nil {
|
||||
errors = append(errors, err)
|
||||
errs = append(errs, err)
|
||||
}
|
||||
if len(errors) > 0 {
|
||||
return newError("failed to close connection").Base(newError(serial.Concat(errors...)))
|
||||
if len(errs) > 0 {
|
||||
return errors.New("failed to close connection").Base(errors.New(serial.Concat(errs...)))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -12,9 +12,9 @@ import (
|
|||
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/xtls/xray-core/common"
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
"github.com/xtls/xray-core/common/net"
|
||||
"github.com/xtls/xray-core/common/platform"
|
||||
"github.com/xtls/xray-core/common/session"
|
||||
"github.com/xtls/xray-core/common/uuid"
|
||||
"github.com/xtls/xray-core/transport/internet"
|
||||
"github.com/xtls/xray-core/transport/internet/stat"
|
||||
|
@ -39,7 +39,7 @@ func init() {
|
|||
if conn, err := upgrader.Upgrade(w, r, nil); err == nil {
|
||||
conns <- conn
|
||||
} else {
|
||||
newError("Browser dialer http upgrade unexpected error").AtError().WriteToLog()
|
||||
errors.LogError(context.Background(), "Browser dialer http upgrade unexpected error")
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
@ -51,7 +51,7 @@ func init() {
|
|||
|
||||
// Dial dials a WebSocket connection to the given destination.
|
||||
func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (stat.Connection, error) {
|
||||
newError("creating connection to ", dest).WriteToLog(session.ExportIDToError(ctx))
|
||||
errors.LogInfo(ctx, "creating connection to ", dest)
|
||||
var conn net.Conn
|
||||
if streamSettings.ProtocolSettings.(*Config).Ed > 0 {
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
|
@ -65,7 +65,7 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me
|
|||
} else {
|
||||
var err error
|
||||
if conn, err = dialWebSocket(ctx, dest, streamSettings, nil); err != nil {
|
||||
return nil, newError("failed to dial WebSocket").Base(err)
|
||||
return nil, errors.New("failed to dial WebSocket").Base(err)
|
||||
}
|
||||
}
|
||||
return stat.Connection(conn), nil
|
||||
|
@ -98,18 +98,18 @@ func dialWebSocket(ctx context.Context, dest net.Destination, streamSettings *in
|
|||
// Like the NetDial in the dialer
|
||||
pconn, err := internet.DialSystem(ctx, dest, streamSettings.SocketSettings)
|
||||
if err != nil {
|
||||
newError("failed to dial to " + addr).Base(err).AtError().WriteToLog()
|
||||
errors.LogErrorInner(ctx, err, "failed to dial to " + addr)
|
||||
return nil, err
|
||||
}
|
||||
// TLS and apply the handshake
|
||||
cn := tls.UClient(pconn, tlsConfig, fingerprint).(*tls.UConn)
|
||||
if err := cn.WebsocketHandshakeContext(ctx); err != nil {
|
||||
newError("failed to dial to " + addr).Base(err).AtError().WriteToLog()
|
||||
errors.LogErrorInner(ctx, err, "failed to dial to " + addr)
|
||||
return nil, err
|
||||
}
|
||||
if !tlsConfig.InsecureSkipVerify {
|
||||
if err := cn.VerifyHostname(tlsConfig.ServerName); err != nil {
|
||||
newError("failed to dial to " + addr).Base(err).AtError().WriteToLog()
|
||||
errors.LogErrorInner(ctx, err, "failed to dial to " + addr)
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
@ -143,7 +143,7 @@ func dialWebSocket(ctx context.Context, dest net.Destination, streamSettings *in
|
|||
return nil, err
|
||||
} else if s := string(p); s != "ok" {
|
||||
conn.Close()
|
||||
return nil, newError(s)
|
||||
return nil, errors.New(s)
|
||||
}
|
||||
return newConnection(conn, conn.RemoteAddr(), nil), nil
|
||||
}
|
||||
|
@ -160,7 +160,7 @@ func dialWebSocket(ctx context.Context, dest net.Destination, streamSettings *in
|
|||
if resp != nil {
|
||||
reason = resp.Status
|
||||
}
|
||||
return nil, newError("failed to dial to (", uri, "): ", reason).Base(err)
|
||||
return nil, errors.New("failed to dial to (", uri, "): ", reason).Base(err)
|
||||
}
|
||||
|
||||
return newConnection(conn, conn.RemoteAddr(), nil), nil
|
||||
|
@ -188,7 +188,7 @@ func (d *delayDialConn) Write(b []byte) (int, error) {
|
|||
var err error
|
||||
if d.Conn, err = dialWebSocket(d.ctx, d.dest, d.streamSettings, ed); err != nil {
|
||||
d.Close()
|
||||
return 0, newError("failed to dial WebSocket").Base(err)
|
||||
return 0, errors.New("failed to dial WebSocket").Base(err)
|
||||
}
|
||||
d.dialed <- true
|
||||
if ed != nil {
|
||||
|
|
|
@ -1,9 +0,0 @@
|
|||
package websocket
|
||||
|
||||
import "github.com/xtls/xray-core/common/errors"
|
||||
|
||||
type errPathObjHolder struct{}
|
||||
|
||||
func newError(values ...interface{}) *errors.Error {
|
||||
return errors.New(values...).WithPathObj(errPathObjHolder{})
|
||||
}
|
|
@ -13,9 +13,9 @@ import (
|
|||
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/xtls/xray-core/common"
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
"github.com/xtls/xray-core/common/net"
|
||||
http_proto "github.com/xtls/xray-core/common/protocol/http"
|
||||
"github.com/xtls/xray-core/common/session"
|
||||
"github.com/xtls/xray-core/transport/internet"
|
||||
v2tls "github.com/xtls/xray-core/transport/internet/tls"
|
||||
)
|
||||
|
@ -39,12 +39,12 @@ var upgrader = &websocket.Upgrader{
|
|||
|
||||
func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
||||
if len(h.host) > 0 && request.Host != h.host {
|
||||
newError("failed to validate host, request:", request.Host, ", config:", h.host).WriteToLog()
|
||||
errors.LogInfo(context.Background(), "failed to validate host, request:", request.Host, ", config:", h.host)
|
||||
writer.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
if request.URL.Path != h.path {
|
||||
newError("failed to validate path, request:", request.URL.Path, ", config:", h.path).WriteToLog()
|
||||
errors.LogInfo(context.Background(), "failed to validate path, request:", request.URL.Path, ", config:", h.path)
|
||||
writer.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
@ -60,7 +60,7 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
|
|||
|
||||
conn, err := upgrader.Upgrade(writer, request, responseHeader)
|
||||
if err != nil {
|
||||
newError("failed to convert to WebSocket connection").Base(err).WriteToLog()
|
||||
errors.LogInfoInner(context.Background(), err, "failed to convert to WebSocket connection")
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -104,22 +104,22 @@ func ListenWS(ctx context.Context, address net.Address, port net.Port, streamSet
|
|||
Net: "unix",
|
||||
}, streamSettings.SocketSettings)
|
||||
if err != nil {
|
||||
return nil, newError("failed to listen unix domain socket(for WS) on ", address).Base(err)
|
||||
return nil, errors.New("failed to listen unix domain socket(for WS) on ", address).Base(err)
|
||||
}
|
||||
newError("listening unix domain socket(for WS) on ", address).WriteToLog(session.ExportIDToError(ctx))
|
||||
errors.LogInfo(ctx, "listening unix domain socket(for WS) on ", address)
|
||||
} else { // tcp
|
||||
listener, err = internet.ListenSystem(ctx, &net.TCPAddr{
|
||||
IP: address.IP(),
|
||||
Port: int(port),
|
||||
}, streamSettings.SocketSettings)
|
||||
if err != nil {
|
||||
return nil, newError("failed to listen TCP(for WS) on ", address, ":", port).Base(err)
|
||||
return nil, errors.New("failed to listen TCP(for WS) on ", address, ":", port).Base(err)
|
||||
}
|
||||
newError("listening TCP(for WS) on ", address, ":", port).WriteToLog(session.ExportIDToError(ctx))
|
||||
errors.LogInfo(ctx, "listening TCP(for WS) on ", address, ":", port)
|
||||
}
|
||||
|
||||
if streamSettings.SocketSettings != nil && streamSettings.SocketSettings.AcceptProxyProtocol {
|
||||
newError("accepting PROXY protocol").AtWarning().WriteToLog(session.ExportIDToError(ctx))
|
||||
errors.LogWarning(ctx, "accepting PROXY protocol")
|
||||
}
|
||||
|
||||
if config := v2tls.ConfigFromStreamSettings(streamSettings); config != nil {
|
||||
|
@ -142,7 +142,7 @@ func ListenWS(ctx context.Context, address net.Address, port net.Port, streamSet
|
|||
|
||||
go func() {
|
||||
if err := l.server.Serve(l.listener); err != nil {
|
||||
newError("failed to serve http for WebSocket").Base(err).AtWarning().WriteToLog(session.ExportIDToError(ctx))
|
||||
errors.LogWarningInner(ctx, err, "failed to serve http for WebSocket")
|
||||
}
|
||||
}()
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue