很多修改

This commit is contained in:
刘河
2019-01-26 17:27:28 +08:00
parent c34e5e1a7d
commit 0b90bf3a18
22 changed files with 433 additions and 357 deletions

View File

@@ -22,7 +22,7 @@ type Flow struct {
}
type Client struct {
Cnf *ServerConfig
Cnf *Config
Id int //id
VerifyKey string //验证密钥
Addr string //客户端ip地址
@@ -32,35 +32,36 @@ type Client struct {
Flow *Flow
}
type ServerConfig struct {
TcpPort int //服务端与客户端通信端口
VerifyKey string
Mode string //启动方式
Target string //目标
type Tunnel struct {
Id int //Id
TcpPort int //服务端与客户端通信端口
Mode string //启动方式
Target string //目标
Status bool //是否开启
Client *Client //所属客户端id
Flow *Flow
Config *Config
UseClientCnf bool //是否继承客户端配置
Remark string //备注
}
type Config struct {
U string //socks5验证用户名
P string //socks5验证密码
Compress string //压缩方式
Start int //是否开启
IsRun int //是否在运行
ClientStatus int //客s户端状态
Crypt bool //是否加密
Mux bool //是否加密
CompressEncode int //加密方式
CompressDecode int //解密方式
Id int //Id
ClientId int //所属客户端id
UseClientCnf bool //是否继承客户端配置
Flow *Flow
Remark string //备注
}
type HostList struct {
ClientId int //服务端与客户端通信端口
type Host struct {
Host string //启动方式
Target string //目标
HeaderChange string //host修改
HostChange string //host修改
Flow *Flow
Client *Client
Remark string //备注
}
@@ -70,19 +71,19 @@ func NewCsv() *Csv {
}
type Csv struct {
Tasks []*ServerConfig
Tasks []*Tunnel
Path string
Hosts []*HostList //域名列表
Clients []*Client //客户端
ClientIncreaseId int //客户端id
TaskIncreaseId int //任务自增ID
Hosts []*Host //域名列表
Clients []*Client //客户端
ClientIncreaseId int //客户端id
TaskIncreaseId int //任务自增ID
sync.Mutex
}
func (s *Csv) Init() {
s.LoadClientFromCsv()
s.LoadTaskFromCsv()
s.LoadHostFromCsv()
s.LoadClientFromCsv()
}
func (s *Csv) StoreTasksToCsv() {
@@ -98,16 +99,16 @@ func (s *Csv) StoreTasksToCsv() {
strconv.Itoa(task.TcpPort),
task.Mode,
task.Target,
task.U,
task.P,
task.Compress,
strconv.Itoa(task.Start),
GetStrByBool(task.Crypt),
GetStrByBool(task.Mux),
strconv.Itoa(task.CompressEncode),
strconv.Itoa(task.CompressDecode),
task.Config.U,
task.Config.P,
task.Config.Compress,
utils.GetStrByBool(task.Status),
GetStrByBool(task.Config.Crypt),
GetStrByBool(task.Config.Mux),
strconv.Itoa(task.Config.CompressEncode),
strconv.Itoa(task.Config.CompressDecode),
strconv.Itoa(task.Id),
strconv.Itoa(task.ClientId),
strconv.Itoa(task.Client.Id),
strconv.FormatBool(task.UseClientCnf),
task.Remark,
}
@@ -143,27 +144,31 @@ func (s *Csv) LoadTaskFromCsv() {
if err != nil {
log.Fatal("配置文件打开错误:", path)
}
var tasks []*ServerConfig
var tasks []*Tunnel
// 将每一行数据保存到内存slice中
for _, item := range records {
post := &ServerConfig{
TcpPort: GetIntNoErrByStr(item[0]),
Mode: item[1],
Target: item[2],
U: item[3],
P: item[4],
Compress: item[5],
Start: GetIntNoErrByStr(item[6]),
Crypt: GetBoolByStr(item[7]),
Mux: GetBoolByStr(item[8]),
CompressEncode: GetIntNoErrByStr(item[9]),
CompressDecode: GetIntNoErrByStr(item[10]),
Id: GetIntNoErrByStr(item[11]),
ClientId: GetIntNoErrByStr(item[12]),
UseClientCnf: GetBoolByStr(item[13]),
Remark: item[14],
post := &Tunnel{
TcpPort: GetIntNoErrByStr(item[0]),
Mode: item[1],
Target: item[2],
Config: &Config{
U: item[3],
P: item[4],
Compress: item[5],
Crypt: GetBoolByStr(item[7]),
Mux: GetBoolByStr(item[8]),
CompressEncode: GetIntNoErrByStr(item[9]),
CompressDecode: GetIntNoErrByStr(item[10]),
},
Status: utils.GetBoolByStr(item[6]),
Id: GetIntNoErrByStr(item[11]),
UseClientCnf: GetBoolByStr(item[13]),
Remark: item[14],
}
post.Flow = new(Flow)
if post.Client, err = s.GetClient(GetIntNoErrByStr(item[12])); err != nil {
continue
}
tasks = append(tasks, post)
if post.Id > s.TaskIncreaseId {
s.TaskIncreaseId = post.Id
@@ -191,13 +196,13 @@ func (s *Csv) GetIdByVerifyKey(vKey string, addr string) (int, error) {
return 0, errors.New("not found")
}
func (s *Csv) NewTask(t *ServerConfig) {
func (s *Csv) NewTask(t *Tunnel) {
t.Flow = new(Flow)
s.Tasks = append(s.Tasks, t)
s.StoreTasksToCsv()
}
func (s *Csv) UpdateTask(t *ServerConfig) error {
func (s *Csv) UpdateTask(t *Tunnel) error {
for k, v := range s.Tasks {
if v.Id == t.Id {
s.Tasks = append(s.Tasks[:k], s.Tasks[k+1:]...)
@@ -220,7 +225,7 @@ func (s *Csv) DelTask(id int) error {
return errors.New("不存在")
}
func (s *Csv) GetTask(id int) (v *ServerConfig, err error) {
func (s *Csv) GetTask(id int) (v *Tunnel, err error) {
for _, v = range s.Tasks {
if v.Id == id {
return
@@ -245,7 +250,7 @@ func (s *Csv) StoreHostToCsv() {
record := []string{
host.Host,
host.Target,
strconv.Itoa(host.ClientId),
strconv.Itoa(host.Client.Id),
host.HeaderChange,
host.HostChange,
host.Remark,
@@ -274,7 +279,7 @@ func (s *Csv) LoadClientFromCsv() {
Addr: item[2],
Remark: item[3],
Status: GetBoolByStr(item[4]),
Cnf: &ServerConfig{
Cnf: &Config{
U: item[5],
P: item[6],
Crypt: GetBoolByStr(item[7]),
@@ -297,17 +302,19 @@ func (s *Csv) LoadHostFromCsv() {
if err != nil {
log.Fatal("配置文件打开错误:", path)
}
var hosts []*HostList
var hosts []*Host
// 将每一行数据保存到内存slice中
for _, item := range records {
post := &HostList{
ClientId: GetIntNoErrByStr(item[2]),
post := &Host{
Host: item[0],
Target: item[1],
HeaderChange: item[3],
HostChange: item[4],
Remark: item[5],
}
if post.Client, err = s.GetClient(GetIntNoErrByStr(item[2])); err != nil {
continue
}
post.Flow = new(Flow)
hosts = append(hosts, post)
}
@@ -325,14 +332,14 @@ func (s *Csv) DelHost(host string) error {
return errors.New("不存在")
}
func (s *Csv) NewHost(t *HostList) {
func (s *Csv) NewHost(t *Host) {
t.Flow = new(Flow)
s.Hosts = append(s.Hosts, t)
s.StoreHostToCsv()
}
func (s *Csv) UpdateHost(t *HostList) error {
func (s *Csv) UpdateHost(t *Host) error {
for k, v := range s.Hosts {
if v.Host == t.Host {
s.Hosts = append(s.Hosts[:k], s.Hosts[k+1:]...)
@@ -344,11 +351,11 @@ func (s *Csv) UpdateHost(t *HostList) error {
return errors.New("不存在")
}
func (s *Csv) GetHostList(start, length int, id int) ([]*HostList, int) {
list := make([]*HostList, 0)
func (s *Csv) GetHost(start, length int, id int) ([]*Host, int) {
list := make([]*Host, 0)
var cnt int
for _, v := range s.Hosts {
if id == 0 || v.ClientId == id {
if id == 0 || v.Client.Id == id {
cnt++
if start--; start < 0 {
if length--; length > 0 {
@@ -461,27 +468,15 @@ func GetCsvDb() *Csv {
return CsvDb
}
//深拷贝serverConfig
func DeepCopyConfig(c *ServerConfig) *ServerConfig {
return &ServerConfig{
TcpPort: c.TcpPort,
VerifyKey: c.VerifyKey,
Mode: c.Mode,
Target: c.Target,
//深拷贝Tunnel
func DeepCopyConfig(c *Config) *Config {
return &Config{
U: c.U,
P: c.P,
Compress: c.Compress,
Start: c.Start,
IsRun: c.IsRun,
ClientStatus: c.ClientStatus,
Crypt: c.Crypt,
Mux: c.Mux,
CompressEncode: c.CompressEncode,
CompressDecode: c.CompressDecode,
Id: c.Id,
ClientId: c.ClientId,
UseClientCnf: c.UseClientCnf,
Flow: c.Flow,
Remark: c.Remark,
}
}

View File

@@ -240,6 +240,5 @@ func ReadAllFromFile(filePth string) ([]byte, error) {
if err != nil {
return nil, err
}
return ioutil.ReadAll(f)
}