@@ -2,7 +2,6 @@ package daemon
22
33import (
44 "errors"
5- "fmt"
65 "github.com/fsnotify/fsnotify"
76 "github.com/labstack/gommon/log"
87 "github.com/spf13/viper"
@@ -17,8 +16,7 @@ import (
1716
1817type (
1918 Server struct {
20- resty * resty.Client
21- Connections map [string ]* Connection
19+ resty * resty.Client
2220 }
2321
2422 Protocol string
@@ -54,6 +52,10 @@ const (
5452 ProtocolTLS = "tls"
5553)
5654
55+ var (
56+ connections = map [string ]* Connection {}
57+ )
58+
5759func (s * Server ) Connect (req * ConnectRequest , rep * ConnectReply ) (err error ) {
5860 c , err := s .newConnection (req )
5961 if err != nil {
@@ -64,15 +66,17 @@ func (s *Server) Connect(req *ConnectRequest, rep *ConnectReply) (err error) {
6466}
6567
6668func (s * Server ) PS (req * PSRequest , rep * PSReply ) (err error ) {
67- for _ , c := range s . Connections {
69+ for _ , c := range connections {
6870 rep .Connections = append (rep .Connections , c )
6971 }
7072 return nil
7173}
7274
7375func (s * Server ) RM (req * RMRequest , rep * RMReply ) error {
74- if c , ok := s .Connections [req .Name ]; ok {
75- c .stop ()
76+ for _ , c := range connections {
77+ if c .Name == req .Name {
78+ c .stop ()
79+ }
7680 }
7781 return nil
7882}
@@ -87,10 +91,7 @@ func Start() {
8791 })
8892 r .SetHeader ("Content-Type" , "application/json" )
8993 r .SetHeader ("User-Agent" , "tunnel/client" )
90- s := & Server {
91- resty : r ,
92- Connections : map [string ]* Connection {},
93- }
94+ s := & Server {resty : r }
9495 rpc .Register (s )
9596
9697 // Shutdown hook
@@ -127,21 +128,5 @@ func (s *Server) findConnection(c *Connection) (err error) {
127128 if res .IsError () {
128129 return errors .New (e .Message )
129130 }
130- s .Connections [c .Name ] = c
131- return
132- }
133-
134- func (s * Server ) deleteAll () (err error ) {
135- log .Warnf ("removing all connections" )
136- e := new (Error )
137- res , err := s .resty .R ().
138- SetError (e ).
139- Delete ("/connections" )
140- if err != nil {
141- return
142- }
143- if res .IsError () {
144- return fmt .Errorf ("failed to delete all connections" )
145- }
146131 return
147132}
0 commit comments