2018-12-11 08:37:12 +00:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
2019-02-09 09:07:47 +00:00
|
|
|
"github.com/cnlh/nps/lib/file"
|
2019-02-05 16:35:23 +00:00
|
|
|
"github.com/cnlh/nps/server"
|
2019-02-16 12:43:26 +00:00
|
|
|
"github.com/cnlh/nps/server/tool"
|
2018-12-11 08:37:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type IndexController struct {
|
|
|
|
BaseController
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IndexController) Index() {
|
2019-01-25 04:10:12 +00:00
|
|
|
s.Data["data"] = server.GetDashboardData()
|
|
|
|
s.SetInfo("dashboard")
|
2018-12-11 08:37:12 +00:00
|
|
|
s.display("index/index")
|
|
|
|
}
|
2019-01-25 04:10:12 +00:00
|
|
|
func (s *IndexController) Help() {
|
2019-03-01 09:23:14 +00:00
|
|
|
s.SetInfo("about")
|
2019-01-25 04:10:12 +00:00
|
|
|
s.display("index/help")
|
|
|
|
}
|
2018-12-11 08:37:12 +00:00
|
|
|
|
|
|
|
func (s *IndexController) Tcp() {
|
2019-03-01 09:23:14 +00:00
|
|
|
s.SetInfo("tcp")
|
|
|
|
s.SetType("tcp")
|
2018-12-11 08:37:12 +00:00
|
|
|
s.display("index/list")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IndexController) Udp() {
|
2019-03-01 09:23:14 +00:00
|
|
|
s.SetInfo("udp")
|
|
|
|
s.SetType("udp")
|
2018-12-11 08:37:12 +00:00
|
|
|
s.display("index/list")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IndexController) Socks5() {
|
2019-03-01 09:23:14 +00:00
|
|
|
s.SetInfo("socks5")
|
|
|
|
s.SetType("socks5")
|
2018-12-11 08:37:12 +00:00
|
|
|
s.display("index/list")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IndexController) Http() {
|
2019-03-01 09:23:14 +00:00
|
|
|
s.SetInfo("http proxy")
|
|
|
|
s.SetType("httpProxy")
|
2018-12-11 08:37:12 +00:00
|
|
|
s.display("index/list")
|
|
|
|
}
|
|
|
|
|
2019-02-23 15:29:48 +00:00
|
|
|
func (s *IndexController) Secret() {
|
2019-03-01 09:23:14 +00:00
|
|
|
s.SetInfo("secret")
|
|
|
|
s.SetType("secret")
|
|
|
|
s.display("index/list")
|
|
|
|
}
|
|
|
|
func (s *IndexController) P2p() {
|
|
|
|
s.SetInfo("p2p")
|
|
|
|
s.SetType("p2p")
|
2019-02-23 15:29:48 +00:00
|
|
|
s.display("index/list")
|
|
|
|
}
|
|
|
|
|
2018-12-11 08:37:12 +00:00
|
|
|
func (s *IndexController) Host() {
|
2019-03-01 09:23:14 +00:00
|
|
|
s.SetInfo("host")
|
2018-12-11 08:37:12 +00:00
|
|
|
s.SetType("hostServer")
|
|
|
|
s.display("index/list")
|
|
|
|
}
|
|
|
|
|
2019-01-25 04:10:12 +00:00
|
|
|
func (s *IndexController) All() {
|
|
|
|
s.Data["menu"] = "client"
|
|
|
|
clientId := s.GetString("client_id")
|
|
|
|
s.Data["client_id"] = clientId
|
2019-03-01 09:23:14 +00:00
|
|
|
s.SetInfo("client id:" + clientId)
|
2019-01-25 04:10:12 +00:00
|
|
|
s.display("index/list")
|
|
|
|
}
|
|
|
|
|
2019-01-26 09:27:28 +00:00
|
|
|
func (s *IndexController) GetTunnel() {
|
2018-12-11 08:37:12 +00:00
|
|
|
start, length := s.GetAjaxParams()
|
|
|
|
taskType := s.GetString("type")
|
2019-01-25 04:10:12 +00:00
|
|
|
clientId := s.GetIntNoErr("client_id")
|
2019-01-26 09:27:28 +00:00
|
|
|
list, cnt := server.GetTunnel(start, length, taskType, clientId)
|
2018-12-11 08:37:12 +00:00
|
|
|
s.AjaxTable(list, cnt, cnt)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IndexController) Add() {
|
|
|
|
if s.Ctx.Request.Method == "GET" {
|
|
|
|
s.Data["type"] = s.GetString("type")
|
2019-01-25 04:10:12 +00:00
|
|
|
s.Data["client_id"] = s.GetString("client_id")
|
2019-03-01 09:23:14 +00:00
|
|
|
s.SetInfo("add tunnel")
|
2018-12-11 08:37:12 +00:00
|
|
|
s.display()
|
|
|
|
} else {
|
2019-02-09 09:07:47 +00:00
|
|
|
t := &file.Tunnel{
|
2019-02-23 15:29:48 +00:00
|
|
|
Port: s.GetIntNoErr("port"),
|
|
|
|
Mode: s.GetString("type"),
|
|
|
|
Target: s.GetString("target"),
|
|
|
|
Id: file.GetCsvDb().GetTaskId(),
|
|
|
|
Status: true,
|
|
|
|
Remark: s.GetString("remark"),
|
|
|
|
Password: s.GetString("password"),
|
|
|
|
Flow: &file.Flow{},
|
2018-12-11 08:37:12 +00:00
|
|
|
}
|
2019-02-16 12:43:26 +00:00
|
|
|
if !tool.TestServerPort(t.Port, t.Mode) {
|
|
|
|
s.AjaxErr("The port cannot be opened because it may has been occupied or is no longer allowed.")
|
|
|
|
}
|
2019-01-26 09:27:28 +00:00
|
|
|
var err error
|
2019-02-09 09:07:47 +00:00
|
|
|
if t.Client, err = file.GetCsvDb().GetClient(s.GetIntNoErr("client_id")); err != nil {
|
2019-01-26 09:27:28 +00:00
|
|
|
s.AjaxErr(err.Error())
|
|
|
|
}
|
2019-02-09 09:07:47 +00:00
|
|
|
file.GetCsvDb().NewTask(t)
|
2019-01-09 12:33:00 +00:00
|
|
|
if err := server.AddTask(t); err != nil {
|
2018-12-11 08:37:12 +00:00
|
|
|
s.AjaxErr(err.Error())
|
|
|
|
} else {
|
2019-03-01 09:23:14 +00:00
|
|
|
s.AjaxOk("add success")
|
2018-12-11 08:37:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-29 03:20:39 +00:00
|
|
|
func (s *IndexController) GetOneTunnel() {
|
|
|
|
id := s.GetIntNoErr("id")
|
|
|
|
data := make(map[string]interface{})
|
2019-02-09 09:07:47 +00:00
|
|
|
if t, err := file.GetCsvDb().GetTask(id); err != nil {
|
2019-01-29 03:20:39 +00:00
|
|
|
data["code"] = 0
|
|
|
|
} else {
|
|
|
|
data["code"] = 1
|
|
|
|
data["data"] = t
|
|
|
|
}
|
|
|
|
s.Data["json"] = data
|
|
|
|
s.ServeJSON()
|
|
|
|
}
|
2018-12-11 08:37:12 +00:00
|
|
|
func (s *IndexController) Edit() {
|
2019-01-25 04:10:12 +00:00
|
|
|
id := s.GetIntNoErr("id")
|
2018-12-11 08:37:12 +00:00
|
|
|
if s.Ctx.Request.Method == "GET" {
|
2019-02-09 09:07:47 +00:00
|
|
|
if t, err := file.GetCsvDb().GetTask(id); err != nil {
|
2018-12-11 08:37:12 +00:00
|
|
|
s.error()
|
|
|
|
} else {
|
|
|
|
s.Data["t"] = t
|
|
|
|
}
|
2019-03-01 09:23:14 +00:00
|
|
|
s.SetInfo("edit tunnel")
|
2018-12-11 08:37:12 +00:00
|
|
|
s.display()
|
|
|
|
} else {
|
2019-02-09 09:07:47 +00:00
|
|
|
if t, err := file.GetCsvDb().GetTask(id); err != nil {
|
2018-12-11 08:37:12 +00:00
|
|
|
s.error()
|
|
|
|
} else {
|
2019-02-12 19:54:00 +00:00
|
|
|
t.Port = s.GetIntNoErr("port")
|
2018-12-11 08:37:12 +00:00
|
|
|
t.Mode = s.GetString("type")
|
|
|
|
t.Target = s.GetString("target")
|
2019-02-23 15:29:48 +00:00
|
|
|
t.Password = s.GetString("password")
|
2019-01-25 04:10:12 +00:00
|
|
|
t.Id = id
|
|
|
|
t.Remark = s.GetString("remark")
|
2019-02-09 09:07:47 +00:00
|
|
|
if t.Client, err = file.GetCsvDb().GetClient(s.GetIntNoErr("client_id")); err != nil {
|
2019-03-01 09:23:14 +00:00
|
|
|
s.AjaxErr("modified error")
|
2019-01-28 06:45:55 +00:00
|
|
|
}
|
2019-02-09 09:07:47 +00:00
|
|
|
file.GetCsvDb().UpdateTask(t)
|
2018-12-11 08:37:12 +00:00
|
|
|
}
|
2019-03-01 09:23:14 +00:00
|
|
|
s.AjaxOk("modified success")
|
2018-12-11 08:37:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IndexController) Stop() {
|
2019-01-25 04:10:12 +00:00
|
|
|
id := s.GetIntNoErr("id")
|
|
|
|
if err := server.StopServer(id); err != nil {
|
2019-03-01 09:23:14 +00:00
|
|
|
s.AjaxErr("stop error")
|
2018-12-11 08:37:12 +00:00
|
|
|
}
|
2019-03-01 09:23:14 +00:00
|
|
|
s.AjaxOk("stop success")
|
2018-12-11 08:37:12 +00:00
|
|
|
}
|
2019-01-25 04:10:12 +00:00
|
|
|
|
2018-12-11 08:37:12 +00:00
|
|
|
func (s *IndexController) Del() {
|
2019-01-25 04:10:12 +00:00
|
|
|
id := s.GetIntNoErr("id")
|
|
|
|
if err := server.DelTask(id); err != nil {
|
2019-03-01 09:23:14 +00:00
|
|
|
s.AjaxErr("delete error")
|
2018-12-11 08:37:12 +00:00
|
|
|
}
|
2019-03-01 09:23:14 +00:00
|
|
|
s.AjaxOk("delete success")
|
2018-12-11 08:37:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IndexController) Start() {
|
2019-01-25 04:10:12 +00:00
|
|
|
id := s.GetIntNoErr("id")
|
|
|
|
if err := server.StartTask(id); err != nil {
|
2019-03-01 09:23:14 +00:00
|
|
|
s.AjaxErr("start error")
|
2018-12-11 08:37:12 +00:00
|
|
|
}
|
2019-03-01 09:23:14 +00:00
|
|
|
s.AjaxOk("start success")
|
2018-12-11 08:37:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IndexController) HostList() {
|
|
|
|
if s.Ctx.Request.Method == "GET" {
|
2019-01-25 04:10:12 +00:00
|
|
|
s.Data["client_id"] = s.GetString("client_id")
|
|
|
|
s.Data["menu"] = "host"
|
2019-03-01 09:23:14 +00:00
|
|
|
s.SetInfo("host list")
|
2018-12-11 08:37:12 +00:00
|
|
|
s.display("index/hlist")
|
|
|
|
} else {
|
|
|
|
start, length := s.GetAjaxParams()
|
2019-01-25 04:10:12 +00:00
|
|
|
clientId := s.GetIntNoErr("client_id")
|
2019-02-09 09:07:47 +00:00
|
|
|
list, cnt := file.GetCsvDb().GetHost(start, length, clientId)
|
2018-12-11 08:37:12 +00:00
|
|
|
s.AjaxTable(list, cnt, cnt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-29 03:20:39 +00:00
|
|
|
func (s *IndexController) GetHost() {
|
|
|
|
if s.Ctx.Request.Method == "POST" {
|
|
|
|
data := make(map[string]interface{})
|
2019-02-15 14:59:28 +00:00
|
|
|
if h, err := file.GetCsvDb().GetHostById(s.GetIntNoErr("id")); err != nil {
|
2019-01-29 03:20:39 +00:00
|
|
|
data["code"] = 0
|
|
|
|
} else {
|
|
|
|
data["data"] = h
|
|
|
|
data["code"] = 1
|
|
|
|
}
|
|
|
|
s.Data["json"] = data
|
|
|
|
s.ServeJSON()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-11 08:37:12 +00:00
|
|
|
func (s *IndexController) DelHost() {
|
2019-02-15 14:59:28 +00:00
|
|
|
id := s.GetIntNoErr("id")
|
|
|
|
if err := file.GetCsvDb().DelHost(id); err != nil {
|
2019-03-01 09:23:14 +00:00
|
|
|
s.AjaxErr("delete error")
|
2018-12-11 08:37:12 +00:00
|
|
|
}
|
2019-03-01 09:23:14 +00:00
|
|
|
s.AjaxOk("delete success")
|
2018-12-11 08:37:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IndexController) AddHost() {
|
|
|
|
if s.Ctx.Request.Method == "GET" {
|
2019-01-25 04:10:12 +00:00
|
|
|
s.Data["client_id"] = s.GetString("client_id")
|
|
|
|
s.Data["menu"] = "host"
|
2019-03-01 09:23:14 +00:00
|
|
|
s.SetInfo("add host")
|
2018-12-11 08:37:12 +00:00
|
|
|
s.display("index/hadd")
|
|
|
|
} else {
|
2019-02-09 09:07:47 +00:00
|
|
|
h := &file.Host{
|
2019-02-15 14:59:28 +00:00
|
|
|
Id: file.GetCsvDb().GetHostId(),
|
2019-01-12 16:09:12 +00:00
|
|
|
Host: s.GetString("host"),
|
|
|
|
Target: s.GetString("target"),
|
|
|
|
HeaderChange: s.GetString("header"),
|
|
|
|
HostChange: s.GetString("hostchange"),
|
2019-01-25 04:10:12 +00:00
|
|
|
Remark: s.GetString("remark"),
|
2019-02-15 14:59:28 +00:00
|
|
|
Location: s.GetString("location"),
|
2019-02-09 09:07:47 +00:00
|
|
|
Flow: &file.Flow{},
|
2019-01-28 06:45:55 +00:00
|
|
|
}
|
|
|
|
var err error
|
2019-02-09 09:07:47 +00:00
|
|
|
if h.Client, err = file.GetCsvDb().GetClient(s.GetIntNoErr("client_id")); err != nil {
|
2019-03-01 09:23:14 +00:00
|
|
|
s.AjaxErr("add error")
|
|
|
|
}
|
|
|
|
if err := file.GetCsvDb().NewHost(h); err != nil {
|
|
|
|
s.AjaxErr("add fail" + err.Error())
|
2018-12-11 08:37:12 +00:00
|
|
|
}
|
2019-03-01 09:23:14 +00:00
|
|
|
s.AjaxOk("add success")
|
2018-12-11 08:37:12 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-12 16:09:12 +00:00
|
|
|
|
|
|
|
func (s *IndexController) EditHost() {
|
2019-02-15 14:59:28 +00:00
|
|
|
id := s.GetIntNoErr("id")
|
2019-01-12 16:09:12 +00:00
|
|
|
if s.Ctx.Request.Method == "GET" {
|
2019-01-25 04:10:12 +00:00
|
|
|
s.Data["menu"] = "host"
|
2019-02-15 14:59:28 +00:00
|
|
|
if h, err := file.GetCsvDb().GetHostById(id); err != nil {
|
2019-01-12 16:09:12 +00:00
|
|
|
s.error()
|
|
|
|
} else {
|
|
|
|
s.Data["h"] = h
|
|
|
|
}
|
2019-03-01 09:23:14 +00:00
|
|
|
s.SetInfo("edit")
|
2019-01-12 16:09:12 +00:00
|
|
|
s.display("index/hedit")
|
|
|
|
} else {
|
2019-02-15 14:59:28 +00:00
|
|
|
if h, err := file.GetCsvDb().GetHostById(id); err != nil {
|
2019-01-12 16:09:12 +00:00
|
|
|
s.error()
|
|
|
|
} else {
|
2019-02-15 14:59:28 +00:00
|
|
|
h.Host = s.GetString("host")
|
2019-01-12 16:09:12 +00:00
|
|
|
h.Target = s.GetString("target")
|
|
|
|
h.HeaderChange = s.GetString("header")
|
|
|
|
h.HostChange = s.GetString("hostchange")
|
2019-01-25 04:10:12 +00:00
|
|
|
h.Remark = s.GetString("remark")
|
2019-02-02 16:54:43 +00:00
|
|
|
h.TargetArr = nil
|
2019-02-15 14:59:28 +00:00
|
|
|
h.Location = s.GetString("location")
|
2019-02-09 09:07:47 +00:00
|
|
|
file.GetCsvDb().UpdateHost(h)
|
2019-01-28 06:45:55 +00:00
|
|
|
var err error
|
2019-02-09 09:07:47 +00:00
|
|
|
if h.Client, err = file.GetCsvDb().GetClient(s.GetIntNoErr("client_id")); err != nil {
|
2019-03-01 09:23:14 +00:00
|
|
|
s.AjaxErr("modified error")
|
2019-01-28 06:45:55 +00:00
|
|
|
}
|
2019-01-12 16:09:12 +00:00
|
|
|
}
|
2019-03-01 09:23:14 +00:00
|
|
|
s.AjaxOk("modified success")
|
2019-01-12 16:09:12 +00:00
|
|
|
}
|
|
|
|
}
|