Changeset 705 in code for trunk/user.go


Ignore:
Timestamp:
Nov 17, 2021, 2:07:58 PM (4 years ago)
Author:
contact
Message:

Add per-user IP addresses

The new upstream-user-ip directive allows bouncer operators to
assign one IP address per user.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/user.go

    r704 r705  
    77        "encoding/hex"
    88        "fmt"
     9        "math/big"
     10        "net"
    911        "time"
    1012
     
    957959        return !isMem
    958960}
     961
     962// localAddrForHost returns the local address to use when connecting to host.
     963// A nil address is returned when the OS should automatically pick one.
     964func (u *user) localTCPAddrForHost(host string) (*net.TCPAddr, error) {
     965        upstreamUserIPs := u.srv.Config().UpstreamUserIPs
     966        if len(upstreamUserIPs) == 0 {
     967                return nil, nil
     968        }
     969
     970        ips, err := net.LookupIP(host)
     971        if err != nil {
     972                return nil, err
     973        }
     974
     975        wantIPv6 := false
     976        for _, ip := range ips {
     977                if ip.To4() == nil {
     978                        wantIPv6 = true
     979                        break
     980                }
     981        }
     982
     983        var ipNet *net.IPNet
     984        for _, in := range upstreamUserIPs {
     985                if wantIPv6 == (in.IP.To4() == nil) {
     986                        ipNet = in
     987                        break
     988                }
     989        }
     990        if ipNet == nil {
     991                return nil, nil
     992        }
     993
     994        var ipInt big.Int
     995        ipInt.SetBytes(ipNet.IP)
     996        ipInt.Add(&ipInt, big.NewInt(u.ID+1))
     997        ip := net.IP(ipInt.Bytes())
     998        if !ipNet.Contains(ip) {
     999                return nil, fmt.Errorf("IP network %v too small", ipNet)
     1000        }
     1001
     1002        return &net.TCPAddr{IP: ip}, nil
     1003}
Note: See TracChangeset for help on using the changeset viewer.