2019-02-09 09:07:47 +00:00
|
|
|
package file
|
2019-01-31 18:06:30 +00:00
|
|
|
|
|
|
|
import (
|
2019-02-09 09:07:47 +00:00
|
|
|
"github.com/cnlh/nps/lib/rate"
|
2019-03-15 06:03:49 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-02-02 16:54:43 +00:00
|
|
|
"strings"
|
2019-01-31 18:06:30 +00:00
|
|
|
"sync"
|
2019-03-29 02:41:57 +00:00
|
|
|
"sync/atomic"
|
2019-03-07 10:07:53 +00:00
|
|
|
"time"
|
2019-01-31 18:06:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Flow struct {
|
2019-03-29 02:41:57 +00:00
|
|
|
ExportFlow int64
|
|
|
|
InletFlow int64
|
|
|
|
FlowLimit int64
|
2019-01-31 18:06:30 +00:00
|
|
|
sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2019-03-01 09:23:14 +00:00
|
|
|
func (s *Flow) Add(in, out int64) {
|
2019-01-31 18:06:30 +00:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
s.InletFlow += int64(in)
|
|
|
|
s.ExportFlow += int64(out)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Client struct {
|
2019-03-23 14:19:59 +00:00
|
|
|
Cnf *Config
|
|
|
|
Id int //id
|
2019-03-29 02:41:57 +00:00
|
|
|
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
|
2019-01-31 18:06:30 +00:00
|
|
|
sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2019-02-12 19:54:00 +00:00
|
|
|
func NewClient(vKey string, noStore bool, noDisplay bool) *Client {
|
|
|
|
return &Client{
|
|
|
|
Cnf: new(Config),
|
|
|
|
Id: 0,
|
|
|
|
VerifyKey: vKey,
|
|
|
|
Addr: "",
|
|
|
|
Remark: "",
|
|
|
|
Status: true,
|
|
|
|
IsConnect: false,
|
|
|
|
RateLimit: 0,
|
|
|
|
Flow: new(Flow),
|
|
|
|
Rate: nil,
|
|
|
|
NoStore: noStore,
|
|
|
|
RWMutex: sync.RWMutex{},
|
|
|
|
NoDisplay: noDisplay,
|
|
|
|
}
|
|
|
|
}
|
2019-01-31 18:06:30 +00:00
|
|
|
|
2019-02-23 15:29:48 +00:00
|
|
|
func (s *Client) CutConn() {
|
2019-03-29 02:41:57 +00:00
|
|
|
atomic.AddInt32(&s.NowConn, 1)
|
2019-02-23 15:29:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Client) AddConn() {
|
2019-03-29 02:41:57 +00:00
|
|
|
atomic.AddInt32(&s.NowConn, -1)
|
2019-02-23 15:29:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Client) GetConn() bool {
|
2019-03-29 02:41:57 +00:00
|
|
|
if s.MaxConn == 0 || int(s.NowConn) < s.MaxConn {
|
2019-02-25 05:53:20 +00:00
|
|
|
s.CutConn()
|
2019-02-23 15:29:48 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-03-23 14:19:59 +00:00
|
|
|
func (s *Client) HasTunnel(t *Tunnel) (exist bool) {
|
|
|
|
GetCsvDb().Tasks.Range(func(key, value interface{}) bool {
|
|
|
|
v := value.(*Tunnel)
|
2019-03-02 09:43:21 +00:00
|
|
|
if v.Client.Id == s.Id && v.Port == t.Port {
|
2019-03-23 14:19:59 +00:00
|
|
|
exist = true
|
|
|
|
return false
|
2019-03-02 09:43:21 +00:00
|
|
|
}
|
2019-03-23 14:19:59 +00:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
return
|
2019-03-02 09:43:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Client) HasHost(h *Host) bool {
|
2019-03-23 14:19:59 +00:00
|
|
|
var has bool
|
|
|
|
GetCsvDb().Hosts.Range(func(key, value interface{}) bool {
|
|
|
|
v := value.(*Host)
|
2019-03-02 09:43:21 +00:00
|
|
|
if v.Client.Id == s.Id && v.Host == h.Host && h.Location == v.Location {
|
2019-03-23 14:19:59 +00:00
|
|
|
has = true
|
|
|
|
return false
|
2019-03-02 09:43:21 +00:00
|
|
|
}
|
2019-03-23 14:19:59 +00:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
return has
|
2019-03-02 09:43:21 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 18:06:30 +00:00
|
|
|
type Tunnel struct {
|
2019-03-26 15:34:55 +00:00
|
|
|
Id int //Id
|
|
|
|
Port int //服务端监听端口
|
|
|
|
ServerIp string
|
2019-03-29 02:41:57 +00:00
|
|
|
Mode string //启动方式
|
|
|
|
Status bool //设置是否开启
|
|
|
|
RunStatus bool //当前运行状态
|
|
|
|
Client *Client //所属客户端id
|
|
|
|
Ports string //客户端与服务端传递
|
2019-02-23 15:29:48 +00:00
|
|
|
Flow *Flow
|
|
|
|
Password string //私密模式密码,唯一
|
|
|
|
Remark string //备注
|
|
|
|
TargetAddr string
|
|
|
|
NoStore bool
|
2019-03-02 09:43:21 +00:00
|
|
|
LocalPath string
|
|
|
|
StripPre string
|
2019-03-29 02:41:57 +00:00
|
|
|
Target *Target
|
2019-03-07 10:07:53 +00:00
|
|
|
Health
|
|
|
|
sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
type Health struct {
|
|
|
|
HealthCheckTimeout int
|
|
|
|
HealthMaxFail int
|
|
|
|
HealthCheckInterval int
|
|
|
|
HealthNextTime time.Time
|
|
|
|
HealthMap map[string]int
|
|
|
|
HttpHealthUrl string
|
|
|
|
HealthRemoveArr []string
|
2019-03-15 06:03:49 +00:00
|
|
|
HealthCheckType string
|
|
|
|
HealthCheckTarget string
|
2019-03-26 03:13:07 +00:00
|
|
|
sync.RWMutex
|
2019-03-07 10:07:53 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 02:41:57 +00:00
|
|
|
type Target struct {
|
|
|
|
nowIndex int
|
|
|
|
TargetStr string
|
|
|
|
TargetArr []string //目标
|
|
|
|
sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Target) GetRandomTarget() (string, error) {
|
2019-03-07 10:07:53 +00:00
|
|
|
if s.TargetArr == nil {
|
2019-03-29 02:41:57 +00:00
|
|
|
s.TargetArr = strings.Split(s.TargetStr, "\n")
|
2019-03-07 10:07:53 +00:00
|
|
|
}
|
|
|
|
if len(s.TargetArr) == 1 {
|
2019-03-15 06:03:49 +00:00
|
|
|
return s.TargetArr[0], nil
|
|
|
|
}
|
|
|
|
if len(s.TargetArr) == 0 {
|
|
|
|
return "", errors.New("all inward-bending targets are offline")
|
2019-03-07 10:07:53 +00:00
|
|
|
}
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
2019-03-29 02:41:57 +00:00
|
|
|
if s.nowIndex >= len(s.TargetArr)-1 {
|
|
|
|
s.nowIndex = -1
|
2019-03-07 10:07:53 +00:00
|
|
|
}
|
2019-03-29 02:41:57 +00:00
|
|
|
s.nowIndex++
|
|
|
|
return s.TargetArr[s.nowIndex], nil
|
2019-01-31 18:06:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Config struct {
|
2019-03-29 02:41:57 +00:00
|
|
|
U string
|
|
|
|
P string
|
|
|
|
Compress bool
|
|
|
|
Crypt bool
|
2019-01-31 18:06:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Host struct {
|
2019-02-15 14:59:28 +00:00
|
|
|
Id int
|
2019-03-29 02:41:57 +00:00
|
|
|
Host string //host
|
|
|
|
HeaderChange string //header change
|
|
|
|
HostChange string //host change
|
|
|
|
Location string //url router
|
|
|
|
Remark string //remark
|
2019-03-07 10:07:53 +00:00
|
|
|
Scheme string //http https all
|
2019-03-29 02:41:57 +00:00
|
|
|
NoStore bool
|
2019-03-15 06:03:49 +00:00
|
|
|
IsClose bool
|
2019-03-29 02:41:57 +00:00
|
|
|
Flow *Flow
|
|
|
|
Client *Client
|
|
|
|
Target *Target //目标
|
2019-03-29 05:31:11 +00:00
|
|
|
Health `json:"-"`
|
2019-02-02 16:54:43 +00:00
|
|
|
sync.RWMutex
|
|
|
|
}
|