Expose network system state table to api

This commit is contained in:
Daniel 2020-05-19 09:39:03 +02:00
parent ad93b19968
commit 3f9876fc09
2 changed files with 59 additions and 7 deletions

View file

@ -5,6 +5,8 @@ import (
"strings"
"sync"
"github.com/safing/portmaster/network/state"
"github.com/safing/portbase/database"
"github.com/safing/portbase/database/iterator"
"github.com/safing/portbase/database/query"
@ -57,13 +59,13 @@ func (s *StorageInterface) Get(key string) (record.Record, error) {
return conn, nil
}
}
// case "system":
// if len(splitted) >= 2 {
// switch splitted[1] {
// case "":
// process.Get
// }
// }
case "system":
if len(splitted) >= 2 {
switch splitted[1] {
case "state":
return state.GetStateInfo(), nil
}
}
}
return nil, storage.ErrNotFound

50
network/state/info.go Normal file
View file

@ -0,0 +1,50 @@
package state
import (
"sync"
"github.com/safing/portbase/database/record"
"github.com/safing/portmaster/network/socket"
)
type StateInfo struct {
record.Base
sync.Mutex
TCP4Connections []*socket.ConnectionInfo
TCP4Listeners []*socket.BindInfo
TCP6Connections []*socket.ConnectionInfo
TCP6Listeners []*socket.BindInfo
UDP4Binds []*socket.BindInfo
UDP6Binds []*socket.BindInfo
}
func GetStateInfo() *StateInfo {
info := &StateInfo{}
tcp4Lock.Lock()
updateTCP4Tables()
info.TCP4Connections = tcp4Connections
info.TCP4Listeners = tcp4Listeners
tcp4Lock.Unlock()
tcp6Lock.Lock()
updateTCP6Tables()
info.TCP6Connections = tcp6Connections
info.TCP6Listeners = tcp6Listeners
tcp6Lock.Unlock()
udp4Lock.Lock()
updateUDP4Table()
info.UDP4Binds = udp4Binds
udp4Lock.Unlock()
udp6Lock.Lock()
updateUDP6Table()
info.UDP6Binds = udp6Binds
udp6Lock.Unlock()
info.UpdateMeta()
return info
}