2019-02-09 09:07:47 +00:00
|
|
|
|
package file
|
2019-01-25 04:10:12 +00:00
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/csv"
|
|
|
|
|
"errors"
|
2019-02-23 15:29:48 +00:00
|
|
|
|
"fmt"
|
2019-02-09 09:07:47 +00:00
|
|
|
|
"github.com/cnlh/nps/lib/common"
|
2019-02-23 15:29:48 +00:00
|
|
|
|
"github.com/cnlh/nps/lib/crypt"
|
2019-02-09 09:07:47 +00:00
|
|
|
|
"github.com/cnlh/nps/lib/rate"
|
2019-02-23 15:29:48 +00:00
|
|
|
|
"github.com/cnlh/nps/vender/github.com/astaxie/beego/logs"
|
2019-02-15 14:59:28 +00:00
|
|
|
|
"net/http"
|
2019-01-25 04:10:12 +00:00
|
|
|
|
"os"
|
2019-02-05 16:35:23 +00:00
|
|
|
|
"path/filepath"
|
2019-02-15 14:59:28 +00:00
|
|
|
|
"regexp"
|
2019-01-25 04:10:12 +00:00
|
|
|
|
"strconv"
|
2019-01-28 06:45:55 +00:00
|
|
|
|
"strings"
|
2019-01-25 04:10:12 +00:00
|
|
|
|
"sync"
|
2019-03-23 14:19:59 +00:00
|
|
|
|
"sync/atomic"
|
2019-01-25 04:10:12 +00:00
|
|
|
|
)
|
|
|
|
|
|
2019-02-09 09:07:47 +00:00
|
|
|
|
func NewCsv(runPath string) *Csv {
|
|
|
|
|
return &Csv{
|
|
|
|
|
RunPath: runPath,
|
|
|
|
|
}
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Csv struct {
|
2019-03-23 14:19:59 +00:00
|
|
|
|
Tasks sync.Map
|
|
|
|
|
Hosts sync.Map //域名列表
|
|
|
|
|
HostsTmp sync.Map
|
|
|
|
|
Clients sync.Map //客户端
|
|
|
|
|
RunPath string //存储根目录
|
|
|
|
|
ClientIncreaseId int32 //客户端id
|
|
|
|
|
TaskIncreaseId int32 //任务自增ID
|
|
|
|
|
HostIncreaseId int32 //host increased id
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Csv) StoreTasksToCsv() {
|
|
|
|
|
// 创建文件
|
2019-02-09 09:07:47 +00:00
|
|
|
|
csvFile, err := os.Create(filepath.Join(s.RunPath, "conf", "tasks.csv"))
|
2019-01-25 04:10:12 +00:00
|
|
|
|
if err != nil {
|
2019-02-23 15:29:48 +00:00
|
|
|
|
logs.Error(err.Error())
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
defer csvFile.Close()
|
|
|
|
|
writer := csv.NewWriter(csvFile)
|
2019-03-23 14:19:59 +00:00
|
|
|
|
s.Tasks.Range(func(key, value interface{}) bool {
|
|
|
|
|
task := value.(*Tunnel)
|
2019-02-12 19:54:00 +00:00
|
|
|
|
if task.NoStore {
|
2019-03-23 14:19:59 +00:00
|
|
|
|
return true
|
2019-02-12 19:54:00 +00:00
|
|
|
|
}
|
2019-01-25 04:10:12 +00:00
|
|
|
|
record := []string{
|
2019-02-12 19:54:00 +00:00
|
|
|
|
strconv.Itoa(task.Port),
|
2019-01-25 04:10:12 +00:00
|
|
|
|
task.Mode,
|
|
|
|
|
task.Target,
|
2019-02-09 09:07:47 +00:00
|
|
|
|
common.GetStrByBool(task.Status),
|
2019-01-25 04:10:12 +00:00
|
|
|
|
strconv.Itoa(task.Id),
|
2019-01-26 09:27:28 +00:00
|
|
|
|
strconv.Itoa(task.Client.Id),
|
2019-01-25 04:10:12 +00:00
|
|
|
|
task.Remark,
|
2019-02-23 15:29:48 +00:00
|
|
|
|
strconv.Itoa(int(task.Flow.ExportFlow)),
|
|
|
|
|
strconv.Itoa(int(task.Flow.InletFlow)),
|
|
|
|
|
task.Password,
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
err := writer.Write(record)
|
|
|
|
|
if err != nil {
|
2019-02-23 15:29:48 +00:00
|
|
|
|
logs.Error(err.Error())
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
2019-03-23 14:19:59 +00:00
|
|
|
|
return true
|
|
|
|
|
})
|
2019-01-25 04:10:12 +00:00
|
|
|
|
writer.Flush()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Csv) openFile(path string) ([][]string, error) {
|
|
|
|
|
// 打开文件
|
|
|
|
|
file, err := os.Open(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
|
|
// 获取csv的reader
|
|
|
|
|
reader := csv.NewReader(file)
|
|
|
|
|
|
|
|
|
|
// 设置FieldsPerRecord为-1
|
|
|
|
|
reader.FieldsPerRecord = -1
|
|
|
|
|
|
|
|
|
|
// 读取文件中所有行保存到slice中
|
|
|
|
|
return reader.ReadAll()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Csv) LoadTaskFromCsv() {
|
2019-02-09 09:07:47 +00:00
|
|
|
|
path := filepath.Join(s.RunPath, "conf", "tasks.csv")
|
2019-01-25 04:10:12 +00:00
|
|
|
|
records, err := s.openFile(path)
|
|
|
|
|
if err != nil {
|
2019-02-23 15:29:48 +00:00
|
|
|
|
logs.Error("Profile Opening Error:", path)
|
|
|
|
|
os.Exit(0)
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
// 将每一行数据保存到内存slice中
|
|
|
|
|
for _, item := range records {
|
2019-01-26 09:27:28 +00:00
|
|
|
|
post := &Tunnel{
|
2019-02-23 15:29:48 +00:00
|
|
|
|
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],
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
post.Flow = new(Flow)
|
2019-02-23 15:29:48 +00:00
|
|
|
|
post.Flow.ExportFlow = int64(common.GetIntNoErrByStr(item[7]))
|
|
|
|
|
post.Flow.InletFlow = int64(common.GetIntNoErrByStr(item[8]))
|
2019-02-12 19:54:00 +00:00
|
|
|
|
if post.Client, err = s.GetClient(common.GetIntNoErrByStr(item[5])); err != nil {
|
2019-01-26 09:27:28 +00:00
|
|
|
|
continue
|
|
|
|
|
}
|
2019-03-23 14:19:59 +00:00
|
|
|
|
s.Tasks.Store(post.Id, post)
|
|
|
|
|
if post.Id > int(s.TaskIncreaseId) {
|
|
|
|
|
s.TaskIncreaseId = int32(s.TaskIncreaseId)
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-15 14:59:28 +00:00
|
|
|
|
}
|
2019-01-25 04:10:12 +00:00
|
|
|
|
|
2019-03-23 14:19:59 +00:00
|
|
|
|
func (s *Csv) GetIdByVerifyKey(vKey string, addr string) (id int, err error) {
|
|
|
|
|
var exist bool
|
|
|
|
|
s.Clients.Range(func(key, value interface{}) bool {
|
|
|
|
|
v := value.(*Client)
|
2019-02-09 09:07:47 +00:00
|
|
|
|
if common.Getverifyval(v.VerifyKey) == vKey && v.Status {
|
2019-03-23 14:19:59 +00:00
|
|
|
|
v.Addr = common.GetIpByAddr(addr)
|
|
|
|
|
id = v.Id
|
|
|
|
|
exist = true
|
|
|
|
|
return false
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
2019-03-23 14:19:59 +00:00
|
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
if exist {
|
|
|
|
|
return
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
return 0, errors.New("not found")
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-23 14:19:59 +00:00
|
|
|
|
func (s *Csv) NewTask(t *Tunnel) (err error) {
|
|
|
|
|
s.Tasks.Range(func(key, value interface{}) bool {
|
|
|
|
|
v := value.(*Tunnel)
|
2019-03-02 09:43:21 +00:00
|
|
|
|
if (v.Mode == "secret" || v.Mode == "p2p") && v.Password == t.Password {
|
2019-03-23 14:19:59 +00:00
|
|
|
|
err = errors.New(fmt.Sprintf("Secret mode keys %s must be unique", t.Password))
|
|
|
|
|
return false
|
2019-02-23 15:29:48 +00:00
|
|
|
|
}
|
2019-03-23 14:19:59 +00:00
|
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
2019-02-23 15:29:48 +00:00
|
|
|
|
}
|
2019-01-25 04:10:12 +00:00
|
|
|
|
t.Flow = new(Flow)
|
2019-03-23 14:19:59 +00:00
|
|
|
|
s.Tasks.Store(t.Id, t)
|
2019-01-25 04:10:12 +00:00
|
|
|
|
s.StoreTasksToCsv()
|
2019-03-23 14:19:59 +00:00
|
|
|
|
return
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-26 09:27:28 +00:00
|
|
|
|
func (s *Csv) UpdateTask(t *Tunnel) error {
|
2019-03-23 14:19:59 +00:00
|
|
|
|
s.Tasks.Store(t.Id, t)
|
|
|
|
|
s.StoreTasksToCsv()
|
|
|
|
|
return nil
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Csv) DelTask(id int) error {
|
2019-03-23 14:19:59 +00:00
|
|
|
|
s.Tasks.Delete(id)
|
|
|
|
|
s.StoreTasksToCsv()
|
|
|
|
|
return nil
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-23 15:29:48 +00:00
|
|
|
|
//md5 password
|
2019-03-23 14:19:59 +00:00
|
|
|
|
func (s *Csv) GetTaskByMd5Password(p string) (t *Tunnel) {
|
|
|
|
|
s.Tasks.Range(func(key, value interface{}) bool {
|
|
|
|
|
if crypt.Md5(value.(*Tunnel).Password) == p {
|
|
|
|
|
t = value.(*Tunnel)
|
|
|
|
|
return false
|
2019-02-23 15:29:48 +00:00
|
|
|
|
}
|
2019-03-23 14:19:59 +00:00
|
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
return
|
2019-02-23 15:29:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-23 14:19:59 +00:00
|
|
|
|
func (s *Csv) GetTask(id int) (t *Tunnel, err error) {
|
|
|
|
|
if v, ok := s.Tasks.Load(id); ok {
|
|
|
|
|
t = v.(*Tunnel)
|
|
|
|
|
return
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
2019-03-23 14:19:59 +00:00
|
|
|
|
err = errors.New("not found")
|
2019-01-25 04:10:12 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Csv) StoreHostToCsv() {
|
|
|
|
|
// 创建文件
|
2019-02-09 09:07:47 +00:00
|
|
|
|
csvFile, err := os.Create(filepath.Join(s.RunPath, "conf", "hosts.csv"))
|
2019-01-25 04:10:12 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
defer csvFile.Close()
|
|
|
|
|
// 获取csv的Writer
|
|
|
|
|
writer := csv.NewWriter(csvFile)
|
|
|
|
|
// 将map中的Post转换成slice,因为csv的Write需要slice参数
|
|
|
|
|
// 并写入csv文件
|
2019-03-23 14:19:59 +00:00
|
|
|
|
s.Hosts.Range(func(key, value interface{}) bool {
|
|
|
|
|
host := value.(*Host)
|
2019-02-12 19:54:00 +00:00
|
|
|
|
if host.NoStore {
|
2019-03-23 14:19:59 +00:00
|
|
|
|
return true
|
2019-02-12 19:54:00 +00:00
|
|
|
|
}
|
2019-01-25 04:10:12 +00:00
|
|
|
|
record := []string{
|
|
|
|
|
host.Host,
|
|
|
|
|
host.Target,
|
2019-01-26 09:27:28 +00:00
|
|
|
|
strconv.Itoa(host.Client.Id),
|
2019-01-25 04:10:12 +00:00
|
|
|
|
host.HeaderChange,
|
|
|
|
|
host.HostChange,
|
|
|
|
|
host.Remark,
|
2019-02-15 14:59:28 +00:00
|
|
|
|
host.Location,
|
|
|
|
|
strconv.Itoa(host.Id),
|
2019-02-23 15:29:48 +00:00
|
|
|
|
strconv.Itoa(int(host.Flow.ExportFlow)),
|
|
|
|
|
strconv.Itoa(int(host.Flow.InletFlow)),
|
2019-03-05 01:23:18 +00:00
|
|
|
|
host.Scheme,
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
err1 := writer.Write(record)
|
|
|
|
|
if err1 != nil {
|
|
|
|
|
panic(err1)
|
|
|
|
|
}
|
2019-03-23 14:19:59 +00:00
|
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
|
2019-01-25 04:10:12 +00:00
|
|
|
|
// 确保所有内存数据刷到csv文件
|
|
|
|
|
writer.Flush()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Csv) LoadClientFromCsv() {
|
2019-02-09 09:07:47 +00:00
|
|
|
|
path := filepath.Join(s.RunPath, "conf", "clients.csv")
|
2019-01-25 04:10:12 +00:00
|
|
|
|
records, err := s.openFile(path)
|
|
|
|
|
if err != nil {
|
2019-02-23 15:29:48 +00:00
|
|
|
|
logs.Error("Profile Opening Error:", path)
|
|
|
|
|
os.Exit(0)
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
// 将每一行数据保存到内存slice中
|
|
|
|
|
for _, item := range records {
|
|
|
|
|
post := &Client{
|
2019-02-09 09:07:47 +00:00
|
|
|
|
Id: common.GetIntNoErrByStr(item[0]),
|
2019-01-25 04:10:12 +00:00
|
|
|
|
VerifyKey: item[1],
|
2019-01-28 06:45:55 +00:00
|
|
|
|
Remark: item[2],
|
2019-02-09 09:07:47 +00:00
|
|
|
|
Status: common.GetBoolByStr(item[3]),
|
|
|
|
|
RateLimit: common.GetIntNoErrByStr(item[8]),
|
2019-01-26 09:27:28 +00:00
|
|
|
|
Cnf: &Config{
|
2019-01-28 06:45:55 +00:00
|
|
|
|
U: item[4],
|
|
|
|
|
P: item[5],
|
2019-02-09 09:07:47 +00:00
|
|
|
|
Crypt: common.GetBoolByStr(item[6]),
|
2019-03-01 09:23:14 +00:00
|
|
|
|
Compress: common.GetBoolByStr(item[7]),
|
2019-01-25 04:10:12 +00:00
|
|
|
|
},
|
2019-02-23 15:29:48 +00:00
|
|
|
|
MaxConn: common.GetIntNoErrByStr(item[10]),
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
2019-03-23 14:19:59 +00:00
|
|
|
|
if post.Id > int(s.ClientIncreaseId) {
|
|
|
|
|
s.ClientIncreaseId = int32(post.Id)
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
2019-01-28 06:45:55 +00:00
|
|
|
|
if post.RateLimit > 0 {
|
2019-02-09 09:07:47 +00:00
|
|
|
|
post.Rate = rate.NewRate(int64(post.RateLimit * 1024))
|
2019-01-28 06:45:55 +00:00
|
|
|
|
post.Rate.Start()
|
2019-03-26 03:13:07 +00:00
|
|
|
|
} else {
|
|
|
|
|
post.Rate = rate.NewRate(int64(2 << 23))
|
|
|
|
|
post.Rate.Start()
|
2019-01-28 06:45:55 +00:00
|
|
|
|
}
|
2019-01-25 04:10:12 +00:00
|
|
|
|
post.Flow = new(Flow)
|
2019-02-09 09:07:47 +00:00
|
|
|
|
post.Flow.FlowLimit = int64(common.GetIntNoErrByStr(item[9]))
|
2019-03-23 14:19:59 +00:00
|
|
|
|
if len(item) >= 12 {
|
|
|
|
|
post.ConfigConnAllow = common.GetBoolByStr(item[11])
|
|
|
|
|
} else {
|
|
|
|
|
post.ConfigConnAllow = true
|
|
|
|
|
}
|
|
|
|
|
s.Clients.Store(post.Id, post)
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Csv) LoadHostFromCsv() {
|
2019-02-09 09:07:47 +00:00
|
|
|
|
path := filepath.Join(s.RunPath, "conf", "hosts.csv")
|
2019-01-25 04:10:12 +00:00
|
|
|
|
records, err := s.openFile(path)
|
|
|
|
|
if err != nil {
|
2019-02-23 15:29:48 +00:00
|
|
|
|
logs.Error("Profile Opening Error:", path)
|
|
|
|
|
os.Exit(0)
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
// 将每一行数据保存到内存slice中
|
|
|
|
|
for _, item := range records {
|
2019-01-26 09:27:28 +00:00
|
|
|
|
post := &Host{
|
2019-01-25 04:10:12 +00:00
|
|
|
|
Host: item[0],
|
|
|
|
|
Target: item[1],
|
|
|
|
|
HeaderChange: item[3],
|
|
|
|
|
HostChange: item[4],
|
|
|
|
|
Remark: item[5],
|
2019-02-15 14:59:28 +00:00
|
|
|
|
Location: item[6],
|
|
|
|
|
Id: common.GetIntNoErrByStr(item[7]),
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
2019-02-09 09:07:47 +00:00
|
|
|
|
if post.Client, err = s.GetClient(common.GetIntNoErrByStr(item[2])); err != nil {
|
2019-01-26 09:27:28 +00:00
|
|
|
|
continue
|
|
|
|
|
}
|
2019-01-25 04:10:12 +00:00
|
|
|
|
post.Flow = new(Flow)
|
2019-02-23 15:29:48 +00:00
|
|
|
|
post.Flow.ExportFlow = int64(common.GetIntNoErrByStr(item[8]))
|
|
|
|
|
post.Flow.InletFlow = int64(common.GetIntNoErrByStr(item[9]))
|
2019-03-05 01:23:18 +00:00
|
|
|
|
if len(item) > 10 {
|
|
|
|
|
post.Scheme = item[10]
|
|
|
|
|
} else {
|
|
|
|
|
post.Scheme = "all"
|
|
|
|
|
}
|
2019-03-23 14:19:59 +00:00
|
|
|
|
s.Hosts.Store(post.Id, post)
|
|
|
|
|
if post.Id > int(s.HostIncreaseId) {
|
|
|
|
|
s.HostIncreaseId = int32(post.Id)
|
2019-02-15 14:59:28 +00:00
|
|
|
|
}
|
2019-03-23 14:19:59 +00:00
|
|
|
|
//store host to hostMap if the host url is none
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-15 14:59:28 +00:00
|
|
|
|
func (s *Csv) DelHost(id int) error {
|
2019-03-23 14:19:59 +00:00
|
|
|
|
s.Hosts.Delete(id)
|
|
|
|
|
s.StoreHostToCsv()
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Csv) GetMapLen(m sync.Map) int {
|
|
|
|
|
var c int
|
|
|
|
|
m.Range(func(key, value interface{}) bool {
|
|
|
|
|
c++
|
|
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
return c
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-16 12:43:26 +00:00
|
|
|
|
func (s *Csv) IsHostExist(h *Host) bool {
|
2019-03-23 14:19:59 +00:00
|
|
|
|
var exist bool
|
|
|
|
|
s.Hosts.Range(func(key, value interface{}) bool {
|
|
|
|
|
v := value.(*Host)
|
2019-03-05 01:23:18 +00:00
|
|
|
|
if v.Host == h.Host && h.Location == v.Location && (v.Scheme == "all" || v.Scheme == h.Scheme) {
|
2019-03-23 14:19:59 +00:00
|
|
|
|
exist = true
|
|
|
|
|
return false
|
2019-02-12 19:54:00 +00:00
|
|
|
|
}
|
2019-03-23 14:19:59 +00:00
|
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
return exist
|
2019-02-12 19:54:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-01 09:23:14 +00:00
|
|
|
|
func (s *Csv) NewHost(t *Host) error {
|
|
|
|
|
if s.IsHostExist(t) {
|
|
|
|
|
return errors.New("host has exist")
|
|
|
|
|
}
|
2019-03-02 09:43:21 +00:00
|
|
|
|
if t.Location == "" {
|
|
|
|
|
t.Location = "/"
|
|
|
|
|
}
|
2019-01-25 04:10:12 +00:00
|
|
|
|
t.Flow = new(Flow)
|
2019-03-23 14:19:59 +00:00
|
|
|
|
s.Hosts.Store(t.Id, t)
|
2019-01-25 04:10:12 +00:00
|
|
|
|
s.StoreHostToCsv()
|
2019-03-01 09:23:14 +00:00
|
|
|
|
return nil
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-23 14:19:59 +00:00
|
|
|
|
func (s *Csv) GetHost(start, length int, id int, search string) ([]*Host, int) {
|
2019-01-26 09:27:28 +00:00
|
|
|
|
list := make([]*Host, 0)
|
2019-01-25 04:10:12 +00:00
|
|
|
|
var cnt int
|
2019-03-23 14:19:59 +00:00
|
|
|
|
keys := common.GetMapKeys(s.Hosts)
|
|
|
|
|
for _, key := range keys {
|
|
|
|
|
if value, ok := s.Hosts.Load(key); ok {
|
|
|
|
|
v := value.(*Host)
|
|
|
|
|
if search != "" && !(v.Id == common.GetIntNoErrByStr(search) || strings.Contains(v.Host, search) || strings.Contains(v.Remark, search)) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if id == 0 || v.Client.Id == id {
|
|
|
|
|
cnt++
|
|
|
|
|
if start--; start < 0 {
|
|
|
|
|
if length--; length > 0 {
|
|
|
|
|
list = append(list, v)
|
|
|
|
|
}
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return list, cnt
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Csv) DelClient(id int) error {
|
2019-03-23 14:19:59 +00:00
|
|
|
|
s.Clients.Delete(id)
|
|
|
|
|
s.StoreClientsToCsv()
|
|
|
|
|
return nil
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-24 05:17:43 +00:00
|
|
|
|
func (s *Csv) NewClient(c *Client) error {
|
|
|
|
|
var isNotSet bool
|
|
|
|
|
reset:
|
|
|
|
|
if c.VerifyKey == "" || isNotSet {
|
|
|
|
|
isNotSet = true
|
|
|
|
|
c.VerifyKey = crypt.GetRandomString(16)
|
|
|
|
|
}
|
2019-03-26 03:13:07 +00:00
|
|
|
|
if c.RateLimit == 0 {
|
|
|
|
|
c.Rate = rate.NewRate(int64(2 << 23))
|
|
|
|
|
c.Rate.Start()
|
|
|
|
|
}
|
2019-02-24 05:17:43 +00:00
|
|
|
|
if !s.VerifyVkey(c.VerifyKey, c.id) {
|
|
|
|
|
if isNotSet {
|
|
|
|
|
goto reset
|
|
|
|
|
}
|
|
|
|
|
return errors.New("Vkey duplicate, please reset")
|
|
|
|
|
}
|
2019-02-12 19:54:00 +00:00
|
|
|
|
if c.Id == 0 {
|
2019-03-23 14:19:59 +00:00
|
|
|
|
c.Id = int(s.GetClientId())
|
2019-02-12 19:54:00 +00:00
|
|
|
|
}
|
2019-02-23 15:29:48 +00:00
|
|
|
|
if c.Flow == nil {
|
|
|
|
|
c.Flow = new(Flow)
|
|
|
|
|
}
|
2019-03-23 14:19:59 +00:00
|
|
|
|
s.Clients.Store(c.Id, c)
|
2019-01-25 04:10:12 +00:00
|
|
|
|
s.StoreClientsToCsv()
|
2019-02-24 05:17:43 +00:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-23 14:19:59 +00:00
|
|
|
|
func (s *Csv) VerifyVkey(vkey string, id int) (res bool) {
|
|
|
|
|
res = true
|
|
|
|
|
s.Clients.Range(func(key, value interface{}) bool {
|
|
|
|
|
v := value.(*Client)
|
2019-02-24 05:17:43 +00:00
|
|
|
|
if v.VerifyKey == vkey && v.Id != id {
|
2019-03-23 14:19:59 +00:00
|
|
|
|
res = false
|
2019-02-24 05:17:43 +00:00
|
|
|
|
return false
|
|
|
|
|
}
|
2019-03-23 14:19:59 +00:00
|
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Csv) GetClientId() int32 {
|
|
|
|
|
return atomic.AddInt32(&s.ClientIncreaseId, 1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Csv) GetTaskId() int32 {
|
|
|
|
|
return atomic.AddInt32(&s.TaskIncreaseId, 1)
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-23 14:19:59 +00:00
|
|
|
|
func (s *Csv) GetHostId() int32 {
|
|
|
|
|
return atomic.AddInt32(&s.HostIncreaseId, 1)
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Csv) UpdateClient(t *Client) error {
|
2019-03-23 14:19:59 +00:00
|
|
|
|
s.Clients.Store(t.Id, t)
|
2019-03-26 03:13:07 +00:00
|
|
|
|
if t.RateLimit == 0 {
|
|
|
|
|
t.Rate = rate.NewRate(int64(2 << 23))
|
|
|
|
|
t.Rate.Start()
|
|
|
|
|
}
|
2019-03-23 14:19:59 +00:00
|
|
|
|
return nil
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-23 14:19:59 +00:00
|
|
|
|
func (s *Csv) GetClientList(start, length int, search string) ([]*Client, int) {
|
2019-01-25 04:10:12 +00:00
|
|
|
|
list := make([]*Client, 0)
|
|
|
|
|
var cnt int
|
2019-03-23 14:19:59 +00:00
|
|
|
|
keys := common.GetMapKeys(s.Clients)
|
|
|
|
|
for _, key := range keys {
|
|
|
|
|
if value, ok := s.Clients.Load(key); ok {
|
|
|
|
|
v := value.(*Client)
|
|
|
|
|
if v.NoDisplay {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if search != "" && !(v.Id == common.GetIntNoErrByStr(search) || strings.Contains(v.VerifyKey, search) || strings.Contains(v.Remark, search)) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
cnt++
|
|
|
|
|
if start--; start < 0 {
|
|
|
|
|
if length--; length > 0 {
|
|
|
|
|
list = append(list, v)
|
|
|
|
|
}
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return list, cnt
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-23 14:19:59 +00:00
|
|
|
|
func (s *Csv) GetClient(id int) (c *Client, err error) {
|
|
|
|
|
if v, ok := s.Clients.Load(id); ok {
|
|
|
|
|
c = v.(*Client)
|
|
|
|
|
return
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
2019-02-12 19:54:00 +00:00
|
|
|
|
err = errors.New("未找到客户端")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
func (s *Csv) GetClientIdByVkey(vkey string) (id int, err error) {
|
2019-03-23 14:19:59 +00:00
|
|
|
|
var exist bool
|
|
|
|
|
s.Clients.Range(func(key, value interface{}) bool {
|
|
|
|
|
v := value.(*Client)
|
2019-03-02 09:43:21 +00:00
|
|
|
|
if crypt.Md5(v.VerifyKey) == vkey {
|
2019-03-23 14:19:59 +00:00
|
|
|
|
exist = true
|
2019-02-12 19:54:00 +00:00
|
|
|
|
id = v.Id
|
2019-03-23 14:19:59 +00:00
|
|
|
|
return false
|
2019-02-12 19:54:00 +00:00
|
|
|
|
}
|
2019-03-23 14:19:59 +00:00
|
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
if exist {
|
|
|
|
|
return
|
2019-02-12 19:54:00 +00:00
|
|
|
|
}
|
|
|
|
|
err = errors.New("未找到客户端")
|
2019-01-25 04:10:12 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
2019-02-09 09:07:47 +00:00
|
|
|
|
|
2019-02-15 14:59:28 +00:00
|
|
|
|
func (s *Csv) GetHostById(id int) (h *Host, err error) {
|
2019-03-23 14:19:59 +00:00
|
|
|
|
if v, ok := s.Hosts.Load(id); ok {
|
|
|
|
|
h = v.(*Host)
|
|
|
|
|
return
|
2019-02-12 19:54:00 +00:00
|
|
|
|
}
|
2019-02-15 14:59:28 +00:00
|
|
|
|
err = errors.New("The host could not be parsed")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//get key by host from x
|
|
|
|
|
func (s *Csv) GetInfoByHost(host string, r *http.Request) (h *Host, err error) {
|
|
|
|
|
var hosts []*Host
|
2019-02-23 15:29:48 +00:00
|
|
|
|
//Handling Ported Access
|
|
|
|
|
host = common.GetIpByAddr(host)
|
2019-03-23 14:19:59 +00:00
|
|
|
|
s.Hosts.Range(func(key, value interface{}) bool {
|
|
|
|
|
v := value.(*Host)
|
2019-03-15 06:03:49 +00:00
|
|
|
|
if v.IsClose {
|
2019-03-23 14:19:59 +00:00
|
|
|
|
return true
|
2019-03-15 06:03:49 +00:00
|
|
|
|
}
|
2019-02-15 14:59:28 +00:00
|
|
|
|
//Remove http(s) http(s)://a.proxy.com
|
|
|
|
|
//*.proxy.com *.a.proxy.com Do some pan-parsing
|
|
|
|
|
tmp := strings.Replace(v.Host, "*", `\w+?`, -1)
|
|
|
|
|
var re *regexp.Regexp
|
|
|
|
|
if re, err = regexp.Compile(tmp); err != nil {
|
2019-03-23 14:19:59 +00:00
|
|
|
|
return true
|
2019-02-15 14:59:28 +00:00
|
|
|
|
}
|
2019-03-05 01:23:18 +00:00
|
|
|
|
if len(re.FindAllString(host, -1)) > 0 && (v.Scheme == "all" || v.Scheme == r.URL.Scheme) {
|
2019-02-15 14:59:28 +00:00
|
|
|
|
//URL routing
|
|
|
|
|
hosts = append(hosts, v)
|
|
|
|
|
}
|
2019-03-23 14:19:59 +00:00
|
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
|
2019-02-15 14:59:28 +00:00
|
|
|
|
for _, v := range hosts {
|
|
|
|
|
//If not set, default matches all
|
|
|
|
|
if v.Location == "" {
|
|
|
|
|
v.Location = "/"
|
|
|
|
|
}
|
|
|
|
|
if strings.Index(r.RequestURI, v.Location) == 0 {
|
2019-03-20 05:47:25 +00:00
|
|
|
|
if h == nil || (len(v.Location) > len(h.Location)) {
|
2019-02-15 14:59:28 +00:00
|
|
|
|
h = v
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if h != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
err = errors.New("The host could not be parsed")
|
2019-02-12 19:54:00 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
2019-03-23 14:19:59 +00:00
|
|
|
|
|
2019-01-25 04:10:12 +00:00
|
|
|
|
func (s *Csv) StoreClientsToCsv() {
|
|
|
|
|
// 创建文件
|
2019-02-09 09:07:47 +00:00
|
|
|
|
csvFile, err := os.Create(filepath.Join(s.RunPath, "conf", "clients.csv"))
|
2019-01-25 04:10:12 +00:00
|
|
|
|
if err != nil {
|
2019-02-23 15:29:48 +00:00
|
|
|
|
logs.Error(err.Error())
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
defer csvFile.Close()
|
|
|
|
|
writer := csv.NewWriter(csvFile)
|
2019-03-23 14:19:59 +00:00
|
|
|
|
s.Clients.Range(func(key, value interface{}) bool {
|
|
|
|
|
client := value.(*Client)
|
2019-02-12 19:54:00 +00:00
|
|
|
|
if client.NoStore {
|
2019-03-23 14:19:59 +00:00
|
|
|
|
return true
|
2019-02-12 19:54:00 +00:00
|
|
|
|
}
|
2019-01-25 04:10:12 +00:00
|
|
|
|
record := []string{
|
|
|
|
|
strconv.Itoa(client.Id),
|
|
|
|
|
client.VerifyKey,
|
|
|
|
|
client.Remark,
|
|
|
|
|
strconv.FormatBool(client.Status),
|
|
|
|
|
client.Cnf.U,
|
|
|
|
|
client.Cnf.P,
|
2019-02-09 09:07:47 +00:00
|
|
|
|
common.GetStrByBool(client.Cnf.Crypt),
|
2019-03-01 09:23:14 +00:00
|
|
|
|
strconv.FormatBool(client.Cnf.Compress),
|
2019-01-28 06:45:55 +00:00
|
|
|
|
strconv.Itoa(client.RateLimit),
|
|
|
|
|
strconv.Itoa(int(client.Flow.FlowLimit)),
|
2019-02-23 15:29:48 +00:00
|
|
|
|
strconv.Itoa(int(client.MaxConn)),
|
2019-03-23 14:19:59 +00:00
|
|
|
|
common.GetStrByBool(client.ConfigConnAllow),
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
err := writer.Write(record)
|
|
|
|
|
if err != nil {
|
2019-02-23 15:29:48 +00:00
|
|
|
|
logs.Error(err.Error())
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
2019-03-23 14:19:59 +00:00
|
|
|
|
return true
|
|
|
|
|
})
|
2019-01-25 04:10:12 +00:00
|
|
|
|
writer.Flush()
|
|
|
|
|
}
|