@@ -27,6 +27,12 @@ var ListenerList map[int]controller.Listener
2727var ListenerListMutex sync.Mutex
2828var ProxyController * controller.Controller
2929
30+ // CurrentAgentID points to the selected agent in the UI (when running session)
31+ var CurrentAgentID int
32+
33+ // Store AgentIDs
34+ var AgentCounter int
35+
3036var (
3137 ErrInvalidAgent = errors .New ("please, select an agent using the session command" )
3238 ErrAlreadyRunning = errors .New ("already running" )
@@ -35,21 +41,76 @@ var (
3541
3642func RegisterAgent (agent * controller.LigoloAgent ) error {
3743 AgentListMutex .Lock ()
38- AgentList [agent .Id ] = agent
39- AgentListMutex .Unlock ()
44+ defer AgentListMutex .Unlock ()
45+
46+ for _ , registeredAgents := range AgentList {
47+ if agent .SessionID == registeredAgents .SessionID {
48+ logrus .Infof ("Recovered an agent: %s" , registeredAgents .Name )
49+ registeredAgents .Session = agent .Session
50+ if registeredAgents .Running {
51+ go StartTunnel (registeredAgents , registeredAgents .Interface )
52+ }
53+ return nil
54+ }
55+ }
56+ AgentCounter ++
57+ AgentList [AgentCounter ] = agent
4058 return nil
4159}
4260
4361func UnregisterAgent (agent * controller.LigoloAgent ) error {
44- AgentListMutex .Lock ()
62+ /* AgentListMutex.Lock()
4563 delete(AgentList, agent.Id)
46- AgentListMutex .Unlock ()
64+ AgentListMutex.Unlock()*/
4765 return nil
4866}
4967
68+ func StartTunnel (agent * controller.LigoloAgent , tunName string ) {
69+ logrus .Infof ("Starting tunnel to %s" , agent .Name )
70+ ligoloStack , err := proxy .NewLigoloTunnel (netstack.StackSettings {
71+ TunName : tunName ,
72+ MaxInflight : 4096 ,
73+ })
74+ if err != nil {
75+ logrus .Error ("Unable to create tunnel, err:" , err )
76+ return
77+ }
78+ ifName , err := ligoloStack .GetStack ().Interface ().Name ()
79+ if err != nil {
80+ logrus .Warn ("unable to get interface name, err:" , err )
81+ ifName = tunName
82+ }
83+ agent .Interface = ifName
84+ agent .Running = true
85+
86+ ctx , cancelTunnel := context .WithCancel (context .Background ())
87+ go ligoloStack .HandleSession (agent .Session , ctx )
88+
89+ for {
90+ select {
91+ case <- agent .CloseChan : // User stopped
92+ logrus .Infof ("Closing tunnel to %s..." , agent .Name )
93+ cancelTunnel ()
94+ return
95+ case <- agent .Session .CloseChan (): // Agent closed
96+ logrus .Warnf ("Lost tunnel connection with agent %s!" , agent .Name )
97+ //agent.Running = false
98+ //agent.Session = nil
99+
100+ if currentAgent , ok := AgentList [CurrentAgentID ]; ok {
101+ if currentAgent .SessionID == agent .SessionID {
102+ App .SetDefaultPrompt ()
103+ agent .Session = nil
104+ }
105+ }
106+
107+ cancelTunnel ()
108+ return
109+ }
110+ }
111+ }
112+
50113func Run () {
51- // CurrentAgent points to the selected agent in the UI (when running session)
52- var CurrentAgentID int
53114 // AgentList contains all the connected agents
54115 AgentList = make (map [int ]* controller.LigoloAgent )
55116 // ListenerList contains all listener relays
@@ -61,7 +122,13 @@ func Run() {
61122 Usage : "session" ,
62123 Run : func (c * grumble.Context ) error {
63124 AgentListMutex .Lock ()
64- if len (AgentList ) == 0 {
125+ sessionCount := 0
126+ for _ , agent := range AgentList {
127+ if agent .Session != nil && ! agent .Session .IsClosed () {
128+ sessionCount += 1
129+ }
130+ }
131+ if sessionCount == 0 {
65132 AgentListMutex .Unlock ()
66133 return errors .New ("no sessions available" )
67134 }
@@ -72,7 +139,9 @@ func Run() {
72139 Options : func () (out []string ) {
73140 AgentListMutex .Lock ()
74141 for id , agent := range AgentList {
75- out = append (out , fmt .Sprintf ("%d - %s" , id , agent .String ()))
142+ if agent .Session != nil && ! agent .Session .IsClosed () {
143+ out = append (out , fmt .Sprintf ("%d - %s" , id , agent .String ()))
144+ }
76145 }
77146 AgentListMutex .Unlock ()
78147 return
@@ -194,48 +263,8 @@ func Run() {
194263 }
195264 }
196265
197- go func () {
198- logrus .Infof ("Starting tunnel to %s" , CurrentAgent .Name )
199- ligoloStack , err := proxy .NewLigoloTunnel (netstack.StackSettings {
200- TunName : c .Flags .String ("tun" ),
201- MaxInflight : 4096 ,
202- })
203- if err != nil {
204- logrus .Error ("Unable to create tunnel, err:" , err )
205- return
206- }
207- ifName , err := ligoloStack .GetStack ().Interface ().Name ()
208- if err != nil {
209- logrus .Warn ("unable to get interface name, err:" , err )
210- ifName = c .Flags .String ("tun" )
211- }
212- CurrentAgent .Interface = ifName
213- CurrentAgent .Running = true
214-
215- ctx , cancelTunnel := context .WithCancel (context .Background ())
216- go ligoloStack .HandleSession (CurrentAgent .Session , ctx )
266+ go StartTunnel (CurrentAgent , c .Flags .String ("tun" ))
217267
218- for {
219- select {
220- case <- CurrentAgent .CloseChan : // User stopped
221- logrus .Infof ("Closing tunnel to %s..." , CurrentAgent .Name )
222- cancelTunnel ()
223- return
224- case <- CurrentAgent .Session .CloseChan (): // Agent closed
225- logrus .Warnf ("Lost connection with agent %s!" , CurrentAgent .Name )
226- // Connection lost, we need to delete the Agent from the list
227- AgentListMutex .Lock ()
228- delete (AgentList , CurrentAgent .Id )
229- AgentListMutex .Unlock ()
230- if CurrentAgent .Id == CurrentAgent .Id {
231- App .SetDefaultPrompt ()
232- CurrentAgent .Session = nil
233- }
234- cancelTunnel ()
235- return
236- }
237- }
238- }()
239268 return nil
240269 },
241270 })
@@ -252,10 +281,10 @@ func Run() {
252281
253282 AgentListMutex .Lock ()
254283
255- for _ , agent := range AgentList {
284+ for id , agent := range AgentList {
256285
257286 if agent .Running {
258- t .AppendRow (table.Row {agent . Id , agent .Name , agent .Interface })
287+ t .AppendRow (table.Row {id , agent .Name , agent .Interface })
259288 }
260289 }
261290 AgentListMutex .Unlock ()
0 commit comments