nps/server/proxy/http.go

191 lines
4.6 KiB
Go
Raw Normal View History

2019-02-12 19:54:00 +00:00
package proxy
import (
"bufio"
"crypto/tls"
2019-02-03 04:40:43 +00:00
"github.com/cnlh/nps/bridge"
2019-02-12 19:54:00 +00:00
"github.com/cnlh/nps/lib/common"
2019-02-09 09:07:47 +00:00
"github.com/cnlh/nps/lib/conn"
"github.com/cnlh/nps/lib/file"
"github.com/cnlh/nps/lib/lg"
2019-02-16 12:43:26 +00:00
"github.com/cnlh/nps/vender/github.com/astaxie/beego"
"net/http"
"net/http/httputil"
2019-02-05 16:35:23 +00:00
"path/filepath"
"strconv"
"sync"
)
type httpServer struct {
server
httpPort int //http端口
httpsPort int //https监听端口
pemPath string
keyPath string
stop chan bool
}
2019-02-09 09:07:47 +00:00
func NewHttp(bridge *bridge.Bridge, c *file.Tunnel) *httpServer {
httpPort, _ := beego.AppConfig.Int("httpProxyPort")
httpsPort, _ := beego.AppConfig.Int("httpsProxyPort")
pemPath := beego.AppConfig.String("pemPath")
keyPath := beego.AppConfig.String("keyPath")
return &httpServer{
server: server{
task: c,
bridge: bridge,
Mutex: sync.Mutex{},
},
httpPort: httpPort,
httpsPort: httpsPort,
pemPath: pemPath,
keyPath: keyPath,
stop: make(chan bool),
}
}
func (s *httpServer) Start() error {
var err error
var http, https *http.Server
2019-02-09 09:07:47 +00:00
if s.errorContent, err = common.ReadAllFromFile(filepath.Join(common.GetRunPath(), "web", "static", "page", "error.html")); err != nil {
s.errorContent = []byte("easyProxy 404")
}
if s.httpPort > 0 {
http = s.NewServer(s.httpPort)
go func() {
2019-02-12 19:54:00 +00:00
lg.Println("Start http listener, port is", s.httpPort)
err := http.ListenAndServe()
if err != nil {
2019-02-09 09:07:47 +00:00
lg.Fatalln(err)
}
}()
}
if s.httpsPort > 0 {
2019-02-09 09:07:47 +00:00
if !common.FileExists(s.pemPath) {
2019-02-12 19:54:00 +00:00
lg.Fatalf("ssl certFile %s is not exist", s.pemPath)
2019-02-02 16:54:43 +00:00
}
2019-02-09 09:07:47 +00:00
if !common.FileExists(s.keyPath) {
2019-02-12 19:54:00 +00:00
lg.Fatalf("ssl keyFile %s exist", s.keyPath)
2019-02-02 16:54:43 +00:00
}
https = s.NewServer(s.httpsPort)
go func() {
2019-02-12 19:54:00 +00:00
lg.Println("Start https listener, port is", s.httpsPort)
err := https.ListenAndServeTLS(s.pemPath, s.keyPath)
if err != nil {
2019-02-09 09:07:47 +00:00
lg.Fatalln(err)
}
}()
}
select {
case <-s.stop:
if http != nil {
http.Close()
}
if https != nil {
https.Close()
}
}
return nil
}
2019-02-17 17:05:05 +00:00
func (s *httpServer) Close() error {
s.stop <- true
2019-02-17 17:05:05 +00:00
return nil
}
func (s *httpServer) handleTunneling(w http.ResponseWriter, r *http.Request) {
hijacker, ok := w.(http.Hijacker)
if !ok {
http.Error(w, "Hijacking not supported", http.StatusInternalServerError)
return
}
2019-02-09 09:07:47 +00:00
c, _, err := hijacker.Hijack()
if err != nil {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
}
2019-02-09 09:07:47 +00:00
s.process(conn.NewConn(c), r)
}
2019-02-09 09:07:47 +00:00
func (s *httpServer) process(c *conn.Conn, r *http.Request) {
//多客户端域名代理
var (
2019-02-15 14:59:28 +00:00
isConn = true
lk *conn.Link
host *file.Host
tunnel *conn.Conn
lastHost *file.Host
err error
)
2019-02-16 12:43:26 +00:00
if host, err = file.GetCsvDb().GetInfoByHost(r.Host, r); err != nil {
2019-02-16 15:18:58 +00:00
lg.Printf("the url %s %s can't be parsed!", r.Host, r.RequestURI)
2019-02-16 12:43:26 +00:00
goto end
} else {
lastHost = host
}
for {
2019-02-16 12:43:26 +00:00
start:
if isConn {
//流量限制
if host.Client.Flow.FlowLimit > 0 && (host.Client.Flow.FlowLimit<<20) < (host.Client.Flow.ExportFlow+host.Client.Flow.InletFlow) {
break
}
2019-02-09 09:07:47 +00:00
host.Client.Cnf.CompressDecode, host.Client.Cnf.CompressEncode = common.GetCompressType(host.Client.Cnf.Compress)
//权限控制
if err = s.auth(r, c, host.Client.Cnf.U, host.Client.Cnf.P); err != nil {
break
}
2019-02-09 09:07:47 +00:00
lk = conn.NewLink(host.Client.GetId(), common.CONN_TCP, host.GetRandomTarget(), host.Client.Cnf.CompressEncode, host.Client.Cnf.CompressDecode, host.Client.Cnf.Crypt, c, host.Flow, nil, host.Client.Rate, nil)
2019-02-16 12:43:26 +00:00
if tunnel, err = s.bridge.SendLinkInfo(host.Client.Id, lk, c.Conn.RemoteAddr().String()); err != nil {
2019-02-17 11:36:48 +00:00
lg.Println(err)
break
}
2019-02-17 11:36:48 +00:00
lk.Run(true)
isConn = false
} else {
r, err = http.ReadRequest(bufio.NewReader(c))
if err != nil {
break
}
2019-02-16 12:43:26 +00:00
if host, err = file.GetCsvDb().GetInfoByHost(r.Host, r); err != nil {
lg.Printf("the url %s %s is not found !", r.Host, r.RequestURI)
break
} else if host != lastHost {
lastHost = host
isConn = true
goto start
}
}
//根据设定修改header和host
2019-02-09 09:07:47 +00:00
common.ChangeHostAndHeader(r, host.HostChange, host.HeaderChange, c.Conn.RemoteAddr().String())
b, err := httputil.DumpRequest(r, true)
if err != nil {
break
}
host.Flow.Add(len(b), 0)
2019-02-09 09:07:47 +00:00
if _, err := tunnel.SendMsg(b, lk); err != nil {
c.Close()
break
}
2019-02-17 11:36:48 +00:00
<-lk.StatusCh
}
2019-02-16 12:43:26 +00:00
end:
if isConn {
s.writeConnFail(c.Conn)
} else {
2019-02-09 09:07:47 +00:00
tunnel.SendMsg([]byte(common.IO_EOF), lk)
}
c.Close()
}
func (s *httpServer) NewServer(port int) *http.Server {
return &http.Server{
Addr: ":" + strconv.Itoa(port),
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
s.handleTunneling(w, r)
}),
// Disable HTTP/2.
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)),
}
}