Skip to content

Commit

Permalink
Db file change
Browse files Browse the repository at this point in the history
  • Loading branch information
刘河 committed Mar 29, 2019
1 parent cd7f990 commit 5fd335f
Show file tree
Hide file tree
Showing 17 changed files with 597 additions and 567 deletions.
36 changes: 18 additions & 18 deletions bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (s *Bridge) GetHealthFromClient(id int, c *conn.Conn) {
if info, status, err := c.GetHealthInfo(); err != nil {
break
} else if !status { //the status is true , return target to the targetArr
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.Client.Id == id && v.Mode == "tcp" && strings.Contains(v.Target.TargetStr, info) {
v.Lock()
Expand All @@ -106,7 +106,7 @@ func (s *Bridge) GetHealthFromClient(id int, c *conn.Conn) {
}
return true
})
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.Client.Id == id && strings.Contains(v.Target.TargetStr, info) {
v.Lock()
Expand All @@ -123,7 +123,7 @@ func (s *Bridge) GetHealthFromClient(id int, c *conn.Conn) {
return true
})
} else { //the status is false,remove target from the targetArr
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.Client.Id == id && v.Mode == "tcp" && common.IsArrContains(v.HealthRemoveArr, info) && !common.IsArrContains(v.Target.TargetArr, info) {
v.Lock()
Expand All @@ -134,7 +134,7 @@ func (s *Bridge) GetHealthFromClient(id int, c *conn.Conn) {
return true
})

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.Client.Id == id && common.IsArrContains(v.HealthRemoveArr, info) && !common.IsArrContains(v.Target.TargetArr, info) {
v.Lock()
Expand Down Expand Up @@ -182,7 +182,7 @@ func (s *Bridge) cliProcess(c *conn.Conn) {
return
}
//verify
id, err := file.GetCsvDb().GetIdByVerifyKey(string(buf), c.Conn.RemoteAddr().String())
id, err := file.GetDb().GetIdByVerifyKey(string(buf), c.Conn.RemoteAddr().String())
if err != nil {
logs.Info("Current client connection validation error, close this client:", c.Conn.RemoteAddr())
s.verifyError(c)
Expand All @@ -204,18 +204,18 @@ func (s *Bridge) DelClient(id int) {
v.(*Client).signal.Close()
}
s.Client.Delete(id)
if file.GetCsvDb().IsPubClient(id) {
if file.GetDb().IsPubClient(id) {
return
}
if c, err := file.GetCsvDb().GetClient(id); err == nil {
if c, err := file.GetDb().GetClient(id); err == nil {
s.CloseClient <- c.Id
}
}
}

//use different
func (s *Bridge) typeDeal(typeVal string, c *conn.Conn, id int) {
isPub := file.GetCsvDb().IsPubClient(id)
isPub := file.GetDb().IsPubClient(id)
switch typeVal {
case common.WORK_MAIN:
if isPub {
Expand All @@ -237,7 +237,7 @@ func (s *Bridge) typeDeal(typeVal string, c *conn.Conn, id int) {
v.(*Client).tunnel = muxConn
}
case common.WORK_CONFIG:
client, err := file.GetCsvDb().GetClient(id)
client, err := file.GetDb().GetClient(id)
if err != nil || (!isPub && !client.ConfigConnAllow) {
c.Close()
return
Expand All @@ -259,7 +259,7 @@ func (s *Bridge) typeDeal(typeVal string, c *conn.Conn, id int) {
//read md5 secret
if b, err := c.GetShortContent(32); err != nil {
return
} else if t := file.GetCsvDb().GetTaskByMd5Password(string(b)); t == nil {
} else if t := file.GetDb().GetTaskByMd5Password(string(b)); t == nil {
return
} else {
if v, ok := s.Client.Load(t.Client.Id); !ok {
Expand Down Expand Up @@ -373,18 +373,18 @@ loop:
break loop
} else {
var str string
id, err := file.GetCsvDb().GetClientIdByVkey(string(b))
id, err := file.GetDb().GetClientIdByVkey(string(b))
if err != nil {
break loop
}
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.Client.Id == id {
str += v.Remark + common.CONN_DATA_SEQ
}
return true
})
file.GetCsvDb().Tasks.Range(func(key, value interface{}) bool {
file.GetDb().JsonDb.Tasks.Range(func(key, value interface{}) bool {
v := value.(*file.Tunnel)
if _, ok := s.runList[v.Id]; ok && v.Client.Id == id {
str += v.Remark + common.CONN_DATA_SEQ
Expand All @@ -401,7 +401,7 @@ loop:
c.WriteAddFail()
break loop
} else {
if err = file.GetCsvDb().NewClient(client); err != nil {
if err = file.GetDb().NewClient(client); err != nil {
fail = true
c.WriteAddFail()
break loop
Expand All @@ -422,12 +422,12 @@ loop:
h.Location = "/"
}
if !client.HasHost(h) {
if file.GetCsvDb().IsHostExist(h) {
if file.GetDb().IsHostExist(h) {
fail = true
c.WriteAddFail()
break loop
} else {
file.GetCsvDb().NewHost(h)
file.GetDb().NewHost(h)
c.WriteAddOk()
}
} else {
Expand Down Expand Up @@ -469,7 +469,7 @@ loop:
tl.Target.TargetStr = strconv.Itoa(targets[i])
}
}
tl.Id = int(file.GetCsvDb().GetTaskId())
tl.Id = int(file.GetDb().JsonDb.GetTaskId())
tl.Status = true
tl.Flow = new(file.Flow)
tl.NoStore = true
Expand All @@ -478,7 +478,7 @@ loop:
tl.LocalPath = t.LocalPath
tl.StripPre = t.StripPre
if !client.HasTunnel(tl) {
if err := file.GetCsvDb().NewTask(tl); err != nil {
if err := file.GetDb().NewTask(tl); err != nil {
logs.Notice("Add task error ", err.Error())
fail = true
c.WriteAddFail()
Expand Down
10 changes: 10 additions & 0 deletions lib/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"regexp"
"strconv"
"strings"
"sync"
)

//Get the corresponding IP address through domain name
Expand Down Expand Up @@ -344,3 +345,12 @@ func BytesToNum(b []byte) int {
x, _ := strconv.Atoi(str)
return int(x)
}

func GeSynctMapLen(m sync.Map) int {
var c int
m.Range(func(key, value interface{}) bool {
c++
return true
})
return c
}
1 change: 0 additions & 1 deletion lib/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ func NewConfig(path string) (c *Config, err error) {
}
}
}

}
return
}
Expand Down
34 changes: 17 additions & 17 deletions lib/conn/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,21 +210,6 @@ func (s *Conn) SendHostInfo(h *file.Host) (int, error) {
return s.Write(raw.Bytes())
}

//get task or host result of add
func (s *Conn) GetAddStatus() (b bool) {
binary.Read(s.Conn, binary.LittleEndian, &b)
return
}

func (s *Conn) WriteAddOk() error {
return binary.Write(s.Conn, binary.LittleEndian, true)
}

func (s *Conn) WriteAddFail() error {
defer s.Close()
return binary.Write(s.Conn, binary.LittleEndian, false)
}

//get task info
func (s *Conn) GetHostInfo() (h *file.Host, err error) {
var l int
Expand All @@ -238,7 +223,7 @@ func (s *Conn) GetHostInfo() (h *file.Host, err error) {
arr := strings.Split(string(buf[:l]), common.CONN_DATA_SEQ)
h = new(file.Host)
h.Target = new(file.Target)
h.Id = int(file.GetCsvDb().GetHostId())
h.Id = int(file.GetDb().JsonDb.GetHostId())
h.Host = arr[0]
h.Target.TargetStr = arr[1]
h.HeaderChange = arr[2]
Expand Down Expand Up @@ -328,7 +313,7 @@ func (s *Conn) GetTaskInfo() (t *file.Tunnel, err error) {
t.Mode = arr[0]
t.Ports = arr[1]
t.Target.TargetStr = arr[2]
t.Id = int(file.GetCsvDb().GetTaskId())
t.Id = int(file.GetDb().JsonDb.GetTaskId())
t.Status = true
t.Flow = new(file.Flow)
t.Remark = arr[3]
Expand Down Expand Up @@ -379,6 +364,21 @@ func (s *Conn) WriteChan() (int, error) {
return s.Write([]byte(common.WORK_CHAN))
}

//get task or host result of add
func (s *Conn) GetAddStatus() (b bool) {
binary.Read(s.Conn, binary.LittleEndian, &b)
return
}

func (s *Conn) WriteAddOk() error {
return binary.Write(s.Conn, binary.LittleEndian, true)
}

func (s *Conn) WriteAddFail() error {
defer s.Close()
return binary.Write(s.Conn, binary.LittleEndian, false)
}

//get the assembled amount data(len 4 and content)
func GetLenBytes(buf []byte) (b []byte, err error) {
raw := bytes.NewBuffer([]byte{})
Expand Down
4 changes: 0 additions & 4 deletions lib/crypt/crypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@ func AesEncrypt(origData, key []byte) ([]byte, error) {
}
blockSize := block.BlockSize()
origData = PKCS5Padding(origData, blockSize)
// origData = ZeroPadding(origData, block.BlockSize())
blockMode := cipher.NewCBCEncrypter(block, key[:blockSize])
crypted := make([]byte, len(origData))
// 根据CryptBlocks方法的说明,如下方式初始化crypted也可以
// crypted := origData
blockMode.CryptBlocks(crypted, origData)
return crypted, nil
}
Expand Down Expand Up @@ -52,7 +49,6 @@ func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
//Remove excess
func PKCS5UnPadding(origData []byte) (error, []byte) {
length := len(origData)
// 去掉最后一个字节 unpadding 次
unpadding := int(origData[length-1])
if (length - unpadding) < 0 {
return errors.New("len error"), nil
Expand Down
35 changes: 0 additions & 35 deletions lib/file/csv.go

This file was deleted.

Loading

0 comments on commit 5fd335f

Please sign in to comment.