This commit is contained in:
刘河
2019-04-08 18:02:54 +08:00
parent 694ebc5477
commit 7ea9001aa4
9 changed files with 45 additions and 35 deletions

View File

@@ -17,7 +17,7 @@ type Service interface {
Close() error
}
//Server BaseServer struct
//BaseServer struct
type BaseServer struct {
id int
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) {
s.Lock()
defer s.Unlock()
@@ -42,6 +43,7 @@ func (s *BaseServer) FlowAdd(in, out int64) {
s.task.Flow.InletFlow += in
}
//change the flow
func (s *BaseServer) FlowAddHost(host *file.Host, in, out int64) {
s.Lock()
defer s.Unlock()
@@ -49,12 +51,13 @@ func (s *BaseServer) FlowAddHost(host *file.Host, in, out int64) {
host.Flow.InletFlow += in
}
//write fail bytes to the connection
func (s *BaseServer) writeConnFail(c net.Conn) {
c.Write([]byte(common.ConnectionFailBytes))
c.Write(s.errorContent)
}
//权限认证
//auth check
func (s *BaseServer) auth(r *http.Request, c *conn.Conn, u, p string) error {
if u != "" && p != "" && !common.CheckAuth(r, u, p) {
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
}
//check flow limit of the client ,and decrease the allow num of client
func (s *BaseServer) CheckFlowAndConnNum(client *file.Client) error {
if client.Flow.FlowLimit > 0 && (client.Flow.FlowLimit<<20) < (client.Flow.ExportFlow+client.Flow.InletFlow) {
return errors.New("Traffic exceeded")
@@ -74,7 +78,7 @@ func (s *BaseServer) CheckFlowAndConnNum(client *file.Client) error {
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 {
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 {