2019-02-12 19:54:00 +00:00
|
|
|
package proxy
|
2019-01-25 04:10:12 +00:00
|
|
|
|
|
|
|
import (
|
2019-01-31 18:06:30 +00:00
|
|
|
"errors"
|
2019-02-03 04:40:43 +00:00
|
|
|
"github.com/cnlh/nps/bridge"
|
2019-02-09 09:07:47 +00:00
|
|
|
"github.com/cnlh/nps/lib/common"
|
|
|
|
"github.com/cnlh/nps/lib/conn"
|
|
|
|
"github.com/cnlh/nps/lib/file"
|
|
|
|
"github.com/cnlh/nps/lib/pool"
|
2019-01-31 18:06:30 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2019-01-25 04:10:12 +00:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2019-02-17 17:05:05 +00:00
|
|
|
type Service interface {
|
|
|
|
Start() error
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
2019-01-25 04:10:12 +00:00
|
|
|
//server base struct
|
|
|
|
type server struct {
|
2019-01-31 18:06:30 +00:00
|
|
|
id int
|
|
|
|
bridge *bridge.Bridge
|
2019-02-09 09:07:47 +00:00
|
|
|
task *file.Tunnel
|
2019-01-31 18:06:30 +00:00
|
|
|
errorContent []byte
|
2019-01-25 04:10:12 +00:00
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) FlowAdd(in, out int64) {
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
2019-01-26 09:27:28 +00:00
|
|
|
s.task.Flow.ExportFlow += out
|
|
|
|
s.task.Flow.InletFlow += in
|
2019-01-25 04:10:12 +00:00
|
|
|
}
|
|
|
|
|
2019-02-09 09:07:47 +00:00
|
|
|
func (s *server) FlowAddHost(host *file.Host, in, out int64) {
|
2019-01-25 04:10:12 +00:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
host.Flow.ExportFlow += out
|
|
|
|
host.Flow.InletFlow += in
|
|
|
|
}
|
|
|
|
|
2019-02-09 09:07:47 +00:00
|
|
|
func (s *server) linkCopy(link *conn.Link, c *conn.Conn, rb []byte, tunnel *conn.Conn, flow *file.Flow) {
|
2019-01-31 18:06:30 +00:00
|
|
|
if rb != nil {
|
|
|
|
if _, err := tunnel.SendMsg(rb, link); err != nil {
|
|
|
|
c.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
flow.Add(len(rb), 0)
|
|
|
|
}
|
2019-02-09 09:07:47 +00:00
|
|
|
|
|
|
|
buf := pool.BufPoolCopy.Get().([]byte)
|
2019-01-31 18:06:30 +00:00
|
|
|
for {
|
2019-02-12 19:54:00 +00:00
|
|
|
if err := s.checkFlow(); err != nil {
|
|
|
|
c.Close()
|
|
|
|
break
|
|
|
|
}
|
2019-01-31 18:06:30 +00:00
|
|
|
if n, err := c.Read(buf); err != nil {
|
2019-02-09 09:07:47 +00:00
|
|
|
tunnel.SendMsg([]byte(common.IO_EOF), link)
|
2019-01-31 18:06:30 +00:00
|
|
|
break
|
|
|
|
} else {
|
|
|
|
if _, err := tunnel.SendMsg(buf[:n], link); err != nil {
|
|
|
|
c.Close()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
flow.Add(n, 0)
|
|
|
|
}
|
2019-02-17 11:36:48 +00:00
|
|
|
<-link.StatusCh
|
2019-01-31 18:06:30 +00:00
|
|
|
}
|
2019-02-09 09:07:47 +00:00
|
|
|
pool.PutBufPoolCopy(buf)
|
2019-01-31 18:06:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) writeConnFail(c net.Conn) {
|
2019-02-09 09:07:47 +00:00
|
|
|
c.Write([]byte(common.ConnectionFailBytes))
|
2019-01-31 18:06:30 +00:00
|
|
|
c.Write(s.errorContent)
|
|
|
|
}
|
|
|
|
|
|
|
|
//权限认证
|
2019-02-09 09:07:47 +00:00
|
|
|
func (s *server) auth(r *http.Request, c *conn.Conn, u, p string) error {
|
|
|
|
if u != "" && p != "" && !common.CheckAuth(r, u, p) {
|
|
|
|
c.Write([]byte(common.UnauthorizedBytes))
|
2019-01-31 18:06:30 +00:00
|
|
|
c.Close()
|
|
|
|
return errors.New("401 Unauthorized")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-02-12 19:54:00 +00:00
|
|
|
|
|
|
|
func (s *server) checkFlow() error {
|
|
|
|
if s.task.Client.Flow.FlowLimit > 0 && (s.task.Client.Flow.FlowLimit<<20) < (s.task.Client.Flow.ExportFlow+s.task.Client.Flow.InletFlow) {
|
|
|
|
return errors.New("Traffic exceeded")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|