Darien Raymond před 8 roky
rodič
revize
503f767862

+ 9 - 10
proxy/shadowsocks/client.go

@@ -8,7 +8,6 @@ import (
 	"v2ray.com/core/app/log"
 	"v2ray.com/core/common"
 	"v2ray.com/core/common/buf"
-	"v2ray.com/core/common/errors"
 	"v2ray.com/core/common/net"
 	"v2ray.com/core/common/protocol"
 	"v2ray.com/core/common/retry"
@@ -40,7 +39,7 @@ func NewClient(ctx context.Context, config *ClientConfig) (*Client, error) {
 func (v *Client) Process(ctx context.Context, outboundRay ray.OutboundRay, dialer proxy.Dialer) error {
 	destination, ok := proxy.TargetFromContext(ctx)
 	if !ok {
-		return errors.New("target not specified").Path("Proxy", "Shadowsocks", "Client")
+		return newError("target not specified")
 	}
 	network := destination.Network
 
@@ -60,9 +59,9 @@ func (v *Client) Process(ctx context.Context, outboundRay ray.OutboundRay, diale
 		return nil
 	})
 	if err != nil {
-		return errors.New("failed to find an available destination").AtWarning().Base(err).Path("Proxy", "Shadowsocks", "Client")
+		return newError("failed to find an available destination").AtWarning().Base(err)
 	}
-	log.Trace(errors.New("tunneling request to ", destination, " via ", server.Destination()).Path("Proxy", "Shadowsocks", "Client"))
+	log.Trace(newError("tunneling request to ", destination, " via ", server.Destination()))
 
 	defer conn.Close()
 
@@ -80,7 +79,7 @@ func (v *Client) Process(ctx context.Context, outboundRay ray.OutboundRay, diale
 	user := server.PickUser()
 	rawAccount, err := user.GetTypedAccount()
 	if err != nil {
-		return errors.New("failed to get a valid user account").AtWarning().Base(err).Path("Proxy", "Shadowsocks", "Client")
+		return newError("failed to get a valid user account").AtWarning().Base(err)
 	}
 	account := rawAccount.(*ShadowsocksAccount)
 	request.User = user
@@ -95,7 +94,7 @@ func (v *Client) Process(ctx context.Context, outboundRay ray.OutboundRay, diale
 		bufferedWriter := buf.NewBufferedWriter(conn)
 		bodyWriter, err := WriteTCPRequest(request, bufferedWriter)
 		if err != nil {
-			return errors.New("failed to write request").Base(err).Path("Proxy", "Shadowsocks", "Client")
+			return newError("failed to write request").Base(err)
 		}
 
 		if err := bufferedWriter.SetBuffered(false); err != nil {
@@ -126,7 +125,7 @@ func (v *Client) Process(ctx context.Context, outboundRay ray.OutboundRay, diale
 		})
 
 		if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
-			return errors.New("connection ends").Base(err).Path("Proxy", "Shadowsocks", "Client")
+			return newError("connection ends").Base(err)
 		}
 
 		return nil
@@ -141,7 +140,7 @@ func (v *Client) Process(ctx context.Context, outboundRay ray.OutboundRay, diale
 
 		requestDone := signal.ExecuteAsync(func() error {
 			if err := buf.PipeUntilEOF(timer, outboundRay.OutboundInput(), writer); err != nil {
-				return errors.New("failed to transport all UDP request").Base(err).Path("Proxy", "Shadowsocks", "Client")
+				return newError("failed to transport all UDP request").Base(err)
 			}
 			return nil
 		})
@@ -155,13 +154,13 @@ func (v *Client) Process(ctx context.Context, outboundRay ray.OutboundRay, diale
 			}
 
 			if err := buf.PipeUntilEOF(timer, reader, outboundRay.OutboundOutput()); err != nil {
-				return errors.New("failed to transport all UDP response").Base(err).Path("Proxy", "Shadowsocks", "Client")
+				return newError("failed to transport all UDP response").Base(err)
 			}
 			return nil
 		})
 
 		if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
-			return errors.New("connection ends").Base(err).Path("Proxy", "Shadowsocks", "Client")
+			return newError("connection ends").Base(err)
 		}
 
 		return nil

+ 7 - 0
proxy/shadowsocks/errors.generated.go

@@ -0,0 +1,7 @@
+package shadowsocks
+
+import "v2ray.com/core/common/errors"
+
+func newError(values ...interface{}) *errors.Error {
+	return errors.New(values...).Path("Proxy", "Shadowsocks")
+}

+ 2 - 0
proxy/shadowsocks/shadowsocks.go

@@ -12,3 +12,5 @@
 //
 // R.I.P Shadowsocks
 package shadowsocks
+
+//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg shadowsocks -path Proxy,Shadowsocks

+ 44 - 0
tools/generrorgen/main.go

@@ -0,0 +1,44 @@
+package main
+
+import (
+	"flag"
+	"fmt"
+	"log"
+	"os"
+	"strings"
+)
+
+var (
+	pkg  = flag.String("pkg", "", "Target package")
+	path = flag.String("path", "", "Path")
+)
+
+func main() {
+	flag.Parse()
+
+	if len(*pkg) == 0 {
+		panic("Package is not specified.")
+	}
+
+	if len(*path) == 0 {
+		panic("Path is not specified.")
+	}
+
+	paths := strings.Split(*path, ",")
+	for i := range paths {
+		paths[i] = "\"" + paths[i] + "\""
+	}
+	pathStr := strings.Join(paths, ", ")
+
+	file, err := os.OpenFile("errors.generated.go", os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
+	if err != nil {
+		log.Fatalf("Failed to generate errors.generated.go: %v", err)
+	}
+	defer file.Close()
+
+	fmt.Fprintln(file, "package", *pkg)
+	fmt.Fprintln(file, "")
+	fmt.Fprintln(file, "import \"v2ray.com/core/common/errors\"")
+	fmt.Fprintln(file, "")
+	fmt.Fprintln(file, "func newError(values ...interface{}) *errors.Error { return errors.New(values...).Path("+pathStr+") }")
+}