Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion config/gobetween.toml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ protocol = "udp"
# [servers.default.udp] # (optional)
# max_requests = 0 # (optional) if > 0 accepts no more requests than max_requests and closes session
# max_responses = 0 # (optional) if > 0 accepts no more responses than max_responses from backend and closes session
# source = 192.0.2.1 # (optional) Specify source address. This option sets the laddr (local address) when calling DialUDP().
# transparent = false # (optional, not supported in Windows)
# # if true - work in transparent mode, when forwarded udp packets have client source address.
# # (requires additional host configuration)
Expand Down Expand Up @@ -210,7 +211,7 @@ protocol = "udp"
# ## Probe sends string to backend and expects specific string in response to check if
# ## backend is alive. Only the beginning of received buffer will be compared with expected value, so that
# ## backend response can contain extra data, not being checked. probe_send and probe_recv are
# ## configured as strings and support escape sequences: \n \r or \xNN for hex. First probe_recv_len bytes of server response
# ## configured as strings and support escape sequences: \n \r or \xNN for hex. First probe_recv_len bytes of server response
# ## could be also matched using regular expression.
# ##
# kind = "probe"
Expand Down
1 change: 1 addition & 0 deletions src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ type Udp struct {
MaxRequests uint64 `toml:"max_requests" json:"max_requests"`
MaxResponses uint64 `toml:"max_responses" json:"max_responses"`
Transparent bool `toml:"transparent" json:"transparent"`
Source string `toml:"source" json:"source"`
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/server/udp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ func (this *Server) serve() {
ClientIdleTimeout: utils.ParseDurationOrDefault(*this.cfg.ClientIdleTimeout, 0),
BackendIdleTimeout: utils.ParseDurationOrDefault(*this.cfg.BackendIdleTimeout, 0),
Transparent: this.cfg.Udp.Transparent,
Source: this.cfg.Udp.Source,
}

var cp *connPool
Expand Down Expand Up @@ -331,6 +332,16 @@ func (this *Server) electAndConnect(pool *connPool, clientAddr *net.UDPAddr) (ne
if err != nil {
return nil, nil, fmt.Errorf("Could not dial UDP addr %v from %v: %v", addr, clientAddr, err)
}
} else if this.cfg.Udp.Source != "" {
laddrStr := this.cfg.Udp.Source + ":0" // 0 means "pick a random source port"
laddr, err := net.ResolveUDPAddr("udp", laddrStr)
if err != nil {
return nil, nil, fmt.Errorf("Could not resolve udp local address %s: %v", laddrStr, err)
}
conn, err = net.DialUDP("udp", laddr, addr)
if err != nil {
return nil, nil, fmt.Errorf("Could not dial UDP addr %v from %v: %v", addr, laddr, err)
}
} else {
conn, err = net.DialUDP("udp", nil, addr)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions src/server/udp/session/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ type Config struct {
ClientIdleTimeout time.Duration
BackendIdleTimeout time.Duration
Transparent bool
Source string
}