mirror of
https://github.com/ehang-io/nps.git
synced 2025-07-02 04:00:42 +00:00
cache
This commit is contained in:
parent
694ebc5477
commit
7ea9001aa4
@ -1,4 +0,0 @@
|
|||||||
{"Cnf":{"U":"","P":"","Compress":false,"Crypt":false},"Id":4,"VerifyKey":"6h7x7tjvkocgltep","Addr":"","Remark":"","Status":true,"IsConnect":false,"RateLimit":0,"Flow":{"ExportFlow":0,"InletFlow":0,"FlowLimit":0},"Rate":{"NowRate":0},"NoStore":false,"NoDisplay":false,"MaxConn":0,"NowConn":0,"WebUserName":"admin22","WebPassword":"123","ConfigConnAllow":false,"MaxTunnelNum":0}
|
|
||||||
*#*{"Cnf":{"U":"","P":"","Compress":false,"Crypt":false},"Id":6,"VerifyKey":"xf3nwghskyw4e7g4","Addr":"","Remark":"","Status":true,"IsConnect":false,"RateLimit":0,"Flow":{"ExportFlow":0,"InletFlow":0,"FlowLimit":0},"Rate":{"NowRate":0},"NoStore":false,"NoDisplay":false,"MaxConn":0,"NowConn":0,"WebUserName":"admin3","WebPassword":"123","ConfigConnAllow":false,"MaxTunnelNum":0}
|
|
||||||
*#*{"Cnf":{"U":"","P":"","Compress":false,"Crypt":false},"Id":2,"VerifyKey":"06j3twjj9vjy2kdg","Addr":"","Remark":"","Status":true,"IsConnect":false,"RateLimit":0,"Flow":{"ExportFlow":2298489,"InletFlow":92324,"FlowLimit":0},"Rate":{"NowRate":0},"NoStore":false,"NoDisplay":false,"MaxConn":0,"NowConn":0,"WebUserName":"admin55","WebPassword":"123","ConfigConnAllow":false,"MaxTunnelNum":0}
|
|
||||||
*#*
|
|
@ -1,2 +0,0 @@
|
|||||||
{"Id":1,"Host":"a.o.com","HeaderChange":"","HostChange":"","Location":"/","Remark":"","Scheme":"all","CertFilePath":"","KeyFilePath":"","NoStore":false,"IsClose":false,"Flow":{"ExportFlow":2298489,"InletFlow":92303,"FlowLimit":0},"Client":{"Cnf":{"U":"","P":"","Compress":false,"Crypt":false},"Id":2,"VerifyKey":"06j3twjj9vjy2kdg","Addr":"127.0.0.1","Remark":"","Status":true,"IsConnect":true,"RateLimit":0,"Flow":{"ExportFlow":2298489,"InletFlow":92324,"FlowLimit":0},"Rate":{"NowRate":0},"NoStore":false,"NoDisplay":false,"MaxConn":0,"NowConn":4,"WebUserName":"admin2","WebPassword":"123","ConfigConnAllow":false,"MaxTunnelNum":0},"Target":{"TargetStr":"127.0.0.1:8082","TargetArr":null,"LocalProxy":false}}
|
|
||||||
*#*
|
|
@ -1,5 +1,5 @@
|
|||||||
[common]
|
[common]
|
||||||
server_addr=123.206.77.88:8024
|
server_addr=127.0.0.1:8024
|
||||||
conn_type=tcp
|
conn_type=tcp
|
||||||
vkey=123
|
vkey=123
|
||||||
auto_reconnection=true
|
auto_reconnection=true
|
||||||
|
@ -64,7 +64,7 @@ allow_multi_ip=false
|
|||||||
system_info_display=false
|
system_info_display=false
|
||||||
|
|
||||||
#cache
|
#cache
|
||||||
http_cache=true
|
http_cache=false
|
||||||
http_cache_length=100
|
http_cache_length=100
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,3 +0,0 @@
|
|||||||
{"Id":1,"Port":0,"ServerIp":"","Mode":"p2p","Status":true,"RunStatus":true,"Client":{"Cnf":{"U":"","P":"","Compress":false,"Crypt":false},"Id":2,"VerifyKey":"06j3twjj9vjy2kdg","Addr":"127.0.0.1","Remark":"","Status":true,"IsConnect":true,"RateLimit":0,"Flow":{"ExportFlow":0,"InletFlow":0,"FlowLimit":0},"Rate":{"NowRate":0},"NoStore":false,"NoDisplay":false,"MaxConn":0,"NowConn":1,"WebUserName":"admin2","WebPassword":"123","ConfigConnAllow":false,"MaxTunnelNum":0},"Ports":"","Flow":{"ExportFlow":0,"InletFlow":0,"FlowLimit":0},"Password":"p2ptest","Remark":"","TargetAddr":"","NoStore":false,"LocalPath":"","StripPre":"","Target":{"TargetStr":"","TargetArr":null,"LocalProxy":false},"HealthCheckTimeout":0,"HealthMaxFail":0,"HealthCheckInterval":0,"HealthNextTime":"0001-01-01T00:00:00Z","HealthMap":null,"HttpHealthUrl":"","HealthRemoveArr":null,"HealthCheckType":"","HealthCheckTarget":""}
|
|
||||||
*#*{"Id":2,"Port":0,"ServerIp":"","Mode":"secret","Status":true,"RunStatus":true,"Client":{"Cnf":{"U":"","P":"","Compress":false,"Crypt":false},"Id":2,"VerifyKey":"06j3twjj9vjy2kdg","Addr":"127.0.0.1","Remark":"","Status":true,"IsConnect":true,"RateLimit":0,"Flow":{"ExportFlow":0,"InletFlow":0,"FlowLimit":0},"Rate":{"NowRate":0},"NoStore":false,"NoDisplay":false,"MaxConn":0,"NowConn":1,"WebUserName":"admin2","WebPassword":"123","ConfigConnAllow":false,"MaxTunnelNum":0},"Ports":"","Flow":{"ExportFlow":0,"InletFlow":21,"FlowLimit":0},"Password":"secrettest","Remark":"","TargetAddr":"","NoStore":false,"LocalPath":"","StripPre":"","Target":{"TargetStr":"118.89.159.126:22","TargetArr":null,"LocalProxy":false},"HealthCheckTimeout":0,"HealthMaxFail":0,"HealthCheckInterval":0,"HealthNextTime":"0001-01-01T00:00:00Z","HealthMap":null,"HttpHealthUrl":"","HealthRemoveArr":null,"HealthCheckType":"","HealthCheckTarget":""}
|
|
||||||
*#*
|
|
@ -1,6 +1,6 @@
|
|||||||
package version
|
package version
|
||||||
|
|
||||||
const VERSION = "0.21.2"
|
const VERSION = "0.22.0"
|
||||||
|
|
||||||
// Compulsory minimum version, Minimum downward compatibility to this version
|
// Compulsory minimum version, Minimum downward compatibility to this version
|
||||||
func GetVersion() string {
|
func GetVersion() string {
|
||||||
|
@ -17,7 +17,7 @@ type Service interface {
|
|||||||
Close() error
|
Close() error
|
||||||
}
|
}
|
||||||
|
|
||||||
//Server BaseServer struct
|
//BaseServer struct
|
||||||
type BaseServer struct {
|
type BaseServer struct {
|
||||||
id int
|
id int
|
||||||
bridge *bridge.Bridge
|
bridge *bridge.Bridge
|
||||||
@ -35,6 +35,7 @@ func NewBaseServer(bridge *bridge.Bridge, task *file.Tunnel) *BaseServer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//add the flow
|
||||||
func (s *BaseServer) FlowAdd(in, out int64) {
|
func (s *BaseServer) FlowAdd(in, out int64) {
|
||||||
s.Lock()
|
s.Lock()
|
||||||
defer s.Unlock()
|
defer s.Unlock()
|
||||||
@ -42,6 +43,7 @@ func (s *BaseServer) FlowAdd(in, out int64) {
|
|||||||
s.task.Flow.InletFlow += in
|
s.task.Flow.InletFlow += in
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//change the flow
|
||||||
func (s *BaseServer) FlowAddHost(host *file.Host, in, out int64) {
|
func (s *BaseServer) FlowAddHost(host *file.Host, in, out int64) {
|
||||||
s.Lock()
|
s.Lock()
|
||||||
defer s.Unlock()
|
defer s.Unlock()
|
||||||
@ -49,12 +51,13 @@ func (s *BaseServer) FlowAddHost(host *file.Host, in, out int64) {
|
|||||||
host.Flow.InletFlow += in
|
host.Flow.InletFlow += in
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//write fail bytes to the connection
|
||||||
func (s *BaseServer) writeConnFail(c net.Conn) {
|
func (s *BaseServer) writeConnFail(c net.Conn) {
|
||||||
c.Write([]byte(common.ConnectionFailBytes))
|
c.Write([]byte(common.ConnectionFailBytes))
|
||||||
c.Write(s.errorContent)
|
c.Write(s.errorContent)
|
||||||
}
|
}
|
||||||
|
|
||||||
//权限认证
|
//auth check
|
||||||
func (s *BaseServer) auth(r *http.Request, c *conn.Conn, u, p string) error {
|
func (s *BaseServer) auth(r *http.Request, c *conn.Conn, u, p string) error {
|
||||||
if u != "" && p != "" && !common.CheckAuth(r, u, p) {
|
if u != "" && p != "" && !common.CheckAuth(r, u, p) {
|
||||||
c.Write([]byte(common.UnauthorizedBytes))
|
c.Write([]byte(common.UnauthorizedBytes))
|
||||||
@ -64,6 +67,7 @@ func (s *BaseServer) auth(r *http.Request, c *conn.Conn, u, p string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//check flow limit of the client ,and decrease the allow num of client
|
||||||
func (s *BaseServer) CheckFlowAndConnNum(client *file.Client) error {
|
func (s *BaseServer) CheckFlowAndConnNum(client *file.Client) error {
|
||||||
if client.Flow.FlowLimit > 0 && (client.Flow.FlowLimit<<20) < (client.Flow.ExportFlow+client.Flow.InletFlow) {
|
if client.Flow.FlowLimit > 0 && (client.Flow.FlowLimit<<20) < (client.Flow.ExportFlow+client.Flow.InletFlow) {
|
||||||
return errors.New("Traffic exceeded")
|
return errors.New("Traffic exceeded")
|
||||||
@ -74,7 +78,7 @@ func (s *BaseServer) CheckFlowAndConnNum(client *file.Client) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//与客户端建立通道
|
//create a new connection and start bytes copying
|
||||||
func (s *BaseServer) DealClient(c *conn.Conn, client *file.Client, addr string, rb []byte, tp string, f func(), flow *file.Flow, localProxy bool) error {
|
func (s *BaseServer) DealClient(c *conn.Conn, client *file.Client, addr string, rb []byte, tp string, f func(), flow *file.Flow, localProxy bool) error {
|
||||||
link := conn.NewLink(tp, addr, client.Cnf.Crypt, client.Cnf.Compress, c.Conn.RemoteAddr().String(), localProxy)
|
link := conn.NewLink(tp, addr, client.Cnf.Crypt, client.Cnf.Compress, c.Conn.RemoteAddr().String(), localProxy)
|
||||||
if target, err := s.bridge.SendLinkInfo(client.Id, link, s.task); err != nil {
|
if target, err := s.bridge.SendLinkInfo(client.Id, link, s.task); err != nil {
|
||||||
|
@ -123,6 +123,7 @@ func (s *httpServer) httpHandle(c *conn.Conn, r *http.Request) {
|
|||||||
lk *conn.Link
|
lk *conn.Link
|
||||||
targetAddr string
|
targetAddr string
|
||||||
readReq bool
|
readReq bool
|
||||||
|
reqCh = make(chan *http.Request)
|
||||||
)
|
)
|
||||||
if host, err = file.GetDb().GetInfoByHost(r.Host, r); err != nil {
|
if host, err = file.GetDb().GetInfoByHost(r.Host, r); err != nil {
|
||||||
logs.Notice("the url %s %s %s can't be parsed!", r.URL.Scheme, r.Host, r.RequestURI)
|
logs.Notice("the url %s %s %s can't be parsed!", r.URL.Scheme, r.Host, r.RequestURI)
|
||||||
@ -156,28 +157,31 @@ func (s *httpServer) httpHandle(c *conn.Conn, r *http.Request) {
|
|||||||
go func() {
|
go func() {
|
||||||
defer connClient.Close()
|
defer connClient.Close()
|
||||||
defer c.Close()
|
defer c.Close()
|
||||||
if resp, err := http.ReadResponse(bufio.NewReader(connClient), r); err != nil {
|
for {
|
||||||
return
|
r := <-reqCh
|
||||||
} else {
|
if resp, err := http.ReadResponse(bufio.NewReader(connClient), r); err != nil {
|
||||||
//if the cache is start and the response is in the extension,store the response to the cache list
|
return
|
||||||
if s.useCache && strings.Contains(r.URL.Path, ".") {
|
|
||||||
b, err := httputil.DumpResponse(resp, true)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
c.Write(b)
|
|
||||||
host.Flow.Add(0, int64(len(b)))
|
|
||||||
s.cache.Add(filepath.Join(host.Host, r.URL.Path), b)
|
|
||||||
} else {
|
} else {
|
||||||
b, err := httputil.DumpResponse(resp, false)
|
//if the cache is start and the response is in the extension,store the response to the cache list
|
||||||
if err != nil {
|
if s.useCache && strings.Contains(r.URL.Path, ".") {
|
||||||
return
|
b, err := httputil.DumpResponse(resp, true)
|
||||||
}
|
if err != nil {
|
||||||
c.Write(b)
|
return
|
||||||
if bodyLen, err := common.CopyBuffer(c, resp.Body); err != nil {
|
}
|
||||||
return
|
c.Write(b)
|
||||||
|
host.Flow.Add(0, int64(len(b)))
|
||||||
|
s.cache.Add(filepath.Join(host.Host, r.URL.Path), b)
|
||||||
} else {
|
} else {
|
||||||
host.Flow.Add(0, int64(len(b))+bodyLen)
|
b, err := httputil.DumpResponse(resp, false)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.Write(b)
|
||||||
|
if bodyLen, err := common.CopyBuffer(c, resp.Body); err != nil {
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
host.Flow.Add(0, int64(len(b))+bodyLen)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -242,6 +246,7 @@ func (s *httpServer) httpHandle(c *conn.Conn, r *http.Request) {
|
|||||||
} else {
|
} else {
|
||||||
host.Flow.Add(int64(len(b))+bodyLen, 0)
|
host.Flow.Add(int64(len(b))+bodyLen, 0)
|
||||||
}
|
}
|
||||||
|
reqCh <- r
|
||||||
}
|
}
|
||||||
end:
|
end:
|
||||||
if isConn {
|
if isConn {
|
||||||
|
@ -32,6 +32,7 @@ func NewHttpsServer(l net.Listener, bridge *bridge.Bridge, useCache bool, cacheL
|
|||||||
return https
|
return https
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//start https server
|
||||||
func (https *HttpsServer) Start() error {
|
func (https *HttpsServer) Start() error {
|
||||||
if b, err := beego.AppConfig.Bool("https_just_proxy"); err == nil && b {
|
if b, err := beego.AppConfig.Bool("https_just_proxy"); err == nil && b {
|
||||||
conn.Accept(https.listener, func(c net.Conn) {
|
conn.Accept(https.listener, func(c net.Conn) {
|
||||||
@ -86,16 +87,19 @@ func (https *HttpsServer) Start() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// close
|
||||||
func (https *HttpsServer) Close() error {
|
func (https *HttpsServer) Close() error {
|
||||||
return https.listener.Close()
|
return https.listener.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// new https server by cert and key file
|
||||||
func (https *HttpsServer) NewHttps(l net.Listener, certFile string, keyFile string) {
|
func (https *HttpsServer) NewHttps(l net.Listener, certFile string, keyFile string) {
|
||||||
go func() {
|
go func() {
|
||||||
logs.Error(https.NewServer(0, "https").ServeTLS(l, certFile, keyFile))
|
logs.Error(https.NewServer(0, "https").ServeTLS(l, certFile, keyFile))
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//handle the https which is just proxy to other client
|
||||||
func (https *HttpsServer) handleHttps(c net.Conn) {
|
func (https *HttpsServer) handleHttps(c net.Conn) {
|
||||||
hostName, rb := GetServerNameFromClientHello(c)
|
hostName, rb := GetServerNameFromClientHello(c)
|
||||||
var targetAddr string
|
var targetAddr string
|
||||||
@ -129,10 +133,12 @@ type HttpsListener struct {
|
|||||||
parentListener net.Listener
|
parentListener net.Listener
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https listener
|
||||||
func NewHttpsListener(l net.Listener) *HttpsListener {
|
func NewHttpsListener(l net.Listener) *HttpsListener {
|
||||||
return &HttpsListener{parentListener: l, acceptConn: make(chan *conn.Conn)}
|
return &HttpsListener{parentListener: l, acceptConn: make(chan *conn.Conn)}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// accept
|
||||||
func (httpsListener *HttpsListener) Accept() (net.Conn, error) {
|
func (httpsListener *HttpsListener) Accept() (net.Conn, error) {
|
||||||
httpsConn := <-httpsListener.acceptConn
|
httpsConn := <-httpsListener.acceptConn
|
||||||
if httpsConn == nil {
|
if httpsConn == nil {
|
||||||
@ -141,14 +147,17 @@ func (httpsListener *HttpsListener) Accept() (net.Conn, error) {
|
|||||||
return httpsConn, nil
|
return httpsConn, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// close
|
||||||
func (httpsListener *HttpsListener) Close() error {
|
func (httpsListener *HttpsListener) Close() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// addr
|
||||||
func (httpsListener *HttpsListener) Addr() net.Addr {
|
func (httpsListener *HttpsListener) Addr() net.Addr {
|
||||||
return httpsListener.parentListener.Addr()
|
return httpsListener.parentListener.Addr()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get server name from connection by read client hello bytes
|
||||||
func GetServerNameFromClientHello(c net.Conn) (string, []byte) {
|
func GetServerNameFromClientHello(c net.Conn) (string, []byte) {
|
||||||
buf := make([]byte, 4096)
|
buf := make([]byte, 4096)
|
||||||
data := make([]byte, 4096)
|
data := make([]byte, 4096)
|
||||||
@ -162,6 +171,7 @@ func GetServerNameFromClientHello(c net.Conn) (string, []byte) {
|
|||||||
return clientHello.GetServerName(), buf[:n]
|
return clientHello.GetServerName(), buf[:n]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// build https request
|
||||||
func buildHttpsRequest(hostName string) *http.Request {
|
func buildHttpsRequest(hostName string) *http.Request {
|
||||||
r := new(http.Request)
|
r := new(http.Request)
|
||||||
r.RequestURI = "/"
|
r.RequestURI = "/"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user