mirror of
https://github.com/nadoo/glider.git
synced 2026-11-19 07:09:42 +08:00
proxy: improve addr handling
This commit is contained in:
1 parent
e12642b47a
commit
7e7c7553cc
12 files changed
+99
-109
No files matched your search
+20
-8
@@ -3,6 +3,7 @@ package dhcpd
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"net/netip"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -29,7 +30,7 @@ func (*dpcpd) Run(args ...string) {
|
||||
return
|
||||
}
|
||||
|
||||
iface, startIP, endIP, leaseMin := args[0], args[1], args[2], args[3]
|
||||
iface, start, end, leaseMin := args[0], args[1], args[2], args[3]
|
||||
if i, err := strconv.Atoi(leaseMin); err != nil {
|
||||
leaseTime = time.Duration(i) * time.Minute
|
||||
}
|
||||
@@ -45,7 +46,19 @@ func (*dpcpd) Run(args ...string) {
|
||||
return
|
||||
}
|
||||
|
||||
pool, err := NewPool(leaseTime, net.ParseIP(startIP), net.ParseIP(endIP))
|
||||
startIP, err := netip.ParseAddr(start)
|
||||
if err != nil {
|
||||
log.F("[dhcpd] startIP %s is not valid: %s", start, err)
|
||||
return
|
||||
}
|
||||
|
||||
endIP, err := netip.ParseAddr(end)
|
||||
if err != nil {
|
||||
log.F("[dhcpd] endIP %s is not valid: %s", end, err)
|
||||
return
|
||||
}
|
||||
|
||||
pool, err := NewPool(leaseTime, startIP, endIP)
|
||||
if err != nil {
|
||||
log.F("[dhcpd] error in pool init: %s", err)
|
||||
return
|
||||
@@ -55,12 +68,11 @@ func (*dpcpd) Run(args ...string) {
|
||||
for _, host := range args[4:] {
|
||||
pair := strings.Split(host, "=")
|
||||
if len(pair) == 2 {
|
||||
mac, err := net.ParseMAC(pair[0])
|
||||
if err != nil {
|
||||
break
|
||||
if mac, err := net.ParseMAC(pair[0]); err == nil {
|
||||
if ip, err := netip.ParseAddr(pair[1]); err == nil {
|
||||
pool.LeaseStaticIP(mac, ip)
|
||||
}
|
||||
}
|
||||
ip := net.ParseIP(pair[1])
|
||||
pool.LeaseStaticIP(mac, ip)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,7 +121,7 @@ func handleDHCP(serverIP net.IP, mask net.IPMask, pool *Pool) server4.Handler {
|
||||
dhcpv4.WithMessageType(replyType),
|
||||
dhcpv4.WithServerIP(serverIP),
|
||||
dhcpv4.WithNetmask(mask),
|
||||
dhcpv4.WithYourIP(replyIP),
|
||||
dhcpv4.WithYourIP(replyIP.AsSlice()),
|
||||
dhcpv4.WithRouter(serverIP),
|
||||
dhcpv4.WithDNS(serverIP),
|
||||
// RFC 2131, Section 4.3.1. Server Identifier: MUST
|
||||
|
||||
+12
-14
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"math/rand"
|
||||
"net"
|
||||
"net/netip"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
@@ -17,18 +18,14 @@ type Pool struct {
|
||||
}
|
||||
|
||||
type item struct {
|
||||
ip net.IP
|
||||
ip netip.Addr
|
||||
mac net.HardwareAddr
|
||||
expire time.Time
|
||||
}
|
||||
|
||||
// NewPool returns a new dhcp ip pool.
|
||||
func NewPool(lease time.Duration, start, end net.IP) (*Pool, error) {
|
||||
if start == nil || end == nil {
|
||||
return nil, errors.New("start ip or end ip is wrong/nil, please check your config")
|
||||
}
|
||||
|
||||
s, e := ip2num(start.To4()), ip2num(end.To4())
|
||||
func NewPool(lease time.Duration, start, end netip.Addr) (*Pool, error) {
|
||||
s, e := ip2num(start), ip2num(end)
|
||||
if e < s {
|
||||
return nil, errors.New("start ip larger than end ip")
|
||||
}
|
||||
@@ -57,7 +54,7 @@ func NewPool(lease time.Duration, start, end net.IP) (*Pool, error) {
|
||||
}
|
||||
|
||||
// LeaseIP leases an ip to mac from dhcp pool.
|
||||
func (p *Pool) LeaseIP(mac net.HardwareAddr) (net.IP, error) {
|
||||
func (p *Pool) LeaseIP(mac net.HardwareAddr) (netip.Addr, error) {
|
||||
p.mutex.Lock()
|
||||
defer p.mutex.Unlock()
|
||||
|
||||
@@ -84,16 +81,16 @@ func (p *Pool) LeaseIP(mac net.HardwareAddr) (net.IP, error) {
|
||||
}
|
||||
}
|
||||
|
||||
return nil, errors.New("no more ip can be leased")
|
||||
return netip.Addr{}, errors.New("no more ip can be leased")
|
||||
}
|
||||
|
||||
// LeaseStaticIP leases static ip from pool according to the given mac.
|
||||
func (p *Pool) LeaseStaticIP(mac net.HardwareAddr, ip net.IP) {
|
||||
func (p *Pool) LeaseStaticIP(mac net.HardwareAddr, ip netip.Addr) {
|
||||
p.mutex.Lock()
|
||||
defer p.mutex.Unlock()
|
||||
|
||||
for _, item := range p.items {
|
||||
if item.ip.Equal(ip) {
|
||||
if item.ip == ip {
|
||||
item.mac = mac
|
||||
item.expire = time.Now().Add(time.Hour * 24 * 365 * 50) // 50 years
|
||||
}
|
||||
@@ -113,11 +110,12 @@ func (p *Pool) ReleaseIP(mac net.HardwareAddr) {
|
||||
}
|
||||
}
|
||||
|
||||
func ip2num(ip net.IP) uint32 {
|
||||
func ip2num(addr netip.Addr) uint32 {
|
||||
ip := addr.As4()
|
||||
n := uint32(ip[0])<<24 + uint32(ip[1])<<16
|
||||
return n + uint32(ip[2])<<8 + uint32(ip[3])
|
||||
}
|
||||
|
||||
func num2ip(n uint32) net.IP {
|
||||
return []byte{byte(n >> 24), byte(n >> 16), byte(n >> 8), byte(n)}
|
||||
func num2ip(n uint32) netip.Addr {
|
||||
return netip.AddrFrom4([4]byte{byte(n >> 24), byte(n >> 16), byte(n >> 8), byte(n)})
|
||||
}
|
||||
Reference in new issue
Block a user