All Projects → antmanler → gnatsd-gw

antmanler / gnatsd-gw

Licence: Apache-2.0 license
A utility pkg make it easy to write a gateway in front of gnatsd with middlewares

Programming Languages

go
31211 projects - #10 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to gnatsd-gw

default-gateway
Get the default network gateway, cross-platform.
Stars: ✭ 77 (+381.25%)
Mutual labels:  gateway
cashier
Cashier is an Elixir library that aims to be an easy to use payment gateway, whilst offering the fault tolerance and scalability benefits of being built on top of Erlang/OTP
Stars: ✭ 43 (+168.75%)
Mutual labels:  gateway
emerald-web3-gateway
The Web3 Gateway for the Oasis Emerald ParaTime.
Stars: ✭ 19 (+18.75%)
Mutual labels:  gateway
safe-client-gateway
Gateway service for Safe apps that serves transaction data conforming with the UI of our mobile apps
Stars: ✭ 33 (+106.25%)
Mutual labels:  gateway
QuickStart-admin-Cloud
基于spring boot 2.0.8 目前集成了spring security oauth2 (server and client)、springboot-admin、openfeign、hystrix,zuul(后续会替换成gateway),config.....等组件
Stars: ✭ 25 (+56.25%)
Mutual labels:  gateway
microservices-v9
Learn Microservices with Spring Boot - v9
Stars: ✭ 40 (+150%)
Mutual labels:  gateway
lemon
The micro service gateway framework. Dubbo, HTTP etc.
Stars: ✭ 17 (+6.25%)
Mutual labels:  gateway
piccolo
Netty4长连接网关
Stars: ✭ 19 (+18.75%)
Mutual labels:  gateway
LoraGW-Setup
SX1301 Lora Concentrator Raspberry PI based gateway setup
Stars: ✭ 70 (+337.5%)
Mutual labels:  gateway
torbox
Container-based Tor access point (Anonymizing Middlebox).
Stars: ✭ 52 (+225%)
Mutual labels:  gateway
PSAVanCanBridge
VAN - CAN protocol bridge (V2C) for cars made by PSA Group (Peugeot, Citroen)
Stars: ✭ 67 (+318.75%)
Mutual labels:  gateway
SerialToTCPBridgeProtocol
An error tolerant serial UART to TCP connection, raw data bridge.
Stars: ✭ 16 (+0%)
Mutual labels:  gateway
mbed-edge
The Mbed Edge
Stars: ✭ 24 (+50%)
Mutual labels:  gateway
graphql-portal-dashboard
Dashboard to manage GraphQL API Gateway
Stars: ✭ 35 (+118.75%)
Mutual labels:  gateway
laravel-hyperpay
Laravel package for Hyperpay payment gateway in MENA.
Stars: ✭ 14 (-12.5%)
Mutual labels:  gateway
gluu-gateway
Gluu API 🚀 and Web Gateway 🎯
Stars: ✭ 29 (+81.25%)
Mutual labels:  gateway
scimgateway
Using SCIM protocol as a gateway for user provisioning to other endpoints
Stars: ✭ 98 (+512.5%)
Mutual labels:  gateway
laravel-viva-payments
A Laravel package for integrating the Viva Payments gateway
Stars: ✭ 29 (+81.25%)
Mutual labels:  gateway
gateway.js
The gateway to Discord.
Stars: ✭ 23 (+43.75%)
Mutual labels:  gateway
RAK831-Zero
Pi Zero RAK831 Adapter board
Stars: ✭ 98 (+512.5%)
Mutual labels:  gateway

gnatsd-gw

A utility lib make it easy to write a gateway in front of gnatsd with middlewares

Features:

  • Leverage the official zero-alloc parser to handle connections
  • Can inspect most commands in nats proto
  • TCP & Websocket gateway
  • Embeded as a library

How it works

Thanks to the clean design of gnatsd, I synced the parser.go from gnatsd/server/parser.go and rewrite missing method in client wrap it as a unidirectional fowarder leaving custom callbacks to handle protocal commands.

  • CLIENT -> BACKEND share the logical of serving a client in nats server,
  • BACKEND -> CLIENT share the logical of serving a router.

Basic usage

Building a websocket gateway to use nats in browser.

package main

import (
	"fmt"
	"log"
	"net"
	"net/http"
	"os"

	fwd "github.com/antmanler/gnatsd-gw/gnatsdgw/server"
	"github.com/antmanler/gnatsd-gw/gnatsdgw/ws"
)

func main() {

	handler, err := ws.NewHandler(
		&fwd.Options{},
		func() (net.Conn, error) {
			return net.Dial("tcp", "localhost:4222")
		},
		nil,
		nil,
	)
	if err != nil {
		log.Fatalln(err)
	}
	http.Handle("/nats", handler)
	http.ListenAndServe("0.0.0.0:8910", nil)
}

Supported NATS commands

To modify the original command a newCmd should be returned with err is nil, if err is not nil, a -ERR command will be sent to client, and close the underlying connections.

// Handlers ars handler to modify of filter client side commands
type Handlers struct {
	// peer to backend
	OnConnect     ConnectHandler
	OnPublish     PublishHandler
	OnSubscribe   SubscribeHandler
	OnUnsubscribe UnsubscribeHandler
	// backend to peer
	OnMsg  MsgHandler
	OnInfo InfoHandler
}

// ClientOptions is options client will pass to server, when auth is required
type ClientOptions struct {
	Verbose       bool   `json:"verbose"`
	Pedantic      bool   `json:"pedantic"`
	TLSRequired   bool   `json:"tls_required"`
	Authorization string `json:"auth_token"`
	Username      string `json:"user"`
	Password      string `json:"pass"`
	Name          string `json:"name"`
	Lang          string `json:"lang"`
	Version       string `json:"version"`
	Protocol      int    `json:"protocol"`
}

// ConnectCmd is client issued CONNECT command
type ConnectCmd struct {
	// Payload is bytes of "ClientOptions"
	Payload []byte
}
type ConnectHandler func(client Forwarder, cmd *ConnectCmd) (newCmd *ConnectCmd, err error)

// PublishCmd is client issued PUB command
type PublishCmd struct {
	Subject []byte
	Reply   []byte
	Msg     []byte
}
type PublishHandler func(client Forwarder, cmd *PublishCmd) (newCmd *PublishCmd, err error)

// SubscribeCmd is client issued SUB command
type SubscribeCmd struct {
	Subject []byte
	Queue   []byte
	SID     []byte
}
type SubscribeHandler func(client Forwarder, cmd *SubscribeCmd) (newCmd *SubscribeCmd, err error)

// UnsubscribeCmd is client issued UNSUB command
type UnsubscribeCmd struct {
	SID []byte
	Max int
}
type UnsubscribeHandler func(client Forwarder, cmd *UnsubscribeCmd) (newCmd *UnsubscribeCmd, err error)

// InfoCmd is callback to handle server side info command.
type InfoCmd struct {
	Payload []byte
}
type InfoHandler func(client Forwarder, cmd *InfoCmd) (newCmd *InfoCmd, err error)

// MsgCmd is callback to handle server pushed msg.
type MsgCmd struct {
	Subject []byte
	SID     []byte
	Reply   []byte
	Msg     []byte
}
type MsgHandler func(client Forwarder, cmd *MsgCmd) (newCmd *MsgCmd, err error)

More use cases

  • Act as border gateway enforce tls for connections from public
  • Rewrite requests, publish subject
  • Filter out illegal messages passing thru the gateway
  • Modify message, for example adding extra fields or tags which is tranparent to users
  • Route message to another subject

Dev

Keep parser.go up to date with officail

curl 'https://raw.githubusercontent.com/nats-io/gnatsd/master/server/parser.go' > /tmp/parser.go && vimdiff gnatsdgw/server/parser.go /tmp/parser.go

Credits

Parser

  • gnatsd High-Performance server for NATS, the cloud native messaging system.

Inspired by

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].