mirror of https://github.com/ehang-io/nps
fixes #353
parent
f73a55f254
commit
0af9943540
|
@ -74,4 +74,5 @@ system_info_display=false
|
||||||
http_cache=false
|
http_cache=false
|
||||||
http_cache_length=100
|
http_cache_length=100
|
||||||
|
|
||||||
|
#get origin ip
|
||||||
|
http_add_origin_header=false
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
# 说明
|
# 说明
|
||||||
## 获取用户真实ip
|
## 获取用户真实ip
|
||||||
|
如需使用需要在`nps.conf`中设置`http_add_origin_header=true`
|
||||||
|
|
||||||
在域名代理模式中,可以通过request请求 header 中的 X-Forwarded-For 和 X-Real-IP 来获取用户真实 IP。
|
在域名代理模式中,可以通过request请求 header 中的 X-Forwarded-For 和 X-Real-IP 来获取用户真实 IP。
|
||||||
|
|
||||||
|
|
|
@ -98,7 +98,7 @@ func Getverifyval(vkey string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
//Change headers and host of request
|
//Change headers and host of request
|
||||||
func ChangeHostAndHeader(r *http.Request, host string, header string, addr string) {
|
func ChangeHostAndHeader(r *http.Request, host string, header string, addr string,addOrigin bool) {
|
||||||
if host != "" {
|
if host != "" {
|
||||||
r.Host = host
|
r.Host = host
|
||||||
}
|
}
|
||||||
|
@ -115,8 +115,10 @@ func ChangeHostAndHeader(r *http.Request, host string, header string, addr strin
|
||||||
if prior, ok := r.Header["X-Forwarded-For"]; ok {
|
if prior, ok := r.Header["X-Forwarded-For"]; ok {
|
||||||
addr = strings.Join(prior, ", ") + ", " + addr
|
addr = strings.Join(prior, ", ") + ", " + addr
|
||||||
}
|
}
|
||||||
|
if addOrigin {
|
||||||
r.Header.Set("X-Forwarded-For", addr)
|
r.Header.Set("X-Forwarded-For", addr)
|
||||||
r.Header.Set("X-Real-IP", addr)
|
r.Header.Set("X-Real-IP", addr)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Read file content by file path
|
//Read file content by file path
|
||||||
|
|
|
@ -30,11 +30,12 @@ type httpServer struct {
|
||||||
httpsServer *http.Server
|
httpsServer *http.Server
|
||||||
httpsListener net.Listener
|
httpsListener net.Listener
|
||||||
useCache bool
|
useCache bool
|
||||||
|
addOrigin bool
|
||||||
cache *cache.Cache
|
cache *cache.Cache
|
||||||
cacheLen int
|
cacheLen int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHttp(bridge *bridge.Bridge, c *file.Tunnel, httpPort, httpsPort int, useCache bool, cacheLen int) *httpServer {
|
func NewHttp(bridge *bridge.Bridge, c *file.Tunnel, httpPort, httpsPort int, useCache bool, cacheLen int, addOrigin bool) *httpServer {
|
||||||
httpServer := &httpServer{
|
httpServer := &httpServer{
|
||||||
BaseServer: BaseServer{
|
BaseServer: BaseServer{
|
||||||
task: c,
|
task: c,
|
||||||
|
@ -45,6 +46,7 @@ func NewHttp(bridge *bridge.Bridge, c *file.Tunnel, httpPort, httpsPort int, use
|
||||||
httpsPort: httpsPort,
|
httpsPort: httpsPort,
|
||||||
useCache: useCache,
|
useCache: useCache,
|
||||||
cacheLen: cacheLen,
|
cacheLen: cacheLen,
|
||||||
|
addOrigin: addOrigin,
|
||||||
}
|
}
|
||||||
if useCache {
|
if useCache {
|
||||||
httpServer.cache = cache.New(cacheLen)
|
httpServer.cache = cache.New(cacheLen)
|
||||||
|
@ -214,7 +216,7 @@ reset:
|
||||||
}
|
}
|
||||||
|
|
||||||
//change the host and header and set proxy setting
|
//change the host and header and set proxy setting
|
||||||
common.ChangeHostAndHeader(r, host.HostChange, host.HeaderChange, c.Conn.RemoteAddr().String())
|
common.ChangeHostAndHeader(r, host.HostChange, host.HeaderChange, c.Conn.RemoteAddr().String(), s.addOrigin)
|
||||||
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)
|
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)
|
||||||
//write
|
//write
|
||||||
lenConn = conn.NewLenConn(connClient)
|
lenConn = conn.NewLenConn(connClient)
|
||||||
|
|
|
@ -147,7 +147,8 @@ func NewMode(Bridge *bridge.Bridge, c *file.Tunnel) proxy.Service {
|
||||||
httpsPort, _ := beego.AppConfig.Int("https_proxy_port")
|
httpsPort, _ := beego.AppConfig.Int("https_proxy_port")
|
||||||
useCache, _ := beego.AppConfig.Bool("http_cache")
|
useCache, _ := beego.AppConfig.Bool("http_cache")
|
||||||
cacheLen, _ := beego.AppConfig.Int("http_cache_length")
|
cacheLen, _ := beego.AppConfig.Int("http_cache_length")
|
||||||
service = proxy.NewHttp(Bridge, c, httpPort, httpsPort, useCache, cacheLen)
|
addOrigin, _ := beego.AppConfig.Bool("http_add_origin_header")
|
||||||
|
service = proxy.NewHttp(Bridge, c, httpPort, httpsPort, useCache, cacheLen, addOrigin)
|
||||||
}
|
}
|
||||||
return service
|
return service
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue