Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nebula
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
exchain
nebula
Commits
16e27bdf
Commit
16e27bdf
authored
Jan 06, 2022
by
inphi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(proxyd): Add X-Forwarded-For header
parent
c9123af4
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
60 additions
and
18 deletions
+60
-18
backend.go
go/proxyd/backend.go
+21
-3
config.go
go/proxyd/config.go
+10
-9
proxyd.go
go/proxyd/proxyd.go
+3
-0
server.go
go/proxyd/server.go
+26
-6
No files found.
go/proxyd/backend.go
View file @
16e27bdf
...
@@ -7,16 +7,18 @@ import (
...
@@ -7,16 +7,18 @@ import (
"encoding/json"
"encoding/json"
"errors"
"errors"
"fmt"
"fmt"
"github.com/ethereum/go-ethereum/log"
"github.com/gorilla/websocket"
"github.com/prometheus/client_golang/prometheus"
"io"
"io"
"io/ioutil"
"io/ioutil"
"math"
"math"
"math/rand"
"math/rand"
"net/http"
"net/http"
"strconv"
"strconv"
"strings"
"time"
"time"
"github.com/ethereum/go-ethereum/log"
"github.com/gorilla/websocket"
"github.com/prometheus/client_golang/prometheus"
)
)
const
(
const
(
...
@@ -84,6 +86,7 @@ type Backend struct {
...
@@ -84,6 +86,7 @@ type Backend struct {
maxRPS
int
maxRPS
int
maxWSConns
int
maxWSConns
int
outOfServiceInterval
time
.
Duration
outOfServiceInterval
time
.
Duration
stripTrailingXFF
bool
}
}
type
BackendOpt
func
(
b
*
Backend
)
type
BackendOpt
func
(
b
*
Backend
)
...
@@ -140,6 +143,12 @@ func WithTLSConfig(tlsConfig *tls.Config) BackendOpt {
...
@@ -140,6 +143,12 @@ func WithTLSConfig(tlsConfig *tls.Config) BackendOpt {
}
}
}
}
func
WithStrippedTrailingXFF
()
BackendOpt
{
return
func
(
b
*
Backend
)
{
b
.
stripTrailingXFF
=
true
}
}
func
NewBackend
(
func
NewBackend
(
name
string
,
name
string
,
rpcURL
string
,
rpcURL
string
,
...
@@ -316,7 +325,16 @@ func (b *Backend) doForward(ctx context.Context, rpcReq *RPCReq) (*RPCRes, error
...
@@ -316,7 +325,16 @@ func (b *Backend) doForward(ctx context.Context, rpcReq *RPCReq) (*RPCRes, error
httpReq
.
SetBasicAuth
(
b
.
authUsername
,
b
.
authPassword
)
httpReq
.
SetBasicAuth
(
b
.
authUsername
,
b
.
authPassword
)
}
}
xForwardedFor
:=
GetXForwardedFor
(
ctx
)
if
b
.
stripTrailingXFF
{
ipList
:=
strings
.
Split
(
xForwardedFor
,
","
)
if
len
(
ipList
)
>
0
{
xForwardedFor
=
ipList
[
0
]
}
}
httpReq
.
Header
.
Set
(
"content-type"
,
"application/json"
)
httpReq
.
Header
.
Set
(
"content-type"
,
"application/json"
)
httpReq
.
Header
.
Set
(
"X-Forwarded-For"
,
xForwardedFor
)
httpRes
,
err
:=
b
.
client
.
Do
(
httpReq
)
httpRes
,
err
:=
b
.
client
.
Do
(
httpReq
)
if
err
!=
nil
{
if
err
!=
nil
{
...
...
go/proxyd/config.go
View file @
16e27bdf
...
@@ -32,15 +32,16 @@ type BackendOptions struct {
...
@@ -32,15 +32,16 @@ type BackendOptions struct {
}
}
type
BackendConfig
struct
{
type
BackendConfig
struct
{
Username
string
`toml:"username"`
Username
string
`toml:"username"`
Password
string
`toml:"password"`
Password
string
`toml:"password"`
RPCURL
string
`toml:"rpc_url"`
RPCURL
string
`toml:"rpc_url"`
WSURL
string
`toml:"ws_url"`
WSURL
string
`toml:"ws_url"`
MaxRPS
int
`toml:"max_rps"`
MaxRPS
int
`toml:"max_rps"`
MaxWSConns
int
`toml:"max_ws_conns"`
MaxWSConns
int
`toml:"max_ws_conns"`
CAFile
string
`toml:"ca_file"`
CAFile
string
`toml:"ca_file"`
ClientCertFile
string
`toml:"client_cert_file"`
ClientCertFile
string
`toml:"client_cert_file"`
ClientKeyFile
string
`toml:"client_key_file"`
ClientKeyFile
string
`toml:"client_key_file"`
StripTrailingXFF
bool
`toml:"strip_trailing_xff"`
}
}
type
BackendsConfig
map
[
string
]
*
BackendConfig
type
BackendsConfig
map
[
string
]
*
BackendConfig
...
...
go/proxyd/proxyd.go
View file @
16e27bdf
...
@@ -96,6 +96,9 @@ func Start(config *Config) error {
...
@@ -96,6 +96,9 @@ func Start(config *Config) error {
log
.
Info
(
"using custom TLS config for backend"
,
"name"
,
name
)
log
.
Info
(
"using custom TLS config for backend"
,
"name"
,
name
)
opts
=
append
(
opts
,
WithTLSConfig
(
tlsConfig
))
opts
=
append
(
opts
,
WithTLSConfig
(
tlsConfig
))
}
}
if
cfg
.
StripTrailingXFF
{
opts
=
append
(
opts
,
WithStrippedTrailingXFF
())
}
back
:=
NewBackend
(
name
,
rpcURL
,
wsURL
,
lim
,
opts
...
)
back
:=
NewBackend
(
name
,
rpcURL
,
wsURL
,
lim
,
opts
...
)
backendNames
=
append
(
backendNames
,
name
)
backendNames
=
append
(
backendNames
,
name
)
backendsByName
[
name
]
=
back
backendsByName
[
name
]
=
back
...
...
go/proxyd/server.go
View file @
16e27bdf
...
@@ -5,20 +5,23 @@ import (
...
@@ -5,20 +5,23 @@ import (
"encoding/json"
"encoding/json"
"errors"
"errors"
"fmt"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"time"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/log"
"github.com/gorilla/mux"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
"github.com/gorilla/websocket"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus"
"github.com/rs/cors"
"github.com/rs/cors"
"io"
"net/http"
"strconv"
"time"
)
)
const
(
const
(
ContextKeyAuth
=
"authorization"
ContextKeyAuth
=
"authorization"
ContextKeyReqID
=
"req_id"
ContextKeyReqID
=
"req_id"
ContextKeyXForwardedFor
=
"x_forwarded_for"
)
)
type
Server
struct
{
type
Server
struct
{
...
@@ -214,7 +217,16 @@ func (s *Server) populateContext(w http.ResponseWriter, r *http.Request) context
...
@@ -214,7 +217,16 @@ func (s *Server) populateContext(w http.ResponseWriter, r *http.Request) context
return
nil
return
nil
}
}
xff
:=
r
.
Header
.
Get
(
"X-Forwarded-For"
)
if
xff
==
""
{
ipPort
:=
strings
.
Split
(
r
.
RemoteAddr
,
":"
)
if
len
(
ipPort
)
==
2
{
xff
=
ipPort
[
0
]
}
}
ctx
:=
context
.
WithValue
(
r
.
Context
(),
ContextKeyAuth
,
s
.
authenticatedPaths
[
authorization
])
ctx
:=
context
.
WithValue
(
r
.
Context
(),
ContextKeyAuth
,
s
.
authenticatedPaths
[
authorization
])
ctx
=
context
.
WithValue
(
ctx
,
ContextKeyXForwardedFor
,
xff
)
return
context
.
WithValue
(
return
context
.
WithValue
(
ctx
,
ctx
,
ContextKeyReqID
,
ContextKeyReqID
,
...
@@ -271,3 +283,11 @@ func GetReqID(ctx context.Context) string {
...
@@ -271,3 +283,11 @@ func GetReqID(ctx context.Context) string {
}
}
return
reqId
return
reqId
}
}
func
GetXForwardedFor
(
ctx
context
.
Context
)
string
{
xff
,
ok
:=
ctx
.
Value
(
ContextKeyXForwardedFor
)
.
(
string
)
if
!
ok
{
return
""
}
return
xff
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment