From fc88f72c6d40d981768297932e227a8d30d41923 Mon Sep 17 00:00:00 2001 From: Hamed Bahadorzadeh Date: Fri, 7 Sep 2018 13:17:35 +0430 Subject: [PATCH] Closed added to tunnels to be able to monitor them --- interface/common/tunnel_interface_client.go | 1 + interface/serial/serial_client.go | 2 +- interface/socks/socks_client.go | 7 +++++ interface/tcp/tcp_client.go | 19 +++++++++++--- interface/tun/tun_iface.go | 29 ++++++++++++++------- interface/udp/udp_client.go | 11 ++++++++ tunnel/common/tunnel_server.go | 12 ++++++++- 7 files changed, 66 insertions(+), 15 deletions(-) diff --git a/interface/common/tunnel_interface_client.go b/interface/common/tunnel_interface_client.go index 062b3e5..aa98953 100644 --- a/interface/common/tunnel_interface_client.go +++ b/interface/common/tunnel_interface_client.go @@ -8,4 +8,5 @@ type TunnelInterfaceClient interface { HandleConnection(conn net.Conn, tconn net.Conn) error WaitingForConnection() Close() + Closed() bool } diff --git a/interface/serial/serial_client.go b/interface/serial/serial_client.go index 5f52e9a..658bc5a 100644 --- a/interface/serial/serial_client.go +++ b/interface/serial/serial_client.go @@ -1,9 +1,9 @@ package serial import ( - "github.com/jacobsa/go-serial/serial" icommon "github.com/hbahadorzadeh/stunning/interface/common" tcommon "github.com/hbahadorzadeh/stunning/tunnel/common" + "github.com/jacobsa/go-serial/serial" "io" "log" "net" diff --git a/interface/socks/socks_client.go b/interface/socks/socks_client.go index 8be772e..e3f1259 100644 --- a/interface/socks/socks_client.go +++ b/interface/socks/socks_client.go @@ -13,6 +13,7 @@ type SocksClient struct { tun_dialer tcommon.TunnelDialer saddress string listen net.Listener + closed bool } func GetSocksClient(url, surl string, tun_dialer tcommon.TunnelDialer) *SocksClient { @@ -25,6 +26,7 @@ func GetSocksClient(url, surl string, tun_dialer tcommon.TunnelDialer) *SocksCli log.Panic(err) } s.listen = listen + s.closed = false return s } @@ -53,6 +55,11 @@ func (t SocksClient) HandleConnection(conn net.Conn, tconn net.Conn) error { func (t SocksClient) Close() { t.listen.Close() + t.closed = true +} + +func (t SocksClient) Closed() bool { + return t.closed } func tcp_reader(conn net.Conn, tconn net.Conn) { diff --git a/interface/tcp/tcp_client.go b/interface/tcp/tcp_client.go index ba1988c..efe18c1 100644 --- a/interface/tcp/tcp_client.go +++ b/interface/tcp/tcp_client.go @@ -13,10 +13,11 @@ type TcpClient struct { tun_dialer tcommon.TunnelDialer saddress string listen net.Listener + closed bool } -func GetTcpClient(url, surl string, tun_dialer tcommon.TunnelDialer) *TcpClient { - s := &TcpClient{} +func GetTcpClient(url, surl string, tun_dialer tcommon.TunnelDialer) TcpClient { + s := TcpClient{} s.address = url s.saddress = surl s.tun_dialer = tun_dialer @@ -25,11 +26,12 @@ func GetTcpClient(url, surl string, tun_dialer tcommon.TunnelDialer) *TcpClient log.Panic(err) } s.listen = listen + s.closed = false return s } func (t *TcpClient) WaitingForConnection() { - for { + for !t.closed { conn, err := t.listen.Accept() if err != nil { log.Fatalln(err) @@ -42,11 +44,20 @@ func (t *TcpClient) WaitingForConnection() { } go t.HandleConnection(conn, sconn) } + t.closed = true } -func (t *TcpClient) HandleConnection(conn net.Conn, tconn net.Conn) error { +func (t TcpClient) HandleConnection(conn net.Conn, tconn net.Conn) error { log.Printf("Socket to %s handling connection \n", t.address) go tcp_reader(conn, tconn) tcp_writer(conn, tconn) return nil } + +func (t *TcpClient) Close() { + t.closed = true +} + +func (t TcpClient) Closed() bool { + return t.closed +} diff --git a/interface/tun/tun_iface.go b/interface/tun/tun_iface.go index 2272f51..a1f68f9 100644 --- a/interface/tun/tun_iface.go +++ b/interface/tun/tun_iface.go @@ -1,10 +1,10 @@ package tun import ( - "github.com/songgao/water" "github.com/hbahadorzadeh/stunning/common" icommon "github.com/hbahadorzadeh/stunning/interface/common" tcommon "github.com/hbahadorzadeh/stunning/tunnel/common" + "github.com/songgao/water" "log" "net" "os" @@ -15,8 +15,8 @@ import ( type TunInterface struct { icommon.TunnelInterfaceServer icommon.TunnelInterfaceClient - conf TunConfig - iface *water.Interface + conf TunConfig + iface *water.Interface stopped bool } @@ -24,6 +24,7 @@ type TunInterfaceClient struct { TunInterface address string dialer tcommon.TunnelDialer + closed bool } type TunConfig struct { @@ -47,8 +48,8 @@ func GetTunIface(config TunConfig) TunInterface { } iface := TunInterface{ - iface: ifce, - conf: config, + iface: ifce, + conf: config, stopped: false, } return iface @@ -73,6 +74,7 @@ func GetTunIfaceClient(config TunConfig, addr string, d tcommon.TunnelDialer) Tu } iface.iface = ifce iface.conf = config + iface.closed = false return iface } @@ -137,21 +139,30 @@ func runIP(args ...string) { } } -func (t TunInterfaceClient) WaitingForConnection() { +func (t *TunInterfaceClient) WaitingForConnection() { conn, err := t.dialer.Dial(t.dialer.Protocol().String(), t.address) if err == nil { t.HandleConnection(conn) } + t.closed = true +} + +func (t *TunInterfaceClient) Close() { + t.iface.Close() + t.closed = true +} + +func (t TunInterfaceClient) Closed() bool { + return t.closed } func (t TunInterface) WaitingForConnection() { - for !t.stopped{ + for !t.stopped { time.Sleep(time.Second) } } - func (t TunInterface) Close() error { t.stopped = true return nil -} \ No newline at end of file +} diff --git a/interface/udp/udp_client.go b/interface/udp/udp_client.go index af4fb58..42ee0bb 100644 --- a/interface/udp/udp_client.go +++ b/interface/udp/udp_client.go @@ -17,6 +17,7 @@ type udp_client struct { conn *net.UDPConn replyMap []*common.UdpAddress mux sync.Mutex + closed bool } func GetUdpClient(url string) *udp_client { @@ -45,8 +46,18 @@ func GetUdpClient(url string) *udp_client { } s.mux.Unlock() }() + s.closed = false return s } + +func (c *udp_client) Close() { + c.closed = true +} + +func (c udp_client) Closed() bool { + return c.closed +} + func (c *udp_client) WaitingForConnection() { } diff --git a/tunnel/common/tunnel_server.go b/tunnel/common/tunnel_server.go index 1d17a74..6d44133 100644 --- a/tunnel/common/tunnel_server.go +++ b/tunnel/common/tunnel_server.go @@ -10,11 +10,13 @@ type TunnelServer interface { SetServer(server icommon.TunnelInterfaceServer) WaitingForConnection() Close() error + Closed() bool HandleConnection(conn net.Conn) } type TunnelServerCommon struct { TunnelServer + closed bool Server icommon.TunnelInterfaceServer Listener net.Listener } @@ -24,6 +26,7 @@ func (s TunnelServerCommon) SetServer(ss icommon.TunnelInterfaceServer) { } func (s TunnelServerCommon) WaitingForConnection() { + s.closed = false log.Printf("listening for connection on %s\n", s.Listener.Addr().String()) for { conn, err := s.Listener.Accept() @@ -34,12 +37,19 @@ func (s TunnelServerCommon) WaitingForConnection() { } go s.HandleConnection(conn) } + s.closed = true log.Printf("Listening on %s stopped\n", s.Listener.Addr().String()) } func (s TunnelServerCommon) Close() error { log.Println("Closing connection") - return s.Listener.Close() + err := s.Listener.Close() + s.closed = true + return err +} + +func (s TunnelServerCommon) Closed() bool { + return s.closed } func (s TunnelServerCommon) HandleConnection(conn net.Conn) {