nps/server/proxy/base.go

101 lines
2.5 KiB
Go
Raw Normal View History

2019-02-12 19:54:00 +00:00
package proxy
import (
"errors"
2019-08-10 03:15:25 +00:00
"net"
"net/http"
"sync"
2020-01-08 13:57:14 +00:00
"ehang.io/nps/bridge"
"ehang.io/nps/lib/common"
"ehang.io/nps/lib/conn"
"ehang.io/nps/lib/file"
2019-08-10 03:15:25 +00:00
"github.com/astaxie/beego/logs"
)
2019-02-17 17:05:05 +00:00
type Service interface {
Start() error
Close() error
}
2019-04-21 15:03:58 +00:00
type NetBridge interface {
SendLinkInfo(clientId int, link *conn.Link, t *file.Tunnel) (target net.Conn, err error)
}
2019-04-08 10:02:54 +00:00
//BaseServer struct
2019-02-23 15:29:48 +00:00
type BaseServer struct {
id int
2019-04-21 15:03:58 +00:00
bridge NetBridge
2019-02-09 09:07:47 +00:00
task *file.Tunnel
errorContent []byte
sync.Mutex
}
2019-02-23 15:29:48 +00:00
func NewBaseServer(bridge *bridge.Bridge, task *file.Tunnel) *BaseServer {
return &BaseServer{
bridge: bridge,
task: task,
errorContent: nil,
Mutex: sync.Mutex{},
}
}
2019-04-08 10:02:54 +00:00
//add the flow
2019-02-23 15:29:48 +00:00
func (s *BaseServer) 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-04-08 10:02:54 +00:00
//change the flow
2019-02-23 15:29:48 +00:00
func (s *BaseServer) FlowAddHost(host *file.Host, in, out int64) {
s.Lock()
defer s.Unlock()
host.Flow.ExportFlow += out
host.Flow.InletFlow += in
}
2019-04-08 10:02:54 +00:00
//write fail bytes to the connection
2019-02-23 15:29:48 +00:00
func (s *BaseServer) writeConnFail(c net.Conn) {
2019-02-09 09:07:47 +00:00
c.Write([]byte(common.ConnectionFailBytes))
c.Write(s.errorContent)
}
2019-04-08 10:02:54 +00:00
//auth check
2019-02-23 15:29:48 +00:00
func (s *BaseServer) auth(r *http.Request, c *conn.Conn, u, p string) error {
2019-02-09 09:07:47 +00:00
if u != "" && p != "" && !common.CheckAuth(r, u, p) {
c.Write([]byte(common.UnauthorizedBytes))
c.Close()
return errors.New("401 Unauthorized")
}
return nil
}
2019-02-12 19:54:00 +00:00
2019-04-08 10:02:54 +00:00
//check flow limit of the client ,and decrease the allow num of client
2019-03-23 14:19:59 +00:00
func (s *BaseServer) CheckFlowAndConnNum(client *file.Client) error {
if client.Flow.FlowLimit > 0 && (client.Flow.FlowLimit<<20) < (client.Flow.ExportFlow+client.Flow.InletFlow) {
2019-02-12 19:54:00 +00:00
return errors.New("Traffic exceeded")
}
2019-03-23 14:19:59 +00:00
if !client.GetConn() {
return errors.New("Connections exceed the current client limit")
}
2019-02-12 19:54:00 +00:00
return nil
}
2019-02-23 15:29:48 +00:00
2019-04-08 10:02:54 +00:00
//create a new connection and start bytes copying
2019-04-08 09:01:08 +00:00
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)
if target, err := s.bridge.SendLinkInfo(client.Id, link, s.task); err != nil {
2019-04-01 05:25:12 +00:00
logs.Warn("get connection from client id %d error %s", client.Id, err.Error())
2019-02-23 15:29:48 +00:00
c.Close()
return err
} else {
2019-03-23 14:19:59 +00:00
if f != nil {
f()
}
2019-03-25 10:39:31 +00:00
conn.CopyWaitGroup(target, c.Conn, link.Crypt, link.Compress, client.Rate, flow, true, rb)
2019-02-23 15:29:48 +00:00
}
return nil
}