Skip to content

Commit 8c1f3c7

Browse files
committed
add x-real-ip forwarder & fix ua changer
Signed-off-by: Yonle <[email protected]>
1 parent 8e934ea commit 8c1f3c7

File tree

6 files changed

+31
-9
lines changed

6 files changed

+31
-9
lines changed

config.example.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,6 @@ max_connections_per_ip: 3
3131

3232
# user agent to use on connecting to upstream
3333
user_agent: bostr2 (https://codeberg.org/Yonle/bostr2) on wss://bostr.example.com - nostr relay proxy aggregator
34+
35+
# whenever forward client ip to upstream.
36+
forward_client_ip: false

config.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ type Config struct {
1616
NIP_11 map[string]interface{} `yaml:"nip_11"`
1717
Favicon string
1818
MaxConnectionsPerIP int `yaml:"max_connections_per_ip"`
19-
UserAgent string `yaml:"user-agent"`
19+
UserAgent string `yaml:"user_agent"`
20+
ForwardClientIP bool `yaml:"forward_client_ip"`
2021
}
2122

2223
//go:embed config.example.yaml

http.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ func LoadFavicon() {
109109
}
110110

111111
func ConfigureRelayHandler() {
112-
h := http.Header{}
113-
h.Add("User-Agent", config.UserAgent)
114-
relayHandler.DialOptions.HTTPHeader = h
112+
if len(config.UserAgent) > 0 {
113+
relayHandler.UserAgent = config.UserAgent
114+
}
115115
}
116116

117117
func Serve() {

relayHandler/relayHandler.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import (
55
"github.com/coder/websocket"
66
)
77

8-
func NewSession(ctx context.Context) RelaySession {
8+
func NewSession(ctx context.Context, ip string) RelaySession {
99
return RelaySession{
10-
ctx: ctx,
10+
ctx: ctx,
11+
clientIP: ip,
1112

1213
relays: make(SessionRelays),
1314

relayHandler/session.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package relayHandler
33
import (
44
"context"
55
"encoding/json"
6+
"net/http"
67
"sync"
78
"time"
89

@@ -18,7 +19,8 @@ type RelaySession struct {
1819
wg sync.WaitGroup
1920
mu sync.RWMutex
2021

21-
relays SessionRelays
22+
clientIP string
23+
relays SessionRelays
2224

2325
UpEVENT MessageChan
2426
UpEOSE MessageChan
@@ -27,7 +29,7 @@ type RelaySession struct {
2729
HowManyRelaysAreConnected int
2830
}
2931

30-
var DialOptions = &websocket.DialOptions{}
32+
var UserAgent string = "bostr2 (https://codeberg.org/Yonle/bostr2) - nostr relay proxy aggregator"
3133

3234
func (s *RelaySession) Init(r []string) {
3335
s.wg.Add(len(r))
@@ -39,6 +41,16 @@ func (s *RelaySession) Init(r []string) {
3941
func (s *RelaySession) connect(url string) {
4042
defer s.wg.Done()
4143

44+
h := http.Header{}
45+
h.Add("User-Agent", UserAgent)
46+
if len(s.clientIP) > 0 {
47+
h.Add("X-Real-IP", s.clientIP)
48+
}
49+
50+
DialOptions := &websocket.DialOptions{
51+
HTTPHeader: h,
52+
}
53+
4254
listener:
4355
for {
4456
dialCtx, dialCancel := context.WithTimeout(s.ctx, 5*time.Second)

websocket.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ func Accept_Websocket(w http.ResponseWriter, r *http.Request, ip string, ua stri
5050

5151
go pingpong.Stare(ctx, cancel, conn)
5252

53-
var relaySession = relayHandler.NewSession(ctx)
53+
var ip2up string
54+
if config.ForwardClientIP {
55+
ip2up = ip
56+
}
57+
58+
var relaySession = relayHandler.NewSession(ctx, ip2up)
5459
var s = Session{
5560
ClientIP: ip,
5661

0 commit comments

Comments
 (0)