2019-02-12 19:54:00 +00:00
|
|
|
|
package proxy
|
2019-01-31 18:06:30 +00:00
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bufio"
|
|
|
|
|
"crypto/tls"
|
2019-03-04 05:41:20 +00:00
|
|
|
|
"io"
|
2019-03-01 09:23:14 +00:00
|
|
|
|
"net"
|
2019-01-31 18:06:30 +00:00
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httputil"
|
2019-02-23 15:29:48 +00:00
|
|
|
|
"os"
|
2019-02-05 16:35:23 +00:00
|
|
|
|
"path/filepath"
|
2019-01-31 18:06:30 +00:00
|
|
|
|
"strconv"
|
2019-04-08 09:01:08 +00:00
|
|
|
|
"strings"
|
2019-01-31 18:06:30 +00:00
|
|
|
|
"sync"
|
2019-08-10 03:15:25 +00:00
|
|
|
|
|
2020-01-08 13:57:14 +00:00
|
|
|
|
"ehang.io/nps/bridge"
|
|
|
|
|
"ehang.io/nps/lib/cache"
|
|
|
|
|
"ehang.io/nps/lib/common"
|
|
|
|
|
"ehang.io/nps/lib/conn"
|
|
|
|
|
"ehang.io/nps/lib/file"
|
|
|
|
|
"ehang.io/nps/server/connection"
|
2019-08-10 03:15:25 +00:00
|
|
|
|
"github.com/astaxie/beego/logs"
|
2019-01-31 18:06:30 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type httpServer struct {
|
2019-02-23 15:29:48 +00:00
|
|
|
|
BaseServer
|
2019-03-30 04:03:17 +00:00
|
|
|
|
httpPort int
|
|
|
|
|
httpsPort int
|
2019-03-23 14:19:59 +00:00
|
|
|
|
httpServer *http.Server
|
|
|
|
|
httpsServer *http.Server
|
|
|
|
|
httpsListener net.Listener
|
2019-04-08 09:01:08 +00:00
|
|
|
|
useCache bool
|
2020-01-13 10:31:03 +00:00
|
|
|
|
addOrigin bool
|
2019-04-08 09:01:08 +00:00
|
|
|
|
cache *cache.Cache
|
|
|
|
|
cacheLen int
|
2019-01-31 18:06:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-13 10:31:03 +00:00
|
|
|
|
func NewHttp(bridge *bridge.Bridge, c *file.Tunnel, httpPort, httpsPort int, useCache bool, cacheLen int, addOrigin bool) *httpServer {
|
2019-04-08 09:01:08 +00:00
|
|
|
|
httpServer := &httpServer{
|
2019-02-23 15:29:48 +00:00
|
|
|
|
BaseServer: BaseServer{
|
2019-01-31 18:06:30 +00:00
|
|
|
|
task: c,
|
|
|
|
|
bridge: bridge,
|
|
|
|
|
Mutex: sync.Mutex{},
|
|
|
|
|
},
|
|
|
|
|
httpPort: httpPort,
|
|
|
|
|
httpsPort: httpsPort,
|
2019-04-08 09:01:08 +00:00
|
|
|
|
useCache: useCache,
|
|
|
|
|
cacheLen: cacheLen,
|
2020-01-13 10:31:03 +00:00
|
|
|
|
addOrigin: addOrigin,
|
2019-01-31 18:06:30 +00:00
|
|
|
|
}
|
2019-04-08 09:01:08 +00:00
|
|
|
|
if useCache {
|
|
|
|
|
httpServer.cache = cache.New(cacheLen)
|
|
|
|
|
}
|
|
|
|
|
return httpServer
|
2019-01-31 18:06:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *httpServer) Start() error {
|
|
|
|
|
var err error
|
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 {
|
2020-01-16 07:17:21 +00:00
|
|
|
|
s.errorContent = []byte("nps 404")
|
2019-01-31 18:06:30 +00:00
|
|
|
|
}
|
|
|
|
|
if s.httpPort > 0 {
|
2019-03-23 14:19:59 +00:00
|
|
|
|
s.httpServer = s.NewServer(s.httpPort, "http")
|
2019-01-31 18:06:30 +00:00
|
|
|
|
go func() {
|
2019-03-05 01:23:18 +00:00
|
|
|
|
l, err := connection.GetHttpListener()
|
|
|
|
|
if err != nil {
|
|
|
|
|
logs.Error(err)
|
|
|
|
|
os.Exit(0)
|
|
|
|
|
}
|
2019-03-23 14:19:59 +00:00
|
|
|
|
err = s.httpServer.Serve(l)
|
2019-01-31 18:06:30 +00:00
|
|
|
|
if err != nil {
|
2019-02-23 15:29:48 +00:00
|
|
|
|
logs.Error(err)
|
|
|
|
|
os.Exit(0)
|
2019-01-31 18:06:30 +00:00
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
if s.httpsPort > 0 {
|
2019-03-23 14:19:59 +00:00
|
|
|
|
s.httpsServer = s.NewServer(s.httpsPort, "https")
|
2019-01-31 18:06:30 +00:00
|
|
|
|
go func() {
|
2019-03-23 14:19:59 +00:00
|
|
|
|
s.httpsListener, err = connection.GetHttpsListener()
|
2019-03-05 01:23:18 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
logs.Error(err)
|
|
|
|
|
os.Exit(0)
|
|
|
|
|
}
|
2019-04-08 09:01:08 +00:00
|
|
|
|
logs.Error(NewHttpsServer(s.httpsListener, s.bridge, s.useCache, s.cacheLen).Start())
|
2019-01-31 18:06:30 +00:00
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-17 17:05:05 +00:00
|
|
|
|
func (s *httpServer) Close() error {
|
2019-03-23 14:19:59 +00:00
|
|
|
|
if s.httpsListener != nil {
|
|
|
|
|
s.httpsListener.Close()
|
|
|
|
|
}
|
|
|
|
|
if s.httpsServer != nil {
|
|
|
|
|
s.httpsServer.Close()
|
|
|
|
|
}
|
|
|
|
|
if s.httpServer != nil {
|
|
|
|
|
s.httpServer.Close()
|
|
|
|
|
}
|
2019-02-17 17:05:05 +00:00
|
|
|
|
return nil
|
2019-01-31 18:06:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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()
|
2019-01-31 18:06:30 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusServiceUnavailable)
|
|
|
|
|
}
|
2019-05-25 11:40:01 +00:00
|
|
|
|
s.handleHttp(conn.NewConn(c), r)
|
2019-01-31 18:06:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-25 11:40:01 +00:00
|
|
|
|
func (s *httpServer) handleHttp(c *conn.Conn, r *http.Request) {
|
2019-01-31 18:06:30 +00:00
|
|
|
|
var (
|
2019-03-04 05:41:20 +00:00
|
|
|
|
host *file.Host
|
|
|
|
|
target net.Conn
|
|
|
|
|
err error
|
|
|
|
|
connClient io.ReadWriteCloser
|
2019-03-05 01:23:18 +00:00
|
|
|
|
scheme = r.URL.Scheme
|
2019-03-15 06:03:49 +00:00
|
|
|
|
lk *conn.Link
|
|
|
|
|
targetAddr string
|
2019-05-25 11:40:01 +00:00
|
|
|
|
lenConn *conn.LenConn
|
|
|
|
|
isReset bool
|
|
|
|
|
wg sync.WaitGroup
|
2019-01-31 18:06:30 +00:00
|
|
|
|
)
|
2019-05-25 11:40:01 +00:00
|
|
|
|
defer func() {
|
|
|
|
|
if connClient != nil {
|
|
|
|
|
s.writeConnFail(c.Conn)
|
|
|
|
|
connClient.Close()
|
|
|
|
|
}
|
|
|
|
|
c.Close()
|
|
|
|
|
}()
|
2020-01-16 07:17:21 +00:00
|
|
|
|
reset:
|
|
|
|
|
if isReset {
|
|
|
|
|
host.Client.AddConn()
|
|
|
|
|
}
|
2019-03-29 07:21:30 +00:00
|
|
|
|
if host, err = file.GetDb().GetInfoByHost(r.Host, r); err != nil {
|
2019-03-05 01:23:18 +00:00
|
|
|
|
logs.Notice("the url %s %s %s can't be parsed!", r.URL.Scheme, r.Host, r.RequestURI)
|
2019-05-25 11:40:01 +00:00
|
|
|
|
return
|
2019-03-23 14:19:59 +00:00
|
|
|
|
}
|
|
|
|
|
if err := s.CheckFlowAndConnNum(host.Client); err != nil {
|
|
|
|
|
logs.Warn("client id %d, host id %d, error %s, when https connection", host.Client.Id, host.Id, err.Error())
|
2019-02-23 15:29:48 +00:00
|
|
|
|
return
|
2019-02-16 12:43:26 +00:00
|
|
|
|
}
|
2020-01-16 07:17:21 +00:00
|
|
|
|
if !isReset {
|
|
|
|
|
defer host.Client.AddConn()
|
|
|
|
|
}
|
2019-05-25 11:40:01 +00:00
|
|
|
|
if err = s.auth(r, c, host.Client.Cnf.U, host.Client.Cnf.P); err != nil {
|
|
|
|
|
logs.Warn("auth error", err, r.RemoteAddr)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if targetAddr, err = host.Target.GetRandomTarget(); err != nil {
|
|
|
|
|
logs.Warn(err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
lk = conn.NewLink("http", targetAddr, host.Client.Cnf.Crypt, host.Client.Cnf.Compress, r.RemoteAddr, host.Target.LocalProxy)
|
|
|
|
|
if target, err = s.bridge.SendLinkInfo(host.Client.Id, lk, nil); err != nil {
|
|
|
|
|
logs.Notice("connect to target %s error %s", lk.Host, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
connClient = conn.GetConn(target, lk.Crypt, lk.Compress, host.Client.Rate, true)
|
|
|
|
|
|
|
|
|
|
//read from inc-client
|
|
|
|
|
go func() {
|
|
|
|
|
wg.Add(1)
|
|
|
|
|
isReset = false
|
|
|
|
|
defer connClient.Close()
|
|
|
|
|
defer func() {
|
|
|
|
|
wg.Done()
|
|
|
|
|
if !isReset {
|
|
|
|
|
c.Close()
|
2019-01-31 18:06:30 +00:00
|
|
|
|
}
|
2019-05-25 11:40:01 +00:00
|
|
|
|
}()
|
|
|
|
|
for {
|
2019-12-03 20:04:05 +00:00
|
|
|
|
if resp, err := http.ReadResponse(bufio.NewReader(connClient), r); err != nil || resp == nil {
|
2019-05-25 11:40:01 +00:00
|
|
|
|
return
|
|
|
|
|
} else {
|
|
|
|
|
//if the cache is start and the response is in the extension,store the response to the cache list
|
2019-12-03 20:04:05 +00:00
|
|
|
|
if s.useCache && r.URL != nil && strings.Contains(r.URL.Path, ".") {
|
2019-05-25 11:40:01 +00:00
|
|
|
|
b, err := httputil.DumpResponse(resp, true)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
c.Write(b)
|
|
|
|
|
host.Flow.Add(0, int64(len(b)))
|
|
|
|
|
s.cache.Add(filepath.Join(host.Host, r.URL.Path), b)
|
|
|
|
|
} else {
|
|
|
|
|
lenConn := conn.NewLenConn(c)
|
|
|
|
|
if err := resp.Write(lenConn); err != nil {
|
|
|
|
|
logs.Error(err)
|
2019-04-08 10:02:54 +00:00
|
|
|
|
return
|
2019-04-08 09:01:08 +00:00
|
|
|
|
}
|
2019-05-25 11:40:01 +00:00
|
|
|
|
host.Flow.Add(0, int64(lenConn.Len))
|
2019-04-08 09:01:08 +00:00
|
|
|
|
}
|
2019-02-16 12:43:26 +00:00
|
|
|
|
}
|
2019-01-31 18:06:30 +00:00
|
|
|
|
}
|
2019-05-25 11:40:01 +00:00
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
for {
|
2019-04-08 09:01:08 +00:00
|
|
|
|
//if the cache start and the request is in the cache list, return the cache
|
|
|
|
|
if s.useCache {
|
|
|
|
|
if v, ok := s.cache.Get(filepath.Join(host.Host, r.URL.Path)); ok {
|
|
|
|
|
n, err := c.Write(v.([]byte))
|
|
|
|
|
if err != nil {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
logs.Trace("%s request, method %s, host %s, url %s, remote address %s, return cache", r.URL.Scheme, r.Method, r.Host, r.URL.Path, c.RemoteAddr().String())
|
|
|
|
|
host.Flow.Add(0, int64(n))
|
|
|
|
|
//if return cache and does not create a new conn with client and Connection is not set or close, close the connection.
|
2019-05-25 11:40:01 +00:00
|
|
|
|
if strings.ToLower(r.Header.Get("Connection")) == "close" || strings.ToLower(r.Header.Get("Connection")) == "" {
|
2019-04-08 09:01:08 +00:00
|
|
|
|
break
|
|
|
|
|
}
|
2019-05-25 11:40:01 +00:00
|
|
|
|
goto readReq
|
2019-04-08 09:01:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-25 11:40:01 +00:00
|
|
|
|
|
2019-03-30 04:03:17 +00:00
|
|
|
|
//change the host and header and set proxy setting
|
2020-01-13 10:31:03 +00:00
|
|
|
|
common.ChangeHostAndHeader(r, host.HostChange, host.HeaderChange, c.Conn.RemoteAddr().String(), s.addOrigin)
|
2019-04-08 09:01:08 +00:00
|
|
|
|
logs.Trace("%s request, method %s, host %s, url %s, remote address %s, target %s", r.URL.Scheme, r.Method, r.Host, r.URL.Path, c.RemoteAddr().String(), lk.Host)
|
2019-03-01 09:23:14 +00:00
|
|
|
|
//write
|
2019-05-25 11:40:01 +00:00
|
|
|
|
lenConn = conn.NewLenConn(connClient)
|
2019-04-12 09:40:27 +00:00
|
|
|
|
if err := r.Write(lenConn); err != nil {
|
|
|
|
|
logs.Error(err)
|
2019-03-18 06:18:58 +00:00
|
|
|
|
break
|
|
|
|
|
}
|
2019-04-12 09:40:27 +00:00
|
|
|
|
host.Flow.Add(int64(lenConn.Len), 0)
|
2019-05-25 11:40:01 +00:00
|
|
|
|
|
|
|
|
|
readReq:
|
|
|
|
|
//read req from connection
|
|
|
|
|
if r, err = http.ReadRequest(bufio.NewReader(c)); err != nil {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
r.URL.Scheme = scheme
|
|
|
|
|
//What happened ,Why one character less???
|
|
|
|
|
r.Method = resetReqMethod(r.Method)
|
|
|
|
|
if hostTmp, err := file.GetDb().GetInfoByHost(r.Host, r); err != nil {
|
|
|
|
|
logs.Notice("the url %s %s %s can't be parsed!", r.URL.Scheme, r.Host, r.RequestURI)
|
|
|
|
|
break
|
2020-01-16 07:17:21 +00:00
|
|
|
|
} else if host != hostTmp {
|
2019-05-25 11:40:01 +00:00
|
|
|
|
host = hostTmp
|
|
|
|
|
isReset = true
|
|
|
|
|
connClient.Close()
|
|
|
|
|
goto reset
|
|
|
|
|
}
|
2019-01-31 18:06:30 +00:00
|
|
|
|
}
|
2019-05-25 11:40:01 +00:00
|
|
|
|
wg.Wait()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func resetReqMethod(method string) string {
|
|
|
|
|
if method == "ET" {
|
|
|
|
|
return "GET"
|
2019-01-31 18:06:30 +00:00
|
|
|
|
}
|
2019-05-25 11:40:01 +00:00
|
|
|
|
if method == "OST" {
|
|
|
|
|
return "POST"
|
2019-03-01 09:23:14 +00:00
|
|
|
|
}
|
2019-05-25 11:40:01 +00:00
|
|
|
|
return method
|
2019-01-31 18:06:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-05 01:23:18 +00:00
|
|
|
|
func (s *httpServer) NewServer(port int, scheme string) *http.Server {
|
2019-01-31 18:06:30 +00:00
|
|
|
|
return &http.Server{
|
|
|
|
|
Addr: ":" + strconv.Itoa(port),
|
|
|
|
|
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2019-03-05 01:23:18 +00:00
|
|
|
|
r.URL.Scheme = scheme
|
2019-01-31 18:06:30 +00:00
|
|
|
|
s.handleTunneling(w, r)
|
|
|
|
|
}),
|
|
|
|
|
// Disable HTTP/2.
|
|
|
|
|
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)),
|
|
|
|
|
}
|
|
|
|
|
}
|