2016-09-12 05:01:24 +03:00
|
|
|
package endpoint
|
2016-09-11 17:49:48 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2016-09-12 05:01:24 +03:00
|
|
|
"time"
|
2018-04-18 00:56:43 +03:00
|
|
|
|
|
|
|
"github.com/streadway/amqp"
|
2016-09-11 17:49:48 +03:00
|
|
|
)
|
|
|
|
|
2017-02-10 15:27:02 +03:00
|
|
|
var errExpired = errors.New("expired")
|
|
|
|
|
2018-04-19 19:25:39 +03:00
|
|
|
// Protocol is the type of protocol that the endpoint represents.
|
|
|
|
type Protocol string
|
2016-09-11 17:49:48 +03:00
|
|
|
|
|
|
|
const (
|
2018-08-14 03:05:30 +03:00
|
|
|
// Local protocol
|
|
|
|
Local = Protocol("local")
|
2018-04-19 19:25:39 +03:00
|
|
|
// HTTP protocol
|
|
|
|
HTTP = Protocol("http")
|
|
|
|
// Disque protocol
|
|
|
|
Disque = Protocol("disque")
|
|
|
|
// GRPC protocol
|
|
|
|
GRPC = Protocol("grpc")
|
|
|
|
// Redis protocol
|
|
|
|
Redis = Protocol("redis")
|
|
|
|
// Kafka protocol
|
|
|
|
Kafka = Protocol("kafka")
|
|
|
|
// MQTT protocol
|
|
|
|
MQTT = Protocol("mqtt")
|
|
|
|
// AMQP protocol
|
|
|
|
AMQP = Protocol("amqp")
|
|
|
|
// SQS protocol
|
|
|
|
SQS = Protocol("sqs")
|
2018-08-07 22:04:04 +03:00
|
|
|
// NATS protocol
|
|
|
|
NATS = Protocol("nats")
|
2016-09-11 17:49:48 +03:00
|
|
|
)
|
|
|
|
|
2016-09-12 05:01:24 +03:00
|
|
|
// Endpoint represents an endpoint.
|
|
|
|
type Endpoint struct {
|
2018-04-19 19:25:39 +03:00
|
|
|
Protocol Protocol
|
2016-09-12 05:01:24 +03:00
|
|
|
Original string
|
2016-09-12 07:09:02 +03:00
|
|
|
GRPC struct {
|
|
|
|
Host string
|
|
|
|
Port int
|
|
|
|
}
|
|
|
|
Disque struct {
|
2016-09-12 05:01:24 +03:00
|
|
|
Host string
|
|
|
|
Port int
|
|
|
|
QueueName string
|
|
|
|
Options struct {
|
|
|
|
Replicate int
|
|
|
|
}
|
|
|
|
}
|
2017-01-13 18:01:34 +03:00
|
|
|
Redis struct {
|
2017-02-10 15:27:02 +03:00
|
|
|
Host string
|
|
|
|
Port int
|
|
|
|
Channel string
|
2017-01-13 18:01:34 +03:00
|
|
|
}
|
2017-03-06 23:18:59 +03:00
|
|
|
Kafka struct {
|
|
|
|
Host string
|
|
|
|
Port int
|
2018-11-15 13:59:08 +03:00
|
|
|
TopicName string
|
2017-03-06 23:18:59 +03:00
|
|
|
}
|
2017-03-15 20:45:35 +03:00
|
|
|
AMQP struct {
|
2018-04-18 00:56:43 +03:00
|
|
|
URI string
|
|
|
|
SSL bool
|
|
|
|
QueueName string
|
|
|
|
RouteKey string
|
|
|
|
Type string
|
|
|
|
Durable bool
|
|
|
|
AutoDelete bool
|
|
|
|
Internal bool
|
|
|
|
NoWait bool
|
|
|
|
Mandatory bool
|
|
|
|
Immediate bool
|
|
|
|
DeliveryMode uint8
|
2017-03-15 20:45:35 +03:00
|
|
|
}
|
2017-03-08 00:15:18 +03:00
|
|
|
MQTT struct {
|
2018-08-04 23:32:53 +03:00
|
|
|
Host string
|
|
|
|
Port int
|
|
|
|
QueueName string
|
|
|
|
Qos byte
|
|
|
|
Retained bool
|
|
|
|
CACertFile string
|
|
|
|
CertFile string
|
|
|
|
KeyFile string
|
2017-03-08 00:15:18 +03:00
|
|
|
}
|
2017-10-06 00:08:03 +03:00
|
|
|
SQS struct {
|
2019-03-14 01:41:49 +03:00
|
|
|
PlainURL string
|
2017-10-06 00:08:03 +03:00
|
|
|
QueueID string
|
|
|
|
Region string
|
|
|
|
CredPath string
|
|
|
|
CredProfile string
|
|
|
|
QueueName string
|
2019-03-11 20:49:25 +03:00
|
|
|
CreateQueue bool
|
2017-10-06 00:08:03 +03:00
|
|
|
}
|
2018-08-07 22:04:04 +03:00
|
|
|
NATS struct {
|
2019-10-30 18:17:40 +03:00
|
|
|
Host string
|
|
|
|
Port int
|
|
|
|
User string
|
|
|
|
Pass string
|
|
|
|
Topic string
|
|
|
|
Token string
|
|
|
|
TLS bool
|
|
|
|
TLSCert string
|
|
|
|
TLSKey string
|
2018-08-07 22:04:04 +03:00
|
|
|
}
|
2018-08-14 03:05:30 +03:00
|
|
|
Local struct {
|
|
|
|
Channel string
|
|
|
|
}
|
2016-09-12 05:01:24 +03:00
|
|
|
}
|
|
|
|
|
2018-04-19 19:25:39 +03:00
|
|
|
// Conn is an endpoint connection
|
|
|
|
type Conn interface {
|
2016-09-12 05:01:24 +03:00
|
|
|
Expired() bool
|
|
|
|
Send(val string) error
|
|
|
|
}
|
|
|
|
|
2018-04-19 19:25:39 +03:00
|
|
|
// Manager manages all endpoints
|
|
|
|
type Manager struct {
|
2018-08-14 03:05:30 +03:00
|
|
|
mu sync.RWMutex
|
|
|
|
conns map[string]Conn
|
|
|
|
publisher LocalPublisher
|
2016-09-11 17:49:48 +03:00
|
|
|
}
|
|
|
|
|
2018-04-19 19:25:39 +03:00
|
|
|
// NewManager returns a new manager
|
2018-08-14 03:05:30 +03:00
|
|
|
func NewManager(publisher LocalPublisher) *Manager {
|
2018-04-19 19:25:39 +03:00
|
|
|
epc := &Manager{
|
2018-08-14 03:05:30 +03:00
|
|
|
conns: make(map[string]Conn),
|
|
|
|
publisher: publisher,
|
2016-09-12 05:01:24 +03:00
|
|
|
}
|
|
|
|
go epc.Run()
|
|
|
|
return epc
|
|
|
|
}
|
2017-03-06 23:18:59 +03:00
|
|
|
|
2018-04-19 19:25:39 +03:00
|
|
|
// Run starts the managing of endpoints
|
|
|
|
func (epc *Manager) Run() {
|
2016-09-12 05:01:24 +03:00
|
|
|
for {
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
func() {
|
|
|
|
epc.mu.Lock()
|
|
|
|
defer epc.mu.Unlock()
|
|
|
|
for endpoint, conn := range epc.conns {
|
|
|
|
if conn.Expired() {
|
|
|
|
delete(epc.conns, endpoint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2016-09-11 17:49:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-19 19:25:39 +03:00
|
|
|
// Validate an endpoint url
|
|
|
|
func (epc *Manager) Validate(url string) error {
|
2016-09-11 17:49:48 +03:00
|
|
|
_, err := parseEndpoint(url)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-04-19 19:25:39 +03:00
|
|
|
// Send send a message to an endpoint
|
|
|
|
func (epc *Manager) Send(endpoint, msg string) error {
|
2017-02-10 15:27:02 +03:00
|
|
|
for {
|
|
|
|
epc.mu.Lock()
|
2018-11-15 13:59:08 +03:00
|
|
|
conn, exists := epc.conns[endpoint]
|
|
|
|
if !exists || conn.Expired() {
|
2017-02-10 15:27:02 +03:00
|
|
|
ep, err := parseEndpoint(endpoint)
|
|
|
|
if err != nil {
|
|
|
|
epc.mu.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch ep.Protocol {
|
|
|
|
default:
|
|
|
|
return errors.New("invalid protocol")
|
|
|
|
case HTTP:
|
2018-04-19 19:25:39 +03:00
|
|
|
conn = newHTTPConn(ep)
|
2017-02-10 15:27:02 +03:00
|
|
|
case Disque:
|
2018-04-19 19:25:39 +03:00
|
|
|
conn = newDisqueConn(ep)
|
2017-02-10 15:27:02 +03:00
|
|
|
case GRPC:
|
2018-04-19 19:25:39 +03:00
|
|
|
conn = newGRPCConn(ep)
|
2017-02-10 15:27:02 +03:00
|
|
|
case Redis:
|
2018-04-19 19:25:39 +03:00
|
|
|
conn = newRedisConn(ep)
|
2017-03-06 23:18:59 +03:00
|
|
|
case Kafka:
|
2018-04-19 19:25:39 +03:00
|
|
|
conn = newKafkaConn(ep)
|
2017-03-08 00:15:18 +03:00
|
|
|
case MQTT:
|
2018-04-19 19:25:39 +03:00
|
|
|
conn = newMQTTConn(ep)
|
2017-03-15 20:45:35 +03:00
|
|
|
case AMQP:
|
2018-04-19 19:25:39 +03:00
|
|
|
conn = newAMQPConn(ep)
|
2017-10-06 00:08:03 +03:00
|
|
|
case SQS:
|
2018-04-19 19:25:39 +03:00
|
|
|
conn = newSQSConn(ep)
|
2018-08-07 22:04:04 +03:00
|
|
|
case NATS:
|
|
|
|
conn = newNATSConn(ep)
|
2018-08-14 03:05:30 +03:00
|
|
|
case Local:
|
|
|
|
conn = newLocalConn(ep, epc.publisher)
|
2017-02-10 15:27:02 +03:00
|
|
|
}
|
|
|
|
epc.conns[endpoint] = conn
|
|
|
|
}
|
|
|
|
epc.mu.Unlock()
|
2018-04-19 19:25:39 +03:00
|
|
|
err := conn.Send(msg)
|
2016-09-12 05:01:24 +03:00
|
|
|
if err != nil {
|
2017-02-10 15:27:02 +03:00
|
|
|
if err == errExpired {
|
|
|
|
// it's possible that the connection has expired in-between
|
|
|
|
// the last conn.Expired() check and now. If so, we should
|
|
|
|
// just try the send again.
|
|
|
|
continue
|
|
|
|
}
|
2016-09-12 05:01:24 +03:00
|
|
|
return err
|
2016-09-11 17:49:48 +03:00
|
|
|
}
|
2017-02-10 15:27:02 +03:00
|
|
|
return nil
|
2016-09-12 05:01:24 +03:00
|
|
|
}
|
2016-09-11 17:49:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseEndpoint(s string) (Endpoint, error) {
|
|
|
|
var endpoint Endpoint
|
|
|
|
endpoint.Original = s
|
|
|
|
switch {
|
|
|
|
default:
|
|
|
|
return endpoint, errors.New("unknown scheme")
|
2018-08-14 03:05:30 +03:00
|
|
|
case strings.HasPrefix(s, "local:"):
|
|
|
|
endpoint.Protocol = Local
|
2016-09-11 17:49:48 +03:00
|
|
|
case strings.HasPrefix(s, "http:"):
|
|
|
|
endpoint.Protocol = HTTP
|
|
|
|
case strings.HasPrefix(s, "https:"):
|
2019-03-14 01:41:49 +03:00
|
|
|
if probeSQS(s) {
|
|
|
|
endpoint.SQS.PlainURL = s
|
|
|
|
endpoint.Protocol = SQS
|
|
|
|
} else {
|
|
|
|
endpoint.Protocol = HTTP
|
|
|
|
}
|
2016-09-11 17:49:48 +03:00
|
|
|
case strings.HasPrefix(s, "disque:"):
|
|
|
|
endpoint.Protocol = Disque
|
2016-09-12 07:09:02 +03:00
|
|
|
case strings.HasPrefix(s, "grpc:"):
|
|
|
|
endpoint.Protocol = GRPC
|
2017-01-13 18:01:34 +03:00
|
|
|
case strings.HasPrefix(s, "redis:"):
|
|
|
|
endpoint.Protocol = Redis
|
2017-03-06 23:18:59 +03:00
|
|
|
case strings.HasPrefix(s, "kafka:"):
|
|
|
|
endpoint.Protocol = Kafka
|
2017-03-15 20:45:35 +03:00
|
|
|
case strings.HasPrefix(s, "amqp:"):
|
|
|
|
endpoint.Protocol = AMQP
|
|
|
|
case strings.HasPrefix(s, "amqps:"):
|
|
|
|
endpoint.Protocol = AMQP
|
2017-03-08 00:15:18 +03:00
|
|
|
case strings.HasPrefix(s, "mqtt:"):
|
|
|
|
endpoint.Protocol = MQTT
|
2017-10-06 00:08:03 +03:00
|
|
|
case strings.HasPrefix(s, "sqs:"):
|
|
|
|
endpoint.Protocol = SQS
|
2018-08-07 22:04:04 +03:00
|
|
|
case strings.HasPrefix(s, "nats:"):
|
|
|
|
endpoint.Protocol = NATS
|
2016-09-11 17:49:48 +03:00
|
|
|
}
|
2017-01-13 18:01:34 +03:00
|
|
|
|
2016-09-11 17:49:48 +03:00
|
|
|
s = s[strings.Index(s, ":")+1:]
|
|
|
|
if !strings.HasPrefix(s, "//") {
|
|
|
|
return endpoint, errors.New("missing the two slashes")
|
|
|
|
}
|
2017-01-13 18:01:34 +03:00
|
|
|
|
2016-09-11 17:49:48 +03:00
|
|
|
sqp := strings.Split(s[2:], "?")
|
|
|
|
sp := strings.Split(sqp[0], "/")
|
|
|
|
s = sp[0]
|
|
|
|
if s == "" {
|
2018-08-14 03:05:30 +03:00
|
|
|
if endpoint.Protocol == Local {
|
|
|
|
return endpoint, errors.New("missing channel")
|
|
|
|
}
|
2016-09-11 17:49:48 +03:00
|
|
|
return endpoint, errors.New("missing host")
|
|
|
|
}
|
2017-01-13 18:01:34 +03:00
|
|
|
|
2018-08-14 03:05:30 +03:00
|
|
|
// Local PubSub channel
|
|
|
|
// local://<channel>
|
|
|
|
if endpoint.Protocol == Local {
|
|
|
|
endpoint.Local.Channel = s
|
|
|
|
}
|
2016-09-12 07:09:02 +03:00
|
|
|
if endpoint.Protocol == GRPC {
|
|
|
|
dp := strings.Split(s, ":")
|
|
|
|
switch len(dp) {
|
|
|
|
default:
|
|
|
|
return endpoint, errors.New("invalid grpc url")
|
|
|
|
case 1:
|
|
|
|
endpoint.GRPC.Host = dp[0]
|
|
|
|
endpoint.GRPC.Port = 80
|
|
|
|
case 2:
|
|
|
|
endpoint.GRPC.Host = dp[0]
|
|
|
|
n, err := strconv.ParseUint(dp[1], 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
return endpoint, errors.New("invalid grpc url")
|
|
|
|
}
|
|
|
|
endpoint.GRPC.Port = int(n)
|
|
|
|
}
|
|
|
|
}
|
2017-01-13 18:01:34 +03:00
|
|
|
|
|
|
|
if endpoint.Protocol == Redis {
|
|
|
|
dp := strings.Split(s, ":")
|
|
|
|
switch len(dp) {
|
|
|
|
default:
|
|
|
|
return endpoint, errors.New("invalid redis url")
|
|
|
|
case 1:
|
|
|
|
endpoint.Redis.Host = dp[0]
|
|
|
|
endpoint.Redis.Port = 6379
|
|
|
|
case 2:
|
|
|
|
endpoint.Redis.Host = dp[0]
|
|
|
|
n, err := strconv.ParseUint(dp[1], 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
return endpoint, errors.New("invalid redis url port")
|
|
|
|
}
|
|
|
|
endpoint.Redis.Port = int(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sp) > 1 {
|
|
|
|
var err error
|
|
|
|
endpoint.Redis.Channel, err = url.QueryUnescape(sp[1])
|
|
|
|
if err != nil {
|
|
|
|
return endpoint, errors.New("invalid redis channel name")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-11 17:49:48 +03:00
|
|
|
if endpoint.Protocol == Disque {
|
|
|
|
dp := strings.Split(s, ":")
|
|
|
|
switch len(dp) {
|
|
|
|
default:
|
|
|
|
return endpoint, errors.New("invalid disque url")
|
|
|
|
case 1:
|
|
|
|
endpoint.Disque.Host = dp[0]
|
|
|
|
endpoint.Disque.Port = 7711
|
|
|
|
case 2:
|
|
|
|
endpoint.Disque.Host = dp[0]
|
|
|
|
n, err := strconv.ParseUint(dp[1], 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
return endpoint, errors.New("invalid disque url")
|
|
|
|
}
|
|
|
|
endpoint.Disque.Port = int(n)
|
|
|
|
}
|
|
|
|
if len(sp) > 1 {
|
|
|
|
var err error
|
|
|
|
endpoint.Disque.QueueName, err = url.QueryUnescape(sp[1])
|
|
|
|
if err != nil {
|
|
|
|
return endpoint, errors.New("invalid disque queue name")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(sqp) > 1 {
|
|
|
|
m, err := url.ParseQuery(sqp[1])
|
|
|
|
if err != nil {
|
|
|
|
return endpoint, errors.New("invalid disque url")
|
|
|
|
}
|
|
|
|
for key, val := range m {
|
|
|
|
if len(val) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch key {
|
|
|
|
case "replicate":
|
|
|
|
n, err := strconv.ParseUint(val[0], 10, 8)
|
|
|
|
if err != nil {
|
|
|
|
return endpoint, errors.New("invalid disque replicate value")
|
|
|
|
}
|
|
|
|
endpoint.Disque.Options.Replicate = int(n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if endpoint.Disque.QueueName == "" {
|
|
|
|
return endpoint, errors.New("missing disque queue name")
|
|
|
|
}
|
|
|
|
}
|
2017-01-13 18:01:34 +03:00
|
|
|
|
2017-03-06 23:18:59 +03:00
|
|
|
if endpoint.Protocol == Kafka {
|
|
|
|
// Parsing connection from URL string
|
|
|
|
hp := strings.Split(s, ":")
|
|
|
|
switch len(hp) {
|
|
|
|
default:
|
|
|
|
return endpoint, errors.New("invalid kafka url")
|
|
|
|
case 1:
|
|
|
|
endpoint.Kafka.Host = hp[0]
|
|
|
|
endpoint.Kafka.Port = 9092
|
|
|
|
case 2:
|
|
|
|
n, err := strconv.ParseUint(hp[1], 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
return endpoint, errors.New("invalid kafka url port")
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoint.Kafka.Host = hp[0]
|
|
|
|
endpoint.Kafka.Port = int(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parsing Kafka queue name
|
|
|
|
if len(sp) > 1 {
|
|
|
|
var err error
|
2018-11-15 13:59:08 +03:00
|
|
|
endpoint.Kafka.TopicName, err = url.QueryUnescape(sp[1])
|
2017-03-06 23:18:59 +03:00
|
|
|
if err != nil {
|
|
|
|
return endpoint, errors.New("invalid kafka topic name")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Throw error if we not provide any queue name
|
2018-11-15 13:59:08 +03:00
|
|
|
if endpoint.Kafka.TopicName == "" {
|
2017-03-06 23:18:59 +03:00
|
|
|
return endpoint, errors.New("missing kafka topic name")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-08 00:15:18 +03:00
|
|
|
if endpoint.Protocol == MQTT {
|
|
|
|
// Parsing connection from URL string
|
|
|
|
hp := strings.Split(s, ":")
|
|
|
|
switch len(hp) {
|
|
|
|
default:
|
|
|
|
return endpoint, errors.New("invalid MQTT url")
|
|
|
|
case 1:
|
|
|
|
endpoint.MQTT.Host = hp[0]
|
|
|
|
endpoint.MQTT.Port = 1883
|
|
|
|
case 2:
|
|
|
|
n, err := strconv.ParseUint(hp[1], 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
return endpoint, errors.New("invalid MQTT url port")
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoint.MQTT.Host = hp[0]
|
|
|
|
endpoint.MQTT.Port = int(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parsing MQTT queue name
|
|
|
|
if len(sp) > 1 {
|
|
|
|
var err error
|
2019-02-12 16:58:43 +03:00
|
|
|
var parts []string
|
|
|
|
for _, part := range sp[1:] {
|
|
|
|
part, err = url.QueryUnescape(part)
|
|
|
|
if err != nil {
|
|
|
|
return endpoint, errors.New("invalid MQTT topic name")
|
|
|
|
}
|
|
|
|
parts = append(parts, part)
|
2017-03-08 00:15:18 +03:00
|
|
|
}
|
2019-02-12 16:58:43 +03:00
|
|
|
endpoint.MQTT.QueueName = strings.Join(parts, "/")
|
2017-03-08 00:15:18 +03:00
|
|
|
}
|
|
|
|
|
2017-03-08 00:39:49 +03:00
|
|
|
// Parsing additional params
|
|
|
|
if len(sqp) > 1 {
|
|
|
|
m, err := url.ParseQuery(sqp[1])
|
|
|
|
if err != nil {
|
|
|
|
return endpoint, errors.New("invalid MQTT url")
|
|
|
|
}
|
|
|
|
for key, val := range m {
|
|
|
|
if len(val) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch key {
|
|
|
|
case "qos":
|
|
|
|
n, err := strconv.ParseUint(val[0], 10, 8)
|
|
|
|
if err != nil {
|
|
|
|
return endpoint, errors.New("invalid MQTT qos value")
|
|
|
|
}
|
|
|
|
endpoint.MQTT.Qos = byte(n)
|
|
|
|
case "retained":
|
|
|
|
n, err := strconv.ParseUint(val[0], 10, 8)
|
|
|
|
if err != nil {
|
|
|
|
return endpoint, errors.New("invalid MQTT retained value")
|
|
|
|
}
|
|
|
|
|
|
|
|
if n != 1 && n != 0 {
|
|
|
|
return endpoint, errors.New("invalid MQTT retained, should be [0, 1]")
|
|
|
|
}
|
|
|
|
|
|
|
|
if n == 1 {
|
|
|
|
endpoint.MQTT.Retained = true
|
|
|
|
}
|
2018-08-04 23:32:53 +03:00
|
|
|
case "cacert":
|
|
|
|
endpoint.MQTT.CACertFile = val[0]
|
|
|
|
case "cert":
|
|
|
|
endpoint.MQTT.CertFile = val[0]
|
|
|
|
case "key":
|
|
|
|
endpoint.MQTT.KeyFile = val[0]
|
2017-03-08 00:39:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-08 00:15:18 +03:00
|
|
|
// Throw error if we not provide any queue name
|
|
|
|
if endpoint.MQTT.QueueName == "" {
|
|
|
|
return endpoint, errors.New("missing MQTT topic name")
|
|
|
|
}
|
|
|
|
}
|
2017-10-06 00:08:03 +03:00
|
|
|
// Basic SQS connection strings in HOOKS interface
|
|
|
|
// sqs://<region>:<queue_id>/<queue_name>/?params=value
|
|
|
|
//
|
|
|
|
// params are:
|
|
|
|
//
|
|
|
|
// credpath - path where aws credentials are located
|
|
|
|
// credprofile - credential profile
|
|
|
|
if endpoint.Protocol == SQS {
|
2019-03-14 01:41:49 +03:00
|
|
|
if endpoint.SQS.PlainURL == "" {
|
|
|
|
// Parsing connection from URL string
|
|
|
|
hp := strings.Split(s, ":")
|
|
|
|
switch len(hp) {
|
|
|
|
default:
|
|
|
|
return endpoint, errors.New("invalid SQS url")
|
|
|
|
case 2:
|
|
|
|
endpoint.SQS.Region = hp[0]
|
|
|
|
endpoint.SQS.QueueID = hp[1]
|
|
|
|
}
|
2017-10-06 00:08:03 +03:00
|
|
|
|
2019-03-14 01:41:49 +03:00
|
|
|
// Parsing SQS queue name
|
|
|
|
if len(sp) > 1 {
|
|
|
|
var err error
|
|
|
|
endpoint.SQS.QueueName, err = url.QueryUnescape(sp[1])
|
|
|
|
if err != nil {
|
|
|
|
return endpoint, errors.New("invalid SQS queue name")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Throw error if we not provide any queue name
|
|
|
|
if endpoint.SQS.QueueName == "" {
|
|
|
|
return endpoint, errors.New("missing SQS queue name")
|
2017-10-06 00:08:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parsing additional params
|
|
|
|
if len(sqp) > 1 {
|
|
|
|
m, err := url.ParseQuery(sqp[1])
|
|
|
|
if err != nil {
|
|
|
|
return endpoint, errors.New("invalid SQS url")
|
|
|
|
}
|
|
|
|
for key, val := range m {
|
|
|
|
if len(val) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch key {
|
|
|
|
case "credpath":
|
|
|
|
endpoint.SQS.CredPath = val[0]
|
|
|
|
case "credprofile":
|
|
|
|
endpoint.SQS.CredProfile = val[0]
|
2019-03-11 20:49:25 +03:00
|
|
|
case "createqueue":
|
|
|
|
switch strings.ToLower(val[0]) {
|
|
|
|
case "0", "false":
|
|
|
|
default:
|
|
|
|
endpoint.SQS.CreateQueue = true
|
|
|
|
}
|
2017-10-06 00:08:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-08 00:15:18 +03:00
|
|
|
|
2017-03-15 20:45:35 +03:00
|
|
|
// Basic AMQP connection strings in HOOKS interface
|
|
|
|
// amqp://guest:guest@localhost:5672/<queue_name>/?params=value
|
2018-08-04 23:32:53 +03:00
|
|
|
// or amqp://guest:guest@localhost:5672/<namespace>/<queue_name>/?params=value
|
2017-03-15 20:45:35 +03:00
|
|
|
//
|
|
|
|
// Default params are:
|
|
|
|
//
|
|
|
|
// Mandatory - false
|
|
|
|
// Immeditate - false
|
|
|
|
// Durable - true
|
|
|
|
// Routing-Key - tile38
|
|
|
|
//
|
|
|
|
// - "route" - [string] routing key
|
|
|
|
//
|
|
|
|
if endpoint.Protocol == AMQP {
|
|
|
|
// Bind connection information
|
|
|
|
endpoint.AMQP.URI = s
|
2018-04-18 00:56:43 +03:00
|
|
|
endpoint.AMQP.Type = "direct"
|
|
|
|
endpoint.AMQP.Durable = true
|
|
|
|
endpoint.AMQP.DeliveryMode = amqp.Transient
|
2017-03-15 20:45:35 +03:00
|
|
|
|
2018-08-04 23:32:53 +03:00
|
|
|
// Fix incase of namespace, e.g. example.com/namespace/queue
|
|
|
|
// but not example.com/queue/ - with an endslash.
|
|
|
|
if len(sp) > 2 && len(sp[2]) > 0 {
|
|
|
|
endpoint.AMQP.URI = endpoint.AMQP.URI + "/" + sp[1]
|
|
|
|
sp = append([]string{endpoint.AMQP.URI}, sp[2:]...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bind queue name with no namespace
|
|
|
|
if len(sp) > 1 {
|
2017-03-15 20:45:35 +03:00
|
|
|
var err error
|
|
|
|
endpoint.AMQP.QueueName, err = url.QueryUnescape(sp[1])
|
|
|
|
if err != nil {
|
|
|
|
return endpoint, errors.New("invalid AMQP queue name")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parsing additional attributes
|
|
|
|
if len(sqp) > 1 {
|
|
|
|
m, err := url.ParseQuery(sqp[1])
|
|
|
|
if err != nil {
|
|
|
|
return endpoint, errors.New("invalid AMQP url")
|
|
|
|
}
|
|
|
|
for key, val := range m {
|
|
|
|
if len(val) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch key {
|
|
|
|
case "route":
|
|
|
|
endpoint.AMQP.RouteKey = val[0]
|
2018-04-18 00:56:43 +03:00
|
|
|
case "type":
|
|
|
|
endpoint.AMQP.Type = val[0]
|
|
|
|
case "durable":
|
|
|
|
endpoint.AMQP.Durable = queryBool(val[0])
|
|
|
|
case "internal":
|
|
|
|
endpoint.AMQP.Internal = queryBool(val[0])
|
|
|
|
case "no_wait":
|
|
|
|
endpoint.AMQP.NoWait = queryBool(val[0])
|
|
|
|
case "auto_delete":
|
|
|
|
endpoint.AMQP.AutoDelete = queryBool(val[0])
|
|
|
|
case "immediate":
|
|
|
|
endpoint.AMQP.Immediate = queryBool(val[0])
|
|
|
|
case "mandatory":
|
|
|
|
endpoint.AMQP.Mandatory = queryBool(val[0])
|
|
|
|
case "delivery_mode":
|
|
|
|
endpoint.AMQP.DeliveryMode = uint8(queryInt(val[0]))
|
2017-03-15 20:45:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(endpoint.Original, "amqps:") {
|
|
|
|
endpoint.AMQP.SSL = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if endpoint.AMQP.QueueName == "" {
|
|
|
|
return endpoint, errors.New("missing AMQP queue name")
|
|
|
|
}
|
|
|
|
|
|
|
|
if endpoint.AMQP.RouteKey == "" {
|
|
|
|
endpoint.AMQP.RouteKey = "tile38"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-07 22:04:04 +03:00
|
|
|
// Basic NATS connection strings in HOOKS interface
|
|
|
|
// nats://<host>:<port>/<topic_name>/?params=value
|
|
|
|
//
|
|
|
|
// params are:
|
|
|
|
//
|
|
|
|
// user - username
|
|
|
|
// pass - password
|
|
|
|
// when user or pass is not set then login without password is used
|
|
|
|
if endpoint.Protocol == NATS {
|
|
|
|
// Parsing connection from URL string
|
|
|
|
hp := strings.Split(s, ":")
|
|
|
|
switch len(hp) {
|
|
|
|
default:
|
|
|
|
return endpoint, errors.New("invalid SQS url")
|
|
|
|
case 2:
|
|
|
|
endpoint.NATS.Host = hp[0]
|
|
|
|
port, err := strconv.Atoi(hp[1])
|
|
|
|
if err != nil {
|
|
|
|
endpoint.NATS.Port = 4222 // default nats port
|
|
|
|
} else {
|
|
|
|
endpoint.NATS.Port = port
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parsing NATS topic name
|
|
|
|
if len(sp) > 1 {
|
|
|
|
var err error
|
|
|
|
endpoint.NATS.Topic, err = url.QueryUnescape(sp[1])
|
|
|
|
if err != nil {
|
|
|
|
return endpoint, errors.New("invalid NATS topic name")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parsing additional params
|
|
|
|
if len(sqp) > 1 {
|
|
|
|
m, err := url.ParseQuery(sqp[1])
|
|
|
|
if err != nil {
|
|
|
|
return endpoint, errors.New("invalid NATS url")
|
|
|
|
}
|
|
|
|
for key, val := range m {
|
|
|
|
if len(val) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch key {
|
|
|
|
case "user":
|
|
|
|
endpoint.NATS.User = val[0]
|
|
|
|
case "pass":
|
|
|
|
endpoint.NATS.Pass = val[0]
|
2019-10-30 18:17:40 +03:00
|
|
|
case "token":
|
|
|
|
endpoint.NATS.Token = val[0]
|
|
|
|
case "tls":
|
|
|
|
endpoint.NATS.TLS = queryBool(val[0])
|
|
|
|
case "tlscert":
|
|
|
|
endpoint.NATS.TLSCert = val[0]
|
|
|
|
case "tlskey":
|
|
|
|
endpoint.NATS.TLSKey = val[0]
|
2018-08-07 22:04:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-11 17:49:48 +03:00
|
|
|
return endpoint, nil
|
|
|
|
}
|
2018-04-18 00:56:43 +03:00
|
|
|
|
|
|
|
func queryInt(s string) int {
|
|
|
|
x, _ := strconv.ParseInt(s, 10, 64)
|
|
|
|
return int(x)
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryBool(s string) bool {
|
|
|
|
if len(s) > 0 {
|
|
|
|
if s[0] >= '1' && s[0] <= '9' {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
switch s[0] {
|
|
|
|
case 'Y', 'y', 'T', 't':
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|