//setupPurgeReceiver set up the tcp socket where ban messages come //when a purge pattern is received it dispatches it to a Pub object func setupPurgeReceiver(incomingAddress *string, publisher *Publisher) { receiver, err := net.Listen("tcp", *incomingAddress) utils.CheckError(err, logger) go func() { ping := []byte("ping") for { time.Sleep(5 * time.Second) publisher.Pub(ping) } }() for { conn, err := receiver.Accept() utils.CheckError(err, logger) go func(c net.Conn) { defer conn.Close() b, err := ioutil.ReadAll(conn) if err != nil { logger.Info(fmt.Sprintln("Client connection error:", err)) } else { clean_purge := bytes.TrimSpace(b) logger.Info(fmt.Sprintln("<-", utils.ReverseName(conn), string(clean_purge))) publisher.Pub(clean_purge) conn.Write([]byte("OK\n")) } }(conn) } return }
//setupPurgeSenderAndListen start listening to the socket where varnish cli connects //when a client connects it calls handleClient func setupPurgeSenderAndListen(outgoingAddress *string, purgeOnStartup bool, publisher *Publisher, secret *string) { ln, err := net.Listen("tcp", *outgoingAddress) utils.CheckError(err, logger) for { conn, err := ln.Accept() if err != nil { // handle error continue } logger.Info(fmt.Sprintln("New client:", utils.ReverseName(conn))) // connect client to the pubsub purge go handleVarnishClient(conn, publisher, purgeOnStartup, secret) } return }