@@ -163,20 +163,28 @@ func listener(config *ServerConfig) (net.Listener, error) {
163163
164164// disconnected clears resources used by client, it's invoked by connection pool when client goes away. 
165165func  (s  * Server ) disconnected (identifier  id.ID ) {
166- 	s .debounce .disconnectedIDs  =  append (s .debounce .disconnectedIDs , identifier )
166+ 	if  s .debounce .Execute  !=  nil  {
167+ 		s .debounce .disconnectedIDs  =  append (s .debounce .disconnectedIDs , identifier )
167168
168- 	s .debounce .Execute (func () {
169- 		for  _ , id  :=  range  s .debounce .disconnectedIDs  {
170- 			s .logger .Log (
171- 				"level" , 1 ,
172- 				"action" , "disconnected" ,
173- 				"identifier" , id ,
174- 			)
175- 		}
176- 		s .debounce .disconnectedIDs  =  nil 
177- 	})
169+ 		s .debounce .Execute (func () {
170+ 			for  _ , id  :=  range  s .debounce .disconnectedIDs  {
171+ 				s .logger .Log (
172+ 					"level" , 1 ,
173+ 					"action" , "disconnected" ,
174+ 					"identifier" , id ,
175+ 				)
176+ 			}
177+ 			s .debounce .disconnectedIDs  =  nil 
178+ 		})
179+ 	} else  {
180+ 		s .logger .Log (
181+ 			"level" , 1 ,
182+ 			"action" , "disconnected" ,
183+ 			"identifier" , identifier ,
184+ 		)
185+ 	}
178186
179- 	i  :=  s .registry . clear (identifier )
187+ 	i  :=  s .unsubscribe (identifier )
180188	if  i  ==  nil  {
181189		return 
182190	}
@@ -191,6 +199,13 @@ func (s *Server) disconnected(identifier id.ID) {
191199	}
192200}
193201
202+ func  (s  * Server ) unsubscribe (identifier  id.ID ) * RegistryItem  {
203+ 	if  s .config .AutoSubscribe  {
204+ 		return  s .Unsubscribe (identifier )
205+ 	}
206+ 	return  s .registry .clear (identifier )
207+ }
208+ 
194209// Start starts accepting connections form clients. For accepting http traffic 
195210// from end users server must be run as handler on http server. 
196211func  (s  * Server ) Start () {
@@ -251,6 +266,7 @@ func (s *Server) handleClient(conn net.Conn) {
251266
252267	var  (
253268		identifier  id.ID 
269+ 		IDInfo      * id.IDInfo 
254270		req         * http.Request 
255271		resp        * http.Response 
256272		tunnels     map [string ]* proto.Tunnel 
@@ -273,7 +289,7 @@ func (s *Server) handleClient(conn net.Conn) {
273289		goto  reject
274290	}
275291
276- 	identifier , err  =  id .PeerID (tlsConn )
292+ 	identifier , IDInfo ,  err  =  id .PeerID (tlsConn )
277293	if  err  !=  nil  {
278294		logger .Log (
279295			"level" , 2 ,
@@ -379,7 +395,16 @@ func (s *Server) handleClient(conn net.Conn) {
379395		goto  reject
380396	}
381397
382- 	if  err  =  s .addTunnels (tunnels , identifier ); err  !=  nil  {
398+ 	if  err  =  s .hasTunnels (tunnels , identifier ); err  !=  nil  {
399+ 		logger .Log (
400+ 			"level" , 2 ,
401+ 			"msg" , "tunnel check failed" ,
402+ 			"err" , err ,
403+ 		)
404+ 		goto  reject
405+ 	}
406+ 
407+ 	if  err  =  s .addTunnels (tunnels , identifier , * IDInfo ); err  !=  nil  {
383408		logger .Log (
384409			"level" , 2 ,
385410			"msg" , "handshake failed" ,
@@ -443,10 +468,25 @@ func (s *Server) notifyError(serverError error, identifier id.ID) {
443468	s .httpClient .Do (req .WithContext (ctx ))
444469}
445470
471+ func  (s  * Server ) hasTunnels (tunnels  map [string ]* proto.Tunnel , identifier  id.ID ) error  {
472+ 	var  err  error 
473+ 	for  name , t  :=  range  tunnels  {
474+ 		// Check the current tunnel 
475+ 		// AutoSubscribe --> Tunnel not yet registered (means that it isn't already opened) 
476+ 		// !AutoSubscribe -> Tunnel has to be already registered, and therefore allowed to be opened 
477+ 		if  s .config .AutoSubscribe  ==  s .HasTunnel (t .Host , identifier ) {
478+ 			err  =  fmt .Errorf ("tunnel %s not allowed for %s" , name , identifier )
479+ 			break 
480+ 		}
481+ 	}
482+ 	return  err 
483+ }
484+ 
446485// addTunnels invokes addHost or addListener based on data from proto.Tunnel. If 
447486// a tunnel cannot be added whole batch is reverted. 
448- func  (s  * Server ) addTunnels (tunnels  map [string ]* proto.Tunnel , identifier  id.ID ) error  {
487+ func  (s  * Server ) addTunnels (tunnels  map [string ]* proto.Tunnel , identifier  id.ID ,  IDInfo  id. IDInfo ) error  {
449488	i  :=  & RegistryItem {
489+ 		IDInfo :    & IDInfo ,
450490		Hosts :     []* HostAuth {},
451491		Listeners : []net.Listener {},
452492	}
@@ -847,6 +887,7 @@ type ListenerInfo struct {
847887// ClientInfo info about the client 
848888type  ClientInfo  struct  {
849889	ID         string 
890+ 	IDInfo     id.IDInfo 
850891	Listeners  []* ListenerInfo 
851892	Hosts      []string 
852893}
@@ -857,7 +898,10 @@ func (s *Server) GetClientInfo() []*ClientInfo {
857898	defer  s .registry .mu .Unlock ()
858899	ret  :=  []* ClientInfo {}
859900	for  k , v  :=  range  s .registry .items  {
860- 		c  :=  & ClientInfo {ID : k .String ()}
901+ 		c  :=  & ClientInfo {
902+ 			ID :     k .String (),
903+ 			IDInfo : * v .IDInfo ,
904+ 		}
861905		ret  =  append (ret , c )
862906		if  v  ==  voidRegistryItem  {
863907			s .logger .Log (
0 commit comments