http.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package proxy
  2. import (
  3. "bufio"
  4. "crypto/tls"
  5. "github.com/cnlh/nps/bridge"
  6. "github.com/cnlh/nps/lib/common"
  7. "github.com/cnlh/nps/lib/conn"
  8. "github.com/cnlh/nps/lib/file"
  9. "github.com/cnlh/nps/lib/lg"
  10. "github.com/cnlh/nps/vender/github.com/astaxie/beego"
  11. "log"
  12. "net/http"
  13. "net/http/httputil"
  14. "path/filepath"
  15. "strconv"
  16. "sync"
  17. )
  18. type httpServer struct {
  19. server
  20. httpPort int //http端口
  21. httpsPort int //https监听端口
  22. pemPath string
  23. keyPath string
  24. stop chan bool
  25. }
  26. func NewHttp(bridge *bridge.Bridge, c *file.Tunnel) *httpServer {
  27. httpPort, _ := beego.AppConfig.Int("httpProxyPort")
  28. httpsPort, _ := beego.AppConfig.Int("httpsProxyPort")
  29. pemPath := beego.AppConfig.String("pemPath")
  30. keyPath := beego.AppConfig.String("keyPath")
  31. return &httpServer{
  32. server: server{
  33. task: c,
  34. bridge: bridge,
  35. Mutex: sync.Mutex{},
  36. },
  37. httpPort: httpPort,
  38. httpsPort: httpsPort,
  39. pemPath: pemPath,
  40. keyPath: keyPath,
  41. stop: make(chan bool),
  42. }
  43. }
  44. func (s *httpServer) Start() error {
  45. var err error
  46. var http, https *http.Server
  47. if s.errorContent, err = common.ReadAllFromFile(filepath.Join(common.GetRunPath(), "web", "static", "page", "error.html")); err != nil {
  48. s.errorContent = []byte("easyProxy 404")
  49. }
  50. if s.httpPort > 0 {
  51. http = s.NewServer(s.httpPort)
  52. go func() {
  53. lg.Println("Start http listener, port is", s.httpPort)
  54. err := http.ListenAndServe()
  55. if err != nil {
  56. lg.Fatalln(err)
  57. }
  58. }()
  59. }
  60. if s.httpsPort > 0 {
  61. if !common.FileExists(s.pemPath) {
  62. lg.Fatalf("ssl certFile %s is not exist", s.pemPath)
  63. }
  64. if !common.FileExists(s.keyPath) {
  65. lg.Fatalf("ssl keyFile %s exist", s.keyPath)
  66. }
  67. https = s.NewServer(s.httpsPort)
  68. go func() {
  69. lg.Println("Start https listener, port is", s.httpsPort)
  70. err := https.ListenAndServeTLS(s.pemPath, s.keyPath)
  71. if err != nil {
  72. lg.Fatalln(err)
  73. }
  74. }()
  75. }
  76. select {
  77. case <-s.stop:
  78. if http != nil {
  79. http.Close()
  80. }
  81. if https != nil {
  82. https.Close()
  83. }
  84. }
  85. return nil
  86. }
  87. func (s *httpServer) Close() {
  88. s.stop <- true
  89. }
  90. func (s *httpServer) handleTunneling(w http.ResponseWriter, r *http.Request) {
  91. hijacker, ok := w.(http.Hijacker)
  92. if !ok {
  93. http.Error(w, "Hijacking not supported", http.StatusInternalServerError)
  94. return
  95. }
  96. c, _, err := hijacker.Hijack()
  97. if err != nil {
  98. http.Error(w, err.Error(), http.StatusServiceUnavailable)
  99. }
  100. s.process(conn.NewConn(c), r)
  101. }
  102. func (s *httpServer) process(c *conn.Conn, r *http.Request) {
  103. //多客户端域名代理
  104. var (
  105. isConn = true
  106. lk *conn.Link
  107. host *file.Host
  108. tunnel *conn.Conn
  109. lastHost *file.Host
  110. err error
  111. )
  112. if host, err = file.GetCsvDb().GetInfoByHost(r.Host, r); err != nil {
  113. lg.Printf("the url %s %s Can't be parsed!", r.Host, r.RequestURI)
  114. goto end
  115. } else {
  116. lastHost = host
  117. }
  118. for {
  119. start:
  120. if isConn {
  121. //流量限制
  122. if host.Client.Flow.FlowLimit > 0 && (host.Client.Flow.FlowLimit<<20) < (host.Client.Flow.ExportFlow+host.Client.Flow.InletFlow) {
  123. break
  124. }
  125. host.Client.Cnf.CompressDecode, host.Client.Cnf.CompressEncode = common.GetCompressType(host.Client.Cnf.Compress)
  126. //权限控制
  127. if err = s.auth(r, c, host.Client.Cnf.U, host.Client.Cnf.P); err != nil {
  128. break
  129. }
  130. 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)
  131. if tunnel, err = s.bridge.SendLinkInfo(host.Client.Id, lk, c.Conn.RemoteAddr().String()); err != nil {
  132. log.Println(err)
  133. break
  134. }
  135. isConn = false
  136. } else {
  137. r, err = http.ReadRequest(bufio.NewReader(c))
  138. if err != nil {
  139. break
  140. }
  141. if host, err = file.GetCsvDb().GetInfoByHost(r.Host, r); err != nil {
  142. lg.Printf("the url %s %s is not found !", r.Host, r.RequestURI)
  143. break
  144. } else if host != lastHost {
  145. lastHost = host
  146. isConn = true
  147. goto start
  148. }
  149. }
  150. //根据设定,修改header和host
  151. common.ChangeHostAndHeader(r, host.HostChange, host.HeaderChange, c.Conn.RemoteAddr().String())
  152. b, err := httputil.DumpRequest(r, true)
  153. if err != nil {
  154. break
  155. }
  156. host.Flow.Add(len(b), 0)
  157. if _, err := tunnel.SendMsg(b, lk); err != nil {
  158. c.Close()
  159. break
  160. }
  161. }
  162. end:
  163. if isConn {
  164. s.writeConnFail(c.Conn)
  165. } else {
  166. tunnel.SendMsg([]byte(common.IO_EOF), lk)
  167. }
  168. c.Close()
  169. }
  170. func (s *httpServer) NewServer(port int) *http.Server {
  171. return &http.Server{
  172. Addr: ":" + strconv.Itoa(port),
  173. Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  174. s.handleTunneling(w, r)
  175. }),
  176. // Disable HTTP/2.
  177. TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)),
  178. }
  179. }