Skip to content

Commit 7ea9001

Browse files
author
刘河
committed
cache
1 parent 694ebc5 commit 7ea9001

File tree

9 files changed

+45
-35
lines changed

9 files changed

+45
-35
lines changed

conf/clients.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +0,0 @@
1-
{"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}
2-
*#*{"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}
3-
*#*{"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}
4-
*#*

conf/hosts.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +0,0 @@
1-
{"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}}
2-
*#*

conf/npc.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[common]
2-
server_addr=123.206.77.88:8024
2+
server_addr=127.0.0.1:8024
33
conn_type=tcp
44
vkey=123
55
auto_reconnection=true

conf/nps.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ allow_multi_ip=false
6464
system_info_display=false
6565

6666
#cache
67-
http_cache=true
67+
http_cache=false
6868
http_cache_length=100
6969

7070

conf/tasks.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
{"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":""}
2-
*#*{"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":""}
3-
*#*

lib/version/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package version
22

3-
const VERSION = "0.21.2"
3+
const VERSION = "0.22.0"
44

55
// Compulsory minimum version, Minimum downward compatibility to this version
66
func GetVersion() string {

server/proxy/base.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type Service interface {
1717
Close() error
1818
}
1919

20-
//Server BaseServer struct
20+
//BaseServer struct
2121
type BaseServer struct {
2222
id int
2323
bridge *bridge.Bridge
@@ -35,26 +35,29 @@ func NewBaseServer(bridge *bridge.Bridge, task *file.Tunnel) *BaseServer {
3535
}
3636
}
3737

38+
//add the flow
3839
func (s *BaseServer) FlowAdd(in, out int64) {
3940
s.Lock()
4041
defer s.Unlock()
4142
s.task.Flow.ExportFlow += out
4243
s.task.Flow.InletFlow += in
4344
}
4445

46+
//change the flow
4547
func (s *BaseServer) FlowAddHost(host *file.Host, in, out int64) {
4648
s.Lock()
4749
defer s.Unlock()
4850
host.Flow.ExportFlow += out
4951
host.Flow.InletFlow += in
5052
}
5153

54+
//write fail bytes to the connection
5255
func (s *BaseServer) writeConnFail(c net.Conn) {
5356
c.Write([]byte(common.ConnectionFailBytes))
5457
c.Write(s.errorContent)
5558
}
5659

57-
//权限认证
60+
//auth check
5861
func (s *BaseServer) auth(r *http.Request, c *conn.Conn, u, p string) error {
5962
if u != "" && p != "" && !common.CheckAuth(r, u, p) {
6063
c.Write([]byte(common.UnauthorizedBytes))
@@ -64,6 +67,7 @@ func (s *BaseServer) auth(r *http.Request, c *conn.Conn, u, p string) error {
6467
return nil
6568
}
6669

70+
//check flow limit of the client ,and decrease the allow num of client
6771
func (s *BaseServer) CheckFlowAndConnNum(client *file.Client) error {
6872
if client.Flow.FlowLimit > 0 && (client.Flow.FlowLimit<<20) < (client.Flow.ExportFlow+client.Flow.InletFlow) {
6973
return errors.New("Traffic exceeded")
@@ -74,7 +78,7 @@ func (s *BaseServer) CheckFlowAndConnNum(client *file.Client) error {
7478
return nil
7579
}
7680

77-
//与客户端建立通道
81+
//create a new connection and start bytes copying
7882
func (s *BaseServer) DealClient(c *conn.Conn, client *file.Client, addr string, rb []byte, tp string, f func(), flow *file.Flow, localProxy bool) error {
7983
link := conn.NewLink(tp, addr, client.Cnf.Crypt, client.Cnf.Compress, c.Conn.RemoteAddr().String(), localProxy)
8084
if target, err := s.bridge.SendLinkInfo(client.Id, link, s.task); err != nil {

server/proxy/http.go

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ func (s *httpServer) httpHandle(c *conn.Conn, r *http.Request) {
123123
lk *conn.Link
124124
targetAddr string
125125
readReq bool
126+
reqCh = make(chan *http.Request)
126127
)
127128
if host, err = file.GetDb().GetInfoByHost(r.Host, r); err != nil {
128129
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) {
156157
go func() {
157158
defer connClient.Close()
158159
defer c.Close()
159-
if resp, err := http.ReadResponse(bufio.NewReader(connClient), r); err != nil {
160-
return
161-
} else {
162-
//if the cache is start and the response is in the extension,store the response to the cache list
163-
if s.useCache && strings.Contains(r.URL.Path, ".") {
164-
b, err := httputil.DumpResponse(resp, true)
165-
if err != nil {
166-
return
167-
}
168-
c.Write(b)
169-
host.Flow.Add(0, int64(len(b)))
170-
s.cache.Add(filepath.Join(host.Host, r.URL.Path), b)
160+
for {
161+
r := <-reqCh
162+
if resp, err := http.ReadResponse(bufio.NewReader(connClient), r); err != nil {
163+
return
171164
} else {
172-
b, err := httputil.DumpResponse(resp, false)
173-
if err != nil {
174-
return
175-
}
176-
c.Write(b)
177-
if bodyLen, err := common.CopyBuffer(c, resp.Body); err != nil {
178-
return
165+
//if the cache is start and the response is in the extension,store the response to the cache list
166+
if s.useCache && strings.Contains(r.URL.Path, ".") {
167+
b, err := httputil.DumpResponse(resp, true)
168+
if err != nil {
169+
return
170+
}
171+
c.Write(b)
172+
host.Flow.Add(0, int64(len(b)))
173+
s.cache.Add(filepath.Join(host.Host, r.URL.Path), b)
179174
} else {
180-
host.Flow.Add(0, int64(len(b))+bodyLen)
175+
b, err := httputil.DumpResponse(resp, false)
176+
if err != nil {
177+
return
178+
}
179+
c.Write(b)
180+
if bodyLen, err := common.CopyBuffer(c, resp.Body); err != nil {
181+
return
182+
} else {
183+
host.Flow.Add(0, int64(len(b))+bodyLen)
184+
}
181185
}
182186
}
183187
}
@@ -242,6 +246,7 @@ func (s *httpServer) httpHandle(c *conn.Conn, r *http.Request) {
242246
} else {
243247
host.Flow.Add(int64(len(b))+bodyLen, 0)
244248
}
249+
reqCh <- r
245250
}
246251
end:
247252
if isConn {

server/proxy/https.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func NewHttpsServer(l net.Listener, bridge *bridge.Bridge, useCache bool, cacheL
3232
return https
3333
}
3434

35+
//start https server
3536
func (https *HttpsServer) Start() error {
3637
if b, err := beego.AppConfig.Bool("https_just_proxy"); err == nil && b {
3738
conn.Accept(https.listener, func(c net.Conn) {
@@ -86,16 +87,19 @@ func (https *HttpsServer) Start() error {
8687
return nil
8788
}
8889

90+
// close
8991
func (https *HttpsServer) Close() error {
9092
return https.listener.Close()
9193
}
9294

95+
// new https server by cert and key file
9396
func (https *HttpsServer) NewHttps(l net.Listener, certFile string, keyFile string) {
9497
go func() {
9598
logs.Error(https.NewServer(0, "https").ServeTLS(l, certFile, keyFile))
9699
}()
97100
}
98101

102+
//handle the https which is just proxy to other client
99103
func (https *HttpsServer) handleHttps(c net.Conn) {
100104
hostName, rb := GetServerNameFromClientHello(c)
101105
var targetAddr string
@@ -129,10 +133,12 @@ type HttpsListener struct {
129133
parentListener net.Listener
130134
}
131135

136+
// https listener
132137
func NewHttpsListener(l net.Listener) *HttpsListener {
133138
return &HttpsListener{parentListener: l, acceptConn: make(chan *conn.Conn)}
134139
}
135140

141+
// accept
136142
func (httpsListener *HttpsListener) Accept() (net.Conn, error) {
137143
httpsConn := <-httpsListener.acceptConn
138144
if httpsConn == nil {
@@ -141,14 +147,17 @@ func (httpsListener *HttpsListener) Accept() (net.Conn, error) {
141147
return httpsConn, nil
142148
}
143149

150+
// close
144151
func (httpsListener *HttpsListener) Close() error {
145152
return nil
146153
}
147154

155+
// addr
148156
func (httpsListener *HttpsListener) Addr() net.Addr {
149157
return httpsListener.parentListener.Addr()
150158
}
151159

160+
// get server name from connection by read client hello bytes
152161
func GetServerNameFromClientHello(c net.Conn) (string, []byte) {
153162
buf := make([]byte, 4096)
154163
data := make([]byte, 4096)
@@ -162,6 +171,7 @@ func GetServerNameFromClientHello(c net.Conn) (string, []byte) {
162171
return clientHello.GetServerName(), buf[:n]
163172
}
164173

174+
// build https request
165175
func buildHttpsRequest(hostName string) *http.Request {
166176
r := new(http.Request)
167177
r.RequestURI = "/"

0 commit comments

Comments
 (0)