|
@@ -27,9 +27,8 @@ import (
|
|
|
*/
|
|
|
|
|
|
type Manager struct {
|
|
|
- StartingPort int
|
|
|
- ReservedPorts map[string]int
|
|
|
- Instances []*Instance
|
|
|
+ StartingPort int
|
|
|
+ Instances []*Instance
|
|
|
}
|
|
|
|
|
|
type Instance struct {
|
|
@@ -40,28 +39,26 @@ type Instance struct {
|
|
|
AssignedPort int
|
|
|
conn *reverseproxy.ReverseProxy //HTTP proxy
|
|
|
tty *exec.Cmd //SSH connection ported to web interface
|
|
|
+ Parent *Manager
|
|
|
}
|
|
|
|
|
|
func NewSSHProxyManager() *Manager {
|
|
|
return &Manager{
|
|
|
- StartingPort: 14810,
|
|
|
- ReservedPorts: map[string]int{},
|
|
|
- Instances: []*Instance{},
|
|
|
+ StartingPort: 14810,
|
|
|
+ Instances: []*Instance{},
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//Get the next free port in the list
|
|
|
func (m *Manager) GetNextPort() int {
|
|
|
nextPort := m.StartingPort
|
|
|
+ occupiedPort := make(map[int]bool)
|
|
|
+ for _, instance := range m.Instances {
|
|
|
+ occupiedPort[instance.AssignedPort] = true
|
|
|
+ }
|
|
|
for {
|
|
|
- if _, exists := m.ReservedPorts[strconv.Itoa(nextPort)]; !exists {
|
|
|
- if !isPortInUse(nextPort) {
|
|
|
- return nextPort
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if nextPort == 65534 {
|
|
|
- return -1
|
|
|
+ if !occupiedPort[nextPort] {
|
|
|
+ return nextPort
|
|
|
}
|
|
|
nextPort++
|
|
|
}
|
|
@@ -127,6 +124,7 @@ func (m *Manager) NewSSHProxy(binaryRoot string) (*Instance, error) {
|
|
|
UUID: uuid.New().String(),
|
|
|
ExecPath: realpath,
|
|
|
AssignedPort: -1,
|
|
|
+ Parent: m,
|
|
|
}
|
|
|
|
|
|
m.Instances = append(m.Instances, &thisInstance)
|
|
@@ -160,3 +158,15 @@ func (i *Instance) CreateNewConnection(listenPort int, remoteIpAddr string, remo
|
|
|
i.conn = proxy
|
|
|
return nil
|
|
|
}
|
|
|
+
|
|
|
+func (i *Instance) Destroy() {
|
|
|
+ // Remove the instance from the Manager's Instances list
|
|
|
+ for idx, inst := range i.Parent.Instances {
|
|
|
+ if inst == i {
|
|
|
+ // Remove the instance from the slice by swapping it with the last instance and slicing the slice
|
|
|
+ i.Parent.Instances[len(i.Parent.Instances)-1], i.Parent.Instances[idx] = i.Parent.Instances[idx], i.Parent.Instances[len(i.Parent.Instances)-1]
|
|
|
+ i.Parent.Instances = i.Parent.Instances[:len(i.Parent.Instances)-1]
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|