Changeset 370 in code for trunk/server.go


Ignore:
Timestamp:
Jul 22, 2020, 3:03:01 PM (5 years ago)
Author:
contact
Message:

Add accept-proxy-ip config directive

This allows to set the list of IPs allowed to act as a proxy. This is
only used for WebSockets right now, but will be expanded to TCP as well
once the PROXY protocol is supported.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/server.go

    r348 r370  
    1212        "gopkg.in/irc.v3"
    1313        "nhooyr.io/websocket"
     14
     15        "git.sr.ht/~emersion/soju/config"
    1416)
    1517
     
    4244
    4345type Server struct {
    44         Hostname     string
    45         Logger       Logger
    46         RingCap      int
    47         HistoryLimit int
    48         LogPath      string
    49         Debug        bool
    50         HTTPOrigins  []string
     46        Hostname       string
     47        Logger         Logger
     48        RingCap        int
     49        HistoryLimit   int
     50        LogPath        string
     51        Debug          bool
     52        HTTPOrigins    []string
     53        AcceptProxyIPs config.IPSet
    5154
    5255        db *DB
     
    154157        }
    155158
    156         isLoopback := false
     159        isProxy := false
    157160        if host, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
    158161                if ip := net.ParseIP(host); ip != nil {
    159                         isLoopback = ip.IsLoopback()
     162                        isProxy = s.AcceptProxyIPs.Contains(ip)
    160163                }
    161164        }
    162165
    163         // Only trust X-Forwarded-* header fields if this is a loopback connection,
     166        // Only trust X-Forwarded-* header fields if this is a trusted proxy IP
    164167        // to prevent users from spoofing the remote address
    165168        remoteAddr := req.RemoteAddr
    166169        forwardedHost := req.Header.Get("X-Forwarded-For")
    167170        forwardedPort := req.Header.Get("X-Forwarded-Port")
    168         if isLoopback && forwardedHost != "" && forwardedPort != "" {
     171        if isProxy && forwardedHost != "" && forwardedPort != "" {
    169172                remoteAddr = net.JoinHostPort(forwardedHost, forwardedPort)
    170173        }
Note: See TracChangeset for help on using the changeset viewer.