Skip to content

Commit

Permalink
add tls support
Browse files Browse the repository at this point in the history
  • Loading branch information
aliiohs committed Aug 9, 2020
1 parent f0017a8 commit 49ebaba
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 16 deletions.
1 change: 1 addition & 0 deletions common/constant/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const (
DUBBO_KEY = "dubbo"
RELEASE_KEY = "release"
ANYHOST_KEY = "anyhost"
SSL_ENABLED_KEY = "ssl-enabled"
)

const (
Expand Down
7 changes: 4 additions & 3 deletions config/protocol_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ import (

// ProtocolConfig is protocol configuration
type ProtocolConfig struct {
Name string `required:"true" yaml:"name" json:"name,omitempty" property:"name"`
Ip string `required:"true" yaml:"ip" json:"ip,omitempty" property:"ip"`
Port string `required:"true" yaml:"port" json:"port,omitempty" property:"port"`
Name string `required:"true" yaml:"name" json:"name,omitempty" property:"name"`
Ip string `required:"true" yaml:"ip" json:"ip,omitempty" property:"ip"`
Port string `required:"true" yaml:"port" json:"port,omitempty" property:"port"`
SslEnabled bool `required:"false" yaml:"sslEnabled" json:"sslEnabled,omitempty" property:"sslEnabled"`
}

// nolint
Expand Down
1 change: 1 addition & 0 deletions config/service_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ func (c *ServiceConfig) Export() error {
common.WithPort(port),
common.WithParams(urlMap),
common.WithParamsValue(constant.BEAN_NAME_KEY, c.id),
common.WithParamsValue(constant.SSL_ENABLED_KEY, strconv.FormatBool(proto.SslEnabled)),
common.WithMethods(strings.Split(methods, ",")),
common.WithToken(c.Token),
)
Expand Down
43 changes: 43 additions & 0 deletions config/ssl_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package config

import (
"github.com/dubbogo/getty"
)

var (
serverTlsConfigBuilder getty.TlsConfigBuilder
clientTlsConfigBuilder getty.TlsConfigBuilder
)

func GetServerTlsConfigBuilder() getty.TlsConfigBuilder {
return serverTlsConfigBuilder
}

func GetClientTlsConfigBuilder() getty.TlsConfigBuilder {
return clientTlsConfigBuilder
}

func SetServerTlsConfigBuilder(configBuilder getty.TlsConfigBuilder) {
serverTlsConfigBuilder = configBuilder
}

func SetClientTlsConfigBuilder(configBuilder getty.TlsConfigBuilder) {
clientTlsConfigBuilder = configBuilder
}
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module github.com/apache/dubbo-go

go 1.14

require (
cloud.google.com/go v0.39.0 // indirect
github.com/Microsoft/go-winio v0.4.13 // indirect
Expand Down Expand Up @@ -66,4 +68,4 @@ require (
k8s.io/kube-openapi v0.0.0-20191107075043-30be4d16710a // indirect
)

go 1.13
replace github.com/dubbogo/getty v1.2.2 => github.com/aliiohs/getty v1.1.1-0.20200802094147-169328c4ff38
1 change: 1 addition & 0 deletions protocol/dubbo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ func (c *Client) call(ct CallType, request *Request, response *Response, callbac
p.Service.Version = request.svcUrl.GetParam(constant.VERSION_KEY, "")
p.Service.Group = request.svcUrl.GetParam(constant.GROUP_KEY, "")
p.Service.Method = request.method
c.pool.sslEnabled = request.svcUrl.GetParamBool(constant.SSL_ENABLED_KEY, false)

p.Service.Timeout = c.opts.RequestTimeout
var timeout = request.svcUrl.GetParam(strings.Join([]string{constant.METHOD_KEYS, request.method + constant.RETRIES_KEY}, "."), "")
Expand Down
31 changes: 22 additions & 9 deletions protocol/dubbo/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (

import (
"github.com/apache/dubbo-go/common/logger"
"github.com/apache/dubbo-go/config"
)

type gettyRPCClient struct {
Expand All @@ -53,15 +54,26 @@ var (
)

func newGettyRPCClientConn(pool *gettyRPCClientPool, protocol, addr string) (*gettyRPCClient, error) {
c := &gettyRPCClient{
protocol: protocol,
addr: addr,
pool: pool,
gettyClient: getty.NewTCPClient(
var gettyClient getty.Client
if pool.sslEnabled {
gettyClient = getty.NewTCPClient(
getty.WithServerAddress(addr),
getty.WithConnectionNumber((int)(pool.rpcClient.conf.ConnectionNum)),
getty.WithReconnectInterval(pool.rpcClient.conf.ReconnectInterval),
getty.WithClientTlsConfigBuilder(config.GetClientTlsConfigBuilder()),
)
} else {
gettyClient = getty.NewTCPClient(
getty.WithServerAddress(addr),
getty.WithConnectionNumber((int)(pool.rpcClient.conf.ConnectionNum)),
getty.WithReconnectInterval(pool.rpcClient.conf.ReconnectInterval),
),
)
}
c := &gettyRPCClient{
protocol: protocol,
addr: addr,
pool: pool,
gettyClient: gettyClient,
}
go c.gettyClient.RunEventLoop(c.newSession)
idx := 1
Expand Down Expand Up @@ -288,9 +300,10 @@ func (c *gettyRPCClient) close() error {
}

type gettyRPCClientPool struct {
rpcClient *Client
size int // size of []*gettyRPCClient
ttl int64 // ttl of every gettyRPCClient, it is checked when getConn
rpcClient *Client
size int // size of []*gettyRPCClient
ttl int64 // ttl of every gettyRPCClient, it is checked when getConn
sslEnabled bool

sync.Mutex
conns []*gettyRPCClient
Expand Down
16 changes: 13 additions & 3 deletions protocol/dubbo/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

import (
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/common/constant"
"github.com/apache/dubbo-go/common/logger"
"github.com/apache/dubbo-go/config"
)
Expand Down Expand Up @@ -163,9 +164,18 @@ func (s *Server) Start(url common.URL) {
)

addr = url.Location
tcpServer = getty.NewTCPServer(
getty.WithLocalAddress(addr),
)
if url.GetParamBool(constant.SSL_ENABLED_KEY, false) {
tcpServer = getty.NewTCPServer(
getty.WithLocalAddress(addr),
getty.WithServerSslEnabled(url.GetParamBool(constant.SSL_ENABLED_KEY, false)),
getty.WithServerTlsConfigBuilder(config.GetServerTlsConfigBuilder()),
)

} else {
tcpServer = getty.NewTCPServer(
getty.WithLocalAddress(addr),
)
}
tcpServer.RunEventLoop(s.newSession)
logger.Debugf("s bind addr{%s} ok!", addr)
s.tcpServer = tcpServer
Expand Down

0 comments on commit 49ebaba

Please sign in to comment.