From 6a44ebfd1f672fec560bc77d44c038a970696843 Mon Sep 17 00:00:00 2001 From: Asim Date: Sat, 16 Jan 2016 23:05:11 +0000 Subject: [PATCH] Add secure option. This is only really for development in the http broker --- broker/http_broker.go | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/broker/http_broker.go b/broker/http_broker.go index bd5cdc68..d50d8b12 100644 --- a/broker/http_broker.go +++ b/broker/http_broker.go @@ -20,6 +20,7 @@ import ( log "github.com/golang/glog" "github.com/micro/go-micro/errors" "github.com/micro/go-micro/registry" + mls "github.com/micro/misc/lib/tls" "github.com/pborman/uuid" ) @@ -82,7 +83,12 @@ func newTransport() *http.Transport { return t } -func newHttpBroker(addrs []string, opt ...Option) Broker { +func newHttpBroker(addrs []string, opts ...Option) Broker { + var options Options + for _, o := range opts { + o(&options) + } + addr := ":0" if len(addrs) > 0 && len(addrs[0]) > 0 { addr = addrs[0] @@ -91,6 +97,7 @@ func newHttpBroker(addrs []string, opt ...Option) Broker { return &httpBroker{ id: "broker-" + uuid.NewUUID().String(), address: addr, + opts: options, c: &http.Client{Transport: newTransport()}, subscribers: make(map[string][]*httpSubscriber), unsubscribe: make(chan *httpSubscriber), @@ -120,6 +127,8 @@ func (h *httpSubscriber) Topic() string { func (h *httpSubscriber) Unsubscribe() error { h.ch <- h + // artificial delay + time.Sleep(time.Millisecond * 10) return nil } @@ -131,7 +140,19 @@ func (h *httpBroker) start() error { return nil } - l, err := net.Listen("tcp", h.address) + var l net.Listener + var err error + + if h.opts.Secure { + cert, err := mls.Certificate(h.address) + if err != nil { + return err + } + l, err = tls.Listen("tcp", h.address, &tls.Config{Certificates: []tls.Certificate{cert}}) + } else { + l, err = net.Listen("tcp", h.address) + } + if err != nil { return err } @@ -236,6 +257,13 @@ func (h *httpBroker) Disconnect() error { } func (h *httpBroker) Init(opts ...Option) error { + var options Options + for _, o := range opts { + o(&options) + } + + h.opts = options + if len(h.id) == 0 { h.id = "broker-" + uuid.NewUUID().String() } @@ -262,6 +290,7 @@ func (h *httpBroker) Publish(topic string, msg *Message, opts ...PublishOption) fn := func(node *registry.Node, b io.Reader) { scheme := "http" + // check if secure is added in metadata if node.Metadata["secure"] == "true" { scheme = "https"