<commit_before>package ovpm

import (
	"encoding/binary"
	"errors"
	"fmt"
	"log"
	"net"

	"time"

	"github.com/Sirupsen/logrus"
	"github.com/asaskevich/govalidator"
	"github.com/coreos/go-iptables/iptables"
	"github.com/jinzhu/gorm"
)

// NetworkType distinguishes different types of networks that is defined in the networks table.
type NetworkType uint

// NetworkTypes
const (
	UNDEFINEDNET NetworkType = iota
	SERVERNET
	ROUTE
)

var networkTypes = [...]struct {
	Type        NetworkType
	String      string
	Description string
}{
	{UNDEFINEDNET, "UNDEFINEDNET", "unknown network type"},
	{SERVERNET, "SERVERNET", "network behind vpn server"},
	{ROUTE, "ROUTE", "network to be pushed as route"},
}

// NetworkTypeFromString returns string representation of the network type.
func NetworkTypeFromString(typ string) NetworkType {
	for _, v := range networkTypes {
		if v.String == typ {
			return v.Type
		}
	}
	return UNDEFINEDNET
}

// GetAllNetworkTypes returns all network types defined in the system.
func GetAllNetworkTypes() []NetworkType {
	var networkTypeList []NetworkType
	for _, v := range networkTypes {
		networkTypeList = append(networkTypeList, v.Type)
	}
	return networkTypeList
}

func (nt NetworkType) String() string {
	for _, v := range networkTypes {
		if v.Type == nt {
			return v.String
		}
	}
	return "UNDEFINEDNET"
}

// Description gives description about the network type.
func (nt NetworkType) Description() string {
	for _, v := range networkTypes {
		if v.Type == nt {
			return v.Description
		}
	}
	return "UNDEFINEDNET"
}

// dbNetworkModel is database model for external networks on the VPN server.
type dbNetworkModel struct {
	gorm.Model
	ServerID uint
	Server   dbServerModel

	Name  string `gorm:"unique_index"`
	CIDR  string
	Type  NetworkType
	Via   string
	Users []*dbUserModel `gorm:"many2many:network_users;"`
}

// Network represents a VPN related network.
type Network struct {
	dbNetworkModel
}

// GetNetwork returns a network specified by its name.
func GetNetwork(name string) (*Network, error) {
	if !IsInitialized() {
		return nil, fmt.Errorf("you first need to create server")
	}
	// Validate user input.
	if govalidator.IsNull(name) {
		return nil, fmt.Errorf("validation error: %s can not be null", name)
	}
	if !govalidator.IsAlphanumeric(name) {
		return nil, fmt.Errorf("validation error: `%s` can only contain letters and numbers", name)
	}

	var network dbNetworkModel
	db.Preload("Users").Where(&dbNetworkModel{Name: name}).First(&network)

	if db.NewRecord(&network) {
		return nil, fmt.Errorf("network not found %s", name)
	}
<commit_msg>fix(net): ensure validation is consistent<commit_after>package ovpm

import (
	"encoding/binary"
	"errors"
	"fmt"
	"log"
	"net"

	"time"

	"github.com/Sirupsen/logrus"
	"github.com/asaskevich/govalidator"
	"github.com/coreos/go-iptables/iptables"
	"github.com/jinzhu/gorm"
)

// NetworkType distinguishes different types of networks that is defined in the networks table.
type NetworkType uint

// NetworkTypes
const (
	UNDEFINEDNET NetworkType = iota
	SERVERNET
	ROUTE
)

var networkTypes = [...]struct {
	Type        NetworkType
	String      string
	Description string
}{
	{UNDEFINEDNET, "UNDEFINEDNET", "unknown network type"},
	{SERVERNET, "SERVERNET", "network behind vpn server"},
	{ROUTE, "ROUTE", "network to be pushed as route"},
}

// NetworkTypeFromString returns string representation of the network type.
func NetworkTypeFromString(typ string) NetworkType {
	for _, v := range networkTypes {
		if v.String == typ {
			return v.Type
		}
	}
	return UNDEFINEDNET
}

// GetAllNetworkTypes returns all network types defined in the system.
func GetAllNetworkTypes() []NetworkType {
	var networkTypeList []NetworkType
	for _, v := range networkTypes {
		networkTypeList = append(networkTypeList, v.Type)
	}
	return networkTypeList
}

func (nt NetworkType) String() string {
	for _, v := range networkTypes {
		if v.Type == nt {
			return v.String
		}
	}
	return "UNDEFINEDNET"
}

// Description gives description about the network type.
func (nt NetworkType) Description() string {
	for _, v := range networkTypes {
		if v.Type == nt {
			return v.Description
		}
	}
	return "UNDEFINEDNET"
}

// dbNetworkModel is database model for external networks on the VPN server.
type dbNetworkModel struct {
	gorm.Model
	ServerID uint
	Server   dbServerModel

	Name  string `gorm:"unique_index"`
	CIDR  string
	Type  NetworkType
	Via   string
	Users []*dbUserModel `gorm:"many2many:network_users;"`
}

// Network represents a VPN related network.
type Network struct {
	dbNetworkModel
}

// GetNetwork returns a network specified by its name.
func GetNetwork(name string) (*Network, error) {
	if !IsInitialized() {
		return nil, fmt.Errorf("you first need to create server")
	}
	// Validate user input.
	if govalidator.IsNull(name) {
		return nil, fmt.Errorf("validation error: %s can not be null", name)
	}
       	if !govalidator.Matches(name, "[\\w.]+") { // allow alphanumeric + dot
		return nil, fmt.Errorf("validation error: `%s` can only contain letters, numbers and dots", name)
	}


	var network dbNetworkModel
	db.Preload("Users").Where(&dbNetworkModel{Name: name}).First(&network)

	if db.NewRecord(&network) {
		return nil, fmt.Errorf("network not found %s", name)
	}

{'fraction_non_alphanumeric': 0.10757575757575757, 'fraction_numerical': 0.0003787878787878788, 'mean_word_length': 4.132167152575316, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6994157', 'n_tokens_mistral': 1746, 'n_tokens_neox': 1689, 'n_words': 523}