mirror of
https://github.com/ehang-io/nps.git
synced 2025-09-01 10:56:53 +00:00
Url路由 泛解析
This commit is contained in:
@@ -200,4 +200,44 @@ func InIntArr(arr []int, val int) bool {
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func GetPorts(p string) []int {
|
||||
var ps []int
|
||||
arr := strings.Split(p, ",")
|
||||
for _, v := range arr {
|
||||
fw := strings.Split(v, "-")
|
||||
if len(fw) == 2 {
|
||||
if IsPort(fw[0]) && IsPort(fw[1]) {
|
||||
start, _ := strconv.Atoi(fw[0])
|
||||
end, _ := strconv.Atoi(fw[1])
|
||||
for i := start; i <= end; i++ {
|
||||
ps = append(ps, i)
|
||||
}
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
} else if IsPort(v) {
|
||||
p, _ := strconv.Atoi(v)
|
||||
ps = append(ps, p)
|
||||
}
|
||||
}
|
||||
return ps
|
||||
}
|
||||
func IsPort(p string) bool {
|
||||
pi, err := strconv.Atoi(p)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if pi > 65536 || pi < 1 {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func FormatAddress(s string) string {
|
||||
if strings.Contains(s, ":") {
|
||||
return s
|
||||
}
|
||||
return "127.0.0.1:" + s
|
||||
}
|
||||
|
@@ -136,7 +136,7 @@ func dealTunnel(s string) *file.Tunnel {
|
||||
}
|
||||
switch item[0] {
|
||||
case "port":
|
||||
t.Port = common.GetIntNoErrByStr(item[1])
|
||||
t.Ports = item[1]
|
||||
case "mode":
|
||||
t.Mode = item[1]
|
||||
case "target":
|
||||
|
@@ -82,7 +82,7 @@ func (s *Conn) ReadLen(cLen int) ([]byte, error) {
|
||||
defer pool.BufPoolMax.Put(buf)
|
||||
}
|
||||
if n, err := io.ReadFull(s, buf); err != nil || n != cLen {
|
||||
return buf, errors.New("读取指定长度错误" + err.Error())
|
||||
return buf, errors.New("Error reading specified length" + err.Error())
|
||||
}
|
||||
return buf, nil
|
||||
}
|
||||
@@ -368,7 +368,7 @@ func (s *Conn) SendTaskInfo(t *file.Tunnel) (int, error) {
|
||||
*/
|
||||
raw := bytes.NewBuffer([]byte{})
|
||||
binary.Write(raw, binary.LittleEndian, []byte(common.NEW_TASK))
|
||||
common.BinaryWrite(raw, t.Mode, strconv.Itoa(t.Port), t.Target, t.Remark)
|
||||
common.BinaryWrite(raw, t.Mode, t.Ports, t.Target, t.Remark)
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
return s.Write(raw.Bytes())
|
||||
@@ -386,7 +386,7 @@ func (s *Conn) GetTaskInfo() (t *file.Tunnel, err error) {
|
||||
arr := strings.Split(string(b), common.CONN_DATA_SEQ)
|
||||
t = new(file.Tunnel)
|
||||
t.Mode = arr[0]
|
||||
t.Port, _ = strconv.Atoi(arr[1])
|
||||
t.Ports = arr[1]
|
||||
t.Target = arr[2]
|
||||
t.Id = file.GetCsvDb().GetTaskId()
|
||||
t.Status = true
|
||||
|
@@ -18,7 +18,6 @@ type Link struct {
|
||||
UdpListener *net.UDPConn
|
||||
Rate *rate.Rate
|
||||
UdpRemoteAddr *net.UDPAddr
|
||||
Stop chan bool
|
||||
}
|
||||
|
||||
func NewLink(id int, connType string, host string, en, de int, crypt bool, c *Conn, flow *file.Flow, udpListener *net.UDPConn, rate *rate.Rate, UdpRemoteAddr *net.UDPAddr) *Link {
|
||||
|
@@ -6,8 +6,10 @@ import (
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/lib/lg"
|
||||
"github.com/cnlh/nps/lib/rate"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -27,6 +29,7 @@ type Csv struct {
|
||||
RunPath string //存储根目录
|
||||
ClientIncreaseId int //客户端id
|
||||
TaskIncreaseId int //任务自增ID
|
||||
HostIncreaseId int
|
||||
sync.Mutex
|
||||
}
|
||||
|
||||
@@ -119,6 +122,12 @@ func (s *Csv) GetTaskId() int {
|
||||
s.TaskIncreaseId++
|
||||
return s.TaskIncreaseId
|
||||
}
|
||||
func (s *Csv) GetHostId() int {
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
s.HostIncreaseId++
|
||||
return s.HostIncreaseId
|
||||
}
|
||||
|
||||
func (s *Csv) GetIdByVerifyKey(vKey string, addr string) (int, error) {
|
||||
s.Lock()
|
||||
@@ -195,6 +204,8 @@ func (s *Csv) StoreHostToCsv() {
|
||||
host.HeaderChange,
|
||||
host.HostChange,
|
||||
host.Remark,
|
||||
host.Location,
|
||||
strconv.Itoa(host.Id),
|
||||
}
|
||||
err1 := writer.Write(record)
|
||||
if err1 != nil {
|
||||
@@ -256,19 +267,24 @@ func (s *Csv) LoadHostFromCsv() {
|
||||
HeaderChange: item[3],
|
||||
HostChange: item[4],
|
||||
Remark: item[5],
|
||||
Location: item[6],
|
||||
Id: common.GetIntNoErrByStr(item[7]),
|
||||
}
|
||||
if post.Client, err = s.GetClient(common.GetIntNoErrByStr(item[2])); err != nil {
|
||||
continue
|
||||
}
|
||||
post.Flow = new(Flow)
|
||||
hosts = append(hosts, post)
|
||||
if post.Id > s.HostIncreaseId {
|
||||
s.HostIncreaseId = post.Id
|
||||
}
|
||||
}
|
||||
s.Hosts = hosts
|
||||
}
|
||||
|
||||
func (s *Csv) DelHost(host string) error {
|
||||
func (s *Csv) DelHost(id int) error {
|
||||
for k, v := range s.Hosts {
|
||||
if v.Host == host {
|
||||
if v.Id == id {
|
||||
s.Hosts = append(s.Hosts[:k], s.Hosts[k+1:]...)
|
||||
s.StoreHostToCsv()
|
||||
return nil
|
||||
@@ -287,9 +303,6 @@ func (s *Csv) IsHostExist(host string) bool {
|
||||
}
|
||||
|
||||
func (s *Csv) NewHost(t *Host) {
|
||||
if s.IsHostExist(t.Host) {
|
||||
return
|
||||
}
|
||||
t.Flow = new(Flow)
|
||||
s.Hosts = append(s.Hosts, t)
|
||||
s.StoreHostToCsv()
|
||||
@@ -367,7 +380,7 @@ func (s *Csv) UpdateClient(t *Client) error {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return errors.New("不存在")
|
||||
return errors.New("该客户端不存在")
|
||||
}
|
||||
|
||||
func (s *Csv) GetClientList(start, length int) ([]*Client, int) {
|
||||
@@ -407,16 +420,54 @@ func (s *Csv) GetClientIdByVkey(vkey string) (id int, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
//get key by host from x
|
||||
func (s *Csv) GetInfoByHost(host string) (h *Host, err error) {
|
||||
func (s *Csv) GetHostById(id int) (h *Host, err error) {
|
||||
for _, v := range s.Hosts {
|
||||
s := strings.Split(host, ":")
|
||||
if s[0] == v.Host {
|
||||
if v.Id == id {
|
||||
h = v
|
||||
return
|
||||
}
|
||||
}
|
||||
err = errors.New("未找到host对应的内网目标")
|
||||
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
|
||||
for _, v := range s.Hosts {
|
||||
//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 {
|
||||
return
|
||||
}
|
||||
if len(re.FindAllString(host, -1)) > 0 {
|
||||
//URL routing
|
||||
hosts = append(hosts, v)
|
||||
}
|
||||
}
|
||||
for _, v := range hosts {
|
||||
//If not set, default matches all
|
||||
if v.Location == "" {
|
||||
v.Location = "/"
|
||||
}
|
||||
if strings.Index(r.RequestURI, v.Location) == 0 {
|
||||
if h == nil || (len(v.Location) > len(h.Location)) {
|
||||
h = v
|
||||
}
|
||||
}
|
||||
}
|
||||
if h != nil {
|
||||
if h.Location != "/" {
|
||||
r.RequestURI = strings.Replace(r.RequestURI, h.Location, "", 1)
|
||||
}
|
||||
if r.RequestURI == "" {
|
||||
r.RequestURI = "/"
|
||||
}
|
||||
return
|
||||
}
|
||||
err = errors.New("The host could not be parsed")
|
||||
return
|
||||
}
|
||||
func (s *Csv) StoreClientsToCsv() {
|
||||
|
@@ -64,12 +64,13 @@ func (s *Client) GetId() int {
|
||||
|
||||
type Tunnel struct {
|
||||
Id int //Id
|
||||
Port int //服务端监听端口
|
||||
Port int //服务端监听端口
|
||||
Mode string //启动方式
|
||||
Target string //目标
|
||||
Status bool //设置是否开启
|
||||
RunStatus bool //当前运行状态
|
||||
Client *Client //所属客户端id
|
||||
Ports string //客户端与服务端传递
|
||||
Flow *Flow
|
||||
Remark string //备注
|
||||
NoStore bool
|
||||
@@ -85,10 +86,12 @@ type Config struct {
|
||||
}
|
||||
|
||||
type Host struct {
|
||||
Id int
|
||||
Host string //启动方式
|
||||
Target string //目标
|
||||
HeaderChange string //host修改
|
||||
HostChange string //host修改
|
||||
Location string //url 路由
|
||||
Flow *Flow
|
||||
Client *Client
|
||||
Remark string //备注
|
||||
|
Reference in New Issue
Block a user