mirror of
https://github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin.git
synced 2026-09-02 20:28:50 +02:00
Compare commits
25
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4a5f1eca6a | ||
|
|
cdda369fb8 | ||
|
|
f0f28fecef | ||
|
|
23620207f7 | ||
|
|
4a674da9f9 | ||
|
|
0dfd18f18e | ||
|
|
d8ee0a34eb | ||
|
|
b50074dca4 | ||
|
|
5044004ec2 | ||
|
|
41a46c0584 | ||
|
|
0a186cf9a9 | ||
|
|
ffcf4356fc | ||
|
|
f94e48aa03 | ||
|
|
a197194591 | ||
|
|
44e329cd57 | ||
|
|
81ffeabcec | ||
|
|
e14d179612 | ||
|
|
4058836678 | ||
|
|
87ed9e9c4e | ||
|
|
395c80dccf | ||
|
|
59268ee33d | ||
|
|
781a83465e | ||
|
|
8696501f61 | ||
|
|
f22fc2cd09 | ||
|
|
8eec1c5656 |
@@ -28,20 +28,33 @@ run_local:
|
||||
run_behindproxy:
|
||||
docker-compose -f exemples/behind-proxy/docker-compose.cloudflare.yml up -d --remove-orphans
|
||||
|
||||
run_cacheredis:
|
||||
docker-compose -f exemples/redis-cache/docker-compose.redis.yml up -d --remove-orphans
|
||||
|
||||
run:
|
||||
docker-compose -f docker-compose.yml up -d --remove-orphans
|
||||
|
||||
restart_docker_dev:
|
||||
restart_dev:
|
||||
docker-compose -f docker-compose.dev.yml restart
|
||||
|
||||
restart_docker_local:
|
||||
restart_local:
|
||||
docker-compose -f docker-compose.local.yml restart
|
||||
|
||||
restart_docker:
|
||||
restart:
|
||||
docker-compose -f docker-compose.yml restart
|
||||
|
||||
show_logs:
|
||||
docker-compose -f docker-compose.yml restart
|
||||
|
||||
show_local_logs:
|
||||
docker-compose -f docker-compose.local.yml logs -f
|
||||
|
||||
show_dev_logs:
|
||||
docker-compose -f docker-compose.dev.yml logs -f
|
||||
|
||||
clean_all_docker:
|
||||
docker-compose -f exemples/behind-proxy/docker-compose.cloudflare.yml down --remove-orphans
|
||||
docker-compose -f exemples/behind-proxy/docker-compose.redis.yml down --remove-orphans
|
||||
docker-compose -f docker-compose.local.yml down --remove-orphans
|
||||
docker-compose -f docker-compose.yml down --remove-orphans
|
||||
|
||||
|
||||
@@ -26,6 +26,9 @@ There are 3 operating modes (CrowdsecMode) for this plugin:
|
||||
|
||||
The recommanded mode for performance is the streaming mode, decisions are updated every 60 sec by default and that's the only communication between traefik and crowdsec. Every requests that happens hits the cache for quick decisions.
|
||||
|
||||
The cache can be local to the Traefik instance using the filesystem or use of a separated redis instance.
|
||||
The redis instance is currently in beta and support Redis 7.0.X version
|
||||
|
||||
## Usage
|
||||
|
||||
To get started, use the `docker-compose.yml` file.
|
||||
@@ -36,19 +39,19 @@ make run
|
||||
```
|
||||
|
||||
### Note
|
||||
Each middleware in traefik has it's own data and is instanciated by service.
|
||||
This means if there are 10 services protected by the bouncer in streaming alone or live mode, the cache will be duplicated to all 10 services.
|
||||
This is because traefik does not allow plugins to store data locally that can be consummed.
|
||||
|
||||
The synchronisation with the crowdsec service will happen also 10 times in the period selected.
|
||||
It should be taken into account when fixing this period so each middleware has time to sync data from crowdsec.
|
||||
**/!\ Since Release 1.10, cache is no longer duplicated but shared by all services**
|
||||
*This lowers the overhead of the cache in memory and the numbers of cache to fetch it from crowdsec in situation with many services*
|
||||
|
||||
At each start of synchronisation, the middleware will wait a random number of seconds to avoid simultaneous calls to crowdsec.
|
||||
|
||||
### Variables
|
||||
- Enabled
|
||||
- bool
|
||||
- enable the plugin
|
||||
- default: false
|
||||
- LogLevel
|
||||
- string
|
||||
- default: `INFO`, expected value are: `INFO`, `DEBUG`
|
||||
- CrowdsecMode
|
||||
- string
|
||||
- default: `live`, expected value are: `none`, `live`, `stream`
|
||||
@@ -58,7 +61,7 @@ At each start of synchronisation, the middleware will wait a random number of se
|
||||
- CrowdsecLapiHost
|
||||
- string
|
||||
- default: "crowdsec:8080"
|
||||
- Crowdsec LAPI available on which host.
|
||||
- Crowdsec LAPI available on which host and port.
|
||||
- CrowdsecLapiKey
|
||||
- string
|
||||
- Crowdsec LAPI generated key for the bouncer : **must be unique by service**.
|
||||
@@ -74,6 +77,19 @@ At each start of synchronisation, the middleware will wait a random number of se
|
||||
- []string
|
||||
- default: []
|
||||
- List of IPs of trusted Proxies that are in front of traefik (ex: Cloudflare)
|
||||
- ForwardedHeadersCustomName
|
||||
- string
|
||||
- default: "X-Forwarded-For"
|
||||
- Name of the header where the real IP of the client should be retrieved
|
||||
- RedisCacheEnabled
|
||||
- bool
|
||||
- default: false
|
||||
- enable redis cache instead of filesystem cache
|
||||
- RedisCacheHost
|
||||
- string
|
||||
- default: "redis:6379"
|
||||
- hostname and port for the redis service
|
||||
|
||||
|
||||
### Configuration
|
||||
|
||||
@@ -116,13 +132,16 @@ http:
|
||||
enabled: false
|
||||
updateIntervalSeconds: 60
|
||||
defaultDecisionSeconds: 60
|
||||
crowdsecMode: stream
|
||||
crowdsecMode: live
|
||||
crowdsecLapiKey: privateKey
|
||||
crowdsecLapiHost: crowdsec:8080
|
||||
crowdsecLapiScheme: http
|
||||
forwardedHeadersTrustedIPs:
|
||||
- 10.0.10.23/32
|
||||
- 10.0.20.0/24
|
||||
forwardedHeadersCustomName: X-Custom-Header
|
||||
redisCacheEnabled: false
|
||||
redisCacheHost: "redis:6379"
|
||||
```
|
||||
These are the default values of the plugin except for LapiKey.
|
||||
|
||||
@@ -216,9 +235,21 @@ We configure the middleware to trust as well the IP:
|
||||
|
||||
To run the environnement run:
|
||||
```bash
|
||||
make run_behind_proxy
|
||||
make run_behindproxy
|
||||
```
|
||||
|
||||
2. With Redis as an external shared cache
|
||||
|
||||
The plugin must be configured to connect to a redis instance
|
||||
```yaml
|
||||
redisCacheHost: "redis:6379"
|
||||
```
|
||||
Here **redis** is the hostname of a container located in the same network as Traefik and **6379** the default port of redis
|
||||
|
||||
To run the demo environnement run:
|
||||
```bash
|
||||
make run_cacheredis
|
||||
```
|
||||
|
||||
### About
|
||||
|
||||
|
||||
+93
-95
@@ -7,16 +7,14 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
ttl_map "github.com/leprosus/golang-ttl-map"
|
||||
ip "github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin/ip"
|
||||
cache "github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin/pkg/cache"
|
||||
ip "github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin/pkg/ip"
|
||||
logger "github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin/pkg/logger"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -26,26 +24,35 @@ const (
|
||||
crowdsecLapiHeader = "X-Api-Key"
|
||||
crowdsecLapiRoute = "v1/decisions"
|
||||
crowdsecLapiStreamRoute = "v1/decisions/stream"
|
||||
cacheBannedValue = "t"
|
||||
cacheNoBannedValue = "f"
|
||||
cacheTimeoutKey = "updated"
|
||||
)
|
||||
|
||||
var (
|
||||
crowdsecStreamHealthy = false
|
||||
ticker chan bool
|
||||
)
|
||||
|
||||
// Config the plugin configuration.
|
||||
type Config struct {
|
||||
Enabled bool `json:"enabled,omitempty"`
|
||||
LogLevel string `json:"logLevel,omitempty"`
|
||||
CrowdsecMode string `json:"crowdsecMode,omitempty"`
|
||||
CrowdsecLapiScheme string `json:"crowdsecLapiScheme,omitempty"`
|
||||
CrowdsecLapiHost string `json:"crowdsecLapiHost,omitempty"`
|
||||
CrowdsecLapiKey string `json:"crowdsecLapiKey,omitempty"`
|
||||
ForwardedHeadersCustomName string `json:"forwardedheaderscustomheader,omitempty"`
|
||||
UpdateIntervalSeconds int64 `json:"updateIntervalSeconds,omitempty"`
|
||||
DefaultDecisionSeconds int64 `json:"defaultDecisionSeconds,omitempty"`
|
||||
ForwardedHeadersTrustedIPs []string `json:"forwardedheaderstrustedips,omitempty"`
|
||||
ForwardedHeadersTrustedIPs []string `json:"forwardedHeadersTrustedIps,omitempty"`
|
||||
RedisCacheEnabled bool `json:"redisCacheEnabled,omitempty"`
|
||||
RedisCacheHost string `json:"redisCacheHost,omitempty"`
|
||||
}
|
||||
|
||||
// CreateConfig creates the default plugin configuration.
|
||||
func CreateConfig() *Config {
|
||||
return &Config{
|
||||
Enabled: false,
|
||||
LogLevel: "INFO",
|
||||
CrowdsecMode: liveMode,
|
||||
CrowdsecLapiScheme: "http",
|
||||
CrowdsecLapiHost: "crowdsec:8080",
|
||||
@@ -53,32 +60,36 @@ func CreateConfig() *Config {
|
||||
UpdateIntervalSeconds: 60,
|
||||
DefaultDecisionSeconds: 60,
|
||||
ForwardedHeadersTrustedIPs: []string{},
|
||||
ForwardedHeadersCustomName: "X-Forwarded-For",
|
||||
RedisCacheEnabled: false,
|
||||
RedisCacheHost: "redis:6379",
|
||||
}
|
||||
}
|
||||
|
||||
// Bouncer a Bouncer plugin.
|
||||
// Bouncer a Bouncer struct.
|
||||
type Bouncer struct {
|
||||
next http.Handler
|
||||
name string
|
||||
template *template.Template
|
||||
|
||||
enabled bool
|
||||
crowdsecStreamHealthy bool
|
||||
crowdsecScheme string
|
||||
crowdsecHost string
|
||||
crowdsecKey string
|
||||
crowdsecMode string
|
||||
updateInterval int64
|
||||
defaultDecisionTimeout int64
|
||||
customHeader string
|
||||
poolStrategy *ip.PoolStrategy
|
||||
client *http.Client
|
||||
cache *ttl_map.Heap
|
||||
}
|
||||
|
||||
// New creates the crowdsec bouncer plugin.
|
||||
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
|
||||
logger.Init(config.LogLevel)
|
||||
err := validateParams(config)
|
||||
if err != nil {
|
||||
logger.Info(fmt.Sprintf("%w", err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -90,12 +101,12 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
|
||||
template: template.New("CrowdsecBouncer").Delims("[[", "]]"),
|
||||
|
||||
enabled: config.Enabled,
|
||||
crowdsecStreamHealthy: false,
|
||||
crowdsecMode: config.CrowdsecMode,
|
||||
crowdsecScheme: config.CrowdsecLapiScheme,
|
||||
crowdsecHost: config.CrowdsecLapiHost,
|
||||
crowdsecKey: config.CrowdsecLapiKey,
|
||||
updateInterval: config.UpdateIntervalSeconds,
|
||||
customHeader: config.ForwardedHeadersCustomName,
|
||||
defaultDecisionTimeout: config.DefaultDecisionSeconds,
|
||||
poolStrategy: &ip.PoolStrategy{
|
||||
Checker: checker,
|
||||
@@ -107,21 +118,17 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
|
||||
},
|
||||
Timeout: 5 * time.Second,
|
||||
},
|
||||
cache: ttl_map.New(),
|
||||
}
|
||||
if config.CrowdsecMode == streamMode {
|
||||
go func() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
timeout := rand.Int63n(30)
|
||||
logger(fmt.Sprintf("Wait: %v", timeout))
|
||||
time.Sleep(time.Duration(timeout) * time.Second)
|
||||
go handleStreamCache(bouncer)
|
||||
ticker := time.NewTicker(time.Duration(config.UpdateIntervalSeconds) * time.Second)
|
||||
for range ticker.C {
|
||||
if config.RedisCacheEnabled {
|
||||
cache.InitRedisClient(config.RedisCacheHost)
|
||||
}
|
||||
if config.CrowdsecMode == streamMode && ticker == nil {
|
||||
ticker = startTicker(config, func() {
|
||||
handleStreamCache(bouncer)
|
||||
})
|
||||
go handleStreamCache(bouncer)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
return bouncer, nil
|
||||
}
|
||||
|
||||
@@ -132,15 +139,18 @@ func (bouncer *Bouncer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
remoteHost, err := getRemoteIP(bouncer, req)
|
||||
remoteHost, err := ip.GetRemoteIP(req, bouncer.poolStrategy, bouncer.customHeader)
|
||||
if err != nil {
|
||||
logger.Info(fmt.Sprintf("%w", err))
|
||||
bouncer.next.ServeHTTP(rw, req)
|
||||
return
|
||||
}
|
||||
logger.Debug(fmt.Sprintf("ServeHTTP ip:%v", remoteHost))
|
||||
|
||||
if bouncer.crowdsecMode != noneMode {
|
||||
isBanned, err := getDecision(bouncer.cache, remoteHost)
|
||||
isBanned, err := cache.GetDecision(remoteHost)
|
||||
if err == nil {
|
||||
logger.Debug(fmt.Sprintf("ServeHTTP cacheHit isBanned:%v", isBanned))
|
||||
if isBanned {
|
||||
rw.WriteHeader(http.StatusForbidden)
|
||||
} else {
|
||||
@@ -152,7 +162,7 @@ func (bouncer *Bouncer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||
|
||||
// Right here if we cannot join the stream we forbid the request to go on.
|
||||
if bouncer.crowdsecMode == streamMode {
|
||||
if bouncer.crowdsecStreamHealthy {
|
||||
if crowdsecStreamHealthy {
|
||||
bouncer.next.ServeHTTP(rw, req)
|
||||
} else {
|
||||
rw.WriteHeader(http.StatusForbidden)
|
||||
@@ -183,10 +193,6 @@ type Stream struct {
|
||||
New []Decision `json:"new"`
|
||||
}
|
||||
|
||||
func logger(str string) {
|
||||
log.Printf("Crowdsec Bouncer Traefik Plugin - %s", str)
|
||||
}
|
||||
|
||||
func contains(source []string, target string) bool {
|
||||
for _, item := range source {
|
||||
if item == target {
|
||||
@@ -196,42 +202,21 @@ func contains(source []string, target string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// It returns the first IP that is not in the pool, or the empty string otherwise.
|
||||
func getRemoteIP(bouncer *Bouncer, req *http.Request) (string, error) {
|
||||
remoteIP := bouncer.poolStrategy.GetIP(req)
|
||||
if len(remoteIP) != 0 {
|
||||
return remoteIP, nil
|
||||
func startTicker(config *Config, work func()) chan bool {
|
||||
ticker := time.NewTicker(time.Duration(config.UpdateIntervalSeconds) * time.Second)
|
||||
stop := make(chan bool, 1)
|
||||
go func() {
|
||||
defer logger.Debug("ticker:stopped")
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
go work()
|
||||
case <-stop:
|
||||
return
|
||||
}
|
||||
remoteIP, _, err := net.SplitHostPort(req.RemoteAddr)
|
||||
if err != nil {
|
||||
logger(fmt.Sprintf("failed to extract ip from remote address: %v", err))
|
||||
return "", err
|
||||
}
|
||||
return remoteIP, nil
|
||||
}
|
||||
|
||||
// Get Decision check in the cache if the IP has the banned / not banned value.
|
||||
// Otherwise return with an error to add the IP in cache if we are on.
|
||||
func getDecision(cache *ttl_map.Heap, clientIP string) (bool, error) {
|
||||
banned, isCached := cache.Get(clientIP)
|
||||
bannedString, isValid := banned.(string)
|
||||
if isCached && isValid && len(bannedString) > 0 {
|
||||
return bannedString == cacheBannedValue, nil
|
||||
}
|
||||
return false, fmt.Errorf("no cache data")
|
||||
}
|
||||
|
||||
func setDecision(cache *ttl_map.Heap, clientIP string, isBanned bool, duration int64) {
|
||||
if isBanned {
|
||||
logger(fmt.Sprintf("%v banned", clientIP))
|
||||
cache.Set(clientIP, cacheBannedValue, duration)
|
||||
} else {
|
||||
cache.Set(clientIP, cacheNoBannedValue, duration)
|
||||
}
|
||||
}
|
||||
|
||||
func deleteDecision(cache *ttl_map.Heap, clientIP string) {
|
||||
cache.Del(clientIP)
|
||||
}()
|
||||
return stop
|
||||
}
|
||||
|
||||
// We are now in none or live mode.
|
||||
@@ -242,26 +227,31 @@ func handleNoStreamCache(bouncer *Bouncer, rw http.ResponseWriter, req *http.Req
|
||||
Path: crowdsecLapiRoute,
|
||||
RawQuery: fmt.Sprintf("ip=%v&banned=true", remoteHost),
|
||||
}
|
||||
body := crowdsecQuery(bouncer, routeURL.String())
|
||||
body, err := crowdsecQuery(bouncer, routeURL.String())
|
||||
if err != nil {
|
||||
logger.Info(fmt.Sprintf("%w", err))
|
||||
rw.WriteHeader(http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
if bytes.Equal(body, []byte("null")) {
|
||||
if bouncer.crowdsecMode == liveMode {
|
||||
setDecision(bouncer.cache, remoteHost, false, bouncer.defaultDecisionTimeout)
|
||||
cache.SetDecision(remoteHost, false, bouncer.defaultDecisionTimeout)
|
||||
}
|
||||
bouncer.next.ServeHTTP(rw, req)
|
||||
return
|
||||
}
|
||||
|
||||
var decisions []Decision
|
||||
err := json.Unmarshal(body, &decisions)
|
||||
err = json.Unmarshal(body, &decisions)
|
||||
if err != nil {
|
||||
logger(fmt.Sprintf("failed to parse body: %s", err))
|
||||
logger.Info(fmt.Sprintf("failed to parse body: %s", err))
|
||||
rw.WriteHeader(http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
if len(decisions) == 0 {
|
||||
if bouncer.crowdsecMode == liveMode {
|
||||
setDecision(bouncer.cache, remoteHost, false, bouncer.defaultDecisionTimeout)
|
||||
cache.SetDecision(remoteHost, false, bouncer.defaultDecisionTimeout)
|
||||
}
|
||||
bouncer.next.ServeHTTP(rw, req)
|
||||
return
|
||||
@@ -269,70 +259,78 @@ func handleNoStreamCache(bouncer *Bouncer, rw http.ResponseWriter, req *http.Req
|
||||
rw.WriteHeader(http.StatusForbidden)
|
||||
duration, err := time.ParseDuration(decisions[0].Duration)
|
||||
if err != nil {
|
||||
logger(fmt.Sprintf("failed to parse duration: %s", err))
|
||||
logger.Info(fmt.Sprintf("failed to parse duration: %s", err))
|
||||
return
|
||||
}
|
||||
if bouncer.crowdsecMode == liveMode {
|
||||
setDecision(bouncer.cache, remoteHost, true, int64(duration.Seconds()))
|
||||
cache.SetDecision(remoteHost, true, int64(duration.Seconds()))
|
||||
}
|
||||
}
|
||||
|
||||
func handleStreamCache(bouncer *Bouncer) {
|
||||
// TODO clean properly on exit.
|
||||
// Instead of blocking the goroutine interval for all the secondary node,
|
||||
// if the master service is shut down, other goroutine can take the lead
|
||||
// because updated routine information is in the cache
|
||||
logger.Debug("handleStreamCache")
|
||||
_, err := cache.GetDecision(cacheTimeoutKey)
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
cache.SetDecision(cacheTimeoutKey, false, bouncer.updateInterval-1)
|
||||
streamRouteURL := url.URL{
|
||||
Scheme: bouncer.crowdsecScheme,
|
||||
Host: bouncer.crowdsecHost,
|
||||
Path: crowdsecLapiStreamRoute,
|
||||
RawQuery: fmt.Sprintf("startup=%t", !bouncer.crowdsecStreamHealthy),
|
||||
RawQuery: fmt.Sprintf("startup=%t", !crowdsecStreamHealthy),
|
||||
}
|
||||
body := crowdsecQuery(bouncer, streamRouteURL.String())
|
||||
var stream Stream
|
||||
err := json.Unmarshal(body, &stream)
|
||||
body, err := crowdsecQuery(bouncer, streamRouteURL.String())
|
||||
if err != nil {
|
||||
logger(fmt.Sprintf("error while parsing body: %s", err))
|
||||
bouncer.crowdsecStreamHealthy = false
|
||||
logger.Info(fmt.Sprintf("%w", err))
|
||||
crowdsecStreamHealthy = false
|
||||
return
|
||||
}
|
||||
var stream Stream
|
||||
err = json.Unmarshal(body, &stream)
|
||||
if err != nil {
|
||||
logger.Info(fmt.Sprintf("error while parsing body: %s", err))
|
||||
crowdsecStreamHealthy = false
|
||||
return
|
||||
}
|
||||
for _, decision := range stream.New {
|
||||
duration, err := time.ParseDuration(decision.Duration)
|
||||
if err == nil {
|
||||
setDecision(bouncer.cache, decision.Value, true, int64(duration.Seconds()))
|
||||
cache.SetDecision(decision.Value, true, int64(duration.Seconds()))
|
||||
}
|
||||
}
|
||||
for _, decision := range stream.Deleted {
|
||||
deleteDecision(bouncer.cache, decision.Value)
|
||||
cache.DeleteDecision(decision.Value)
|
||||
}
|
||||
bouncer.crowdsecStreamHealthy = true
|
||||
crowdsecStreamHealthy = true
|
||||
}
|
||||
|
||||
func crowdsecQuery(bouncer *Bouncer, stringURL string) []byte {
|
||||
func crowdsecQuery(bouncer *Bouncer, stringURL string) ([]byte, error) {
|
||||
var req *http.Request
|
||||
req, _ = http.NewRequest(http.MethodGet, stringURL, nil)
|
||||
req.Header.Add(crowdsecLapiHeader, bouncer.crowdsecKey)
|
||||
res, err := bouncer.client.Do(req)
|
||||
if err != nil {
|
||||
logger(fmt.Sprintf("error while fetching %v: %s", stringURL, err))
|
||||
bouncer.crowdsecStreamHealthy = false
|
||||
return nil
|
||||
return nil, fmt.Errorf("error while fetching %v: %s", stringURL, err)
|
||||
}
|
||||
if res.StatusCode != http.StatusOK {
|
||||
logger(fmt.Sprintf("error while fetching %v, status code: %d", stringURL, res.StatusCode))
|
||||
bouncer.crowdsecStreamHealthy = false
|
||||
return nil
|
||||
return nil, fmt.Errorf("error while fetching %v, status code: %d", stringURL, res.StatusCode)
|
||||
}
|
||||
defer func(body io.ReadCloser) {
|
||||
err = body.Close()
|
||||
if err != nil {
|
||||
logger(fmt.Sprintf("failed to close body reader: %s", err))
|
||||
logger.Info(fmt.Sprintf("failed to close body reader: %s", err))
|
||||
}
|
||||
}(res.Body)
|
||||
body, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
logger(fmt.Sprintf("error while reading body: %s", err))
|
||||
bouncer.crowdsecStreamHealthy = false
|
||||
return nil
|
||||
return nil, fmt.Errorf("error while reading body: %s", err)
|
||||
}
|
||||
return body
|
||||
return body, nil
|
||||
}
|
||||
|
||||
func validateParams(config *Config) error {
|
||||
@@ -373,10 +371,10 @@ func validateParams(config *Config) error {
|
||||
if len(config.ForwardedHeadersTrustedIPs) > 0 {
|
||||
_, err = ip.NewChecker(config.ForwardedHeadersTrustedIPs)
|
||||
if err != nil {
|
||||
return fmt.Errorf("ForwardedHeadersTrustedIPs must be a list of IP/CIDR :%v", err)
|
||||
return fmt.Errorf("ForwardedHeadersTrustedIPs must be a list of IP/CIDR :%w", err)
|
||||
}
|
||||
} else {
|
||||
logger("No IP provided for ForwardedHeadersTrustedIPs")
|
||||
logger.Debug("No IP provided for ForwardedHeadersTrustedIPs")
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -2,7 +2,7 @@ version: "3.8"
|
||||
|
||||
services:
|
||||
traefik:
|
||||
image: "traefik:v2.8.8"
|
||||
image: "traefik:v2.9.4"
|
||||
container_name: "traefik"
|
||||
command:
|
||||
# - "--log.level=DEBUG"
|
||||
@@ -35,7 +35,6 @@ services:
|
||||
- "traefik.http.services.service1.loadbalancer.server.port=80"
|
||||
- "traefik.http.middlewares.crowdsec1.plugin.bouncer.enabled=true"
|
||||
- "traefik.http.middlewares.crowdsec1.plugin.bouncer.crowdseclapikey=40796d93c2958f9e58345514e67740e5"
|
||||
- "traefik.http.middlewares.crowdsec1.plugin.bouncer.forwardedheaderstrustedips=172.26.0.1"
|
||||
|
||||
whoami2:
|
||||
image: traefik/whoami
|
||||
|
||||
+4
-4
@@ -2,7 +2,7 @@ version: "3.8"
|
||||
|
||||
services:
|
||||
traefik:
|
||||
image: "traefik:v2.8.8"
|
||||
image: "traefik:v2.9.4"
|
||||
container_name: "traefik"
|
||||
command:
|
||||
- "--accesslog"
|
||||
@@ -13,7 +13,7 @@ services:
|
||||
- "--entrypoints.web.address=:80"
|
||||
|
||||
- "--experimental.plugins.bouncer.modulename=github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin"
|
||||
- "--experimental.plugins.bouncer.version=v1.0.9"
|
||||
- "--experimental.plugins.bouncer.version=v1.1.0"
|
||||
volumes:
|
||||
- "/var/run/docker.sock:/var/run/docker.sock:ro"
|
||||
- "logs:/var/log/traefik"
|
||||
@@ -29,7 +29,7 @@ services:
|
||||
labels:
|
||||
- "traefik.enable=true"
|
||||
# Definition of the router
|
||||
- "traefik.http.routers.router1.rule=Host(`localhost`) && Path(`/foo`)"
|
||||
- "traefik.http.routers.router1.rule=Path(`/foo`)"
|
||||
- "traefik.http.routers.router1.entrypoints=web"
|
||||
- "traefik.http.routers.router1.middlewares=crowdsec2@docker"
|
||||
# Definition of the service
|
||||
@@ -47,7 +47,7 @@ services:
|
||||
labels:
|
||||
- "traefik.enable=true"
|
||||
# Definition of the router
|
||||
- "traefik.http.routers.router2.rule=Host(`localhost`) && Path(`/bar`)"
|
||||
- "traefik.http.routers.router2.rule=Path(`/bar`)"
|
||||
- "traefik.http.routers.router2.entrypoints=web"
|
||||
- "traefik.http.routers.router2.middlewares=crowdsec2@docker"
|
||||
# Definition of the service
|
||||
|
||||
@@ -2,7 +2,7 @@ version: "3.8"
|
||||
|
||||
services:
|
||||
cloudflare:
|
||||
image: "traefik:v2.8.8"
|
||||
image: "traefik:v2.9.1"
|
||||
container_name: "cloudflare"
|
||||
command:
|
||||
# - "--log.level=DEBUG"
|
||||
@@ -21,7 +21,7 @@ services:
|
||||
- 8080:8080
|
||||
|
||||
traefik:
|
||||
image: "traefik:v2.8.8"
|
||||
image: "traefik:v2.9.1"
|
||||
container_name: "traefik"
|
||||
command:
|
||||
# - "--log.level=DEBUG"
|
||||
@@ -34,7 +34,7 @@ services:
|
||||
- "--entrypoints.web.forwardedheaders.trustedips=172.21.0.5"
|
||||
|
||||
- "--experimental.plugins.bouncer.modulename=github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin"
|
||||
- "--experimental.plugins.bouncer.version=v1.0.9"
|
||||
- "--experimental.plugins.bouncer.version=v1.1.0"
|
||||
volumes:
|
||||
- /var/run/docker.sock:/var/run/docker.sock:ro
|
||||
- logs-dev:/var/log/traefik
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
filenames:
|
||||
- /var/log/traefik/access.log
|
||||
labels:
|
||||
type: traefik
|
||||
@@ -0,0 +1,94 @@
|
||||
version: "3.8"
|
||||
|
||||
services:
|
||||
traefik:
|
||||
image: "traefik:v2.9.4"
|
||||
container_name: "traefik"
|
||||
command:
|
||||
# - "--log.level=DEBUG"
|
||||
- "--accesslog"
|
||||
- "--accesslog.filepath=/var/log/traefik/access.log"
|
||||
- "--api.insecure=true"
|
||||
- "--providers.docker=true"
|
||||
- "--providers.docker.exposedbydefault=false"
|
||||
- "--entrypoints.web.address=:80"
|
||||
|
||||
#- "--experimental.plugins.bouncer.modulename=github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin"
|
||||
#- "--experimental.plugins.bouncer.version=v1.0.9"
|
||||
- "--experimental.localplugins.bouncer.modulename=github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin"
|
||||
volumes:
|
||||
- /var/run/docker.sock:/var/run/docker.sock:ro
|
||||
- logs-redis:/var/log/traefik
|
||||
- ./../../:/plugins-local/src/github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin
|
||||
ports:
|
||||
- 80:80
|
||||
- 8080:8080
|
||||
depends_on:
|
||||
- crowdsec
|
||||
- redis
|
||||
|
||||
whoami1:
|
||||
image: traefik/whoami
|
||||
container_name: "simple-service1"
|
||||
labels:
|
||||
- "traefik.enable=true"
|
||||
# Definition of the router
|
||||
- "traefik.http.routers.router1.rule=Path(`/foo`)"
|
||||
- "traefik.http.routers.router1.entrypoints=web"
|
||||
- "traefik.http.routers.router1.middlewares=crowdsec1@docker"
|
||||
# Definition of the service
|
||||
- "traefik.http.services.service1.loadbalancer.server.port=80"
|
||||
# Definition of the middleware
|
||||
- "traefik.http.middlewares.crowdsec1.plugin.bouncer.enabled=true"
|
||||
# crowdseclapikey must be uniq to the middleware attached to the service
|
||||
- "traefik.http.middlewares.crowdsec1.plugin.bouncer.crowdseclapikey=40796d93c2958f9e58345514e67740e5"
|
||||
- "traefik.http.middlewares.crowdsec1.plugin.bouncer.rediscacheenabled=true"
|
||||
|
||||
whoami2:
|
||||
image: traefik/whoami
|
||||
container_name: "simple-service2"
|
||||
labels:
|
||||
- "traefik.enable=true"
|
||||
# Definition of the router
|
||||
- "traefik.http.routers.router2.rule=Path(`/bar`)"
|
||||
- "traefik.http.routers.router2.entrypoints=web"
|
||||
- "traefik.http.routers.router2.middlewares=crowdsec1@docker"
|
||||
# Definition of the service
|
||||
- "traefik.http.services.service2.loadbalancer.server.port=80"
|
||||
# Definition of the middleware
|
||||
- "traefik.http.middlewares.crowdsec2.plugin.bouncer.enabled=true"
|
||||
# crowdseclapikey must be uniq to the middleware attached to the service
|
||||
- "traefik.http.middlewares.crowdsec2.plugin.bouncer.crowdseclapikey=44c36dac5c4140af9f06f397508e82c7"
|
||||
- "traefik.http.middlewares.crowdsec2.plugin.bouncer.rediscacheenabled=true"
|
||||
|
||||
|
||||
crowdsec:
|
||||
image: crowdsecurity/crowdsec:v1.4.1
|
||||
container_name: "crowdsec"
|
||||
environment:
|
||||
COLLECTIONS: crowdsecurity/traefik
|
||||
CUSTOM_HOSTNAME: crowdsec
|
||||
BOUNCER_KEY_TRAEFIK_DEV_1: 40796d93c2958f9e58345514e67740e5
|
||||
BOUNCER_KEY_TRAEFIK_DEV_2: 44c36dac5c4140af9f06f397508e82c7
|
||||
volumes:
|
||||
- ./acquis.yaml:/etc/crowdsec/acquis.yaml:ro
|
||||
- logs-redis:/var/log/traefik:ro
|
||||
- crowdsec-db-redis:/var/lib/crowdsec/data/
|
||||
- crowdsec-config-redis:/etc/crowdsec/
|
||||
labels:
|
||||
- "traefik.enable=false"
|
||||
|
||||
redis:
|
||||
image: "redis:7.0.5-alpine"
|
||||
container_name: "redis"
|
||||
command: "redis-server --save 60 1"
|
||||
volumes:
|
||||
- redis-data:/data
|
||||
ports:
|
||||
- 6379:6379
|
||||
|
||||
volumes:
|
||||
logs-redis:
|
||||
crowdsec-db-redis:
|
||||
crowdsec-config-redis:
|
||||
redis-data:
|
||||
@@ -3,3 +3,5 @@ module github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin
|
||||
go 1.17
|
||||
|
||||
require github.com/leprosus/golang-ttl-map v1.1.7
|
||||
|
||||
require github.com/tehnerd/goUtils v0.0.0-20150515130609-5a2d8fb2ded8 // indirect
|
||||
|
||||
@@ -1,2 +1,4 @@
|
||||
github.com/leprosus/golang-ttl-map v1.1.7 h1:cF4AAFDDnJTFSV+/42sKLhmMluvLdRlCGS2UaifH6UM=
|
||||
github.com/leprosus/golang-ttl-map v1.1.7/go.mod h1:4QWHJPeVBbrkhOhXdhCv9IEiyj/YzkO04/iexy4vSe0=
|
||||
github.com/tehnerd/goUtils v0.0.0-20150515130609-5a2d8fb2ded8 h1:/b777evAfRRdUJHasZLgQ/w8D/s1HtbaeDXsCVsV0B0=
|
||||
github.com/tehnerd/goUtils v0.0.0-20150515130609-5a2d8fb2ded8/go.mod h1:UuuqaOb+pZOxJZtjF1mBWTo8HYa7HQCbNkwUEaG9uU0=
|
||||
|
||||
Vendored
+93
@@ -0,0 +1,93 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
ttl_map "github.com/leprosus/golang-ttl-map"
|
||||
|
||||
logger "github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin/pkg/logger"
|
||||
simpleredis "github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin/pkg/redis"
|
||||
)
|
||||
|
||||
const (
|
||||
cacheBannedValue = "t"
|
||||
cacheNoBannedValue = "f"
|
||||
)
|
||||
|
||||
var cache = ttl_map.New()
|
||||
var redis simpleredis.SimpleRedis
|
||||
|
||||
var redisEnabled = false
|
||||
|
||||
func getDecisionLocalCache(clientIP string) (bool, error) {
|
||||
banned, isCached := cache.Get(clientIP)
|
||||
bannedString, isValid := banned.(string)
|
||||
if isCached && isValid && len(bannedString) > 0 {
|
||||
return bannedString == cacheBannedValue, nil
|
||||
}
|
||||
return false, fmt.Errorf("no cache data")
|
||||
}
|
||||
|
||||
func setDecisionLocalCache(clientIP string, value string, duration int64) {
|
||||
cache.Set(clientIP, value, duration)
|
||||
}
|
||||
|
||||
func deleteDecisionLocalCache(clientIP string) {
|
||||
cache.Del(clientIP)
|
||||
}
|
||||
|
||||
func getDecisionRedisCache(clientIP string) (bool, error) {
|
||||
banned, err := redis.Get(clientIP)
|
||||
bannedString := string(banned)
|
||||
if err == nil && len(bannedString) > 0 {
|
||||
return bannedString == cacheBannedValue, nil
|
||||
}
|
||||
return false, fmt.Errorf("no cache data")
|
||||
}
|
||||
|
||||
func setDecisionRedisCache(clientIP string, value string, duration int64) {
|
||||
redis.Set(clientIP, []byte(value), duration)
|
||||
}
|
||||
|
||||
func deleteDecisionRedisCache(clientIP string) {
|
||||
redis.Del(clientIP)
|
||||
}
|
||||
|
||||
func DeleteDecision(clientIP string) {
|
||||
if redisEnabled {
|
||||
deleteDecisionRedisCache(clientIP)
|
||||
} else {
|
||||
deleteDecisionLocalCache(clientIP)
|
||||
}
|
||||
}
|
||||
|
||||
// GetDecision check in the cache if the IP has the banned / not banned value.
|
||||
// Otherwise return with an error to add the IP in cache if we are on.
|
||||
func GetDecision(clientIP string) (bool, error) {
|
||||
if redisEnabled {
|
||||
return getDecisionRedisCache(clientIP)
|
||||
} else {
|
||||
return getDecisionLocalCache(clientIP)
|
||||
}
|
||||
}
|
||||
|
||||
func SetDecision(clientIP string, isBanned bool, duration int64) {
|
||||
var value string
|
||||
if isBanned {
|
||||
logger.Debug(fmt.Sprintf("%v banned", clientIP))
|
||||
value = cacheBannedValue
|
||||
} else {
|
||||
value = cacheNoBannedValue
|
||||
}
|
||||
if redisEnabled {
|
||||
setDecisionRedisCache(clientIP, value, duration)
|
||||
} else {
|
||||
setDecisionLocalCache(clientIP, value, duration)
|
||||
}
|
||||
}
|
||||
|
||||
func InitRedisClient(host string) {
|
||||
redisEnabled = true
|
||||
redis.Init(host)
|
||||
logger.Debug("connect to redis")
|
||||
}
|
||||
+17
-11
@@ -8,7 +8,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
//CHECKER
|
||||
// CHECKER
|
||||
|
||||
// Checker allows to check that addresses are in a trusted IPs.
|
||||
type Checker struct {
|
||||
@@ -80,16 +80,8 @@ func parseIP(addr string) (net.IP, error) {
|
||||
return userIP, nil
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// STRATEGY
|
||||
|
||||
const (
|
||||
xForwardedFor = "X-Forwarded-For"
|
||||
)
|
||||
|
||||
// PoolStrategy is a strategy based on an IP Checker.
|
||||
// It allows to check whether addresses are in a given pool of IPs.
|
||||
type PoolStrategy struct {
|
||||
@@ -99,12 +91,13 @@ type PoolStrategy struct {
|
||||
// GetIP checks the list of Forwarded IPs (most recent first) against the
|
||||
// Checker pool of IPs. It returns the first IP that is not in the pool, or the
|
||||
// empty string otherwise.
|
||||
func (s *PoolStrategy) GetIP(req *http.Request) string {
|
||||
func (s *PoolStrategy) getIP(req *http.Request, customHeader string) string {
|
||||
if s.Checker == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
xff := req.Header.Get(xForwardedFor)
|
||||
xff := req.Header.Get(customHeader)
|
||||
|
||||
xffs := strings.Split(xff, ",")
|
||||
|
||||
for i := len(xffs) - 1; i >= 0; i-- {
|
||||
@@ -119,3 +112,16 @@ func (s *PoolStrategy) GetIP(req *http.Request) string {
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
// GetRemoteIP It returns the first IP that is not in the pool, or the empty string otherwise.
|
||||
func GetRemoteIP(req *http.Request, strategy *PoolStrategy, customHeader string) (string, error) {
|
||||
remoteIP := strategy.getIP(req, customHeader)
|
||||
if len(remoteIP) != 0 {
|
||||
return remoteIP, nil
|
||||
}
|
||||
remoteIP, _, err := net.SplitHostPort(req.RemoteAddr)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to extract ip from remote address: %w", err)
|
||||
}
|
||||
return remoteIP, nil
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
var (
|
||||
loggerInfo = log.New(io.Discard, "INFO: CrowdsecBouncerTraefikPlugin: ", log.Ldate|log.Ltime)
|
||||
loggerDebug = log.New(io.Discard, "DEBUG: CrowdsecBouncerTraefikPlugin: ", log.Ldate|log.Ltime)
|
||||
)
|
||||
|
||||
// Init Set Default log level to info in case log level to defined
|
||||
func Init(logLevel string) {
|
||||
switch logLevel {
|
||||
case "INFO":
|
||||
loggerInfo.SetOutput(os.Stdout)
|
||||
case "DEBUG":
|
||||
loggerInfo.SetOutput(os.Stdout)
|
||||
loggerDebug.SetOutput(os.Stdout)
|
||||
default:
|
||||
loggerInfo.SetOutput(os.Stdout)
|
||||
}
|
||||
}
|
||||
|
||||
// Info Log info
|
||||
func Info(str string) {
|
||||
loggerInfo.Printf(str)
|
||||
}
|
||||
|
||||
// Info Log debug
|
||||
func Debug(str string) {
|
||||
loggerDebug.Printf(str)
|
||||
}
|
||||
@@ -0,0 +1,212 @@
|
||||
package simpleredis
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/tehnerd/goUtils/netutils"
|
||||
)
|
||||
|
||||
type RedisCmd struct {
|
||||
Command string
|
||||
Name string
|
||||
Data []byte
|
||||
Duration int64
|
||||
Error error
|
||||
}
|
||||
|
||||
type SimpleRedis struct {
|
||||
redisChanRead chan RedisCmd
|
||||
redisChanWrite chan RedisCmd
|
||||
redisHost string
|
||||
redisCmd RedisCmd
|
||||
}
|
||||
|
||||
func genRedisArray(params ...[]byte) []byte {
|
||||
MSG := ""
|
||||
for cntr := 0; cntr < len(params); cntr++ {
|
||||
MSG = strings.Join([]string{MSG, string(params[cntr])}, " ")
|
||||
}
|
||||
MSG = strings.Trim(MSG, " ")
|
||||
MSG = strings.Join([]string{MSG, "\r\n"}, "")
|
||||
return []byte(MSG)
|
||||
}
|
||||
|
||||
func parseResponse(response []byte, dataBuf []byte, Len *int) ([]byte, []byte, error) {
|
||||
dataBuf = append(dataBuf, response...)
|
||||
lenCRLF := 2
|
||||
if *Len != 0 {
|
||||
if len(dataBuf) < *Len {
|
||||
return nil, dataBuf, nil
|
||||
} else {
|
||||
return dataBuf[:*Len], dataBuf[*Len:], nil
|
||||
}
|
||||
}
|
||||
for {
|
||||
switch string(dataBuf[0]) {
|
||||
case "+", "-", ":":
|
||||
//simple strings, error,int. usually ther are in format (+|-|:)DATA\r\n"
|
||||
if len(dataBuf) < 3 {
|
||||
return nil, dataBuf, nil
|
||||
}
|
||||
cntr := 1
|
||||
for ; cntr < len(dataBuf); cntr++ {
|
||||
if dataBuf[cntr] == '\r' {
|
||||
break
|
||||
}
|
||||
}
|
||||
if cntr == len(dataBuf) {
|
||||
return nil, dataBuf, nil
|
||||
}
|
||||
response = dataBuf[1:cntr]
|
||||
return response, dataBuf[cntr+2:], nil
|
||||
case "$":
|
||||
//bulk string. format $LEN\r\nDATA\r\n. up to 512MB
|
||||
cntr := 1
|
||||
for ; cntr < len(dataBuf); cntr++ {
|
||||
if string(dataBuf[cntr]) == "\r" {
|
||||
break
|
||||
}
|
||||
}
|
||||
if cntr == len(dataBuf) || cntr+lenCRLF > len(dataBuf) {
|
||||
return nil, dataBuf, nil
|
||||
}
|
||||
dataLen, err := strconv.Atoi(string(dataBuf[1:cntr]))
|
||||
if err != nil {
|
||||
return nil, dataBuf[cntr:], nil
|
||||
}
|
||||
|
||||
if dataLen == -1 {
|
||||
return nil, dataBuf[cntr:], fmt.Errorf("NOT FOUND")
|
||||
}
|
||||
if cntr+lenCRLF > len(dataBuf)-lenCRLF {
|
||||
*Len = dataLen
|
||||
return nil, dataBuf[cntr+lenCRLF:], nil
|
||||
}
|
||||
if len(dataBuf[cntr+lenCRLF:len(dataBuf)-lenCRLF]) < dataLen {
|
||||
*Len = dataLen
|
||||
return nil, dataBuf[cntr+lenCRLF:], nil
|
||||
} else {
|
||||
return dataBuf[cntr+lenCRLF : cntr+dataLen+lenCRLF], dataBuf[cntr+dataLen+lenCRLF:], nil
|
||||
}
|
||||
case "*":
|
||||
panic("array")
|
||||
default:
|
||||
if len(dataBuf) > 1 {
|
||||
dataBuf = dataBuf[1:]
|
||||
} else {
|
||||
return nil, dataBuf, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func initContext(hostnamePort string, redisCmdWrite, redisCmdRead chan RedisCmd) {
|
||||
tcpRemoteAddress, err := net.ResolveTCPAddr("tcp", hostnamePort)
|
||||
if err != nil {
|
||||
panic("cant resolve remote redis address")
|
||||
}
|
||||
var ladr *net.TCPAddr
|
||||
msgBuf := make([]byte, 65000)
|
||||
initMsg := []byte("*1\r\n$4\r\nPING\r\n")
|
||||
writeChan := make(chan []byte)
|
||||
readChan := make(chan []byte)
|
||||
flushChan := make(chan int)
|
||||
go netutils.AutoRecoonectedTCP(ladr, tcpRemoteAddress, msgBuf, initMsg, writeChan, readChan, flushChan)
|
||||
<-readChan
|
||||
dataBuf := make([]byte, 0)
|
||||
dataLen := 0
|
||||
for {
|
||||
select {
|
||||
case cmd := <-redisCmdWrite:
|
||||
switch cmd.Command {
|
||||
case "SET":
|
||||
data := genRedisArray([]byte("SET"), []byte(cmd.Name), cmd.Data, []byte("EX"), []byte(fmt.Sprintf("%v", cmd.Duration)))
|
||||
writeChan <- data
|
||||
case "GET":
|
||||
data := genRedisArray([]byte("GET"), []byte(cmd.Name))
|
||||
writeChan <- data
|
||||
case "DEL":
|
||||
data := genRedisArray([]byte("DEL"), []byte(cmd.Name))
|
||||
writeChan <- data
|
||||
}
|
||||
case response := <-readChan:
|
||||
data, dataBuf, err := parseResponse(response, dataBuf, &dataLen)
|
||||
if dataLen != 0 {
|
||||
for data == nil {
|
||||
response = <-readChan
|
||||
data, dataBuf, err = parseResponse(response, dataBuf, &dataLen)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
select {
|
||||
case redisCmdRead <- RedisCmd{
|
||||
Error: err,
|
||||
}:
|
||||
case <-time.After(time.Second * 5):
|
||||
}
|
||||
}
|
||||
if data != nil && string(data) != "PONG" {
|
||||
select {
|
||||
case redisCmdRead <- RedisCmd{
|
||||
Data: data,
|
||||
}:
|
||||
case <-time.After(time.Second * 5):
|
||||
}
|
||||
dataLen = 0
|
||||
}
|
||||
case <-flushChan:
|
||||
dataBuf = dataBuf[:]
|
||||
dataLen = 0
|
||||
select {
|
||||
case redisCmdRead <- RedisCmd{}:
|
||||
case <-time.After(time.Second * 5):
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (sr *SimpleRedis) Init(redisHost string) {
|
||||
sr.redisHost = redisHost
|
||||
sr.redisChanWrite = make(chan RedisCmd)
|
||||
sr.redisChanRead = make(chan RedisCmd)
|
||||
go initContext(sr.redisHost, sr.redisChanWrite, sr.redisChanRead)
|
||||
}
|
||||
|
||||
func (sr *SimpleRedis) Get(name string) ([]byte, error) {
|
||||
sr.redisCmd.Command = "GET"
|
||||
sr.redisCmd.Name = name
|
||||
sr.redisChanWrite <- sr.redisCmd
|
||||
resp := <-sr.redisChanRead
|
||||
if resp.Error != nil {
|
||||
return nil, resp.Error
|
||||
}
|
||||
return resp.Data, nil
|
||||
}
|
||||
|
||||
func (sr *SimpleRedis) Set(name string, data []byte, duration int64) error {
|
||||
sr.redisCmd.Command = "SET"
|
||||
sr.redisCmd.Name = name
|
||||
sr.redisCmd.Data = data
|
||||
sr.redisCmd.Duration = duration
|
||||
sr.redisChanWrite <- sr.redisCmd
|
||||
resp := <-sr.redisChanRead
|
||||
if resp.Error != nil {
|
||||
return resp.Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (sr *SimpleRedis) Del(name string) error {
|
||||
sr.redisCmd.Command = "DEL"
|
||||
sr.redisCmd.Name = name
|
||||
sr.redisChanWrite <- sr.redisCmd
|
||||
resp := <-sr.redisChanRead
|
||||
if resp.Error != nil {
|
||||
return resp.Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
+525
@@ -0,0 +1,525 @@
|
||||
package netutils
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
//"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
//Receive msg from tcp socket and send it as a []byte to readChan
|
||||
func ReadFromTCP2(sock *net.TCPConn, msgBuf []byte, readChan chan []byte,
|
||||
feedbackChanFromSocket chan int) {
|
||||
loop := 1
|
||||
for loop == 1 {
|
||||
bytes, err := sock.Read(msgBuf)
|
||||
if err != nil {
|
||||
feedbackChanFromSocket <- 1
|
||||
loop = 0
|
||||
continue
|
||||
}
|
||||
b := make([]byte, 0)
|
||||
b = append(b, msgBuf[:bytes]...)
|
||||
readChan <- b
|
||||
}
|
||||
}
|
||||
|
||||
//Receive msg from tcp socket and send it as a []byte to readChan
|
||||
func ReadFromTCP(sock *net.TCPConn, msgBuf []byte, readChan chan []byte,
|
||||
feedbackChanFromSocket chan int) {
|
||||
feedbackToSocket := make(chan bool)
|
||||
feedbackFromSocket := make(chan bool)
|
||||
reuseBufferChan := make(chan []byte, 1)
|
||||
go readFromTCP(sock, readChan, feedbackFromSocket,
|
||||
feedbackToSocket, reuseBufferChan)
|
||||
go func() {
|
||||
<-feedbackFromSocket
|
||||
feedbackChanFromSocket <- 1
|
||||
}()
|
||||
}
|
||||
|
||||
/*simple write to TCP, for oneway connections only (no communitcation w/ "read" part of the socket
|
||||
in terms of error propogation)*/
|
||||
func WriteToTCPw2(sock *net.TCPConn, writeChan chan []byte,
|
||||
feedbackChan chan int) {
|
||||
loop := 1
|
||||
for loop == 1 {
|
||||
select {
|
||||
case msg := <-writeChan:
|
||||
_, err := sock.Write(msg)
|
||||
if err != nil {
|
||||
feedbackChan <- 1
|
||||
continue
|
||||
}
|
||||
case <-feedbackChan:
|
||||
loop = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*simple write to TCP, for oneway connections only (no communitcation w/ "read" part of the socket
|
||||
in terms of error propogation)*/
|
||||
func WriteToTCPw(sock *net.TCPConn, writeChan chan []byte,
|
||||
feedbackChan chan int) {
|
||||
feedbackToSocket := make(chan bool)
|
||||
feedbackFromSocket := make(chan bool)
|
||||
go writeToTCP(sock, writeChan, feedbackFromSocket, feedbackToSocket)
|
||||
go func() {
|
||||
<-feedbackFromSocket
|
||||
feedbackChan <- 1
|
||||
}()
|
||||
}
|
||||
|
||||
//simple write to tcp w/ erorr propagation to/from "read" part of the socket
|
||||
func WriteToTCPrw2(sock *net.TCPConn, writeChan chan []byte,
|
||||
feedbackChanFromSocket, feedbackChanToSocket chan int) {
|
||||
loop := 1
|
||||
for loop == 1 {
|
||||
select {
|
||||
case msg := <-writeChan:
|
||||
_, err := sock.Write(msg)
|
||||
if err != nil {
|
||||
select {
|
||||
case feedbackChanFromSocket <- 1:
|
||||
continue
|
||||
case loop = <-feedbackChanToSocket:
|
||||
loop = 0
|
||||
continue
|
||||
}
|
||||
}
|
||||
case <-feedbackChanToSocket:
|
||||
loop = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//simple write to tcp w/ erorr propagation to/from "read" part of the socket
|
||||
func WriteToTCPrw(sock *net.TCPConn, writeChan chan []byte,
|
||||
feedbackChanFromSocket, feedbackChanToSocket chan int) {
|
||||
feedbackToSocket := make(chan bool)
|
||||
feedbackFromSocket := make(chan bool)
|
||||
go writeToTCP(sock, writeChan, feedbackFromSocket, feedbackToSocket)
|
||||
go func() {
|
||||
select {
|
||||
case <-feedbackFromSocket:
|
||||
feedbackChanFromSocket <- 1
|
||||
case <-feedbackChanToSocket:
|
||||
feedbackToSocket <- true
|
||||
|
||||
}
|
||||
}()
|
||||
|
||||
}
|
||||
|
||||
func makeBuffer(reuseBufferChan chan []byte) []byte {
|
||||
select {
|
||||
case buf := <-reuseBufferChan:
|
||||
return buf
|
||||
default:
|
||||
return make([]byte, 10000)
|
||||
}
|
||||
}
|
||||
|
||||
//Receive msg from tcp socket and send it as a []byte to readChan,with buffer reuse
|
||||
func readFromTCP(sock *net.TCPConn, readChan chan []byte,
|
||||
feedbackFromSocket, feedbackToSocket chan bool,
|
||||
reuseBufferChan chan []byte) {
|
||||
loop := 1
|
||||
var buf []byte
|
||||
for loop == 1 {
|
||||
buf = makeBuffer(reuseBufferChan)
|
||||
bytes, err := sock.Read(buf)
|
||||
if err != nil {
|
||||
select {
|
||||
case feedbackFromSocket <- true:
|
||||
loop = 0
|
||||
continue
|
||||
case <-feedbackToSocket:
|
||||
loop = 0
|
||||
continue
|
||||
}
|
||||
}
|
||||
select {
|
||||
case readChan <- buf[:bytes]:
|
||||
case <-feedbackToSocket:
|
||||
loop = 0
|
||||
continue
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//TCP's write routine, with feedback's chans
|
||||
func writeToTCP(sock *net.TCPConn, writeChan chan []byte,
|
||||
feedbackFromSocket, feedbackToSocket chan bool) {
|
||||
loop := 1
|
||||
for loop == 1 {
|
||||
select {
|
||||
case msg := <-writeChan:
|
||||
_, err := sock.Write(msg)
|
||||
if err != nil {
|
||||
select {
|
||||
case feedbackFromSocket <- true:
|
||||
case <-feedbackToSocket:
|
||||
}
|
||||
loop = 0
|
||||
continue
|
||||
}
|
||||
case <-feedbackToSocket:
|
||||
loop = 0
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//reconnecting to remote host for both read and write purpose
|
||||
func ReconnectTCPRW(ladr, radr *net.TCPAddr, msgBuf []byte, writeChan chan []byte,
|
||||
readChan chan []byte, feedbackChanToSocket, feedbackChanFromSocket chan int,
|
||||
init_msg []byte) {
|
||||
loop := 1
|
||||
for loop == 1 {
|
||||
sock, err := net.DialTCP("tcp", ladr, radr)
|
||||
if err != nil {
|
||||
time.Sleep(time.Duration(20+rand.Intn(15)) * time.Second)
|
||||
continue
|
||||
}
|
||||
//testing health of the new socket. GO sometimes doesnt rise the error when
|
||||
// we receive RST from remote side
|
||||
_, err = sock.Write(init_msg)
|
||||
if err != nil {
|
||||
sock.Close()
|
||||
time.Sleep(time.Duration(20+rand.Intn(15)) * time.Second)
|
||||
continue
|
||||
}
|
||||
loop = 0
|
||||
go ReadFromTCP(sock, msgBuf, readChan, feedbackChanFromSocket)
|
||||
go WriteToTCPrw(sock, writeChan, feedbackChanFromSocket, feedbackChanToSocket)
|
||||
}
|
||||
}
|
||||
|
||||
func ReconnectTCPRWReuse(ladr, radr *net.TCPAddr,
|
||||
readChan, writeChan, reuseBufferChan chan []byte,
|
||||
readFeedbackFrom, readFeedbackTo chan bool,
|
||||
writeFeedbackFrom, writeFeedbackTo chan bool) {
|
||||
loop := 1
|
||||
for loop == 1 {
|
||||
sock, err := net.DialTCP("tcp", ladr, radr)
|
||||
if err != nil {
|
||||
time.Sleep(time.Duration(20+rand.Intn(15)) * time.Second)
|
||||
continue
|
||||
}
|
||||
loop = 0
|
||||
go readFromTCP(sock, readChan, readFeedbackFrom, readFeedbackTo,
|
||||
reuseBufferChan)
|
||||
go writeToTCP(sock, writeChan, writeFeedbackFrom, writeFeedbackTo)
|
||||
}
|
||||
}
|
||||
|
||||
func AutoRecoonectedTCP(ladr, radr *net.TCPAddr, msgBuf, initMsg []byte,
|
||||
writeChan, readChan chan []byte, flushChan chan int) {
|
||||
feedbackChanFromSocket := make(chan int)
|
||||
feedbackChanToSocket := make(chan int)
|
||||
go ReconnectTCPRW(ladr, radr, msgBuf, writeChan, readChan, feedbackChanToSocket,
|
||||
feedbackChanFromSocket, initMsg)
|
||||
for {
|
||||
select {
|
||||
case feedbackFromSocket := <-feedbackChanFromSocket:
|
||||
feedbackChanToSocket <- feedbackFromSocket
|
||||
flushChan <- 1
|
||||
go ReconnectTCPRW(ladr, radr, msgBuf, writeChan,
|
||||
readChan, feedbackChanToSocket,
|
||||
feedbackChanFromSocket, initMsg)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func AutoRecoonectedTCPReuse(ladr, radr *net.TCPAddr,
|
||||
readChan, writeChan chan []byte,
|
||||
reuseChan chan []byte,
|
||||
flushChan chan bool) {
|
||||
readFeedbackFrom := make(chan bool)
|
||||
readFeedbackTo := make(chan bool)
|
||||
writeFeedbackFrom := make(chan bool)
|
||||
writeFeedbackTo := make(chan bool)
|
||||
go ReconnectTCPRWReuse(ladr, radr, readChan, writeChan, reuseChan,
|
||||
readFeedbackFrom, readFeedbackTo,
|
||||
writeFeedbackFrom, writeFeedbackTo)
|
||||
for {
|
||||
select {
|
||||
case <-readFeedbackFrom:
|
||||
writeFeedbackTo <- true
|
||||
case <-writeFeedbackFrom:
|
||||
readFeedbackTo <- true
|
||||
}
|
||||
flushChan <- true
|
||||
go ReconnectTCPRWReuse(ladr, radr, readChan, writeChan, reuseChan,
|
||||
readFeedbackFrom, readFeedbackTo,
|
||||
writeFeedbackFrom, writeFeedbackTo)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//reconnecting to remote host for write only
|
||||
func ReconnectTCPW(radr net.TCPAddr, writeChan chan []byte, feedbackChan chan int) {
|
||||
loop := 1
|
||||
for loop == 1 {
|
||||
time.Sleep(time.Duration(20+rand.Intn(15)) * time.Second)
|
||||
sock, err := net.DialTCP("tcp", nil, &radr)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
//testing health of the new socket. GO sometimes doesnt rise the error when
|
||||
// we receive RST from remote side
|
||||
_, err = sock.Write([]byte{1})
|
||||
if err != nil {
|
||||
sock.Close()
|
||||
continue
|
||||
}
|
||||
loop = 0
|
||||
go WriteToTCPw(sock, writeChan, feedbackChan)
|
||||
}
|
||||
}
|
||||
|
||||
/* --------------------- CONNECTION MANAGER -------------------------
|
||||
Connection manager will allow send data and receive data from remote hosts.
|
||||
it will have single ConnectionMsg (see below) read chan and single
|
||||
ConnectionMsg write chan toward it's clients
|
||||
as well as single read chan from sockets, but multiple write sockets.
|
||||
it will route msgs according to Host field in connectionManager struct
|
||||
(if it received from client, it will send this msg toward Host's sockets;
|
||||
if recved from socket, will proxy it toward client(and client will know from which remote host
|
||||
it was received)
|
||||
-------------------------------------------------------------------- */
|
||||
/*
|
||||
MsgType's could be:
|
||||
from Api's client to ConnectionManager:
|
||||
"Data" - msg with Data to Host
|
||||
"Connect" - connect to new Host
|
||||
...
|
||||
from ConnectionManager to Api's client:
|
||||
"BufferFlush" - notification that connection to remote Host not longer working.
|
||||
advice to flush all the msg buffers assosiated with remote host
|
||||
*/
|
||||
type ConnectionMsg struct {
|
||||
Host string
|
||||
Data []byte
|
||||
Type string
|
||||
}
|
||||
|
||||
/*
|
||||
Receive msg from tcp socket and send it as a ConnectionMsg to readChan
|
||||
TODO: think about more generic version to be more DRYer (to work in both CM and []byte chans
|
||||
*/
|
||||
func CMReadFromTCP(sock *net.TCPConn, readChan chan ConnectionMsg,
|
||||
peerAddress string) {
|
||||
msgBuf := make([]byte, 65000)
|
||||
loop := 1
|
||||
var msg ConnectionMsg
|
||||
msg.Host = peerAddress
|
||||
msg.Type = "Data"
|
||||
for loop == 1 {
|
||||
bytes, err := sock.Read(msgBuf)
|
||||
if err != nil {
|
||||
msg.Type = "ReadError"
|
||||
readChan <- msg
|
||||
loop = 0
|
||||
continue
|
||||
}
|
||||
b := make([]byte, 0)
|
||||
b = append(b, msgBuf[:bytes]...)
|
||||
msg.Data = b
|
||||
readChan <- msg
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
ConnectionManager write instance to tcp w/ erorr propagation to/from "read" part of the socket
|
||||
TODO: think about more generic version to be more DRYer (to work in both CM and []byte chans
|
||||
*/
|
||||
func CMWriteToTCP(sock *net.TCPConn, writeChan, readChan chan ConnectionMsg,
|
||||
peerAddress string) {
|
||||
loop := 1
|
||||
var errorMsg ConnectionMsg
|
||||
errorMsg.Host = peerAddress
|
||||
errorMsg.Type = "WriteError"
|
||||
for loop == 1 {
|
||||
select {
|
||||
case msg := <-writeChan:
|
||||
switch msg.Type {
|
||||
case "Data":
|
||||
_, err := sock.Write(msg.Data)
|
||||
if err != nil {
|
||||
for loop == 1 {
|
||||
select {
|
||||
case readChan <- errorMsg:
|
||||
case errorMsg := <-writeChan:
|
||||
if errorMsg.Type != "ConnectionError" {
|
||||
continue
|
||||
}
|
||||
}
|
||||
loop = 0
|
||||
}
|
||||
}
|
||||
case "ConnectionError":
|
||||
loop = 0
|
||||
default:
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func StartConnection(tcpConn *net.TCPConn, writeChan,
|
||||
readChan chan ConnectionMsg, peerAddress string) {
|
||||
go CMReadFromTCP(tcpConn, readChan, peerAddress)
|
||||
go CMWriteToTCP(tcpConn, writeChan, readChan, peerAddress)
|
||||
}
|
||||
|
||||
func CMListenForConnection(mutex *sync.RWMutex, localPort int,
|
||||
writeChanMap map[string]chan ConnectionMsg,
|
||||
connectionStateMap map[string]int,
|
||||
readChan chan ConnectionMsg) {
|
||||
laddr := strings.Join([]string{":", strconv.Itoa(localPort)}, "")
|
||||
tcpLaddr, err := net.ResolveTCPAddr("tcp", laddr)
|
||||
if err != nil {
|
||||
panic("cant resolve local address for binding")
|
||||
}
|
||||
tcpListener, err := net.ListenTCP("tcp", tcpLaddr)
|
||||
if err != nil {
|
||||
panic("cant listen on local address for binding")
|
||||
}
|
||||
for {
|
||||
tcpConn, err := tcpListener.AcceptTCP()
|
||||
if err == nil {
|
||||
radr := strings.Split(tcpConn.RemoteAddr().String(), ":")[0]
|
||||
// check if we already has connection to remote peer as a client
|
||||
mutex.Lock()
|
||||
if val, exist := connectionStateMap[radr]; exist && val == 1 {
|
||||
tcpConn.Close()
|
||||
mutex.Unlock()
|
||||
continue
|
||||
}
|
||||
connectionStateMap[radr] = 1
|
||||
if writeChan, exist := writeChanMap[radr]; exist {
|
||||
mutex.Unlock()
|
||||
go StartConnection(tcpConn, writeChan, readChan, radr)
|
||||
} else {
|
||||
writeChanMap[radr] = make(chan ConnectionMsg)
|
||||
mutex.Unlock()
|
||||
go StartConnection(tcpConn, writeChanMap[radr], readChan, radr)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func CMConnectToRemotePeer(mutex *sync.RWMutex, peerTcpAddr *net.TCPAddr,
|
||||
radr string,
|
||||
writeChan chan ConnectionMsg,
|
||||
readChan chan ConnectionMsg,
|
||||
connectionStateMap map[string]int) {
|
||||
connectLoop := 1
|
||||
for connectLoop == 1 {
|
||||
mutex.RLock()
|
||||
if connectionStateMap[radr] == 1 {
|
||||
connectLoop = 0
|
||||
mutex.RUnlock()
|
||||
continue
|
||||
}
|
||||
mutex.RUnlock()
|
||||
tcpConn, err := net.DialTCP("tcp", nil, peerTcpAddr)
|
||||
if err != nil {
|
||||
time.Sleep(time.Second * time.Duration(rand.Int63n(15)))
|
||||
continue
|
||||
}
|
||||
mutex.Lock()
|
||||
if connectionStateMap[radr] == 0 {
|
||||
connectionStateMap[radr] = 1
|
||||
go StartConnection(tcpConn, writeChan, readChan, radr)
|
||||
mutex.Unlock()
|
||||
connectLoop = 0
|
||||
continue
|
||||
} else {
|
||||
mutex.Unlock()
|
||||
tcpConn.Close()
|
||||
connectLoop = 0
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func ConnectionManager(msgChan chan ConnectionMsg, localPort int) {
|
||||
writeChanMap := make(map[string]chan ConnectionMsg)
|
||||
connectionStateMap := make(map[string]int)
|
||||
readChan := make(chan ConnectionMsg)
|
||||
var connectionMutex sync.RWMutex
|
||||
go CMListenForConnection(&connectionMutex, localPort, writeChanMap,
|
||||
connectionStateMap, readChan)
|
||||
for {
|
||||
select {
|
||||
case msgToPeer := <-msgChan:
|
||||
switch msgToPeer.Type {
|
||||
case "Data":
|
||||
if state, exists := connectionStateMap[msgToPeer.Host]; exists && state == 1 {
|
||||
/*FIXME/THINK: There could be deadlock if connectin closes before we will
|
||||
be able to send to the chan */
|
||||
writeChan := writeChanMap[msgToPeer.Host]
|
||||
writeChan <- msgToPeer
|
||||
} else {
|
||||
msgChan <- ConnectionMsg{Type: "ConnectionNotExist"}
|
||||
}
|
||||
case "Connect":
|
||||
if len(strings.Split(msgToPeer.Host, ":")) > 1 {
|
||||
radr := strings.Split(msgToPeer.Host, ":")[0]
|
||||
connectionMutex.Lock()
|
||||
if _, exist := writeChanMap[radr]; !exist {
|
||||
writeChanMap[radr] = make(chan ConnectionMsg)
|
||||
}
|
||||
connectionMutex.Unlock()
|
||||
peerTcpAddr, err := net.ResolveTCPAddr("tcp", msgToPeer.Host)
|
||||
if err != nil {
|
||||
//XXX: think about , mb make something less drastic
|
||||
panic("cant resolve remote address")
|
||||
}
|
||||
go CMConnectToRemotePeer(&connectionMutex, peerTcpAddr, radr,
|
||||
writeChanMap[radr], readChan, connectionStateMap)
|
||||
} else {
|
||||
connectionMutex.Lock()
|
||||
if _, exist := writeChanMap[msgToPeer.Host]; !exist {
|
||||
writeChanMap[msgToPeer.Host] = make(chan ConnectionMsg)
|
||||
}
|
||||
connectionMutex.Unlock()
|
||||
remoteAddr := strings.Join([]string{msgToPeer.Host, strconv.Itoa(localPort)}, ":")
|
||||
peerTcpAddr, err := net.ResolveTCPAddr("tcp", remoteAddr)
|
||||
if err != nil {
|
||||
//XXX: again panic could be overkill
|
||||
panic("cant resolve remote address")
|
||||
}
|
||||
go CMConnectToRemotePeer(&connectionMutex, peerTcpAddr, msgToPeer.Host,
|
||||
writeChanMap[msgToPeer.Host], readChan, connectionStateMap)
|
||||
}
|
||||
}
|
||||
case msgFromPeer := <-readChan:
|
||||
switch msgFromPeer.Type {
|
||||
case "Data":
|
||||
msgChan <- msgFromPeer
|
||||
case "WriteError", "ReadError":
|
||||
connectionMutex.Lock()
|
||||
connectionStateMap[msgFromPeer.Host] = 0
|
||||
connectionMutex.Unlock()
|
||||
if msgFromPeer.Type == "ReadError" {
|
||||
writeChanMap[msgFromPeer.Host] <- ConnectionMsg{Type: "ConnectionError"}
|
||||
}
|
||||
var msgToApiClient ConnectionMsg
|
||||
msgToApiClient.Host = msgFromPeer.Host
|
||||
msgToApiClient.Type = "BufferFlush"
|
||||
msgChan <- msgToApiClient
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package netutils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
)
|
||||
|
||||
/*
|
||||
we need to provide a function, which will read/write to/from socket and read/write to/from sockets feedback chans
|
||||
*/
|
||||
func ListenForConnection(port string, fn func(chan []byte, chan []byte, chan int, chan int)) error {
|
||||
addr := ":" + port
|
||||
tcpAddr, err := net.ResolveTCPAddr("tcp", addr)
|
||||
if err != nil {
|
||||
return errors.New("cant resolve local tcp address")
|
||||
}
|
||||
loop := 1
|
||||
servSock, err := net.ListenTCP("tcp", tcpAddr)
|
||||
if err != nil {
|
||||
return errors.New("cant bind to local tcp address")
|
||||
}
|
||||
|
||||
for loop == 1 {
|
||||
sock, err := servSock.AcceptTCP()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
go ServeTcpConn(sock, fn)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ServeTcpConn(sock *net.TCPConn, fn func(chan []byte, chan []byte, chan int, chan int)) {
|
||||
readChan := make(chan []byte)
|
||||
writeChan := make(chan []byte)
|
||||
feedbackFrom := make(chan int, 1)
|
||||
feedbackTo := make(chan int, 1)
|
||||
buf := make([]byte, 65535)
|
||||
go ReadFromTCP(sock, buf, readChan, feedbackFrom)
|
||||
go WriteToTCPrw(sock, writeChan, feedbackFrom, feedbackTo)
|
||||
fn(readChan, writeChan, feedbackFrom, feedbackTo)
|
||||
}
|
||||
Vendored
+3
@@ -1,3 +1,6 @@
|
||||
# github.com/leprosus/golang-ttl-map v1.1.7
|
||||
## explicit; go 1.15
|
||||
github.com/leprosus/golang-ttl-map
|
||||
# github.com/tehnerd/goUtils v0.0.0-20150515130609-5a2d8fb2ded8
|
||||
## explicit
|
||||
github.com/tehnerd/goUtils/netutils
|
||||
|
||||
Reference in New Issue
Block a user