mirror of
https://github.com/ehang-io/nps.git
synced 2025-09-02 03:16:53 +00:00
Code optimization
This commit is contained in:
@@ -13,10 +13,8 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
//Get the corresponding IP address through domain name
|
||||
@@ -346,12 +344,3 @@ func BytesToNum(b []byte) int {
|
||||
x, _ := strconv.Atoi(str)
|
||||
return int(x)
|
||||
}
|
||||
|
||||
func GetMapKeys(m sync.Map) (keys []int) {
|
||||
m.Range(func(key, value interface{}) bool {
|
||||
keys = append(keys, key.(int))
|
||||
return true
|
||||
})
|
||||
sort.Ints(keys)
|
||||
return
|
||||
}
|
||||
|
@@ -17,6 +17,7 @@ type CommonConfig struct {
|
||||
ProxyUrl string
|
||||
Client *file.Client
|
||||
}
|
||||
|
||||
type LocalServer struct {
|
||||
Type string
|
||||
Port int
|
||||
@@ -24,6 +25,7 @@ type LocalServer struct {
|
||||
Password string
|
||||
Target string
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
content string
|
||||
title []string
|
||||
@@ -146,6 +148,7 @@ func dealCommon(s string) *CommonConfig {
|
||||
|
||||
func dealHost(s string) *file.Host {
|
||||
h := &file.Host{}
|
||||
h.Target = new(file.Target)
|
||||
var headerChange string
|
||||
for _, v := range splitStr(s) {
|
||||
item := strings.Split(v, "=")
|
||||
@@ -158,7 +161,7 @@ func dealHost(s string) *file.Host {
|
||||
case "host":
|
||||
h.Host = item[1]
|
||||
case "target_addr":
|
||||
h.Target = strings.Replace(item[1], ",", "\n", -1)
|
||||
h.Target.TargetStr = strings.Replace(item[1], ",", "\n", -1)
|
||||
case "host_change":
|
||||
h.HostChange = item[1]
|
||||
case "scheme":
|
||||
@@ -204,6 +207,7 @@ func dealHealth(s string) *file.Health {
|
||||
|
||||
func dealTunnel(s string) *file.Tunnel {
|
||||
t := &file.Tunnel{}
|
||||
t.Target = new(file.Target)
|
||||
for _, v := range splitStr(s) {
|
||||
item := strings.Split(v, "=")
|
||||
if len(item) == 0 {
|
||||
@@ -219,7 +223,7 @@ func dealTunnel(s string) *file.Tunnel {
|
||||
case "mode":
|
||||
t.Mode = item[1]
|
||||
case "target_port", "target_addr":
|
||||
t.Target = strings.Replace(item[1], ",", "\n", -1)
|
||||
t.Target.TargetStr = strings.Replace(item[1], ",", "\n", -1)
|
||||
case "target_ip":
|
||||
t.TargetAddr = item[1]
|
||||
case "password":
|
||||
|
@@ -25,17 +25,14 @@ import (
|
||||
|
||||
type Conn struct {
|
||||
Conn net.Conn
|
||||
sync.Mutex
|
||||
}
|
||||
|
||||
//new conn
|
||||
func NewConn(conn net.Conn) *Conn {
|
||||
c := new(Conn)
|
||||
c.Conn = conn
|
||||
return c
|
||||
return &Conn{Conn: conn}
|
||||
}
|
||||
|
||||
//从tcp报文中解析出host,连接类型等
|
||||
//get host 、connection type、method...from connection
|
||||
func (s *Conn) GetHost() (method, address string, rb []byte, err error, r *http.Request) {
|
||||
var b [32 * 1024]byte
|
||||
var n int
|
||||
@@ -53,14 +50,14 @@ func (s *Conn) GetHost() (method, address string, rb []byte, err error, r *http.
|
||||
err = nil
|
||||
return
|
||||
}
|
||||
if hostPortURL.Opaque == "443" { //https访问
|
||||
if strings.Index(r.Host, ":") == -1 { //host不带端口, 默认80
|
||||
if hostPortURL.Opaque == "443" {
|
||||
if strings.Index(r.Host, ":") == -1 {
|
||||
address = r.Host + ":443"
|
||||
} else {
|
||||
address = r.Host
|
||||
}
|
||||
} else { //http访问
|
||||
if strings.Index(r.Host, ":") == -1 { //host不带端口, 默认80
|
||||
} else {
|
||||
if strings.Index(r.Host, ":") == -1 {
|
||||
address = r.Host + ":80"
|
||||
} else {
|
||||
address = r.Host
|
||||
@@ -117,7 +114,7 @@ func (s *Conn) ReadFlag() (string, error) {
|
||||
return string(buf), binary.Read(s, binary.LittleEndian, &buf)
|
||||
}
|
||||
|
||||
//设置连接为长连接
|
||||
//set alive
|
||||
func (s *Conn) SetAlive(tp string) {
|
||||
switch s.Conn.(type) {
|
||||
case *kcp.UDPSession:
|
||||
@@ -132,7 +129,7 @@ func (s *Conn) SetAlive(tp string) {
|
||||
}
|
||||
}
|
||||
|
||||
//设置连接为长连接
|
||||
//set read deadline
|
||||
func (s *Conn) SetReadDeadline(t time.Duration, tp string) {
|
||||
switch s.Conn.(type) {
|
||||
case *kcp.UDPSession:
|
||||
@@ -176,8 +173,6 @@ func (s *Conn) GetLinkInfo() (lk *Link, err error) {
|
||||
func (s *Conn) SendHealthInfo(info, status string) (int, error) {
|
||||
raw := bytes.NewBuffer([]byte{})
|
||||
common.BinaryWrite(raw, info, status)
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
return s.Write(raw.Bytes())
|
||||
}
|
||||
|
||||
@@ -211,9 +206,7 @@ func (s *Conn) SendHostInfo(h *file.Host) (int, error) {
|
||||
*/
|
||||
raw := bytes.NewBuffer([]byte{})
|
||||
binary.Write(raw, binary.LittleEndian, []byte(common.NEW_HOST))
|
||||
common.BinaryWrite(raw, h.Host, h.Target, h.HeaderChange, h.HostChange, h.Remark, h.Location, h.Scheme)
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
common.BinaryWrite(raw, h.Host, h.Target.TargetStr, h.HeaderChange, h.HostChange, h.Remark, h.Location, h.Scheme)
|
||||
return s.Write(raw.Bytes())
|
||||
}
|
||||
|
||||
@@ -244,9 +237,10 @@ func (s *Conn) GetHostInfo() (h *file.Host, err error) {
|
||||
} else {
|
||||
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.Host = arr[0]
|
||||
h.Target = arr[1]
|
||||
h.Target.TargetStr = arr[1]
|
||||
h.HeaderChange = arr[2]
|
||||
h.HostChange = arr[3]
|
||||
h.Remark = arr[4]
|
||||
@@ -275,8 +269,6 @@ func (s *Conn) SendConfigInfo(c *config.CommonConfig) (int, error) {
|
||||
binary.Write(raw, binary.LittleEndian, []byte(common.NEW_CONF))
|
||||
common.BinaryWrite(raw, c.Cnf.U, c.Cnf.P, common.GetStrByBool(c.Cnf.Crypt), common.GetStrByBool(c.Cnf.Compress), strconv.Itoa(c.Client.RateLimit),
|
||||
strconv.Itoa(int(c.Client.Flow.FlowLimit)), strconv.Itoa(c.Client.MaxConn), c.Client.Remark)
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
return s.Write(raw.Bytes())
|
||||
}
|
||||
|
||||
@@ -316,9 +308,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, t.LocalPath, t.StripPre, t.ServerIp)
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
common.BinaryWrite(raw, t.Mode, t.Ports, t.Target.TargetStr, t.Remark, t.TargetAddr, t.Password, t.LocalPath, t.StripPre, t.ServerIp)
|
||||
return s.Write(raw.Bytes())
|
||||
}
|
||||
|
||||
@@ -334,9 +324,10 @@ func (s *Conn) GetTaskInfo() (t *file.Tunnel, err error) {
|
||||
} else {
|
||||
arr := strings.Split(string(buf[:l]), common.CONN_DATA_SEQ)
|
||||
t = new(file.Tunnel)
|
||||
t.Target = new(file.Target)
|
||||
t.Mode = arr[0]
|
||||
t.Ports = arr[1]
|
||||
t.Target = arr[2]
|
||||
t.Target.TargetStr = arr[2]
|
||||
t.Id = int(file.GetCsvDb().GetTaskId())
|
||||
t.Status = true
|
||||
t.Flow = new(file.Flow)
|
||||
@@ -375,26 +366,20 @@ func (s *Conn) WriteClose() (int, error) {
|
||||
|
||||
//write main
|
||||
func (s *Conn) WriteMain() (int, error) {
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
return s.Write([]byte(common.WORK_MAIN))
|
||||
}
|
||||
|
||||
//write main
|
||||
func (s *Conn) WriteConfig() (int, error) {
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
return s.Write([]byte(common.WORK_CONFIG))
|
||||
}
|
||||
|
||||
//write chan
|
||||
func (s *Conn) WriteChan() (int, error) {
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
return s.Write([]byte(common.WORK_CHAN))
|
||||
}
|
||||
|
||||
//获取长度+内容
|
||||
//get the assembled amount data(len 4 and content)
|
||||
func GetLenBytes(buf []byte) (b []byte, err error) {
|
||||
raw := bytes.NewBuffer([]byte{})
|
||||
if err = binary.Write(raw, binary.LittleEndian, int32(len(buf))); err != nil {
|
||||
@@ -407,6 +392,7 @@ func GetLenBytes(buf []byte) (b []byte, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
//udp connection setting
|
||||
func SetUdpSession(sess *kcp.UDPSession) {
|
||||
sess.SetStreamMode(true)
|
||||
sess.SetWindowSize(1024, 1024)
|
||||
@@ -450,13 +436,7 @@ func GetConn(conn net.Conn, cpt, snappy bool, rt *rate.Rate, isServer bool) (io.
|
||||
}
|
||||
return rate.NewRateConn(crypt.NewTlsClientConn(conn), rt)
|
||||
} else if snappy {
|
||||
return NewSnappyConn(conn, cpt, rt)
|
||||
return rate.NewRateConn(NewSnappyConn(conn), rt)
|
||||
}
|
||||
return rate.NewRateConn(conn, rt)
|
||||
}
|
||||
|
||||
//read length or id (content length=4)
|
||||
func GetLen(reader io.Reader) (int, error) {
|
||||
var l int32
|
||||
return int(l), binary.Read(reader, binary.LittleEndian, &l)
|
||||
}
|
||||
|
@@ -2,22 +2,20 @@ package conn
|
||||
|
||||
import (
|
||||
"github.com/cnlh/nps/lib/pool"
|
||||
"github.com/cnlh/nps/lib/rate"
|
||||
"github.com/cnlh/nps/vender/github.com/golang/snappy"
|
||||
"github.com/fatedier/frp/utils/net"
|
||||
"io"
|
||||
)
|
||||
|
||||
type SnappyConn struct {
|
||||
w *snappy.Writer
|
||||
r *snappy.Reader
|
||||
rate *rate.Rate
|
||||
w *snappy.Writer
|
||||
r *snappy.Reader
|
||||
}
|
||||
|
||||
func NewSnappyConn(conn io.ReadWriteCloser, crypt bool, rate *rate.Rate) *SnappyConn {
|
||||
func NewSnappyConn(conn io.ReadWriteCloser) net.Conn {
|
||||
c := new(SnappyConn)
|
||||
c.w = snappy.NewBufferedWriter(conn)
|
||||
c.r = snappy.NewReader(conn)
|
||||
c.rate = rate
|
||||
return c
|
||||
}
|
||||
|
||||
@@ -29,9 +27,6 @@ func (s *SnappyConn) Write(b []byte) (n int, err error) {
|
||||
if err = s.w.Flush(); err != nil {
|
||||
return
|
||||
}
|
||||
if s.rate != nil {
|
||||
s.rate.Get(int64(n))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -43,9 +38,6 @@ func (s *SnappyConn) Read(b []byte) (n int, err error) {
|
||||
return
|
||||
}
|
||||
copy(b, buf[:n])
|
||||
if s.rate != nil {
|
||||
s.rate.Get(int64(n))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
@@ -2,6 +2,7 @@ package file
|
||||
|
||||
import (
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"sort"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@@ -20,3 +21,15 @@ func GetCsvDb() *Csv {
|
||||
})
|
||||
return CsvDb
|
||||
}
|
||||
|
||||
func GetMapKeys(m sync.Map, isSort bool, sortKey, order string) (keys []int) {
|
||||
if sortKey != "" && isSort {
|
||||
return sortClientByKey(m, sortKey, order)
|
||||
}
|
||||
m.Range(func(key, value interface{}) bool {
|
||||
keys = append(keys, key.(int))
|
||||
return true
|
||||
})
|
||||
sort.Ints(keys)
|
||||
return
|
||||
}
|
||||
|
@@ -7,7 +7,6 @@ import (
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/lib/crypt"
|
||||
"github.com/cnlh/nps/lib/rate"
|
||||
"github.com/cnlh/nps/vender/github.com/astaxie/beego"
|
||||
"github.com/cnlh/nps/vender/github.com/astaxie/beego/logs"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -52,7 +51,7 @@ func (s *Csv) StoreTasksToCsv() {
|
||||
record := []string{
|
||||
strconv.Itoa(task.Port),
|
||||
task.Mode,
|
||||
task.Target,
|
||||
task.Target.TargetStr,
|
||||
common.GetStrByBool(task.Status),
|
||||
strconv.Itoa(task.Id),
|
||||
strconv.Itoa(task.Client.Id),
|
||||
@@ -101,12 +100,13 @@ func (s *Csv) LoadTaskFromCsv() {
|
||||
post := &Tunnel{
|
||||
Port: common.GetIntNoErrByStr(item[0]),
|
||||
Mode: item[1],
|
||||
Target: item[2],
|
||||
Status: common.GetBoolByStr(item[3]),
|
||||
Id: common.GetIntNoErrByStr(item[4]),
|
||||
Remark: item[6],
|
||||
Password: item[9],
|
||||
}
|
||||
post.Target = new(Target)
|
||||
post.Target.TargetStr = item[2]
|
||||
post.Flow = new(Flow)
|
||||
post.Flow.ExportFlow = int64(common.GetIntNoErrByStr(item[7]))
|
||||
post.Flow.InletFlow = int64(common.GetIntNoErrByStr(item[8]))
|
||||
@@ -212,7 +212,7 @@ func (s *Csv) StoreHostToCsv() {
|
||||
}
|
||||
record := []string{
|
||||
host.Host,
|
||||
host.Target,
|
||||
host.Target.TargetStr,
|
||||
strconv.Itoa(host.Client.Id),
|
||||
host.HeaderChange,
|
||||
host.HostChange,
|
||||
@@ -274,6 +274,16 @@ func (s *Csv) LoadClientFromCsv() {
|
||||
} else {
|
||||
post.ConfigConnAllow = true
|
||||
}
|
||||
if len(item) >= 13 {
|
||||
post.WebUserName = item[12]
|
||||
} else {
|
||||
post.WebUserName = ""
|
||||
}
|
||||
if len(item) >= 14 {
|
||||
post.WebPassword = item[13]
|
||||
} else {
|
||||
post.WebPassword = ""
|
||||
}
|
||||
s.Clients.Store(post.Id, post)
|
||||
}
|
||||
}
|
||||
@@ -289,7 +299,6 @@ func (s *Csv) LoadHostFromCsv() {
|
||||
for _, item := range records {
|
||||
post := &Host{
|
||||
Host: item[0],
|
||||
Target: item[1],
|
||||
HeaderChange: item[3],
|
||||
HostChange: item[4],
|
||||
Remark: item[5],
|
||||
@@ -299,6 +308,8 @@ func (s *Csv) LoadHostFromCsv() {
|
||||
if post.Client, err = s.GetClient(common.GetIntNoErrByStr(item[2])); err != nil {
|
||||
continue
|
||||
}
|
||||
post.Target = new(Target)
|
||||
post.Target.TargetStr = item[1]
|
||||
post.Flow = new(Flow)
|
||||
post.Flow.ExportFlow = int64(common.GetIntNoErrByStr(item[8]))
|
||||
post.Flow.InletFlow = int64(common.GetIntNoErrByStr(item[9]))
|
||||
@@ -344,12 +355,12 @@ func (s *Csv) IsHostExist(h *Host) bool {
|
||||
}
|
||||
|
||||
func (s *Csv) NewHost(t *Host) error {
|
||||
if s.IsHostExist(t) {
|
||||
return errors.New("host has exist")
|
||||
}
|
||||
if t.Location == "" {
|
||||
t.Location = "/"
|
||||
}
|
||||
if s.IsHostExist(t) {
|
||||
return errors.New("host has exist")
|
||||
}
|
||||
t.Flow = new(Flow)
|
||||
s.Hosts.Store(t.Id, t)
|
||||
s.StoreHostToCsv()
|
||||
@@ -359,7 +370,7 @@ func (s *Csv) NewHost(t *Host) error {
|
||||
func (s *Csv) GetHost(start, length int, id int, search string) ([]*Host, int) {
|
||||
list := make([]*Host, 0)
|
||||
var cnt int
|
||||
keys := common.GetMapKeys(s.Hosts)
|
||||
keys := GetMapKeys(s.Hosts, false, "", "")
|
||||
for _, key := range keys {
|
||||
if value, ok := s.Hosts.Load(key); ok {
|
||||
v := value.(*Host)
|
||||
@@ -387,6 +398,9 @@ func (s *Csv) DelClient(id int) error {
|
||||
|
||||
func (s *Csv) NewClient(c *Client) error {
|
||||
var isNotSet bool
|
||||
if c.WebUserName != "" && !s.VerifyUserName(c.WebUserName, c.Id) {
|
||||
return errors.New("web login username duplicate, please reset")
|
||||
}
|
||||
reset:
|
||||
if c.VerifyKey == "" || isNotSet {
|
||||
isNotSet = true
|
||||
@@ -396,7 +410,7 @@ reset:
|
||||
c.Rate = rate.NewRate(int64(2 << 23))
|
||||
c.Rate.Start()
|
||||
}
|
||||
if !s.VerifyVkey(c.VerifyKey, c.id) {
|
||||
if !s.VerifyVkey(c.VerifyKey, c.Id) {
|
||||
if isNotSet {
|
||||
goto reset
|
||||
}
|
||||
@@ -425,6 +439,18 @@ func (s *Csv) VerifyVkey(vkey string, id int) (res bool) {
|
||||
})
|
||||
return res
|
||||
}
|
||||
func (s *Csv) VerifyUserName(username string, id int) (res bool) {
|
||||
res = true
|
||||
s.Clients.Range(func(key, value interface{}) bool {
|
||||
v := value.(*Client)
|
||||
if v.WebUserName == username && v.Id != id {
|
||||
res = false
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
return res
|
||||
}
|
||||
|
||||
func (s *Csv) GetClientId() int32 {
|
||||
return atomic.AddInt32(&s.ClientIncreaseId, 1)
|
||||
@@ -447,10 +473,10 @@ func (s *Csv) UpdateClient(t *Client) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Csv) GetClientList(start, length int, search string, clientId int) ([]*Client, int) {
|
||||
func (s *Csv) GetClientList(start, length int, search, sort, order string, clientId int) ([]*Client, int) {
|
||||
list := make([]*Client, 0)
|
||||
var cnt int
|
||||
keys := common.GetMapKeys(s.Clients)
|
||||
keys := GetMapKeys(s.Clients, true, sort, order)
|
||||
for _, key := range keys {
|
||||
if value, ok := s.Clients.Load(key); ok {
|
||||
v := value.(*Client)
|
||||
@@ -477,11 +503,7 @@ func (s *Csv) GetClientList(start, length int, search string, clientId int) ([]*
|
||||
func (s *Csv) IsPubClient(id int) bool {
|
||||
client, err := s.GetClient(id)
|
||||
if err == nil {
|
||||
if client.VerifyKey == beego.AppConfig.String("public_vkey") {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
return client.NoDisplay
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -589,6 +611,8 @@ func (s *Csv) StoreClientsToCsv() {
|
||||
strconv.Itoa(int(client.Flow.FlowLimit)),
|
||||
strconv.Itoa(int(client.MaxConn)),
|
||||
common.GetStrByBool(client.ConfigConnAllow),
|
||||
client.WebUserName,
|
||||
client.WebPassword,
|
||||
}
|
||||
err := writer.Write(record)
|
||||
if err != nil {
|
||||
|
131
lib/file/obj.go
131
lib/file/obj.go
@@ -5,13 +5,14 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Flow struct {
|
||||
ExportFlow int64 //出口流量
|
||||
InletFlow int64 //入口流量
|
||||
FlowLimit int64 //流量限制,出口+入口 /M
|
||||
ExportFlow int64
|
||||
InletFlow int64
|
||||
FlowLimit int64
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
@@ -25,20 +26,21 @@ func (s *Flow) Add(in, out int64) {
|
||||
type Client struct {
|
||||
Cnf *Config
|
||||
Id int //id
|
||||
VerifyKey string //验证密钥
|
||||
Addr string //客户端ip地址
|
||||
Remark string //备注
|
||||
Status bool //是否开启
|
||||
IsConnect bool //是否连接
|
||||
RateLimit int //速度限制 /kb
|
||||
Flow *Flow //流量
|
||||
Rate *rate.Rate //速度控制
|
||||
NoStore bool
|
||||
NoDisplay bool
|
||||
MaxConn int //客户端最大连接数
|
||||
NowConn int //当前连接数
|
||||
id int
|
||||
ConfigConnAllow bool
|
||||
VerifyKey string //verify key
|
||||
Addr string //the ip of client
|
||||
Remark string //remark
|
||||
Status bool //is allow connect
|
||||
IsConnect bool //is the client connect
|
||||
RateLimit int //rate /kb
|
||||
Flow *Flow //flow setting
|
||||
Rate *rate.Rate //rate limit
|
||||
NoStore bool //no store to file
|
||||
NoDisplay bool //no display on web
|
||||
MaxConn int //the max connection num of client allow
|
||||
NowConn int32 //the connection num of now
|
||||
WebUserName string //the username of web login
|
||||
WebPassword string //the password of web login
|
||||
ConfigConnAllow bool //is allow connected by config file
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
@@ -61,30 +63,21 @@ func NewClient(vKey string, noStore bool, noDisplay bool) *Client {
|
||||
}
|
||||
|
||||
func (s *Client) CutConn() {
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
s.NowConn++
|
||||
atomic.AddInt32(&s.NowConn, 1)
|
||||
}
|
||||
|
||||
func (s *Client) AddConn() {
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
s.NowConn--
|
||||
atomic.AddInt32(&s.NowConn, -1)
|
||||
}
|
||||
|
||||
func (s *Client) GetConn() bool {
|
||||
if s.MaxConn == 0 || s.NowConn < s.MaxConn {
|
||||
if s.MaxConn == 0 || int(s.NowConn) < s.MaxConn {
|
||||
s.CutConn()
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
//modify the hosts and the tunnels by health information
|
||||
func (s *Client) ModifyTarget() {
|
||||
|
||||
}
|
||||
|
||||
func (s *Client) HasTunnel(t *Tunnel) (exist bool) {
|
||||
GetCsvDb().Tasks.Range(func(key, value interface{}) bool {
|
||||
v := value.(*Tunnel)
|
||||
@@ -114,13 +107,11 @@ type Tunnel struct {
|
||||
Id int //Id
|
||||
Port int //服务端监听端口
|
||||
ServerIp string
|
||||
Mode string //启动方式
|
||||
Target string //目标
|
||||
TargetArr []string //目标
|
||||
Status bool //设置是否开启
|
||||
RunStatus bool //当前运行状态
|
||||
Client *Client //所属客户端id
|
||||
Ports string //客户端与服务端传递
|
||||
Mode string //启动方式
|
||||
Status bool //设置是否开启
|
||||
RunStatus bool //当前运行状态
|
||||
Client *Client //所属客户端id
|
||||
Ports string //客户端与服务端传递
|
||||
Flow *Flow
|
||||
Password string //私密模式密码,唯一
|
||||
Remark string //备注
|
||||
@@ -128,7 +119,7 @@ type Tunnel struct {
|
||||
NoStore bool
|
||||
LocalPath string
|
||||
StripPre string
|
||||
NowIndex int
|
||||
Target *Target
|
||||
Health
|
||||
sync.RWMutex
|
||||
}
|
||||
@@ -146,9 +137,16 @@ type Health struct {
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
func (s *Tunnel) GetRandomTarget() (string, error) {
|
||||
type Target struct {
|
||||
nowIndex int
|
||||
TargetStr string
|
||||
TargetArr []string //目标
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
func (s *Target) GetRandomTarget() (string, error) {
|
||||
if s.TargetArr == nil {
|
||||
s.TargetArr = strings.Split(s.Target, "\n")
|
||||
s.TargetArr = strings.Split(s.TargetStr, "\n")
|
||||
}
|
||||
if len(s.TargetArr) == 1 {
|
||||
return s.TargetArr[0], nil
|
||||
@@ -158,54 +156,33 @@ func (s *Tunnel) GetRandomTarget() (string, error) {
|
||||
}
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
if s.NowIndex >= len(s.TargetArr)-1 {
|
||||
s.NowIndex = -1
|
||||
if s.nowIndex >= len(s.TargetArr)-1 {
|
||||
s.nowIndex = -1
|
||||
}
|
||||
s.NowIndex++
|
||||
return s.TargetArr[s.NowIndex], nil
|
||||
s.nowIndex++
|
||||
return s.TargetArr[s.nowIndex], nil
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
U string //socks5验证用户名
|
||||
P string //socks5验证密码
|
||||
Compress bool //压缩方式
|
||||
Crypt bool //是否加密
|
||||
U string
|
||||
P string
|
||||
Compress bool
|
||||
Crypt bool
|
||||
}
|
||||
|
||||
type Host struct {
|
||||
Id int
|
||||
Host string //启动方式
|
||||
Target string //目标
|
||||
HeaderChange string //host修改
|
||||
HostChange string //host修改
|
||||
Location string //url 路由
|
||||
Host string //host
|
||||
HeaderChange string //header change
|
||||
HostChange string //host change
|
||||
Location string //url router
|
||||
Remark string //remark
|
||||
Scheme string //http https all
|
||||
NoStore bool
|
||||
IsClose bool
|
||||
Flow *Flow
|
||||
Client *Client
|
||||
Remark string //备注
|
||||
NowIndex int
|
||||
TargetArr []string
|
||||
NoStore bool
|
||||
Scheme string //http https all
|
||||
IsClose bool
|
||||
Target *Target //目标
|
||||
Health
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
func (s *Host) GetRandomTarget() (string, error) {
|
||||
if s.TargetArr == nil {
|
||||
s.TargetArr = strings.Split(s.Target, "\n")
|
||||
}
|
||||
if len(s.TargetArr) == 1 {
|
||||
return s.TargetArr[0], nil
|
||||
}
|
||||
if len(s.TargetArr) == 0 {
|
||||
return "", errors.New("all inward-bending targets are offline")
|
||||
}
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
if s.NowIndex >= len(s.TargetArr)-1 {
|
||||
s.NowIndex = -1
|
||||
}
|
||||
s.NowIndex++
|
||||
return s.TargetArr[s.NowIndex], nil
|
||||
}
|
||||
|
41
lib/file/sort.go
Normal file
41
lib/file/sort.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"sort"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// A data structure to hold a key/value pair.
|
||||
type Pair struct {
|
||||
key string //sort key
|
||||
cId int
|
||||
order string
|
||||
clientFlow *Flow
|
||||
}
|
||||
|
||||
// A slice of Pairs that implements sort.Interface to sort by Value.
|
||||
type PairList []*Pair
|
||||
|
||||
func (p PairList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
||||
func (p PairList) Len() int { return len(p) }
|
||||
func (p PairList) Less(i, j int) bool {
|
||||
if p[i].order == "desc" {
|
||||
return reflect.ValueOf(*p[i].clientFlow).FieldByName(p[i].key).Int() < reflect.ValueOf(*p[j].clientFlow).FieldByName(p[j].key).Int()
|
||||
}
|
||||
return reflect.ValueOf(*p[i].clientFlow).FieldByName(p[i].key).Int() > reflect.ValueOf(*p[j].clientFlow).FieldByName(p[j].key).Int()
|
||||
}
|
||||
|
||||
// A function to turn a map into a PairList, then sort and return it.
|
||||
func sortClientByKey(m sync.Map, sortKey, order string) (res []int) {
|
||||
p := make(PairList, 0)
|
||||
m.Range(func(key, value interface{}) bool {
|
||||
p = append(p, &Pair{sortKey, value.(*Client).Id, order, value.(*Client).Flow})
|
||||
return true
|
||||
})
|
||||
sort.Sort(p)
|
||||
for _, v := range p {
|
||||
res = append(res, v.cId)
|
||||
}
|
||||
return
|
||||
}
|
@@ -59,16 +59,9 @@ func (s *Mux) NewConn() (*conn, error) {
|
||||
return nil, errors.New("the mux has closed")
|
||||
}
|
||||
conn := NewConn(s.getId(), s)
|
||||
raw := bytes.NewBuffer([]byte{})
|
||||
if err := binary.Write(raw, binary.LittleEndian, MUX_NEW_CONN); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := binary.Write(raw, binary.LittleEndian, conn.connId); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
//it must be set before send
|
||||
s.connMap.Set(conn.connId, conn)
|
||||
if _, err := s.conn.Write(raw.Bytes()); err != nil {
|
||||
if err := s.sendInfo(MUX_NEW_CONN, conn.connId, nil); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
//set a timer timeout 30 second
|
||||
|
@@ -25,14 +25,14 @@ func TestNewMux(t *testing.T) {
|
||||
client()
|
||||
time.Sleep(time.Second * 3)
|
||||
go func() {
|
||||
m2 := NewMux(conn2)
|
||||
m2 := NewMux(conn2, "tcp")
|
||||
for {
|
||||
c, err := m2.Accept()
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
go func(c net.Conn) {
|
||||
c2, err := net.Dial("tcp", "10.1.50.196:4000")
|
||||
c2, err := net.Dial("tcp", "127.0.0.1:8082")
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
@@ -45,7 +45,7 @@ func TestNewMux(t *testing.T) {
|
||||
}()
|
||||
|
||||
go func() {
|
||||
m1 := NewMux(conn1)
|
||||
m1 := NewMux(conn1, "tcp")
|
||||
l, err := net.Listen("tcp", "127.0.0.1:7777")
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
|
@@ -52,12 +52,6 @@ func GetBufPoolCopy() ([]byte) {
|
||||
return (*BufPoolCopy.Get().(*[]byte))[:PoolSizeCopy]
|
||||
}
|
||||
|
||||
func PutBufPoolSmall(buf []byte) {
|
||||
if cap(buf) == PoolSizeSmall {
|
||||
BufPoolSmall.Put(buf[:PoolSizeSmall])
|
||||
}
|
||||
}
|
||||
|
||||
func PutBufPoolMax(buf []byte) {
|
||||
if cap(buf) == PoolSize {
|
||||
BufPoolMax.Put(buf[:PoolSize])
|
||||
|
@@ -6,10 +6,10 @@ import (
|
||||
)
|
||||
|
||||
type Rate struct {
|
||||
bucketSize int64 //木桶容量
|
||||
bucketSurplusSize int64 //当前桶中体积
|
||||
bucketAddSize int64 //每次加水大小
|
||||
stopChan chan bool //停止
|
||||
bucketSize int64
|
||||
bucketSurplusSize int64
|
||||
bucketAddSize int64
|
||||
stopChan chan bool
|
||||
NowRate int64
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user