File mode|pubVkey optimization

This commit is contained in:
刘河
2019-03-02 17:43:21 +08:00
parent f526c56784
commit 1c1aa5ec5b
29 changed files with 477 additions and 195 deletions

View File

@@ -9,6 +9,7 @@ const (
WORK_CONFIG = "conf"
WORK_REGISTER = "rgst"
WORK_SECRET = "sert"
WORK_FILE = "file"
WORK_P2P = "p2pm"
WORK_P2P_VISITOR = "p2pv"
WORK_P2P_PROVIDER = "p2pp"

View File

@@ -183,6 +183,10 @@ func dealTunnel(s string) *file.Tunnel {
t.TargetAddr = item[1]
case "password":
t.Password = item[1]
case "local_path":
t.LocalPath = item[1]
case "strip_pre":
t.StripPre = item[1]
}
}
return t

View File

@@ -302,7 +302,7 @@ func (s *Conn) SendTaskInfo(t *file.Tunnel) (int, error) {
*/
raw := bytes.NewBuffer([]byte{})
binary.Write(raw, binary.LittleEndian, []byte(common.NEW_TASK))
common.BinaryWrite(raw, t.Mode, t.Ports, t.Target, t.Remark, t.TargetAddr, t.Password)
common.BinaryWrite(raw, t.Mode, t.Ports, t.Target, t.Remark, t.TargetAddr, t.Password, t.LocalPath, t.StripPre)
s.Lock()
defer s.Unlock()
return s.Write(raw.Bytes())
@@ -329,6 +329,8 @@ func (s *Conn) GetTaskInfo() (t *file.Tunnel, err error) {
t.Remark = arr[3]
t.TargetAddr = arr[4]
t.Password = arr[5]
t.LocalPath = arr[6]
t.StripPre = arr[7]
t.NoStore = true
}
return

View File

@@ -148,7 +148,7 @@ func (s *Csv) GetIdByVerifyKey(vKey string, addr string) (int, error) {
func (s *Csv) NewTask(t *Tunnel) error {
for _, v := range s.Tasks {
if v.Mode == "secret" && v.Password == t.Password {
if (v.Mode == "secret" || v.Mode == "p2p") && v.Password == t.Password {
return errors.New(fmt.Sprintf("Secret mode keys %s must be unique", t.Password))
}
}
@@ -159,10 +159,8 @@ func (s *Csv) NewTask(t *Tunnel) error {
}
func (s *Csv) UpdateTask(t *Tunnel) error {
for k, v := range s.Tasks {
for _, v := range s.Tasks {
if v.Id == t.Id {
s.Tasks = append(s.Tasks[:k], s.Tasks[k+1:]...)
s.Tasks = append(s.Tasks, t)
s.StoreTasksToCsv()
return nil
}
@@ -332,6 +330,9 @@ func (s *Csv) NewHost(t *Host) error {
if s.IsHostExist(t) {
return errors.New("host has exist")
}
if t.Location == "" {
t.Location = "/"
}
t.Flow = new(Flow)
s.Hosts = append(s.Hosts, t)
s.StoreHostToCsv()
@@ -339,10 +340,8 @@ func (s *Csv) NewHost(t *Host) error {
}
func (s *Csv) UpdateHost(t *Host) error {
for k, v := range s.Hosts {
for _, v := range s.Hosts {
if v.Host == t.Host {
s.Hosts = append(s.Hosts[:k], s.Hosts[k+1:]...)
s.Hosts = append(s.Hosts, t)
s.StoreHostToCsv()
return nil
}
@@ -465,7 +464,7 @@ func (s *Csv) GetClient(id int) (v *Client, err error) {
}
func (s *Csv) GetClientIdByVkey(vkey string) (id int, err error) {
for _, v := range s.Clients {
if v.VerifyKey == vkey {
if crypt.Md5(v.VerifyKey) == vkey {
id = v.Id
return
}

View File

@@ -77,6 +77,24 @@ func (s *Client) GetConn() bool {
return false
}
func (s *Client) HasTunnel(t *Tunnel) bool {
for _, v := range GetCsvDb().Tasks {
if v.Client.Id == s.Id && v.Port == t.Port {
return true
}
}
return false
}
func (s *Client) HasHost(h *Host) bool {
for _, v := range GetCsvDb().Hosts {
if v.Client.Id == s.Id && v.Host == h.Host && h.Location == v.Location {
return true
}
}
return false
}
type Tunnel struct {
Id int //Id
Port int //服务端监听端口
@@ -91,6 +109,8 @@ type Tunnel struct {
Remark string //备注
TargetAddr string
NoStore bool
LocalPath string
StripPre string
}
type Config struct {

View File

@@ -140,7 +140,7 @@ func (s *conn) Close() error {
close(s.connStatusOkCh)
close(s.connStatusFailCh)
close(s.readCh)
if !s.mux.isClose {
if !s.mux.IsClose {
s.sendMsgCh <- NewMsg(s.connId, nil)
}
return nil

View File

@@ -21,6 +21,8 @@ const (
MUX_NEW_CONN
MUX_PING
MUX_CONN_CLOSE
MUX_PING_RETURN
RETRY_TIME = 2 //Heart beat allowed fault tolerance times
)
type Mux struct {
@@ -32,7 +34,8 @@ type Mux struct {
newConnCh chan *conn
id int32
closeChan chan struct{}
isClose bool
IsClose bool
pingOk int
sync.Mutex
}
@@ -45,7 +48,7 @@ func NewMux(c net.Conn) *Mux {
id: 0,
closeChan: make(chan struct{}),
newConnCh: make(chan *conn),
isClose: false,
IsClose: false,
}
//read session by flag
go m.readSession()
@@ -57,7 +60,7 @@ func NewMux(c net.Conn) *Mux {
}
func (s *Mux) NewConn() (*conn, error) {
if s.isClose {
if s.IsClose {
return nil, errors.New("the mux has closed")
}
conn := NewConn(s.getId(), s, s.sendMsgCh, s.sendStatusCh)
@@ -82,7 +85,7 @@ func (s *Mux) NewConn() (*conn, error) {
}
func (s *Mux) Accept() (net.Conn, error) {
if s.isClose {
if s.IsClose {
return nil, errors.New("accpet error,the conn has closed")
}
return <-s.newConnCh, nil
@@ -107,10 +110,11 @@ func (s *Mux) ping() {
raw.Reset()
binary.Write(raw, binary.LittleEndian, MUX_PING_FLAG)
binary.Write(raw, binary.LittleEndian, MUX_PING)
if _, err := s.conn.Write(raw.Bytes()); err != nil {
if _, err := s.conn.Write(raw.Bytes()); err != nil || s.pingOk > RETRY_TIME {
s.Close()
break
}
s.pingOk += 1
}
}()
select {
@@ -176,6 +180,13 @@ func (s *Mux) readSession() {
s.conn.Write(raw.Bytes())
continue
case MUX_PING_FLAG: //ping
raw.Reset()
binary.Write(raw, binary.LittleEndian, MUX_PING_RETURN)
binary.Write(raw, binary.LittleEndian, MUX_PING)
s.conn.Write(raw.Bytes())
continue
case MUX_PING_RETURN:
s.pingOk -= 1
continue
case MUX_NEW_MSG:
if n, err = ReadLenBytes(buf, s.conn); err != nil {
@@ -212,10 +223,10 @@ func (s *Mux) readSession() {
}
func (s *Mux) Close() error {
if s.isClose {
if s.IsClose {
return errors.New("the mux has closed")
}
s.isClose = true
s.IsClose = true
s.connMap.Close()
s.closeChan <- struct{}{}
s.closeChan <- struct{}{}