Ip限制 npc代理连接

This commit is contained in:
刘河
2019-02-16 20:43:26 +08:00
parent 9f6b33a62b
commit 3b18d66835
64 changed files with 1414 additions and 132 deletions

View File

@@ -4,11 +4,11 @@ import (
"bufio"
"crypto/tls"
"github.com/cnlh/nps/bridge"
"github.com/cnlh/nps/lib/beego"
"github.com/cnlh/nps/lib/common"
"github.com/cnlh/nps/lib/conn"
"github.com/cnlh/nps/lib/file"
"github.com/cnlh/nps/lib/lg"
"github.com/cnlh/nps/vender/github.com/astaxie/beego"
"log"
"net/http"
"net/http/httputil"
@@ -117,14 +117,14 @@ func (s *httpServer) process(c *conn.Conn, r *http.Request) {
lastHost *file.Host
err error
)
if host, err = file.GetCsvDb().GetInfoByHost(r.Host, r); err != nil {
lg.Printf("the url %s %s Can't be parsed!", r.Host, r.RequestURI)
goto end
} else {
lastHost = host
}
for {
if host, err = file.GetCsvDb().GetInfoByHost(r.Host, r); err != nil {
lg.Printf("the url %s %s is not found !", r.Host, r.RequestURI)
break
} else if host != lastHost {
lastHost = host
isConn = true
}
start:
if isConn {
//流量限制
if host.Client.Flow.FlowLimit > 0 && (host.Client.Flow.FlowLimit<<20) < (host.Client.Flow.ExportFlow+host.Client.Flow.InletFlow) {
@@ -136,7 +136,7 @@ func (s *httpServer) process(c *conn.Conn, r *http.Request) {
break
}
lk = conn.NewLink(host.Client.GetId(), common.CONN_TCP, host.GetRandomTarget(), host.Client.Cnf.CompressEncode, host.Client.Cnf.CompressDecode, host.Client.Cnf.Crypt, c, host.Flow, nil, host.Client.Rate, nil)
if tunnel, err = s.bridge.SendLinkInfo(host.Client.Id, lk); err != nil {
if tunnel, err = s.bridge.SendLinkInfo(host.Client.Id, lk, c.Conn.RemoteAddr().String()); err != nil {
log.Println(err)
break
}
@@ -146,11 +146,18 @@ func (s *httpServer) process(c *conn.Conn, r *http.Request) {
if err != nil {
break
}
if host, err = file.GetCsvDb().GetInfoByHost(r.Host, r); err != nil {
lg.Printf("the url %s %s is not found !", r.Host, r.RequestURI)
break
} else if host != lastHost {
lastHost = host
isConn = true
goto start
}
}
//根据设定修改header和host
common.ChangeHostAndHeader(r, host.HostChange, host.HeaderChange, c.Conn.RemoteAddr().String())
b, err := httputil.DumpRequest(r, true)
lg.Println(string(b), r.RequestURI)
if err != nil {
break
}
@@ -160,7 +167,7 @@ func (s *httpServer) process(c *conn.Conn, r *http.Request) {
break
}
}
end:
if isConn {
s.writeConnFail(c.Conn)
} else {

View File

@@ -143,7 +143,7 @@ func (s *Sock5ModeServer) doConnect(c net.Conn, command uint8) {
}
link := conn.NewLink(s.task.Client.GetId(), ltype, addr, s.task.Client.Cnf.CompressEncode, s.task.Client.Cnf.CompressDecode, s.task.Client.Cnf.Crypt, conn.NewConn(c), s.task.Flow, nil, s.task.Client.Rate, nil)
if tunnel, err := s.bridge.SendLinkInfo(s.task.Client.Id, link); err != nil {
if tunnel, err := s.bridge.SendLinkInfo(s.task.Client.Id, link, c.RemoteAddr().String()); err != nil {
c.Close()
return
} else {
@@ -244,7 +244,7 @@ func (s *Sock5ModeServer) Auth(c net.Conn) error {
if _, err := io.ReadAtLeast(c, pass, passLen); err != nil {
return err
}
if string(pass) == s.task.Client.Cnf.U && string(user) == s.task.Client.Cnf.P {
if string(user) == s.task.Client.Cnf.U && string(pass) == s.task.Client.Cnf.P {
if _, err := c.Write([]byte{userAuthVersion, authSuccess}); err != nil {
return err
}
@@ -255,7 +255,6 @@ func (s *Sock5ModeServer) Auth(c net.Conn) error {
}
return errors.New("验证不通过")
}
return errors.New("未知错误")
}
//start

View File

@@ -3,11 +3,11 @@ package proxy
import (
"errors"
"github.com/cnlh/nps/bridge"
"github.com/cnlh/nps/lib/beego"
"github.com/cnlh/nps/lib/common"
"github.com/cnlh/nps/lib/conn"
"github.com/cnlh/nps/lib/file"
"github.com/cnlh/nps/lib/lg"
"github.com/cnlh/nps/vender/github.com/astaxie/beego"
"net"
"path/filepath"
"strings"
@@ -53,7 +53,7 @@ func (s *TunnelModeServer) Start() error {
func (s *TunnelModeServer) dealClient(c *conn.Conn, cnf *file.Config, addr string, method string, rb []byte) error {
link := conn.NewLink(s.task.Client.GetId(), common.CONN_TCP, addr, cnf.CompressEncode, cnf.CompressDecode, cnf.Crypt, c, s.task.Flow, nil, s.task.Client.Rate, nil)
if tunnel, err := s.bridge.SendLinkInfo(s.task.Client.Id, link); err != nil {
if tunnel, err := s.bridge.SendLinkInfo(s.task.Client.Id, link, c.Conn.RemoteAddr().String()); err != nil {
c.Close()
return err
} else {
@@ -105,8 +105,13 @@ func ProcessHttp(c *conn.Conn, s *TunnelModeServer) error {
method, addr, rb, err, r := c.GetHost()
if err != nil {
c.Close()
lg.Println(err)
return err
}
if r.Method == "CONNECT" {
c.Write([]byte("HTTP/1.1 200 Connection Established\r\n"))
rb = nil //reset
}
if err := s.auth(r, c, s.task.Client.Cnf.U, s.task.Client.Cnf.P); err != nil {
return err
}

View File

@@ -50,7 +50,7 @@ func (s *UdpModeServer) process(addr *net.UDPAddr, data []byte) {
if err := s.checkFlow(); err != nil {
return
}
if tunnel, err := s.bridge.SendLinkInfo(s.task.Client.Id, link); err != nil {
if tunnel, err := s.bridge.SendLinkInfo(s.task.Client.Id, link, addr.String()); err != nil {
return
} else {
s.task.Flow.Add(len(data), 0)