Add option to send GET request to url when something connected

This commit is contained in:
Oleksii Shevchuk 2018-01-30 17:55:47 +02:00
parent 4add394614
commit 33198d3e91
3 changed files with 42 additions and 0 deletions

View File

@ -3,6 +3,9 @@ package main
import ( import (
"crypto/tls" "crypto/tls"
"net" "net"
"net/http"
"sync/atomic"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@ -37,6 +40,28 @@ func (d *Daemon) ListenAndServe() error {
return nil return nil
} }
func (d *Daemon) onListenerEnabled() {
if atomic.AddInt32(&d.UsersCount, 1) == 1 && OnListenerEnabledURL != "" {
response, err := http.Get(OnListenerEnabledURL)
if err != nil {
log.Error("Register failed: ", err)
} else {
log.Info("Register:", OnListenerEnabledURL, ": ", response.Status)
}
}
}
func (d *Daemon) onListenerDisabled() {
if atomic.AddInt32(&d.UsersCount, -1) == 0 && OnListenerDisabledURL != "" {
response, err := http.Get(OnListenerDisabledURL)
if err != nil {
log.Error("Register failed: ", err)
} else {
log.Info("Register:", OnListenerDisabledURL, ": ", response.Status)
}
}
}
func (d *Daemon) handle(conn net.Conn) { func (d *Daemon) handle(conn net.Conn) {
defer conn.Close() defer conn.Close()
@ -68,7 +93,9 @@ func (d *Daemon) handle(conn net.Conn) {
d.DNSCheck.Unlock() d.DNSCheck.Unlock()
d.DNSLock.Lock() d.DNSLock.Lock()
d.onListenerEnabled()
d.serveDNS(conn, brh.BindInfo) d.serveDNS(conn, brh.BindInfo)
d.onListenerDisabled()
d.DNSCheck.Lock() d.DNSCheck.Lock()
d.DNSListener = nil d.DNSListener = nil
d.DNSCheck.Unlock() d.DNSCheck.Unlock()
@ -89,17 +116,23 @@ func (d *Daemon) handle(conn net.Conn) {
case TCP: case TCP:
log.Warning("Request: TCP handler with port:", brh.BindInfo, " client: ", client, " - start") log.Warning("Request: TCP handler with port:", brh.BindInfo, " client: ", client, " - start")
d.onListenerEnabled()
d.serveStream(-1, conn, brh.BindInfo, d.listenAcceptTCP) d.serveStream(-1, conn, brh.BindInfo, d.listenAcceptTCP)
d.onListenerDisabled()
log.Warning("Request: TCP handler with port:", brh.BindInfo, " client: ", client, " - complete") log.Warning("Request: TCP handler with port:", brh.BindInfo, " client: ", client, " - complete")
case KCP: case KCP:
log.Warning("Request: KCP handler with port:", brh.BindInfo, " client: ", client, " - start") log.Warning("Request: KCP handler with port:", brh.BindInfo, " client: ", client, " - start")
d.onListenerEnabled()
d.serveStream(int(UDPSize-24), conn, brh.BindInfo, d.listenAcceptKCP) d.serveStream(int(UDPSize-24), conn, brh.BindInfo, d.listenAcceptKCP)
d.onListenerDisabled()
log.Warning("Request: KCP handler with port:", brh.BindInfo, " client: ", client, " - complete") log.Warning("Request: KCP handler with port:", brh.BindInfo, " client: ", client, " - complete")
case TLS: case TLS:
log.Warning("Request: SSL handler with port:", brh.BindInfo, " client: ", client, " - start") log.Warning("Request: SSL handler with port:", brh.BindInfo, " client: ", client, " - start")
d.onListenerEnabled()
d.serveStream(-1, conn, brh.BindInfo, d.listenAcceptTLS) d.serveStream(-1, conn, brh.BindInfo, d.listenAcceptTLS)
d.onListenerDisabled()
log.Warning("Request: SSL handler with port:", brh.BindInfo, " client: ", client, " - complete") log.Warning("Request: SSL handler with port:", brh.BindInfo, " client: ", client, " - complete")
default: default:

View File

@ -30,6 +30,9 @@ var (
ClientKey = path.Join("..", "crypto", "proxy-client.key") ClientKey = path.Join("..", "crypto", "proxy-client.key")
ClientCert = path.Join("..", "crypto", "proxy-client.crt") ClientCert = path.Join("..", "crypto", "proxy-client.crt")
OnListenerEnabledURL = ""
OnListenerDisabledURL = ""
ListenerConfig *tls.Config ListenerConfig *tls.Config
) )
@ -48,6 +51,10 @@ func init() {
flag.StringVar(&ProxyHostname, "hostname-proxy", ProxyHostname, flag.StringVar(&ProxyHostname, "hostname-proxy", ProxyHostname,
"Hostname for pupysh listener side (used with generate)") "Hostname for pupysh listener side (used with generate)")
flag.StringVar(&loglevel, "loglevel", loglevel, "Set log level") flag.StringVar(&loglevel, "loglevel", loglevel, "Set log level")
flag.StringVar(&OnListenerEnabledURL, "on-enabled-url", OnListenerEnabledURL,
"Send GET request when at least one client connected")
flag.StringVar(&OnListenerDisabledURL, "on-disabled-url", OnListenerDisabledURL,
"Send GET request when at least one client connected")
flag.BoolVar(&generate, "generate", false, "Generate all the keys") flag.BoolVar(&generate, "generate", false, "Generate all the keys")
iniflags.Parse() iniflags.Parse()

View File

@ -71,6 +71,8 @@ type (
Listeners map[int]*Listener Listeners map[int]*Listener
ListenersLock sync.Mutex ListenersLock sync.Mutex
UsersCount int32
} }
IPInfo struct { IPInfo struct {