1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2026-04-24 06:29:10 -04:00

refactor log and error

This commit is contained in:
Darien Raymond
2017-04-06 21:13:17 +02:00
parent fc31f9b94c
commit 75f5cb9afb
33 changed files with 123 additions and 99 deletions

View File

@@ -44,7 +44,7 @@ func (d *DokodemoDoor) Network() net.NetworkList {
}
func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn internet.Connection, dispatcher dispatcher.Interface) error {
log.Debug("Dokodemo: processing connection from: ", conn.RemoteAddr())
log.Trace(errors.New("Dokodemo: processing connection from: ", conn.RemoteAddr()).AtDebug())
conn.SetReusable(false)
dest := net.Destination{
Network: network,
@@ -57,8 +57,7 @@ func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn in
}
}
if !dest.IsValid() || dest.Address == nil {
log.Info("Dokodemo: Invalid destination. Discarding...")
return errors.New("Dokodemo: Unable to get destination.")
return errors.New("unable to get destination").Path("Proxy", "Dokodemo")
}
timeout := time.Second * time.Duration(d.config.Timeout)
@@ -78,8 +77,7 @@ func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn in
chunkReader := buf.NewReader(conn)
if err := buf.PipeUntilEOF(timer, chunkReader, inboundRay.InboundInput()); err != nil {
log.Info("Dokodemo: Failed to transport request: ", err)
return err
return errors.New("failed to transport request").Base(err).Path("Proxy", "Dokodemo")
}
return nil
@@ -89,8 +87,7 @@ func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn in
v2writer := buf.NewWriter(conn)
if err := buf.PipeUntilEOF(timer, inboundRay.InboundOutput(), v2writer); err != nil {
log.Info("Dokodemo: Failed to transport response: ", err)
return err
return errors.New("failed to transport response").Base(err).Path("Proxy", "Dokodemo")
}
return nil
})
@@ -98,8 +95,7 @@ func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn in
if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
inboundRay.InboundInput().CloseError()
inboundRay.InboundOutput().CloseError()
log.Info("Dokodemo: Connection ends with ", err)
return err
return errors.New("connection ends").Base(err).Path("Proxy", "Dokodemo")
}
runtime.KeepAlive(timer)

View File

@@ -31,7 +31,7 @@ type Handler struct {
func New(ctx context.Context, config *Config) (*Handler, error) {
space := app.SpaceFromContext(ctx)
if space == nil {
return nil, errors.New("Freedom: No space in context.")
return nil, errors.New("no space in context").Path("Proxy", "Freedom")
}
f := &Handler{
domainStrategy: config.DomainStrategy,
@@ -42,7 +42,7 @@ func New(ctx context.Context, config *Config) (*Handler, error) {
if config.DomainStrategy == Config_USE_IP {
f.dns = dns.FromSpace(space)
if f.dns == nil {
return errors.New("Freedom: DNS server is not found in the space.")
return errors.New("DNS server is not found in the space").Path("Proxy", "Freedom")
}
}
return nil
@@ -50,7 +50,6 @@ func New(ctx context.Context, config *Config) (*Handler, error) {
return f, nil
}
// Private: Visible for testing.
func (v *Handler) ResolveIP(destination net.Destination) net.Destination {
if !destination.Address.Family().IsDomain() {
return destination
@@ -58,7 +57,7 @@ func (v *Handler) ResolveIP(destination net.Destination) net.Destination {
ips := v.dns.Get(destination.Address.Domain())
if len(ips) == 0 {
log.Info("Freedom: DNS returns nil answer. Keep domain as is.")
log.Trace(errors.New("DNS returns nil answer. Keep domain as is.").Path("Proxy", "Freedom"))
return destination
}
@@ -69,7 +68,7 @@ func (v *Handler) ResolveIP(destination net.Destination) net.Destination {
} else {
newDest = net.UDPDestination(net.IPAddress(ip), destination.Port)
}
log.Info("Freedom: Changing destination from ", destination, " to ", newDest)
log.Trace(errors.New("changing destination from ", destination, " to ", newDest).Path("Proxy", "Freedom"))
return newDest
}
@@ -83,7 +82,7 @@ func (v *Handler) Process(ctx context.Context, outboundRay ray.OutboundRay, dial
Port: net.Port(server.Port),
}
}
log.Info("Freedom: Opening connection to ", destination)
log.Trace(errors.New("opening connection to ", destination).Path("Proxy", "Freedom"))
input := outboundRay.OutboundInput()
output := outboundRay.OutboundOutput()
@@ -102,7 +101,7 @@ func (v *Handler) Process(ctx context.Context, outboundRay ray.OutboundRay, dial
return nil
})
if err != nil {
return errors.New("failed to open connection to ", destination).Base(err).Path("Freedom")
return errors.New("failed to open connection to ", destination).Base(err).Path("Proxy", "Freedom")
}
defer conn.Close()
@@ -112,7 +111,6 @@ func (v *Handler) Process(ctx context.Context, outboundRay ray.OutboundRay, dial
if timeout == 0 {
timeout = time.Minute * 5
}
log.Debug("Freedom: Cancel after ", timeout)
ctx, timer := signal.CancelAfterInactivity(ctx, timeout)
requestDone := signal.ExecuteAsync(func() error {
@@ -136,7 +134,7 @@ func (v *Handler) Process(ctx context.Context, outboundRay ray.OutboundRay, dial
if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
input.CloseError()
output.CloseError()
return errors.New("connection ends").Base(err).Path("Freedom")
return errors.New("connection ends").Base(err).Path("Proxy", "Freedom")
}
runtime.KeepAlive(timer)

View File

@@ -77,11 +77,11 @@ func (s *Server) Process(ctx context.Context, network v2net.Network, conn intern
request, err := http.ReadRequest(reader)
if err != nil {
if errors.Cause(err) != io.EOF {
log.Warning("HTTP: Failed to read http request: ", err)
log.Trace(errors.New("HTTP: Failed to read http request: ", err).AtWarning())
}
return err
}
log.Info("HTTP: Request to Method [", request.Method, "] Host [", request.Host, "] with URL [", request.URL, "]")
log.Trace(errors.New("request to Method [", request.Method, "] Host [", request.Host, "] with URL [", request.URL, "]").Path("Proxy", "HTTP", "Server"))
conn.SetReadDeadline(time.Time{})
defaultPort := v2net.Port(80)
@@ -234,7 +234,7 @@ func (s *Server) handlePlainHTTP(ctx context.Context, request *http.Request, rea
responseReader := bufio.NewReader(buf.ToBytesReader(ray.InboundOutput()))
response, err := http.ReadResponse(responseReader, request)
if err != nil {
log.Warning("HTTP: Failed to read response: ", err)
log.Trace(errors.New("HTTP: Failed to read response: ", err).AtWarning())
response = generateResponse(503, "Service Unavailable")
}
responseWriter := buf.NewBufferedWriter(writer)

View File

@@ -62,7 +62,7 @@ func (v *Client) Process(ctx context.Context, outboundRay ray.OutboundRay, diale
if err != nil {
return errors.New("failed to find an available destination").AtWarning().Base(err).Path("Shadowsocks", "Client")
}
log.Info("Shadowsocks|Client: Tunneling request to ", destination, " via ", server.Destination())
log.Trace(errors.New("Shadowsocks|Client: Tunneling request to ", destination, " via ", server.Destination()))
defer conn.Close()
conn.SetReusable(false)

View File

@@ -78,14 +78,14 @@ func (s *Server) processTCP(ctx context.Context, conn internet.Connection, dispa
if source, ok := proxy.SourceFromContext(ctx); ok {
log.Access(source, "", log.AccessRejected, err)
}
log.Info("Socks|Server: Failed to read request: ", err)
log.Trace(errors.New("Socks|Server: Failed to read request: ", err))
return err
}
conn.SetReadDeadline(time.Time{})
if request.Command == protocol.RequestCommandTCP {
dest := request.Destination()
log.Info("Socks|Server: TCP Connect request to ", dest)
log.Trace(errors.New("Socks|Server: TCP Connect request to ", dest))
if source, ok := proxy.SourceFromContext(ctx); ok {
log.Access(source, dest, log.AccessAccepted, "")
}
@@ -169,7 +169,7 @@ func (v *Server) handleUDPPayload(ctx context.Context, conn internet.Connection,
request, data, err := DecodeUDPPacket(payload.Bytes())
if err != nil {
log.Info("Socks|Server: Failed to parse UDP request: ", err)
log.Trace(errors.New("Socks|Server: Failed to parse UDP request: ", err))
continue
}
@@ -177,7 +177,7 @@ func (v *Server) handleUDPPayload(ctx context.Context, conn internet.Connection,
continue
}
log.Info("Socks: Send packet to ", request.Destination(), " with ", len(data), " bytes")
log.Trace(errors.New("Socks: Send packet to ", request.Destination(), " with ", len(data), " bytes"))
if source, ok := proxy.SourceFromContext(ctx); ok {
log.Access(source, request.Destination, log.AccessAccepted, "")
}
@@ -187,7 +187,7 @@ func (v *Server) handleUDPPayload(ctx context.Context, conn internet.Connection,
udpServer.Dispatch(ctx, request.Destination(), dataBuf, func(payload *buf.Buffer) {
defer payload.Release()
log.Info("Socks|Server: Writing back UDP response with ", payload.Len(), " bytes")
log.Trace(errors.New("Socks|Server: Writing back UDP response with ", payload.Len(), " bytes"))
udpMessage := EncodeUDPPacket(request, payload.Bytes())
defer udpMessage.Release()

View File

@@ -3,6 +3,7 @@ package vmess
import (
"v2ray.com/core/app/log"
"v2ray.com/core/common/dice"
"v2ray.com/core/common/errors"
"v2ray.com/core/common/protocol"
"v2ray.com/core/common/uuid"
)
@@ -32,7 +33,7 @@ func (v *InternalAccount) Equals(account protocol.Account) bool {
func (v *Account) AsAccount() (protocol.Account, error) {
id, err := uuid.ParseString(v.Id)
if err != nil {
log.Error("VMess: Failed to parse ID: ", err)
log.Trace(errors.New("failed to parse ID").Path("VMess", "Account").Base(err).AtError())
return nil, err
}
protoID := protocol.NewID(id)

View File

@@ -63,7 +63,7 @@ func (v *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, writ
timestamp := protocol.NewTimestampGenerator(protocol.NowTime(), 30)()
account, err := header.User.GetTypedAccount()
if err != nil {
log.Error("VMess: Failed to get user account: ", err)
log.Trace(errors.New("VMess: Failed to get user account: ", err).AtError())
return
}
idHash := v.idHash(account.(*vmess.InternalAccount).AnyValidID().Bytes())
@@ -186,7 +186,7 @@ func (v *ClientSession) DecodeResponseHeader(reader io.Reader) (*protocol.Respon
_, err := io.ReadFull(v.responseReader, buffer[:4])
if err != nil {
log.Info("VMess|Client: Failed to read response header: ", err)
log.Trace(errors.New("VMess|Client: Failed to read response header: ", err))
return nil, err
}
@@ -203,7 +203,7 @@ func (v *ClientSession) DecodeResponseHeader(reader io.Reader) (*protocol.Respon
dataLen := int(buffer[3])
_, err := io.ReadFull(v.responseReader, buffer[:dataLen])
if err != nil {
log.Info("VMess|Client: Failed to read response command: ", err)
log.Trace(errors.New("VMess|Client: Failed to read response command: ", err))
return nil, err
}
data := buffer[:dataLen]

View File

@@ -179,13 +179,13 @@ func (v *Handler) Process(ctx context.Context, network net.Network, connection i
if err != nil {
if errors.Cause(err) != io.EOF {
log.Access(connection.RemoteAddr(), "", log.AccessRejected, err)
log.Info("VMess|Inbound: Invalid request from ", connection.RemoteAddr(), ": ", err)
log.Trace(errors.New("VMess|Inbound: Invalid request from ", connection.RemoteAddr(), ": ", err))
}
connection.SetReusable(false)
return err
}
log.Access(connection.RemoteAddr(), request.Destination(), log.AccessAccepted, "")
log.Info("VMess|Inbound: Received request for ", request.Destination())
log.Trace(errors.New("VMess|Inbound: Received request for ", request.Destination()))
connection.SetReadDeadline(time.Time{})
@@ -245,7 +245,7 @@ func (v *Handler) generateCommand(ctx context.Context, request *protocol.Request
if v.inboundHandlerManager != nil {
handler, err := v.inboundHandlerManager.GetHandler(ctx, tag)
if err != nil {
log.Warning("VMess|Inbound: Failed to get detour handler: ", tag, err)
log.Trace(errors.New("VMess|Inbound: Failed to get detour handler: ", tag, err).AtWarning())
return nil
}
proxyHandler, port, availableMin := handler.GetRandomInboundProxy()
@@ -255,7 +255,7 @@ func (v *Handler) generateCommand(ctx context.Context, request *protocol.Request
availableMin = 255
}
log.Info("VMess|Inbound: Pick detour handler for port ", port, " for ", availableMin, " minutes.")
log.Trace(errors.New("VMess|Inbound: Pick detour handler for port ", port, " for ", availableMin, " minutes."))
user := inboundHandler.GetUser(request.User.Email)
if user == nil {
return nil

View File

@@ -69,7 +69,7 @@ func (v *Handler) Process(ctx context.Context, outboundRay ray.OutboundRay, dial
if !ok {
return errors.New("VMess|Outbound: Target not specified.")
}
log.Info("VMess|Outbound: Tunneling request to ", target, " via ", rec.Destination())
log.Trace(errors.New("VMess|Outbound: Tunneling request to ", target, " via ", rec.Destination()))
command := protocol.RequestCommandTCP
if target.Network == net.Network_UDP {