This is an automated email from the ASF dual-hosted git repository. littlecui pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-service-center.git
The following commit(s) were added to refs/heads/master by this push: new 08f0814 SCB-561 Support ipv6 (#343) 08f0814 is described below commit 08f08147b744dcea017c3099121d8c11fdf78ef9 Author: little-cui <sure_0...@qq.com> AuthorDate: Wed May 9 10:06:59 2018 +0800 SCB-561 Support ipv6 (#343) * SCB-561 Support ipv6 * SCB-561 Support ipv6 * SCB-561 Support ipv6 * SCB-561 Support ipv6 --- etc/conf/app.conf | 3 +++ frontend/main.go | 6 ++++-- pkg/util/net.go | 6 +----- pkg/util/net_test.go | 31 +++++++++++++++++++++++++++++-- server/api.go | 17 +++++++++-------- server/core/backend/store/common.go | 1 + server/core/backend/store/extend.go | 2 +- server/core/common.go | 24 ++++++++++++------------ server/rest/server.go | 7 +------ server/rpc/server.go | 7 +------ server/server.go | 8 ++++++-- server/service/schema_test.go | 2 +- 12 files changed, 69 insertions(+), 45 deletions(-) diff --git a/etc/conf/app.conf b/etc/conf/app.conf index c2e7e88..df25c30 100644 --- a/etc/conf/app.conf +++ b/etc/conf/app.conf @@ -26,6 +26,9 @@ frontend_host_port = 30103 ################################################################### # sever options ################################################################### +# if you want to listen at ipv6 address, then set the httpaddr value like: +# httpaddr = 2400:A480:AAAA:200::159 (global scope) +# httpaddr = fe80::f816:3eff:fe17:c38b%eth0 (link-local scope) httpaddr = 127.0.0.1 httpport = 30100 diff --git a/frontend/main.go b/frontend/main.go index dc5a9a3..237b9ff 100644 --- a/frontend/main.go +++ b/frontend/main.go @@ -21,6 +21,8 @@ import ( "fmt" "github.com/astaxie/beego" + "net" + "net/url" ) type Config struct { @@ -40,8 +42,8 @@ func main() { flag.Parse() cfg := Config{} - cfg.scAddr = fmt.Sprintf("http://%s:%d", scIp, scPort) - cfg.frontendAddr = fmt.Sprintf("%s:%d", frontendIp, *port) + cfg.scAddr = fmt.Sprintf("http://%s/", net.JoinHostPort(url.PathEscape(scIp), fmt.Sprint(scPort))) + cfg.frontendAddr = net.JoinHostPort(frontendIp, fmt.Sprint(*port)) // run frontend web server Serve(cfg) diff --git a/pkg/util/net.go b/pkg/util/net.go index 457f9b6..777a96d 100644 --- a/pkg/util/net.go +++ b/pkg/util/net.go @@ -55,11 +55,7 @@ func ParseEndpoint(ep string) (string, error) { if err != nil { return "", err } - port := u.Port() - if len(port) > 0 { - return u.Hostname() + ":" + port, nil - } - return u.Hostname(), nil + return u.Host, nil } func ParseIpPort(addr string) IpPort { diff --git a/pkg/util/net_test.go b/pkg/util/net_test.go index e4d41b8..0116f3d 100644 --- a/pkg/util/net_test.go +++ b/pkg/util/net_test.go @@ -60,10 +60,37 @@ func TestInetNtoa(t *testing.T) { func TestParseIpPort(t *testing.T) { ipPort := ParseIpPort("0.0.0.0") if ipPort.IP != "0.0.0.0" || ipPort.Port != 0 { - t.Fatalf("ParseIpPort(0.0.0.0) error", n3) + t.Fatalf("ParseIpPort(0.0.0.0) error") } ipPort = ParseIpPort("0.0.0.0:1") if ipPort.IP != "0.0.0.0" || ipPort.Port != 1 { - t.Fatalf("ParseIpPort(0.0.0.0) error", n3) + t.Fatalf("ParseIpPort(0.0.0.0) error") + } +} + +func TestParseEndpoint(t *testing.T) { + ep, err := ParseEndpoint("") + if err != nil || len(ep) > 0 { + t.Fatalf("ParseEndpoint(\"\") failed, err = %s, ep = %s", err, ep) + } + ep, err = ParseEndpoint(":sssss") + if err == nil || len(ep) > 0 { + t.Fatalf("ParseEndpoint(\":sssss\") failed, err = %s, ep = %s", err, ep) + } + ep, err = ParseEndpoint("rest://127.0.0.1/?a=b") + if err != nil || ep != "127.0.0.1" { + t.Fatalf("ParseEndpoint(\"rest://127.0.0.1/?a=b\") failed, err = %s, ep = %s", err, ep) + } + ep, err = ParseEndpoint("rest://127.0.0.1:30100/?a=b") + if err != nil || ep != "127.0.0.1:30100" { + t.Fatalf("ParseEndpoint(\"rest://127.0.0.1:30100/?a=b\") failed, err = %s, ep = %s", err, ep) + } + ep, err = ParseEndpoint("rest://[2400:A480:AAAA:200::159]:30100/?a=b") + if err != nil || ep != "[2400:A480:AAAA:200::159]:30100" { + t.Fatalf("ParseEndpoint(\"rest://[2400:A480:AAAA:200::159]:30100/?a=b\") failed, err = %s, ep = %s", err, ep) + } + ep, err = ParseEndpoint("rest://[fe80::f816:3eff:fe17:c38b%25eht0]:30100/?a=b") + if err != nil || ep != "[fe80::f816:3eff:fe17:c38b%eht0]:30100" { + t.Fatalf("ParseEndpoint(\"rest://[fe80::f816:3eff:fe17:c38b%%25eht0]:30100/?a=b\") failed, err = %s, ep = %s", err, ep) } } diff --git a/server/api.go b/server/api.go index f434bdc..1672742 100644 --- a/server/api.go +++ b/server/api.go @@ -61,6 +61,7 @@ func (t APIType) String() string { type APIServer struct { HostName string + Listeners map[APIType]string Endpoints map[APIType]string restSrv *rest.Server rpcSrv *rpc.Server @@ -205,45 +206,45 @@ func (s *APIServer) MarkForked() { } func (s *APIServer) startRESTServer() (err error) { - ep, ok := s.Endpoints[REST] + addr, ok := s.Listeners[REST] if !ok { return } - s.restSrv, err = rs.NewServer(ep) + s.restSrv, err = rs.NewServer(addr) if err != nil { return } - util.Logger().Infof("Local listen address: %s, host: %s.", ep, s.HostName) + util.Logger().Infof("Local listen address: %s, host: %s.", addr, s.HostName) s.goroutine.Do(func(_ context.Context) { err := s.restSrv.Serve() if s.isClose { return } - util.Logger().Errorf(err, "error to start REST API server %s", ep) + util.Logger().Errorf(err, "error to start REST API server %s", addr) s.err <- err }) return } func (s *APIServer) startRPCServer() (err error) { - ep, ok := s.Endpoints[RPC] + addr, ok := s.Listeners[RPC] if !ok { return } - s.rpcSrv, err = rpc.NewServer(ep) + s.rpcSrv, err = rpc.NewServer(addr) if err != nil { return } - util.Logger().Infof("Local listen address: %s, host: %s.", ep, s.HostName) + util.Logger().Infof("Local listen address: %s, host: %s.", addr, s.HostName) s.goroutine.Do(func(_ context.Context) { err := s.rpcSrv.Serve() if s.isClose { return } - util.Logger().Errorf(err, "error to start RPC API server %s", ep) + util.Logger().Errorf(err, "error to start RPC API server %s", addr) s.err <- err }) return diff --git a/server/core/backend/store/common.go b/server/core/backend/store/common.go index 0de9e05..c9d9272 100644 --- a/server/core/backend/store/common.go +++ b/server/core/backend/store/common.go @@ -73,6 +73,7 @@ var TypeNames = []string{ INSTANCE: "INSTANCE", LEASE: "LEASE", ENDPOINTS: "ENDPOINTS", + typeEnd: "TYPEEND", } var TypeRoots = map[StoreType]string{ diff --git a/server/core/backend/store/extend.go b/server/core/backend/store/extend.go index bffd0c1..90b053f 100644 --- a/server/core/backend/store/extend.go +++ b/server/core/backend/store/extend.go @@ -60,8 +60,8 @@ func InstallType(e Entity) (id StoreType, err error) { } } + id = StoreType(len(TypeNames)) TypeNames = append(TypeNames, e.Name()) - id = StoreType(len(TypeNames) + 1) // +1 for typeEnd TypeRoots[id] = e.Prefix() TypeInitSize[id] = e.InitSize() diff --git a/server/core/common.go b/server/core/common.go index 0d2ae75..84d068e 100644 --- a/server/core/common.go +++ b/server/core/common.go @@ -86,7 +86,7 @@ func init() { hbModeRegex, _ := regexp.Compile(`^(push|pull)$`) numberAllowEmptyRegex, _ := regexp.Compile(`^[0-9]*$`) numberRegex, _ := regexp.Compile(`^[0-9]+$`) - epRegex, _ := regexp.Compile(`^[A-Za-z0-9:/?=&%_.-]+$`) + epRegex, _ := regexp.Compile(`.+`) simpleNameAllowEmptyRegex, _ := regexp.Compile(`^[A-Za-z0-9_.-]*$`) simpleNameRegex, _ := regexp.Compile(`^[A-Za-z0-9_.-]+$`) regionRegex, _ := regexp.Compile(`^[A-Za-z0-9_.-]+$`) @@ -100,18 +100,18 @@ func init() { serviceIdRule := &validate.ValidateRule{Min: 1, Length: 64, Regexp: serviceIdRegex} instanceStatusRule := &validate.ValidateRule{Regexp: instStatusRegex} updateInstStatusRule := &validate.ValidateRule{Regexp: updateInstStatusRegex} - serviceNameRule := &validate.ValidateRule{Min: 1, Max: 128, Regexp: nameRegex} - versionFuzzyRule := &validate.ValidateRule{Min: 1, Max: 128, Regexp: versionFuzzyRegex} + serviceNameRule := &validate.ValidateRule{Min: 1, Length: 128, Regexp: nameRegex} + versionFuzzyRule := &validate.ValidateRule{Min: 1, Length: 128, Regexp: versionFuzzyRegex} instanceRule := &validate.ValidateRule{Length: 64, Regexp: simpleNameAllowEmptyRegex} MicroServiceKeyValidator.AddRule("Environment", &validate.ValidateRule{Regexp: envRegex}) - MicroServiceKeyValidator.AddRule("AppId", &validate.ValidateRule{Min: 1, Max: 160, Regexp: nameRegex}) + MicroServiceKeyValidator.AddRule("AppId", &validate.ValidateRule{Min: 1, Length: 160, Regexp: nameRegex}) MicroServiceKeyValidator.AddRule("ServiceName", serviceNameRule) - MicroServiceKeyValidator.AddRule("Version", &validate.ValidateRule{Min: 1, Max: 64, Regexp: versionRegex}) + MicroServiceKeyValidator.AddRule("Version", &validate.ValidateRule{Min: 1, Length: 64, Regexp: versionRegex}) ServicePathValidator.AddRule("Path", &validate.ValidateRule{Regexp: pathRegex}) - FrameWKValidator.AddRule("Name", &validate.ValidateRule{Max: 64, Regexp: nameRegex}) + FrameWKValidator.AddRule("Name", &validate.ValidateRule{Length: 64, Regexp: nameRegex}) FrameWKValidator.AddRule("Version", &validate.ValidateRule{Length: 64}) MicroServiceValidator.AddRules(MicroServiceKeyValidator.GetRules()) @@ -132,7 +132,7 @@ func init() { var subSchemaValidator validate.Validator subSchemaValidator.AddRule("SchemaId", SchemaIdRule) - subSchemaValidator.AddRule("Summary", &validate.ValidateRule{Min: 1, Max: 512, Regexp: schemaSummaryRegex}) + subSchemaValidator.AddRule("Summary", &validate.ValidateRule{Min: 1, Length: 128, Regexp: schemaSummaryRegex}) subSchemaValidator.AddRule("Schema", &validate.ValidateRule{Min: 1}) SchemasValidator.AddRule("ServiceId", serviceIdRule) @@ -140,7 +140,7 @@ func init() { SchemaValidator.AddRules(subSchemaValidator.GetRules()) SchemaValidator.AddRule("ServiceId", serviceIdRule) - SchemaValidator.AddRule("Summary", &validate.ValidateRule{Max: 512, Regexp: schemaSummaryRegex}) + SchemaValidator.AddRule("Summary", &validate.ValidateRule{Length: 128, Regexp: schemaSummaryRegex}) GetServiceReqValidator.AddRule("ServiceId", serviceIdRule) @@ -150,7 +150,7 @@ func init() { ConsumerMsValidator.AddRules(MicroServiceKeyValidator.GetRules()) ProviderMsValidator.AddRules(MicroServiceKeyValidator.GetRules()) - ProviderMsValidator.AddRule("ServiceName", &validate.ValidateRule{Min: 1, Max: 128, Regexp: nameFuzzyRegex}) + ProviderMsValidator.AddRule("ServiceName", &validate.ValidateRule{Min: 1, Length: 128, Regexp: nameFuzzyRegex}) ProviderMsValidator.AddRule("Version", versionFuzzyRule) TagReqValidator.AddRule("ServiceId", serviceIdRule) @@ -168,7 +168,7 @@ func init() { MicroServiceInstanceValidator.AddRule("InstanceId", instanceRule) MicroServiceInstanceValidator.AddRule("ServiceId", serviceIdRule) MicroServiceInstanceValidator.AddRule("Endpoints", &validate.ValidateRule{Regexp: epRegex}) - MicroServiceInstanceValidator.AddRule("HostName", &validate.ValidateRule{Length: 64, Regexp: simpleNameRegex}) + MicroServiceInstanceValidator.AddRule("HostName", &validate.ValidateRule{Length: 64, Min: 1}) MicroServiceInstanceValidator.AddSub("HealthCheck", &HealthCheckInfoValidator) MicroServiceInstanceValidator.AddRule("Status", instanceStatusRule) MicroServiceInstanceValidator.AddSub("DataCenterInfo", &DataCenterInfoValidator) @@ -179,12 +179,12 @@ func init() { ServiceRuleValidator.AddRule("RuleType", &validate.ValidateRule{Regexp: ruleRegex}) ServiceRuleValidator.AddRule("Attribute", &validate.ValidateRule{Regexp: ruleAttrRegex}) - ServiceRuleValidator.AddRule("Pattern", &validate.ValidateRule{Max: 64, Min: 1}) + ServiceRuleValidator.AddRule("Pattern", &validate.ValidateRule{Length: 64, Min: 1}) ServiceRuleValidator.AddRule("Description", MicroServiceValidator.GetRule("Description")) FindInstanceReqValidator.AddRule("ConsumerServiceId", serviceIdRule) FindInstanceReqValidator.AddRule("AppId", MicroServiceKeyValidator.GetRule("AppId")) - FindInstanceReqValidator.AddRule("ServiceName", &validate.ValidateRule{Min: 1, Max: 128, Regexp: serviceNameForFindRegex}) + FindInstanceReqValidator.AddRule("ServiceName", &validate.ValidateRule{Min: 1, Length: 128, Regexp: serviceNameForFindRegex}) FindInstanceReqValidator.AddRule("VersionRule", versionFuzzyRule) FindInstanceReqValidator.AddRule("Tags", TagRule) diff --git a/server/rest/server.go b/server/rest/server.go index 1ef48ae..005d919 100644 --- a/server/rest/server.go +++ b/server/rest/server.go @@ -95,12 +95,7 @@ func DefaultServer() *rest.Server { return defaultRESTfulServer } -func NewServer(ep string) (srv *rest.Server, err error) { - ipAddr, err := util.ParseEndpoint(ep) - if err != nil { - return - } - +func NewServer(ipAddr string) (srv *rest.Server, err error) { srvCfg, err := LoadConfig() if err != nil { return diff --git a/server/rpc/server.go b/server/rpc/server.go index 683f72f..140323b 100644 --- a/server/rpc/server.go +++ b/server/rpc/server.go @@ -35,12 +35,7 @@ func (srv *Server) Serve() error { return srv.Server.Serve(srv.innerListener) } -func NewServer(ep string) (_ *Server, err error) { - ipAddr, err := util.ParseEndpoint(ep) - if err != nil { - return - } - +func NewServer(ipAddr string) (_ *Server, err error) { var grpcSrv *grpc.Server if core.ServerInfo.Config.SslEnabled { tlsConfig, err := sctls.GetServerTLSConfig() diff --git a/server/server.go b/server/server.go index 15e0abd..611b83c 100644 --- a/server/server.go +++ b/server/server.go @@ -29,6 +29,8 @@ import ( "github.com/apache/incubator-servicecomb-service-center/version" "github.com/astaxie/beego" "golang.org/x/net/context" + "net" + "net/url" "os" "strings" "time" @@ -167,16 +169,18 @@ func (s *ServiceCenterServer) startApiServer() { func (s *ServiceCenterServer) addEndpoint(t APIType, ip, port string) { if s.apiServer.Endpoints == nil { + s.apiServer.Listeners = map[APIType]string{} s.apiServer.Endpoints = map[APIType]string{} } if len(ip) == 0 { return } - address := util.StringJoin([]string{ip, port}, ":") + address := fmt.Sprintf("%s://%s/", t, net.JoinHostPort(url.PathEscape(ip), port)) if core.ServerInfo.Config.SslEnabled { address += "?sslEnabled=true" } - s.apiServer.Endpoints[t] = fmt.Sprintf("%s://%s", t, address) + s.apiServer.Listeners[t] = net.JoinHostPort(ip, port) + s.apiServer.Endpoints[t] = address } func (s *ServiceCenterServer) Stop() { diff --git a/server/service/schema_test.go b/server/service/schema_test.go index 0b73d29..1ef9117 100644 --- a/server/service/schema_test.go +++ b/server/service/schema_test.go @@ -34,7 +34,7 @@ const ( var ( tooLongSchemaId = strings.Repeat("x", 161) - tooLongSummary = strings.Repeat("x", 513) + tooLongSummary = strings.Repeat("x", 129) ) var _ = Describe("'Schema' service", func() { -- To stop receiving notification emails like this one, please contact little...@apache.org.