2019-02-09 09:07:47 +00:00
|
|
|
|
package file
|
2019-01-25 04:10:12 +00:00
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/csv"
|
|
|
|
|
"errors"
|
2019-02-09 09:07:47 +00:00
|
|
|
|
"github.com/cnlh/nps/lib/common"
|
|
|
|
|
"github.com/cnlh/nps/lib/lg"
|
|
|
|
|
"github.com/cnlh/nps/lib/rate"
|
2019-01-25 04:10:12 +00:00
|
|
|
|
"os"
|
2019-02-05 16:35:23 +00:00
|
|
|
|
"path/filepath"
|
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-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-01-26 09:27:28 +00:00
|
|
|
|
Tasks []*Tunnel
|
2019-01-25 04:10:12 +00:00
|
|
|
|
Path string
|
2019-01-26 09:27:28 +00:00
|
|
|
|
Hosts []*Host //域名列表
|
|
|
|
|
Clients []*Client //客户端
|
2019-02-09 09:07:47 +00:00
|
|
|
|
RunPath string //存储根目录
|
2019-01-26 09:27:28 +00:00
|
|
|
|
ClientIncreaseId int //客户端id
|
|
|
|
|
TaskIncreaseId int //任务自增ID
|
2019-01-25 04:10:12 +00:00
|
|
|
|
sync.Mutex
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Csv) Init() {
|
2019-01-26 09:27:28 +00:00
|
|
|
|
s.LoadClientFromCsv()
|
2019-01-25 04:10:12 +00:00
|
|
|
|
s.LoadTaskFromCsv()
|
|
|
|
|
s.LoadHostFromCsv()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-09 09:07:47 +00:00
|
|
|
|
lg.Fatalf(err.Error())
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
defer csvFile.Close()
|
|
|
|
|
writer := csv.NewWriter(csvFile)
|
|
|
|
|
for _, task := range s.Tasks {
|
2019-02-12 19:54:00 +00:00
|
|
|
|
if task.NoStore {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
lg.Println(task)
|
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,
|
|
|
|
|
}
|
|
|
|
|
err := writer.Write(record)
|
|
|
|
|
if err != nil {
|
2019-02-09 09:07:47 +00:00
|
|
|
|
lg.Fatalf(err.Error())
|
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-09 09:07:47 +00:00
|
|
|
|
lg.Fatalln("配置文件打开错误:", path)
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
2019-01-26 09:27:28 +00:00
|
|
|
|
var tasks []*Tunnel
|
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-12 19:54:00 +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],
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
post.Flow = new(Flow)
|
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-01-25 04:10:12 +00:00
|
|
|
|
tasks = append(tasks, post)
|
|
|
|
|
if post.Id > s.TaskIncreaseId {
|
|
|
|
|
s.TaskIncreaseId = post.Id
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
s.Tasks = tasks
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Csv) GetTaskId() int {
|
|
|
|
|
s.Lock()
|
|
|
|
|
defer s.Unlock()
|
|
|
|
|
s.TaskIncreaseId++
|
|
|
|
|
return s.TaskIncreaseId
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Csv) GetIdByVerifyKey(vKey string, addr string) (int, error) {
|
|
|
|
|
s.Lock()
|
|
|
|
|
defer s.Unlock()
|
|
|
|
|
for _, v := range s.Clients {
|
2019-02-09 09:07:47 +00:00
|
|
|
|
if common.Getverifyval(v.VerifyKey) == vKey && v.Status {
|
2019-01-28 06:45:55 +00:00
|
|
|
|
if arr := strings.Split(addr, ":"); len(arr) > 0 {
|
|
|
|
|
v.Addr = arr[0]
|
|
|
|
|
}
|
2019-01-25 04:10:12 +00:00
|
|
|
|
return v.Id, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0, errors.New("not found")
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-26 09:27:28 +00:00
|
|
|
|
func (s *Csv) NewTask(t *Tunnel) {
|
2019-01-25 04:10:12 +00:00
|
|
|
|
t.Flow = new(Flow)
|
|
|
|
|
s.Tasks = append(s.Tasks, t)
|
|
|
|
|
s.StoreTasksToCsv()
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-26 09:27:28 +00:00
|
|
|
|
func (s *Csv) UpdateTask(t *Tunnel) error {
|
2019-01-25 04:10:12 +00:00
|
|
|
|
for k, 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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return errors.New("不存在")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Csv) DelTask(id int) error {
|
|
|
|
|
for k, v := range s.Tasks {
|
|
|
|
|
if v.Id == id {
|
|
|
|
|
s.Tasks = append(s.Tasks[:k], s.Tasks[k+1:]...)
|
|
|
|
|
s.StoreTasksToCsv()
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return errors.New("不存在")
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-26 09:27:28 +00:00
|
|
|
|
func (s *Csv) GetTask(id int) (v *Tunnel, err error) {
|
2019-01-25 04:10:12 +00:00
|
|
|
|
for _, v = range s.Tasks {
|
|
|
|
|
if v.Id == id {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
err = errors.New("未找到")
|
|
|
|
|
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文件
|
|
|
|
|
for _, host := range s.Hosts {
|
2019-02-12 19:54:00 +00:00
|
|
|
|
if host.NoStore {
|
|
|
|
|
continue
|
|
|
|
|
}
|
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,
|
|
|
|
|
}
|
|
|
|
|
err1 := writer.Write(record)
|
|
|
|
|
if err1 != nil {
|
|
|
|
|
panic(err1)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 确保所有内存数据刷到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-09 09:07:47 +00:00
|
|
|
|
lg.Fatalln("配置文件打开错误:", path)
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
var clients []*Client
|
|
|
|
|
// 将每一行数据保存到内存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-01-31 18:06:30 +00:00
|
|
|
|
Compress: item[7],
|
2019-01-25 04:10:12 +00:00
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
if post.Id > s.ClientIncreaseId {
|
|
|
|
|
s.ClientIncreaseId = post.Id
|
|
|
|
|
}
|
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-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-01-25 04:10:12 +00:00
|
|
|
|
clients = append(clients, post)
|
|
|
|
|
}
|
|
|
|
|
s.Clients = clients
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-09 09:07:47 +00:00
|
|
|
|
lg.Fatalln("配置文件打开错误:", path)
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
2019-01-26 09:27:28 +00:00
|
|
|
|
var hosts []*Host
|
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-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)
|
|
|
|
|
hosts = append(hosts, post)
|
|
|
|
|
}
|
|
|
|
|
s.Hosts = hosts
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Csv) DelHost(host string) error {
|
|
|
|
|
for k, v := range s.Hosts {
|
|
|
|
|
if v.Host == host {
|
|
|
|
|
s.Hosts = append(s.Hosts[:k], s.Hosts[k+1:]...)
|
|
|
|
|
s.StoreHostToCsv()
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return errors.New("不存在")
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-12 19:54:00 +00:00
|
|
|
|
func (s *Csv) IsHostExist(host string) bool {
|
|
|
|
|
for _, v := range s.Hosts {
|
|
|
|
|
if v.Host == host {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-26 09:27:28 +00:00
|
|
|
|
func (s *Csv) NewHost(t *Host) {
|
2019-02-12 19:54:00 +00:00
|
|
|
|
if s.IsHostExist(t.Host) {
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-01-25 04:10:12 +00:00
|
|
|
|
t.Flow = new(Flow)
|
|
|
|
|
s.Hosts = append(s.Hosts, t)
|
|
|
|
|
s.StoreHostToCsv()
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-26 09:27:28 +00:00
|
|
|
|
func (s *Csv) UpdateHost(t *Host) error {
|
2019-01-25 04:10:12 +00:00
|
|
|
|
for k, 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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return errors.New("不存在")
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-26 09:27:28 +00:00
|
|
|
|
func (s *Csv) GetHost(start, length int, id int) ([]*Host, int) {
|
|
|
|
|
list := make([]*Host, 0)
|
2019-01-25 04:10:12 +00:00
|
|
|
|
var cnt int
|
|
|
|
|
for _, v := range s.Hosts {
|
2019-01-26 09:27:28 +00:00
|
|
|
|
if id == 0 || v.Client.Id == id {
|
2019-01-25 04:10:12 +00:00
|
|
|
|
cnt++
|
|
|
|
|
if start--; start < 0 {
|
|
|
|
|
if length--; length > 0 {
|
|
|
|
|
list = append(list, v)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return list, cnt
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Csv) DelClient(id int) error {
|
|
|
|
|
for k, v := range s.Clients {
|
|
|
|
|
if v.Id == id {
|
|
|
|
|
s.Clients = append(s.Clients[:k], s.Clients[k+1:]...)
|
|
|
|
|
s.StoreClientsToCsv()
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return errors.New("不存在")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Csv) NewClient(c *Client) {
|
2019-02-12 19:54:00 +00:00
|
|
|
|
if c.Id == 0 {
|
|
|
|
|
c.Id = s.GetClientId()
|
|
|
|
|
}
|
|
|
|
|
c.Flow = new(Flow)
|
2019-01-25 04:10:12 +00:00
|
|
|
|
s.Lock()
|
|
|
|
|
defer s.Unlock()
|
|
|
|
|
s.Clients = append(s.Clients, c)
|
|
|
|
|
s.StoreClientsToCsv()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Csv) GetClientId() int {
|
|
|
|
|
s.Lock()
|
|
|
|
|
defer s.Unlock()
|
|
|
|
|
s.ClientIncreaseId++
|
|
|
|
|
return s.ClientIncreaseId
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Csv) UpdateClient(t *Client) error {
|
|
|
|
|
s.Lock()
|
|
|
|
|
defer s.Unlock()
|
2019-01-31 18:06:30 +00:00
|
|
|
|
for _, v := range s.Clients {
|
2019-01-25 04:10:12 +00:00
|
|
|
|
if v.Id == t.Id {
|
2019-01-31 18:06:30 +00:00
|
|
|
|
v.Cnf = t.Cnf
|
|
|
|
|
v.VerifyKey = t.VerifyKey
|
|
|
|
|
v.Remark = t.Remark
|
|
|
|
|
v.RateLimit = t.RateLimit
|
|
|
|
|
v.Flow = t.Flow
|
|
|
|
|
v.Rate = t.Rate
|
2019-01-25 04:10:12 +00:00
|
|
|
|
s.StoreClientsToCsv()
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return errors.New("不存在")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Csv) GetClientList(start, length int) ([]*Client, int) {
|
|
|
|
|
list := make([]*Client, 0)
|
|
|
|
|
var cnt int
|
|
|
|
|
for _, v := range s.Clients {
|
2019-02-12 19:54:00 +00:00
|
|
|
|
if v.NoDisplay {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2019-01-25 04:10:12 +00:00
|
|
|
|
cnt++
|
|
|
|
|
if start--; start < 0 {
|
|
|
|
|
if length--; length > 0 {
|
|
|
|
|
list = append(list, v)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return list, cnt
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Csv) GetClient(id int) (v *Client, err error) {
|
|
|
|
|
for _, v = range s.Clients {
|
|
|
|
|
if v.Id == id {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-12 19:54:00 +00:00
|
|
|
|
err = errors.New("未找到客户端")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
func (s *Csv) GetClientIdByVkey(vkey string) (id int, err error) {
|
|
|
|
|
for _, v := range s.Clients {
|
|
|
|
|
if v.VerifyKey == vkey {
|
|
|
|
|
id = v.Id
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
err = errors.New("未找到客户端")
|
2019-01-25 04:10:12 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
2019-02-09 09:07:47 +00:00
|
|
|
|
|
2019-02-12 19:54:00 +00:00
|
|
|
|
//get key by host from x
|
|
|
|
|
func (s *Csv) GetInfoByHost(host string) (h *Host, err error) {
|
|
|
|
|
for _, v := range s.Hosts {
|
|
|
|
|
s := strings.Split(host, ":")
|
|
|
|
|
if s[0] == v.Host {
|
|
|
|
|
h = v
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
err = errors.New("未找到host对应的内网目标")
|
|
|
|
|
return
|
|
|
|
|
}
|
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-09 09:07:47 +00:00
|
|
|
|
lg.Fatalln(err.Error())
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
defer csvFile.Close()
|
|
|
|
|
writer := csv.NewWriter(csvFile)
|
|
|
|
|
for _, client := range s.Clients {
|
2019-02-12 19:54:00 +00:00
|
|
|
|
if client.NoStore {
|
|
|
|
|
continue
|
|
|
|
|
}
|
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-01-25 04:10:12 +00:00
|
|
|
|
client.Cnf.Compress,
|
2019-01-28 06:45:55 +00:00
|
|
|
|
strconv.Itoa(client.RateLimit),
|
|
|
|
|
strconv.Itoa(int(client.Flow.FlowLimit)),
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
err := writer.Write(record)
|
|
|
|
|
if err != nil {
|
2019-02-09 09:07:47 +00:00
|
|
|
|
lg.Fatalln(err.Error())
|
2019-01-25 04:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
writer.Flush()
|
|
|
|
|
}
|