tcprouter

package module
v0.0.0-...-c8b441f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 17, 2020 License: BSD-3-Clause Imports: 15 Imported by: 0

README

tcprouter

A down to earth tcp router based on traefik tcp streaming and supports multiple backends using valkyrie

Build

git clone https://github.com/xmonader/tcprouter 
make all

This will generate two binaries in bin dir

  • trs: tcp router server
  • trc: tcp router client

Running

configfile: router.toml

[server]
addr = "0.0.0.0"
port = 443
httpport = 80

[server.dbbackend]
type 	 = "redis"
addr     = "127.0.0.1"
port     = 6379
refresh  = 10

[server.services]
    [server.services."www.google.com"]
        addr = "172.217.19.46"
        tlsport = 443
        httpport = 80

then ./tcprouter router.toml

Please notice if you are using low numbered port like 80 or 443 you can use sudo or setcap before running the binary.

  • sudo ./tcprouter router.toml
  • setcap: sudo setcap CAP_NET_BIND_SERVICE=+eip PATH_TO_TCPROUTER
router.toml

We have two toml sections so far

[server]
[server]
addr = "0.0.0.0"
port = 443
httpport = 80

in [server] section we define the listening interface/port the tcprouter intercepting: typically that's 443 for TLS connections.

[server.dbbackend]
[server.dbbackend]
type    = "redis"
addr    = "127.0.0.1"
port    = 6379
refresh = 10

in server.dbbackend we define the backend kv store and its connection information addr,port and how often we want to reload the data from the kv store using refresh key in seconds.

Data representation in KV

127.0.0.1:6379> KEYS *
1) "/tcprouter/services/www.bing.com"
2) "/tcprouter/services/www.google.com"
3) "/tcprouter/services/www.facebook.com"

127.0.0.1:6379> get /tcprouter/services/www.google.com
"{\"Key\":\"tcprouter/services/www.google.com\",\"Value\":\"eyJhZGRyIjogIjE3Mi4yMTcuMTkuNDYiLCAiaHR0cHBvcnQiIDgwLCAidGxzcG9ydCI6IDQ0M30=\",\"LastIndex\":75292246}"
Decoding data from python

In [64]: res = r.get("/tcprouter/service/www.google.com")

In [65]: decoded = json.loads(res)

In [66]: decoded
Out[66]:
{'Key': '/tcprouter/service/www.google.com',
 'Value': 'eyJhZGRyIjogIjE3Mi4yMTcuMTkuNDYiLCAiaHR0cHBvcnQiIDgwLCAidGxzcG9ydCI6IDQ0M30='}

Value payload is base64 encoded because of how golang is marshaling.

In [67]: base64.b64decode(decoded['Value'])
Out[67]: b'{"addr": "172.217.19.46", "httpport" 80, "tlsport": 443}'

Examples

Go

This example can be found at examples/main.go

package main

import (
    "encoding/json"
    "log"
    "time"

    "github.com/abronan/valkeyrie"
    "github.com/abronan/valkeyrie/store"

    "github.com/abronan/valkeyrie/store/redis"
)

func init() {
	redis.Register()
}

type Service struct {
	Addr string `json:"addr"`
	SNI  string `json:"sni"`
	Name string `json:"bing"`
}

func main() {

	// Initialize a new store with redis
	kv, err := valkeyrie.NewStore(
		store.REDIS,
		[]string{"127.0.0.1:6379" },
		&store.Config{
			ConnectionTimeout: 10 * time.Second,
		},
	)
	if err != nil {
		log.Fatal("Cannot create store redis")
	}
	google := &Service{Addr:"172.217.19.46:443", SNI:"www.google.com", Name:"google"}
	encGoogle, _ := json.Marshal(google)
	bing := &Service{Addr:"13.107.21.200:443", SNI:"www.bing.com", Name:"bing"}
	encBing, _ := json.Marshal(bing)

	kv.Put("/tcprouter/services/google", encGoogle, nil)
	kv.Put("/tcprouter/services/bing", encBing, nil)
}
Python
import base64
import json
import redis

r = redis.Redis()

def create_service(name, sni, addr):
    service = {}
    service['Key'] = '/tcprouter/service/{}'.format(name)
    record = {"addr":addr, "sni":sni, "name":name}
    json_dumped_record_bytes = json.dumps(record).encode()
    b64_record = base64.b64encode(json_dumped_record_bytes).decode()
    service['Value'] = b64_record
    r.set(service['Key'], json.dumps(service))

create_service('facebook', "www.facebook.com", "102.132.97.35:443")
create_service('google', 'www.google.com', '172.217.19.46:443')
create_service('bing', 'www.bing.com', '13.107.21.200:443')

If you want to test that locally you can modify /etc/hosts

127.0.0.1 www.google.com
127.0.0.1 www.bing.com
127.0.0.1 www.facebook.com

So your browser go to your 127.0.0.1:443 on requesting google or bing.

CATCH_ALL

to add a global catch all service

python3 create_service.py CATCH_ALL 'CATCH_ALL' '127.0.0.1:9092'

Documentation

Index

Constants

View Source
const (
	// TODO: chose a valid magic number
	MagicNr = 0x1111
)

Variables

This section is empty.

Functions

func GetConn

func GetConn(conn net.Conn, peeked string) net.Conn

GetConn creates a connection proxy with a peeked string

func NewClient

func NewClient(secret string) *client

NewClient creates a new TCP router client

Types

type Config

type Config struct {
	Server ServerConfig `toml:"server"`
}

func ParseCfg

func ParseCfg(r io.Reader) (Config, error)

type Conn

type Conn struct {
	// Peeked are the bytes that have been read from Conn for the
	// purposes of route matching, but have not yet been consumed
	// by Read calls. It set to nil by Read when fully consumed.
	Peeked []byte

	// Conn is the underlying connection.
	// It can be type asserted against *net.TCPConn or other types
	// as needed. It should not be read from directly unless
	// Peeked is nil.
	net.Conn
}

Conn is a connection proxy that handles Peeked bytes

func (*Conn) Read

func (c *Conn) Read(p []byte) (n int, err error)

Read reads bytes from the connection (using the buffer prior to actually reading)

type DbBackendConfig

type DbBackendConfig struct {
	DbType   string `toml:"type"`
	Host     string `toml:"addr"`
	Port     uint   `toml:"port"`
	Username string `toml:"username"`
	Password string `toml:"password"`
	Token    string `toml:"token"`
	Refresh  uint   `toml:"refresh"`
}

func (DbBackendConfig) Addr

func (b DbBackendConfig) Addr() string

func (DbBackendConfig) Backend

func (b DbBackendConfig) Backend() store.Backend

type Handler

type Handler interface {
	ServeTCP(conn WriteCloser)
}

Handler is the TCP Handlers interface

type HandlerFunc

type HandlerFunc func(conn WriteCloser)

The HandlerFunc type is an adapter to allow the use of ordinary functions as handlers.

func (HandlerFunc) ServeTCP

func (f HandlerFunc) ServeTCP(conn WriteCloser)

ServeTCP serves tcp

type Handshake

type Handshake struct {
	MagicNr uint16
	Secret  []byte
}

Handshake is the struct used to serialize the first frame sent to the server

func (*Handshake) Read

func (h *Handshake) Read(r io.Reader) error

func (Handshake) Write

func (h Handshake) Write(w io.Writer) error

type Server

type Server struct {
	ServerOptions ServerOptions
	DbStore       store.Store
	Services      map[string]Service
	// contains filtered or unexported fields
}

func NewServer

func NewServer(forwardOptions ServerOptions, store store.Store, services map[string]Service) *Server

func (*Server) Start

func (s *Server) Start(ctx context.Context) error

type ServerConfig

type ServerConfig struct {
	Host        string             `toml:"addr"`
	Port        uint               `toml:"port"`
	HTTPPort    uint               `toml:"httpport"`
	ClientsPort uint               `toml:"clientsport"`
	DbBackend   DbBackendConfig    `toml:"dbbackend"`
	Services    map[string]Service `toml:"services"`
}

func (ServerConfig) Addr

func (s ServerConfig) Addr() string

type ServerOptions

type ServerOptions struct {
	ListeningAddr           string
	ListeningTLSPort        uint
	ListeningHTTPPort       uint
	ListeningForClientsPort uint
}

func (ServerOptions) ClientsAddr

func (o ServerOptions) ClientsAddr() string

func (ServerOptions) HTTPAddr

func (o ServerOptions) HTTPAddr() string

func (ServerOptions) TLSAddr

func (o ServerOptions) TLSAddr() string

type Service

type Service struct {
	Addr         string `toml:"addr"`
	ClientSecret string `toml:"clientsecret` // will forward connection to it directly instead of hitting the Addr.
	TLSPort      int    `toml:"tlsport"`
	HTTPPort     int    `toml:"httpport"`
}

type WriteCloser

type WriteCloser interface {
	net.Conn
	// CloseWrite on a network connection, indicates that the issuer of the call
	// has terminated sending on that connection.
	// It corresponds to sending a FIN packet.
	CloseWrite() error
}

WriteCloser describes a net.Conn with a CloseWrite method.

Directories

Path Synopsis
cmds

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL