Db file change

This commit is contained in:
刘河
2019-03-29 15:21:30 +08:00
parent cd7f99063c
commit 5fd335f330
17 changed files with 597 additions and 567 deletions

View File

@@ -58,7 +58,7 @@ func (s *httpServer) processHttps(c net.Conn) {
return
}
var host *file.Host
file.GetCsvDb().Hosts.Range(func(key, value interface{}) bool {
file.GetDb().JsonDb.Hosts.Range(func(key, value interface{}) bool {
v := value.(*file.Host)
if v.Scheme != "https" && v.Scheme != "all" {
return true
@@ -193,7 +193,7 @@ func (s *httpServer) process(c *conn.Conn, r *http.Request) {
targetAddr string
wg sync.WaitGroup
)
if host, err = file.GetCsvDb().GetInfoByHost(r.Host, r); err != nil {
if host, err = file.GetDb().GetInfoByHost(r.Host, r); err != nil {
logs.Notice("the url %s %s %s can't be parsed!", r.URL.Scheme, r.Host, r.RequestURI)
goto end
}
@@ -245,7 +245,7 @@ func (s *httpServer) process(c *conn.Conn, r *http.Request) {
r.Method = "POST"
}
logs.Trace("new %s connection,clientId %d,host %s,url %s,remote address %s", r.URL.Scheme, host.Client.Id, r.Host, r.URL, r.RemoteAddr)
if hostTmp, err := file.GetCsvDb().GetInfoByHost(r.Host, r); err != nil {
if hostTmp, err := file.GetDb().GetInfoByHost(r.Host, r); err != nil {
logs.Notice("the url %s %s %s can't be parsed!", r.URL.Scheme, r.Host, r.RequestURI)
break
} else if host != lastHost {

View File

@@ -21,27 +21,24 @@ import (
)
var (
Bridge *bridge.Bridge
RunList map[int]interface{} //运行中的任务
serverStatus []map[string]interface{}
Bridge *bridge.Bridge
RunList map[int]interface{}
)
func init() {
RunList = make(map[int]interface{})
serverStatus = make([]map[string]interface{}, 0, 1500)
go getSeverStatus()
}
//从csv文件中恢复任务
//init task from db
func InitFromCsv() {
//Add a public password
if vkey := beego.AppConfig.String("public_vkey"); vkey != "" {
c := file.NewClient(vkey, true, true)
file.GetCsvDb().NewClient(c)
file.GetDb().NewClient(c)
RunList[c.Id] = nil
}
//Initialize services in server-side files
file.GetCsvDb().Tasks.Range(func(key, value interface{}) bool {
file.GetDb().JsonDb.Tasks.Range(func(key, value interface{}) bool {
if value.(*file.Tunnel).Status {
AddTask(value.(*file.Tunnel))
}
@@ -49,6 +46,7 @@ func InitFromCsv() {
})
}
//get bridge command
func DealBridgeTask() {
for {
select {
@@ -58,16 +56,16 @@ func DealBridgeTask() {
StopServer(t.Id)
case id := <-Bridge.CloseClient:
DelTunnelAndHostByClientId(id, true)
if v, ok := file.GetCsvDb().Clients.Load(id); ok {
if v, ok := file.GetDb().JsonDb.Clients.Load(id); ok {
if v.(*file.Client).NoStore {
file.GetCsvDb().DelClient(id)
file.GetDb().DelClient(id)
}
}
case tunnel := <-Bridge.OpenTask:
StartTask(tunnel.Id)
case s := <-Bridge.SecretChan:
logs.Trace("New secret connection, addr", s.Conn.Conn.RemoteAddr())
if t := file.GetCsvDb().GetTaskByMd5Password(s.Password); t != nil {
if t := file.GetDb().GetTaskByMd5Password(s.Password); t != nil {
if !t.Client.GetConn() {
logs.Info("Connections exceed the current client %d limit", t.Client.Id)
s.Conn.Close()
@@ -158,11 +156,11 @@ func StopServer(id int) error {
} else {
logs.Warn("stop server id %d error", id)
}
if t, err := file.GetCsvDb().GetTask(id); err != nil {
if t, err := file.GetDb().GetTask(id); err != nil {
return err
} else {
t.Status = false
file.GetCsvDb().UpdateTask(t)
file.GetDb().UpdateTask(t)
}
delete(RunList, id)
return nil
@@ -202,12 +200,12 @@ func AddTask(t *file.Tunnel) error {
//start task
func StartTask(id int) error {
if t, err := file.GetCsvDb().GetTask(id); err != nil {
if t, err := file.GetDb().GetTask(id); err != nil {
return err
} else {
AddTask(t)
t.Status = true
file.GetCsvDb().UpdateTask(t)
file.GetDb().UpdateTask(t)
}
return nil
}
@@ -219,16 +217,16 @@ func DelTask(id int) error {
return err
}
}
return file.GetCsvDb().DelTask(id)
return file.GetDb().DelTask(id)
}
//get task list by page num
func GetTunnel(start, length int, typeVal string, clientId int, search string) ([]*file.Tunnel, int) {
list := make([]*file.Tunnel, 0)
var cnt int
keys := file.GetMapKeys(file.GetCsvDb().Tasks, false, "", "")
keys := file.GetMapKeys(file.GetDb().JsonDb.Tasks, false, "", "")
for _, key := range keys {
if value, ok := file.GetCsvDb().Tasks.Load(key); ok {
if value, ok := file.GetDb().JsonDb.Tasks.Load(key); ok {
v := value.(*file.Tunnel)
if (typeVal != "" && v.Mode != typeVal || (clientId != 0 && v.Client.Id != clientId)) || (typeVal == "" && clientId != v.Client.Id) {
continue
@@ -257,15 +255,15 @@ func GetTunnel(start, length int, typeVal string, clientId int, search string) (
return list, cnt
}
//获取客户端列表
//get client list
func GetClientList(start, length int, search, sort, order string, clientId int) (list []*file.Client, cnt int) {
list, cnt = file.GetCsvDb().GetClientList(start, length, search, sort, order, clientId)
list, cnt = file.GetDb().GetClientList(start, length, search, sort, order, clientId)
dealClientData()
return
}
func dealClientData() {
file.GetCsvDb().Clients.Range(func(key, value interface{}) bool {
file.GetDb().JsonDb.Clients.Range(func(key, value interface{}) bool {
v := value.(*file.Client)
if _, ok := Bridge.Client.Load(v.Id); ok {
v.IsConnect = true
@@ -274,7 +272,7 @@ func dealClientData() {
}
v.Flow.InletFlow = 0
v.Flow.ExportFlow = 0
file.GetCsvDb().Hosts.Range(func(key, value interface{}) bool {
file.GetDb().JsonDb.Hosts.Range(func(key, value interface{}) bool {
h := value.(*file.Host)
if h.Client.Id == v.Id {
v.Flow.InletFlow += h.Flow.InletFlow
@@ -282,7 +280,7 @@ func dealClientData() {
}
return true
})
file.GetCsvDb().Tasks.Range(func(key, value interface{}) bool {
file.GetDb().JsonDb.Tasks.Range(func(key, value interface{}) bool {
t := value.(*file.Tunnel)
if t.Client.Id == v.Id {
v.Flow.InletFlow += t.Flow.InletFlow
@@ -295,10 +293,10 @@ func dealClientData() {
return
}
//根据客户端id删除其所属的所有隧道和域名
//delete all host and tasks by client id
func DelTunnelAndHostByClientId(clientId int, justDelNoStore bool) {
var ids []int
file.GetCsvDb().Tasks.Range(func(key, value interface{}) bool {
file.GetDb().JsonDb.Tasks.Range(func(key, value interface{}) bool {
v := value.(*file.Tunnel)
if justDelNoStore && !v.NoStore {
return true
@@ -312,7 +310,7 @@ func DelTunnelAndHostByClientId(clientId int, justDelNoStore bool) {
DelTask(id)
}
ids = ids[:0]
file.GetCsvDb().Hosts.Range(func(key, value interface{}) bool {
file.GetDb().JsonDb.Hosts.Range(func(key, value interface{}) bool {
v := value.(*file.Host)
if justDelNoStore && !v.NoStore {
return true
@@ -323,23 +321,23 @@ func DelTunnelAndHostByClientId(clientId int, justDelNoStore bool) {
return true
})
for _, id := range ids {
file.GetCsvDb().DelHost(id)
file.GetDb().DelHost(id)
}
}
//关闭客户端连接
//close the client
func DelClientConnect(clientId int) {
Bridge.DelClient(clientId)
}
func GetDashboardData() map[string]interface{} {
data := make(map[string]interface{})
data["hostCount"] = file.GetCsvDb().GetMapLen(file.GetCsvDb().Hosts)
data["clientCount"] = file.GetCsvDb().GetMapLen(file.GetCsvDb().Clients) - 1 //Remove the public key client
data["hostCount"] = common.GeSynctMapLen(file.GetDb().JsonDb.Hosts)
data["clientCount"] = common.GeSynctMapLen(file.GetDb().JsonDb.Clients) - 1 //Remove the public key client
dealClientData()
c := 0
var in, out int64
file.GetCsvDb().Clients.Range(func(key, value interface{}) bool {
file.GetDb().JsonDb.Clients.Range(func(key, value interface{}) bool {
v := value.(*file.Client)
if v.IsConnect {
c += 1
@@ -352,7 +350,7 @@ func GetDashboardData() map[string]interface{} {
data["inletFlowCount"] = int(in)
data["exportFlowCount"] = int(out)
var tcp, udp, secret, socks5, p2p, http int
file.GetCsvDb().Tasks.Range(func(key, value interface{}) bool {
file.GetDb().JsonDb.Tasks.Range(func(key, value interface{}) bool {
switch value.(*file.Tunnel).Mode {
case "tcp":
tcp += 1
@@ -386,7 +384,7 @@ func GetDashboardData() map[string]interface{} {
data["logLevel"] = beego.AppConfig.String("log_level")
tcpCount := 0
file.GetCsvDb().Clients.Range(func(key, value interface{}) bool {
file.GetDb().JsonDb.Clients.Range(func(key, value interface{}) bool {
tcpCount += int(value.(*file.Client).NowConn)
return true
})
@@ -416,10 +414,10 @@ func GetDashboardData() map[string]interface{} {
}
//chart
var fg int
if len(serverStatus) >= 10 {
fg = len(serverStatus) / 10
if len(tool.ServerStatus) >= 10 {
fg = len(tool.ServerStatus) / 10
for i := 0; i <= 9; i++ {
data["sys"+strconv.Itoa(i+1)] = serverStatus[i*fg]
data["sys"+strconv.Itoa(i+1)] = tool.ServerStatus[i*fg]
}
}
return data
@@ -430,51 +428,9 @@ func flowSession(m time.Duration) {
for {
select {
case <-ticker.C:
file.GetCsvDb().StoreHostToCsv()
file.GetCsvDb().StoreTasksToCsv()
file.GetDb().JsonDb.StoreHostToJsonFile()
file.GetDb().JsonDb.StoreTasksToJsonFile()
file.GetDb().JsonDb.StoreClientsToJsonFile()
}
}
}
func getSeverStatus() {
for {
if len(serverStatus) < 10 {
time.Sleep(time.Second)
} else {
time.Sleep(time.Minute)
}
cpuPercet, _ := cpu.Percent(0, true)
var cpuAll float64
for _, v := range cpuPercet {
cpuAll += v
}
m := make(map[string]interface{})
loads, _ := load.Avg()
m["load1"] = loads.Load1
m["load5"] = loads.Load5
m["load15"] = loads.Load15
m["cpu"] = math.Round(cpuAll / float64(len(cpuPercet)))
swap, _ := mem.SwapMemory()
m["swap_mem"] = math.Round(swap.UsedPercent)
vir, _ := mem.VirtualMemory()
m["virtual_mem"] = math.Round(vir.UsedPercent)
conn, _ := net.ProtoCounters(nil)
io1, _ := net.IOCounters(false)
time.Sleep(time.Millisecond * 500)
io2, _ := net.IOCounters(false)
if len(io2) > 0 && len(io1) > 0 {
m["io_send"] = (io2[0].BytesSent - io1[0].BytesSent) * 2
m["io_recv"] = (io2[0].BytesRecv - io1[0].BytesRecv) * 2
}
t := time.Now()
m["time"] = strconv.Itoa(t.Hour()) + ":" + strconv.Itoa(t.Minute()) + ":" + strconv.Itoa(t.Second())
for _, v := range conn {
m[v.Protocol] = v.Stats["CurrEstab"]
}
if len(serverStatus) >= 1440 {
serverStatus = serverStatus[1:]
}
serverStatus = append(serverStatus, m)
}
}

View File

@@ -12,7 +12,7 @@ import (
func TestServerConfig() {
var postTcpArr []int
var postUdpArr []int
file.GetCsvDb().Tasks.Range(func(key, value interface{}) bool {
file.GetDb().JsonDb.Tasks.Range(func(key, value interface{}) bool {
v := value.(*file.Tunnel)
if v.Mode == "udp" {
isInArr(&postUdpArr, v.Port, v.Remark, "udp")

View File

@@ -3,9 +3,24 @@ package tool
import (
"github.com/cnlh/nps/lib/common"
"github.com/cnlh/nps/vender/github.com/astaxie/beego"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/load"
"github.com/shirou/gopsutil/mem"
"github.com/shirou/gopsutil/net"
"math"
"strconv"
"time"
)
var ports []int
var (
ports []int
ServerStatus []map[string]interface{}
)
func init() {
ServerStatus = make([]map[string]interface{}, 0, 1500)
go getSeverStatus()
}
func InitAllowPort() {
p := beego.AppConfig.String("allow_ports")
@@ -28,3 +43,48 @@ func TestServerPort(p int, m string) (b bool) {
}
return
}
func getSeverStatus() {
for {
if len(ServerStatus) < 10 {
time.Sleep(time.Second)
} else {
time.Sleep(time.Minute)
}
cpuPercet, _ := cpu.Percent(0, true)
var cpuAll float64
for _, v := range cpuPercet {
cpuAll += v
}
m := make(map[string]interface{})
loads, _ := load.Avg()
m["load1"] = loads.Load1
m["load5"] = loads.Load5
m["load15"] = loads.Load15
m["cpu"] = math.Round(cpuAll / float64(len(cpuPercet)))
swap, _ := mem.SwapMemory()
m["swap_mem"] = math.Round(swap.UsedPercent)
vir, _ := mem.VirtualMemory()
m["virtual_mem"] = math.Round(vir.UsedPercent)
conn, _ := net.ProtoCounters(nil)
io1, _ := net.IOCounters(false)
time.Sleep(time.Millisecond * 500)
io2, _ := net.IOCounters(false)
if len(io2) > 0 && len(io1) > 0 {
m["io_send"] = (io2[0].BytesSent - io1[0].BytesSent) * 2
m["io_recv"] = (io2[0].BytesRecv - io1[0].BytesRecv) * 2
}
t := time.Now()
m["time"] = strconv.Itoa(t.Hour()) + ":" + strconv.Itoa(t.Minute()) + ":" + strconv.Itoa(t.Second())
for _, v := range conn {
m[v.Protocol] = v.Stats["CurrEstab"]
}
if len(ServerStatus) >= 1440 {
ServerStatus = ServerStatus[1:]
}
ServerStatus = append(ServerStatus, m)
}
}