From d43a23aa961770765cf2be4e51154afc76c76bfc Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Sun, 3 Dec 2017 01:04:57 +0100 Subject: [PATCH] move error generator to common --- app/app.go | 2 +- app/dispatcher/impl/default.go | 2 +- app/dns/dns.go | 2 +- app/dns/server/server.go | 2 +- app/log/log.go | 2 +- app/proxyman/inbound/inbound.go | 2 +- app/proxyman/mux/mux.go | 2 +- app/proxyman/outbound/outbound.go | 2 +- app/proxyman/proxyman.go | 2 +- app/router/router.go | 2 +- common/buf/buf.go | 2 +- common/common.go | 2 +- common/crypto/crypto.go | 2 +- common/errors/errorgen/main.go | 47 +++++++++++++++++++++++++ common/net/net.go | 2 +- common/protocol/protocol.go | 2 +- common/retry/retry.go | 2 +- core.go | 2 +- main/main.go | 2 +- proxy/blackhole/blackhole.go | 2 +- proxy/dokodemo/dokodemo.go | 2 +- proxy/freedom/freedom.go | 2 +- proxy/http/http.go | 2 +- proxy/proxy.go | 2 +- proxy/shadowsocks/shadowsocks.go | 2 +- proxy/socks/socks.go | 2 +- proxy/vmess/encoding/encoding.go | 2 +- proxy/vmess/inbound/inbound.go | 2 +- proxy/vmess/outbound/outbound.go | 2 +- proxy/vmess/vmess.go | 2 +- transport/internet/headers/http/http.go | 2 +- transport/internet/internet.go | 2 +- transport/internet/kcp/kcp.go | 2 +- transport/internet/tcp/tcp.go | 2 +- transport/internet/tls/tls.go | 2 +- transport/internet/udp/udp.go | 2 +- transport/internet/websocket/ws.go | 2 +- v2ray.go | 2 +- 38 files changed, 84 insertions(+), 37 deletions(-) create mode 100644 common/errors/errorgen/main.go diff --git a/app/app.go b/app/app.go index e6da6744b..c688df1e7 100644 --- a/app/app.go +++ b/app/app.go @@ -1,3 +1,3 @@ package app -//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg app -path App +//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg app -path App diff --git a/app/dispatcher/impl/default.go b/app/dispatcher/impl/default.go index e9e5c0804..26cb019b9 100644 --- a/app/dispatcher/impl/default.go +++ b/app/dispatcher/impl/default.go @@ -1,6 +1,6 @@ package impl -//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg impl -path App,Dispatcher,Default +//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg impl -path App,Dispatcher,Default import ( "context" diff --git a/app/dns/dns.go b/app/dns/dns.go index 49b522848..6dfcabdd5 100644 --- a/app/dns/dns.go +++ b/app/dns/dns.go @@ -1,6 +1,6 @@ package dns -//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg dns -path App,DNS +//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg dns -path App,DNS import ( "net" diff --git a/app/dns/server/server.go b/app/dns/server/server.go index 904feec7a..0bb26e8ac 100644 --- a/app/dns/server/server.go +++ b/app/dns/server/server.go @@ -1,6 +1,6 @@ package server -//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg server -path App,DNS,Server +//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg server -path App,DNS,Server import ( "context" diff --git a/app/log/log.go b/app/log/log.go index d83547445..1fcbce53e 100644 --- a/app/log/log.go +++ b/app/log/log.go @@ -1,6 +1,6 @@ package log -//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg log -path App,Log +//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg log -path App,Log import ( "context" diff --git a/app/proxyman/inbound/inbound.go b/app/proxyman/inbound/inbound.go index 964a14911..f2d647e94 100644 --- a/app/proxyman/inbound/inbound.go +++ b/app/proxyman/inbound/inbound.go @@ -1,6 +1,6 @@ package inbound -//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg inbound -path App,Proxyman,Inbound +//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg inbound -path App,Proxyman,Inbound import ( "context" diff --git a/app/proxyman/mux/mux.go b/app/proxyman/mux/mux.go index 4acbb2569..75ce96c95 100644 --- a/app/proxyman/mux/mux.go +++ b/app/proxyman/mux/mux.go @@ -1,6 +1,6 @@ package mux -//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg mux -path App,Proxyman,Mux +//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg mux -path App,Proxyman,Mux import ( "context" diff --git a/app/proxyman/outbound/outbound.go b/app/proxyman/outbound/outbound.go index 9460242c3..5835cb0e3 100644 --- a/app/proxyman/outbound/outbound.go +++ b/app/proxyman/outbound/outbound.go @@ -1,6 +1,6 @@ package outbound -//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg outbound -path App,Proxyman,Outbound +//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg outbound -path App,Proxyman,Outbound import ( "context" diff --git a/app/proxyman/proxyman.go b/app/proxyman/proxyman.go index 8302a884c..f782a77b5 100644 --- a/app/proxyman/proxyman.go +++ b/app/proxyman/proxyman.go @@ -1,7 +1,7 @@ // Package proxyman defines applications for manageing inbound and outbound proxies. package proxyman -//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg proxyman -path App,Proxyman +//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg proxyman -path App,Proxyman import ( "context" diff --git a/app/router/router.go b/app/router/router.go index 33d6aa912..8761bc015 100644 --- a/app/router/router.go +++ b/app/router/router.go @@ -1,6 +1,6 @@ package router -//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg router -path App,Router +//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg router -path App,Router import ( "context" diff --git a/common/buf/buf.go b/common/buf/buf.go index 5074848f1..949ec6456 100644 --- a/common/buf/buf.go +++ b/common/buf/buf.go @@ -1,3 +1,3 @@ package buf -//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg buf -path Buf +//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg buf -path Buf diff --git a/common/common.go b/common/common.go index 36b654900..b26ba7704 100644 --- a/common/common.go +++ b/common/common.go @@ -2,7 +2,7 @@ // See each sub-package for detail. package common -//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg common -path Common +//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg common -path Common // Must panics if err is not nil. func Must(err error) { diff --git a/common/crypto/crypto.go b/common/crypto/crypto.go index 7ef376cf8..8483b9540 100644 --- a/common/crypto/crypto.go +++ b/common/crypto/crypto.go @@ -1,4 +1,4 @@ // Package crypto provides common crypto libraries for V2Ray. package crypto -//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg crypto -path Crypto +//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg crypto -path Crypto diff --git a/common/errors/errorgen/main.go b/common/errors/errorgen/main.go new file mode 100644 index 000000000..859b62912 --- /dev/null +++ b/common/errors/errorgen/main.go @@ -0,0 +1,47 @@ +package main + +import ( + "flag" + "fmt" + "log" + "os" + "strings" + + "v2ray.com/core/common" +) + +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) + } + + common.Must2(fmt.Fprintln(file, "package", *pkg)) + common.Must2(fmt.Fprintln(file, "")) + common.Must2(fmt.Fprintln(file, "import \"v2ray.com/core/common/errors\"")) + common.Must2(fmt.Fprintln(file, "")) + common.Must2(fmt.Fprintln(file, "func newError(values ...interface{}) *errors.Error { return errors.New(values...).Path("+pathStr+") }")) + + common.Must(file.Close()) +} diff --git a/common/net/net.go b/common/net/net.go index 4d41e90af..9cddfe530 100644 --- a/common/net/net.go +++ b/common/net/net.go @@ -1,4 +1,4 @@ // Package net is a drop-in replacement to Golang's net package, with some more functionalities. package net -//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg net -path Net +//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg net -path Net diff --git a/common/protocol/protocol.go b/common/protocol/protocol.go index 7e52ec467..6c89418d4 100644 --- a/common/protocol/protocol.go +++ b/common/protocol/protocol.go @@ -1,3 +1,3 @@ package protocol -//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg protocol -path Protocol +//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg protocol -path Protocol diff --git a/common/retry/retry.go b/common/retry/retry.go index e9db3e539..c2f94ce3f 100644 --- a/common/retry/retry.go +++ b/common/retry/retry.go @@ -1,6 +1,6 @@ package retry -//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg retry -path Retry +//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg retry -path Retry import ( "time" diff --git a/core.go b/core.go index f59caa62d..0deb594be 100644 --- a/core.go +++ b/core.go @@ -9,7 +9,7 @@ // connections. package core -//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg core -path Core +//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg core -path Core import ( "fmt" diff --git a/main/main.go b/main/main.go index 84bc1bf96..2755ee730 100644 --- a/main/main.go +++ b/main/main.go @@ -1,6 +1,6 @@ package main -//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg main -path Main +//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg main -path Main import ( "flag" diff --git a/proxy/blackhole/blackhole.go b/proxy/blackhole/blackhole.go index b324a97ab..d7d6197c9 100644 --- a/proxy/blackhole/blackhole.go +++ b/proxy/blackhole/blackhole.go @@ -1,7 +1,7 @@ // Package blackhole is an outbound handler that blocks all connections. package blackhole -//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg blackhole -path Proxy,Blackhole +//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg blackhole -path Proxy,Blackhole import ( "context" diff --git a/proxy/dokodemo/dokodemo.go b/proxy/dokodemo/dokodemo.go index 8c1c0083f..777f14da7 100644 --- a/proxy/dokodemo/dokodemo.go +++ b/proxy/dokodemo/dokodemo.go @@ -1,6 +1,6 @@ package dokodemo -//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg dokodemo -path Proxy,Dokodemo +//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg dokodemo -path Proxy,Dokodemo import ( "context" diff --git a/proxy/freedom/freedom.go b/proxy/freedom/freedom.go index 0fbe4f400..49730bec4 100644 --- a/proxy/freedom/freedom.go +++ b/proxy/freedom/freedom.go @@ -1,6 +1,6 @@ package freedom -//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg freedom -path Proxy,Freedom +//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg freedom -path Proxy,Freedom import ( "context" diff --git a/proxy/http/http.go b/proxy/http/http.go index cc6a88f7c..8ed94a4ab 100644 --- a/proxy/http/http.go +++ b/proxy/http/http.go @@ -1,3 +1,3 @@ package http -//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg http -path Proxy,HTTP +//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg http -path Proxy,HTTP diff --git a/proxy/proxy.go b/proxy/proxy.go index 9ff5caad3..ea359f630 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -5,7 +5,7 @@ // 2. Register a config creator through common.RegisterConfig. package proxy -//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg proxy -path Proxy +//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg proxy -path Proxy import ( "context" diff --git a/proxy/shadowsocks/shadowsocks.go b/proxy/shadowsocks/shadowsocks.go index c2ca049e0..b01bc5b5f 100644 --- a/proxy/shadowsocks/shadowsocks.go +++ b/proxy/shadowsocks/shadowsocks.go @@ -13,4 +13,4 @@ // R.I.P Shadowsocks package shadowsocks -//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg shadowsocks -path Proxy,Shadowsocks +//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg shadowsocks -path Proxy,Shadowsocks diff --git a/proxy/socks/socks.go b/proxy/socks/socks.go index 2c77c26ab..c09977298 100644 --- a/proxy/socks/socks.go +++ b/proxy/socks/socks.go @@ -1,4 +1,4 @@ // Package socks provides implements of Socks protocol 4, 4a and 5. package socks -//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg socks -path Proxy,Socks +//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg socks -path Proxy,Socks diff --git a/proxy/vmess/encoding/encoding.go b/proxy/vmess/encoding/encoding.go index 26a6bf930..3772a39d3 100644 --- a/proxy/vmess/encoding/encoding.go +++ b/proxy/vmess/encoding/encoding.go @@ -1,3 +1,3 @@ package encoding -//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg encoding -path Proxy,VMess,Encoding +//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg encoding -path Proxy,VMess,Encoding diff --git a/proxy/vmess/inbound/inbound.go b/proxy/vmess/inbound/inbound.go index 9becd2497..ed21c7194 100644 --- a/proxy/vmess/inbound/inbound.go +++ b/proxy/vmess/inbound/inbound.go @@ -1,6 +1,6 @@ package inbound -//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg inbound -path Proxy,VMess,Inbound +//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg inbound -path Proxy,VMess,Inbound import ( "context" diff --git a/proxy/vmess/outbound/outbound.go b/proxy/vmess/outbound/outbound.go index dd218edfc..16cb42ba1 100644 --- a/proxy/vmess/outbound/outbound.go +++ b/proxy/vmess/outbound/outbound.go @@ -1,6 +1,6 @@ package outbound -//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg outbound -path Proxy,VMess,Outbound +//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg outbound -path Proxy,VMess,Outbound import ( "context" diff --git a/proxy/vmess/vmess.go b/proxy/vmess/vmess.go index 47baf2f60..d40f77f71 100644 --- a/proxy/vmess/vmess.go +++ b/proxy/vmess/vmess.go @@ -5,7 +5,7 @@ // clients with 'socks' for proxying. package vmess -//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg vmess -path Proxy,VMess +//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg vmess -path Proxy,VMess import ( "context" diff --git a/transport/internet/headers/http/http.go b/transport/internet/headers/http/http.go index 6ee743a3e..397b5497a 100644 --- a/transport/internet/headers/http/http.go +++ b/transport/internet/headers/http/http.go @@ -1,6 +1,6 @@ package http -//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg http -path Transport,Internet,Headers,HTTP +//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg http -path Transport,Internet,Headers,HTTP import ( "bytes" diff --git a/transport/internet/internet.go b/transport/internet/internet.go index 6ddfeb347..226e42aea 100644 --- a/transport/internet/internet.go +++ b/transport/internet/internet.go @@ -1,3 +1,3 @@ package internet -//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg internet -path Transport,Internet +//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg internet -path Transport,Internet diff --git a/transport/internet/kcp/kcp.go b/transport/internet/kcp/kcp.go index d44b01b04..c0c581686 100644 --- a/transport/internet/kcp/kcp.go +++ b/transport/internet/kcp/kcp.go @@ -5,4 +5,4 @@ // xtaci@github for translating to Golang package kcp -//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg kcp -path Transport,Internet,mKCP +//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg kcp -path Transport,Internet,mKCP diff --git a/transport/internet/tcp/tcp.go b/transport/internet/tcp/tcp.go index a40aa37e5..72409305c 100644 --- a/transport/internet/tcp/tcp.go +++ b/transport/internet/tcp/tcp.go @@ -1,3 +1,3 @@ package tcp -//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg tcp -path Transport,Internet,TCP +//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg tcp -path Transport,Internet,TCP diff --git a/transport/internet/tls/tls.go b/transport/internet/tls/tls.go index c9b6e6f4d..7ce8ecbaf 100644 --- a/transport/internet/tls/tls.go +++ b/transport/internet/tls/tls.go @@ -7,7 +7,7 @@ import ( "v2ray.com/core/common/buf" ) -//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg tls -path Transport,Internet,TLS +//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg tls -path Transport,Internet,TLS var ( _ buf.Writer = (*conn)(nil) diff --git a/transport/internet/udp/udp.go b/transport/internet/udp/udp.go index e45a3322d..977d8704b 100644 --- a/transport/internet/udp/udp.go +++ b/transport/internet/udp/udp.go @@ -1,3 +1,3 @@ package udp -//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg udp -path Transport,Internet,UDP +//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg udp -path Transport,Internet,UDP diff --git a/transport/internet/websocket/ws.go b/transport/internet/websocket/ws.go index 5c8e72583..7e5f1c7af 100644 --- a/transport/internet/websocket/ws.go +++ b/transport/internet/websocket/ws.go @@ -4,4 +4,4 @@ Websocket transport implements a HTTP(S) compliable, surveillance proof transpor */ package websocket -//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg websocket -path Transport,Internet,WebSocket +//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg websocket -path Transport,Internet,WebSocket diff --git a/v2ray.go b/v2ray.go index 687ca8edb..b09eb9a7a 100644 --- a/v2ray.go +++ b/v2ray.go @@ -121,7 +121,7 @@ func newSimpleServer(config *Config) (*simpleServer, error) { if p := policy.FromSpace(space); p == nil { p, err := app.CreateAppFromConfig(ctx, &policy.Config{ Level: map[uint32]*policy.Policy{ - 1: &policy.Policy{ + 1: { Timeout: &policy.Policy_Timeout{ ConnectionIdle: &policy.Second{ Value: 600,