wip
This commit is contained in:
69
hub/api/db/sanitize-validate.go
Normal file
69
hub/api/db/sanitize-validate.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/netip"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInvalidIP = errors.New("invalid IP")
|
||||
ErrInvalidPort = errors.New("invalid port")
|
||||
)
|
||||
|
||||
func Config_Sanitize(c *Config) {
|
||||
if u, err := url.Parse(c.HubAddress); err == nil {
|
||||
c.HubAddress = u.String()
|
||||
}
|
||||
|
||||
if addr, ok := netip.AddrFromSlice(c.VPNNetwork); ok {
|
||||
c.VPNNetwork = addr.AsSlice()
|
||||
}
|
||||
}
|
||||
|
||||
func Config_Validate(c *Config) error {
|
||||
if _, err := url.Parse(c.HubAddress); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
addr, ok := netip.AddrFromSlice(c.VPNNetwork)
|
||||
if !ok || !addr.Is4() || addr.As4()[3] != 0 || addr.As4()[0] == 0 {
|
||||
return ErrInvalidIP
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func Session_Sanitize(s *Session) {
|
||||
}
|
||||
|
||||
func Session_Validate(s *Session) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func Peer_Sanitize(p *Peer) {
|
||||
p.Name = strings.TrimSpace(p.Name)
|
||||
if len(p.IP) != 0 {
|
||||
addr, ok := netip.AddrFromSlice(p.IP)
|
||||
if ok && addr.Is4() {
|
||||
p.IP = addr.AsSlice()
|
||||
}
|
||||
}
|
||||
if p.Port == 0 {
|
||||
p.Port = 515
|
||||
}
|
||||
}
|
||||
|
||||
func Peer_Validate(p *Peer) error {
|
||||
if len(p.IP) > 0 {
|
||||
_, ok := netip.AddrFromSlice(p.IP)
|
||||
if !ok {
|
||||
return ErrInvalidIP
|
||||
}
|
||||
}
|
||||
if p.Port == 0 {
|
||||
return ErrInvalidPort
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user