Files

155 lines
3.9 KiB
Go

package proxy
import (
"errors"
"net"
"net/http"
"sort"
"sync"
"ehang.io/nps/bridge"
"ehang.io/nps/lib/common"
"ehang.io/nps/lib/conn"
"ehang.io/nps/lib/file"
"github.com/astaxie/beego/logs"
)
type Service interface {
Start() error
Close() error
}
type NetBridge interface {
SendLinkInfo(clientId int, link *conn.Link, t *file.Tunnel) (target net.Conn, err error)
}
// BaseServer struct
type BaseServer struct {
id int
bridge NetBridge
task *file.Tunnel
errorContent []byte
errorCode int
sync.Mutex
}
func NewBaseServer(bridge *bridge.Bridge, task *file.Tunnel) *BaseServer {
return &BaseServer{
bridge: bridge,
task: task,
errorContent: nil,
Mutex: sync.Mutex{},
}
}
// add the flow
func (s *BaseServer) FlowAdd(in, out int64) {
s.Lock()
defer s.Unlock()
s.task.Flow.ExportFlow += out
s.task.Flow.InletFlow += in
}
// change the flow
func (s *BaseServer) FlowAddHost(host *file.Host, in, out int64) {
s.Lock()
defer s.Unlock()
host.Flow.ExportFlow += out
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 for reverse-proxy hosts (401 + WWW-Authenticate).
func (s *BaseServer) auth(r *http.Request, c *conn.Conn, u, p string) error {
return s.doAuth(r, c, u, p, common.UnauthorizedBytes, "401 Unauthorized")
}
// proxyAuth check for HTTP forward proxy (407 + Proxy-Authenticate).
func (s *BaseServer) proxyAuth(r *http.Request, c *conn.Conn, u, p string) error {
return s.doAuth(r, c, u, p, common.ProxyAuthRequiredBytes, "407 Proxy Authentication Required")
}
func (s *BaseServer) doAuth(r *http.Request, c *conn.Conn, u, p, failBytes, errMsg string) error {
if u != "" && p != "" && !common.CheckAuth(r, u, p) {
c.Write([]byte(failBytes))
c.Close()
return errors.New(errMsg)
}
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")
}
if !client.GetConn() {
return errors.New("Connections exceed the current client limit")
}
return nil
}
func in(target string, str_array []string) bool {
sort.Strings(str_array)
index := sort.SearchStrings(str_array, target)
if index < len(str_array) && str_array[index] == target {
return true
}
return false
}
// 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, task *file.Tunnel, host *file.Host) error {
// 判断访问地址是否在全局黑名单内
if IsGlobalBlackIp(c.RemoteAddr().String()) {
c.Close()
return nil
}
// 判断访问地址是否在黑名单内
if common.IsBlackIp(c.RemoteAddr().String(), client.VerifyKey, client.BlackIpList) {
c.Close()
return nil
}
protoVersion := ""
if task != nil {
protoVersion = task.ProtoVersion
}
link := conn.NewLink(tp, addr, client.Cnf.Crypt, client.Cnf.Compress, c.Conn.RemoteAddr().String(), localProxy, protoVersion)
if target, err := s.bridge.SendLinkInfo(client.Id, link, s.task); err != nil {
logs.Warn("get connection from client id %d error %s", client.Id, err.Error())
c.Close()
return err
} else {
if f != nil {
f()
}
conn.CopyWaitGroup(target, c.Conn, link.Crypt, link.Compress, client.Rate, flow, true, rb, task, host)
}
return nil
}
// 判断访问地址是否在全局黑名单内
func IsGlobalBlackIp(ipPort string) bool {
// 判断访问地址是否在全局黑名单内
global := file.GetDb().GetGlobal()
if global != nil {
ip := common.GetIpByAddr(ipPort)
if in(ip, global.BlackIpList) {
logs.Error("IP地址[" + ip + "]在全局黑名单列表内")
return true
}
}
return false
}