updated ipbl
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Paul 2023-05-27 18:36:15 +02:00
parent e5658ff99f
commit 670f8a6f9f
4 changed files with 29 additions and 23 deletions

View File

@ -236,6 +236,7 @@ func QueryInfo(client *http.Client, ip string) (query QueryIP, err error) {
res, err := client.Do(req) res, err := client.Do(req)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return
} }
data, err := io.ReadAll(res.Body) data, err := io.ReadAll(res.Body)
if err != nil { if err != nil {

View File

@ -8,32 +8,32 @@ import (
"golang.org/x/net/websocket" "golang.org/x/net/websocket"
) )
var WebSocketChannel = make(chan models.APIEvent, 100) var webSocketChannel = make(chan models.APIEvent, 100)
var WebSocketChannelDone = make(chan bool) var webSocketChannelDone = make(chan bool)
var LISTENERS map[string]*ConnectionInfo var listeners map[string]*connectionInfo
func Init(cfg *config.Config) { func Init(cfg *config.Config) {
LISTENERS = make(map[string]*ConnectionInfo) listeners = make(map[string]*connectionInfo)
} }
func WelcomeAgents(ws *websocket.Conn, welcome WSWelcome, t string) { func welcomeAgents(ws *websocket.Conn, welcome wsWelcome, t string) {
connectinfo, ok := LISTENERS[welcome.Hostname] connectinfo, ok := listeners[welcome.Hostname]
if !ok { if !ok {
switch t { switch t {
case "ps": case "ps":
connectinfo := ConnectionInfo{ connectinfo := connectionInfo{
ConnectionPS: ws, ConnectionPS: ws,
InitDate: time.Now(), InitDate: time.Now(),
} }
LISTENERS[welcome.Hostname] = &connectinfo listeners[welcome.Hostname] = &connectinfo
case "rr": case "rr":
connectinfo := ConnectionInfo{ connectinfo := connectionInfo{
ConnectionRR: ws, ConnectionRR: ws,
InitDate: time.Now(), InitDate: time.Now(),
} }
LISTENERS[welcome.Hostname] = &connectinfo listeners[welcome.Hostname] = &connectinfo
} }
} else { } else {
switch t { switch t {
@ -46,22 +46,25 @@ func WelcomeAgents(ws *websocket.Conn, welcome WSWelcome, t string) {
} }
func gcConnOnError(ws *websocket.Conn) (err error) { func gcConnOnError(ws *websocket.Conn) (err error) {
for index, value := range LISTENERS { for index, value := range listeners {
if value.ConnectionPS == ws || value.ConnectionRR == ws { if value.ConnectionPS == ws {
value.ConnectionPS.Close() value.ConnectionPS.Close()
delete(listeners, index)
} else if value.ConnectionRR == ws {
value.ConnectionRR.Close() value.ConnectionRR.Close()
delete(LISTENERS, index) delete(listeners, index)
} }
} }
return err return err
} }
type ConnectionInfo struct { type connectionInfo struct {
ConnectionPS *websocket.Conn ConnectionPS *websocket.Conn
ConnectionRR *websocket.Conn ConnectionRR *websocket.Conn
InitDate time.Time InitDate time.Time
} }
type WSWelcome struct { // WSWelcome
type wsWelcome struct {
Hostname string Hostname string
} }

View File

@ -10,11 +10,12 @@ import (
"golang.org/x/net/websocket" "golang.org/x/net/websocket"
) )
// HandleWSPS handle pub sub flows
func HandleWSPS(c echo.Context, cfg *config.Config) (err error) { func HandleWSPS(c echo.Context, cfg *config.Config) (err error) {
websocket.Handler(func(ws *websocket.Conn) { websocket.Handler(func(ws *websocket.Conn) {
defer ws.Close() defer ws.Close()
var welcome = WSWelcome{} var welcome = wsWelcome{}
var msg []byte var msg []byte
err := websocket.Message.Receive(ws, &msg) err := websocket.Message.Receive(ws, &msg)
@ -26,7 +27,7 @@ func HandleWSPS(c echo.Context, cfg *config.Config) (err error) {
if err != nil { if err != nil {
log.Println(err) log.Println(err)
} else { } else {
WelcomeAgents(ws, welcome, "ps") welcomeAgents(ws, welcome, "ps")
} }
err = websocket.Message.Receive(ws, "OK") err = websocket.Message.Receive(ws, "OK")

View File

@ -10,11 +10,12 @@ import (
"golang.org/x/net/websocket" "golang.org/x/net/websocket"
) )
// HandleWSRR handle req rep flows
func HandleWSRR(c echo.Context, cfg *config.Config) error { func HandleWSRR(c echo.Context, cfg *config.Config) error {
websocket.Handler(func(ws *websocket.Conn) { websocket.Handler(func(ws *websocket.Conn) {
defer ws.Close() defer ws.Close()
var welcome = WSWelcome{} var welcome = wsWelcome{}
var msg []byte var msg []byte
err := websocket.Message.Receive(ws, &msg) err := websocket.Message.Receive(ws, &msg)
@ -24,7 +25,7 @@ func HandleWSRR(c echo.Context, cfg *config.Config) error {
err = json.Unmarshal(msg, &welcome) err = json.Unmarshal(msg, &welcome)
if err == nil { if err == nil {
WelcomeAgents(ws, welcome, "rr") welcomeAgents(ws, welcome, "rr")
} }
var lastip string var lastip string
@ -51,7 +52,7 @@ func HandleWSRR(c echo.Context, cfg *config.Config) error {
switch apievent.MsgType { switch apievent.MsgType {
case "bootstrap": case "bootstrap":
log.Printf("bootstrap: %s\n", apievent.Hostname) log.Printf("bootstrap: %s\n", apievent.Hostname)
for index, value := range LISTENERS { for index, value := range listeners {
if index != apievent.Hostname && value.ConnectionPS != nil { if index != apievent.Hostname && value.ConnectionPS != nil {
err = websocket.Message.Send(value.ConnectionPS, msg) err = websocket.Message.Send(value.ConnectionPS, msg)
if err != nil { if err != nil {
@ -70,7 +71,7 @@ func HandleWSRR(c echo.Context, cfg *config.Config) error {
log.Println(err) log.Println(err)
} }
for _, value := range LISTENERS { for _, value := range listeners {
if value.ConnectionPS != nil { if value.ConnectionPS != nil {
err = websocket.Message.Send(value.ConnectionPS, msg) err = websocket.Message.Send(value.ConnectionPS, msg)
if err != nil { if err != nil {
@ -81,7 +82,7 @@ func HandleWSRR(c echo.Context, cfg *config.Config) error {
} }
log.Printf("ws: Inserted event") log.Printf("ws: Inserted event")
case "init": case "init":
for _, value := range LISTENERS { for _, value := range listeners {
if value.ConnectionPS != nil { if value.ConnectionPS != nil {
err = websocket.Message.Send(value.ConnectionPS, msg) err = websocket.Message.Send(value.ConnectionPS, msg)
if err != nil { if err != nil {
@ -104,7 +105,7 @@ func HandleWSRR(c echo.Context, cfg *config.Config) error {
err = websocket.Message.Send(ws, "OK") err = websocket.Message.Send(ws, "OK")
if err != nil { if err != nil {
//gcConn(ws, "rr") //gcConn(ws, "rr")
log.Println(LISTENERS) log.Println(listeners)
return return
} }
*/ */