nps/server/process.go

112 lines
2.9 KiB
Go
Raw Normal View History

2019-01-15 12:59:50 +00:00
package server
import (
"bufio"
"github.com/cnlh/easyProxy/utils"
2019-01-28 06:45:55 +00:00
"github.com/pkg/errors"
2019-01-15 12:59:50 +00:00
"log"
"net/http"
"net/http/httputil"
"sync"
)
type process func(c *utils.Conn, s *TunnelModeServer) error
//tcp隧道模式
func ProcessTunnel(c *utils.Conn, s *TunnelModeServer) error {
2019-01-28 06:45:55 +00:00
if !s.ResetConfig() {
c.Close()
return errors.New("流量超出")
}
2019-01-26 09:27:28 +00:00
return s.dealClient(c, s.config, s.task.Target, "", nil)
2019-01-15 12:59:50 +00:00
}
//http代理模式
func ProcessHttp(c *utils.Conn, s *TunnelModeServer) error {
2019-01-28 06:45:55 +00:00
if !s.ResetConfig() {
c.Close()
return errors.New("流量超出")
}
2019-01-15 12:59:50 +00:00
method, addr, rb, err, r := c.GetHost()
if err != nil {
log.Println(err)
c.Close()
return err
}
if err := s.auth(r, c, s.config.U, s.config.P); err != nil {
return err
}
return s.dealClient(c, s.config, addr, method, rb)
}
//多客户端域名代理
func ProcessHost(c *utils.Conn, s *TunnelModeServer) error {
var (
isConn = true
link *utils.Conn
2019-01-26 09:27:28 +00:00
host *utils.Host
2019-01-15 12:59:50 +00:00
wg sync.WaitGroup
)
for {
r, err := http.ReadRequest(bufio.NewReader(c))
if err != nil {
break
}
//首次获取conn
if isConn {
2019-01-26 09:27:28 +00:00
if host, err = GetInfoByHost(r.Host); err != nil {
2019-01-15 12:59:50 +00:00
log.Printf("the host %s is not found !", r.Host)
break
}
2019-01-28 06:45:55 +00:00
//流量限制
if host.Client.Flow.FlowLimit > 0 && (host.Client.Flow.FlowLimit<<20) < (host.Client.Flow.ExportFlow+host.Client.Flow.InletFlow) {
break
}
2019-01-26 09:27:28 +00:00
host.Client.Cnf.CompressDecode, host.Client.Cnf.CompressEncode = utils.GetCompressType(host.Client.Cnf.Compress)
2019-01-28 06:45:55 +00:00
//权限控制
2019-01-26 09:27:28 +00:00
if err = s.auth(r, c, host.Client.Cnf.U, host.Client.Cnf.P); err != nil {
2019-01-15 12:59:50 +00:00
break
}
2019-01-26 09:27:28 +00:00
if link, err = s.GetTunnelAndWriteHost(utils.CONN_TCP, host.Client.Id, host.Client.Cnf, host.Target); err != nil {
2019-01-15 12:59:50 +00:00
log.Println("get bridge tunnel error: ", err)
break
}
if flag, err := link.ReadFlag(); err != nil || flag == utils.CONN_ERROR {
log.Printf("the host %s connection to %s error", r.Host, host.Target)
break
} else {
wg.Add(1)
go func() {
2019-01-28 06:45:55 +00:00
out, _ := utils.Relay(c.Conn, link.Conn, host.Client.Cnf.CompressDecode, host.Client.Cnf.Crypt, host.Client.Cnf.Mux, host.Client.Rate)
2019-01-15 12:59:50 +00:00
wg.Done()
s.FlowAddHost(host, 0, out)
2019-01-15 12:59:50 +00:00
}()
}
isConn = false
2019-01-15 12:59:50 +00:00
}
2019-01-26 09:27:28 +00:00
//根据设定修改header和host
2019-01-15 12:59:50 +00:00
utils.ChangeHostAndHeader(r, host.HostChange, host.HeaderChange, c.Conn.RemoteAddr().String())
b, err := httputil.DumpRequest(r, true)
if err != nil {
break
}
2019-01-26 09:27:28 +00:00
s.FlowAddHost(host, int64(len(b)), 0)
2019-01-28 06:45:55 +00:00
if _, err := link.WriteTo(b, host.Client.Cnf.CompressEncode, host.Client.Cnf.Crypt, host.Client.Rate); err != nil {
2019-01-15 12:59:50 +00:00
break
}
}
wg.Wait()
2019-01-26 09:27:28 +00:00
if host != nil && host.Client.Cnf != nil && host.Client.Cnf.Mux && link != nil {
2019-01-28 06:45:55 +00:00
link.WriteTo([]byte(utils.IO_EOF), host.Client.Cnf.CompressEncode, host.Client.Cnf.Crypt, host.Client.Rate)
2019-01-26 09:27:28 +00:00
s.bridge.ReturnTunnel(link, host.Client.Id)
2019-01-15 12:59:50 +00:00
} else if link != nil {
link.Close()
}
if isConn {
s.writeConnFail(c.Conn)
}
2019-01-15 12:59:50 +00:00
c.Close()
return nil
}