nps/lib/file/obj.go

211 lines
4.3 KiB
Go
Raw Permalink Normal View History

2019-02-09 01:07:47 -08:00
package file
import (
2019-02-02 08:54:43 -08:00
"strings"
"sync"
2019-03-28 19:41:57 -07:00
"sync/atomic"
2019-03-07 02:07:53 -08:00
"time"
2019-08-09 20:15:25 -07:00
2020-01-08 05:57:14 -08:00
"ehang.io/nps/lib/rate"
2019-08-09 20:15:25 -07:00
"github.com/pkg/errors"
)
type Flow struct {
2019-03-28 19:41:57 -07:00
ExportFlow int64
InletFlow int64
FlowLimit int64
sync.RWMutex
}
func (s *Flow) Add(in, out int64) {
s.Lock()
defer s.Unlock()
s.InletFlow += int64(in)
s.ExportFlow += int64(out)
}
2019-03-29 00:21:30 -07:00
type Config struct {
U string
P string
Compress bool
Crypt bool
2019-03-29 00:21:30 -07:00
}
type Client struct {
2019-03-23 07:19:59 -07:00
Cnf *Config
Id int //id
2019-03-28 19:41:57 -07: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-04-08 02:01:08 -07:00
MaxTunnelNum int
Version string
sync.RWMutex
}
2019-02-12 11:54:00 -08: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-02-23 07:29:48 -08:00
func (s *Client) CutConn() {
2019-03-28 19:41:57 -07:00
atomic.AddInt32(&s.NowConn, 1)
2019-02-23 07:29:48 -08:00
}
func (s *Client) AddConn() {
2019-03-28 19:41:57 -07:00
atomic.AddInt32(&s.NowConn, -1)
2019-02-23 07:29:48 -08:00
}
func (s *Client) GetConn() bool {
2019-03-28 19:41:57 -07:00
if s.MaxConn == 0 || int(s.NowConn) < s.MaxConn {
2019-02-24 21:53:20 -08:00
s.CutConn()
2019-02-23 07:29:48 -08:00
return true
}
return false
}
2019-03-23 07:19:59 -07:00
func (s *Client) HasTunnel(t *Tunnel) (exist bool) {
2019-03-29 00:21:30 -07:00
GetDb().JsonDb.Tasks.Range(func(key, value interface{}) bool {
2019-03-23 07:19:59 -07:00
v := value.(*Tunnel)
2019-03-30 01:35:43 -07:00
if v.Client.Id == s.Id && v.Port == t.Port && t.Port != 0 {
2019-03-23 07:19:59 -07:00
exist = true
return false
2019-03-02 01:43:21 -08:00
}
2019-03-23 07:19:59 -07:00
return true
})
return
2019-03-02 01:43:21 -08:00
}
2019-04-08 02:01:08 -07:00
func (s *Client) GetTunnelNum() (num int) {
GetDb().JsonDb.Tasks.Range(func(key, value interface{}) bool {
v := value.(*Tunnel)
if v.Client.Id == s.Id {
num++
}
return true
})
return
}
2019-03-02 01:43:21 -08:00
func (s *Client) HasHost(h *Host) bool {
2019-03-23 07:19:59 -07:00
var has bool
2019-03-29 00:21:30 -07:00
GetDb().JsonDb.Hosts.Range(func(key, value interface{}) bool {
2019-03-23 07:19:59 -07:00
v := value.(*Host)
2019-03-02 01:43:21 -08:00
if v.Client.Id == s.Id && v.Host == h.Host && h.Location == v.Location {
2019-03-23 07:19:59 -07:00
has = true
return false
2019-03-02 01:43:21 -08:00
}
2019-03-23 07:19:59 -07:00
return true
})
return has
2019-03-02 01:43:21 -08:00
}
type Tunnel struct {
Id int
Port int
ServerIp string
Mode string
Status bool
RunStatus bool
Client *Client
Ports string
Flow *Flow
Password string
Remark string
TargetAddr string
NoStore bool
LocalPath string
StripPre string
Target *Target
MultiAccount *MultiAccount
2019-03-07 02:07:53 -08: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-14 23:03:49 -07:00
HealthCheckType string
HealthCheckTarget string
sync.RWMutex
2019-03-07 02:07:53 -08:00
}
2019-03-29 00:21:30 -07:00
type Host struct {
Id int
Host string //host
HeaderChange string //header change
HostChange string //host change
Location string //url router
Remark string //remark
Scheme string //http https all
2019-03-30 01:35:43 -07:00
CertFilePath string
KeyFilePath string
2019-03-29 00:21:30 -07:00
NoStore bool
IsClose bool
Flow *Flow
Client *Client
Target *Target //目标
Health `json:"-"`
sync.RWMutex
}
2019-03-28 19:41:57 -07:00
type Target struct {
2019-04-08 02:01:08 -07:00
nowIndex int
TargetStr string
TargetArr []string
LocalProxy bool
2019-03-28 19:41:57 -07:00
sync.RWMutex
}
type MultiAccount struct {
AccountMap map[string]string // multi account and pwd
}
2019-03-28 19:41:57 -07:00
func (s *Target) GetRandomTarget() (string, error) {
2019-03-07 02:07:53 -08:00
if s.TargetArr == nil {
2019-03-28 19:41:57 -07:00
s.TargetArr = strings.Split(s.TargetStr, "\n")
2019-03-07 02:07:53 -08:00
}
if len(s.TargetArr) == 1 {
2019-03-14 23:03:49 -07:00
return s.TargetArr[0], nil
}
if len(s.TargetArr) == 0 {
return "", errors.New("all inward-bending targets are offline")
2019-03-07 02:07:53 -08:00
}
s.Lock()
defer s.Unlock()
2019-03-28 19:41:57 -07:00
if s.nowIndex >= len(s.TargetArr)-1 {
s.nowIndex = -1
2019-03-07 02:07:53 -08:00
}
2019-03-28 19:41:57 -07:00
s.nowIndex++
return s.TargetArr[s.nowIndex], nil
}