2021-03-26 00:17:52 +08:00
|
|
|
package models
|
|
|
|
|
2022-01-29 04:33:30 +08:00
|
|
|
import (
|
2022-06-28 01:51:09 +08:00
|
|
|
"strings"
|
2022-09-14 03:25:56 +08:00
|
|
|
"time"
|
2022-06-28 01:51:09 +08:00
|
|
|
|
2022-01-29 04:33:30 +08:00
|
|
|
jwt "github.com/golang-jwt/jwt/v4"
|
2022-04-26 04:30:18 +08:00
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
2022-01-29 04:33:30 +08:00
|
|
|
)
|
2021-03-26 00:17:52 +08:00
|
|
|
|
2021-11-17 01:20:48 +08:00
|
|
|
const PLACEHOLDER_KEY_TEXT = "ACCESS_KEY"
|
|
|
|
const PLACEHOLDER_TOKEN_TEXT = "ACCESS_TOKEN"
|
|
|
|
|
2022-09-01 21:45:54 +08:00
|
|
|
// CustomExtClient - struct for CustomExtClient params
|
|
|
|
type CustomExtClient struct {
|
|
|
|
ClientID string `json:"clientid"`
|
|
|
|
}
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// AuthParams - struct for auth params
|
2021-03-26 00:17:52 +08:00
|
|
|
type AuthParams struct {
|
2021-04-30 06:14:13 +08:00
|
|
|
MacAddress string `json:"macaddress"`
|
2022-01-11 06:52:21 +08:00
|
|
|
ID string `json:"id"`
|
2021-04-30 06:14:13 +08:00
|
|
|
Password string `json:"password"`
|
2021-03-26 00:17:52 +08:00
|
|
|
}
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// User struct - struct for Users
|
2021-03-26 00:17:52 +08:00
|
|
|
type User struct {
|
2022-06-28 01:51:09 +08:00
|
|
|
UserName string `json:"username" bson:"username" validate:"min=3,max=40,in_charset|email"`
|
2021-07-24 06:24:34 +08:00
|
|
|
Password string `json:"password" bson:"password" validate:"required,min=5"`
|
2021-07-02 12:03:46 +08:00
|
|
|
Networks []string `json:"networks" bson:"networks"`
|
2021-07-24 06:24:34 +08:00
|
|
|
IsAdmin bool `json:"isadmin" bson:"isadmin"`
|
2022-09-14 03:25:56 +08:00
|
|
|
Groups []string `json:"groups" bson:"groups" yaml:"groups"`
|
2021-07-24 06:24:34 +08:00
|
|
|
}
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// ReturnUser - return user struct
|
2021-07-24 06:24:34 +08:00
|
|
|
type ReturnUser struct {
|
2022-06-28 01:51:09 +08:00
|
|
|
UserName string `json:"username" bson:"username"`
|
2021-07-24 06:24:34 +08:00
|
|
|
Networks []string `json:"networks" bson:"networks"`
|
|
|
|
IsAdmin bool `json:"isadmin" bson:"isadmin"`
|
2022-09-14 03:25:56 +08:00
|
|
|
Groups []string `json:"groups" bson:"groups"`
|
2021-03-26 00:17:52 +08:00
|
|
|
}
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// UserAuthParams - user auth params struct
|
2021-03-26 00:17:52 +08:00
|
|
|
type UserAuthParams struct {
|
2021-04-30 06:14:13 +08:00
|
|
|
UserName string `json:"username"`
|
|
|
|
Password string `json:"password"`
|
2021-03-26 00:17:52 +08:00
|
|
|
}
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// UserClaims - user claims struct
|
2021-03-26 00:17:52 +08:00
|
|
|
type UserClaims struct {
|
2021-04-30 06:14:13 +08:00
|
|
|
IsAdmin bool
|
|
|
|
UserName string
|
2021-07-02 12:03:46 +08:00
|
|
|
Networks []string
|
2022-09-14 03:25:56 +08:00
|
|
|
Groups []string
|
2022-06-27 22:47:28 +08:00
|
|
|
jwt.RegisteredClaims
|
2021-03-26 00:17:52 +08:00
|
|
|
}
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// SuccessfulUserLoginResponse - successlogin struct
|
2021-03-26 00:17:52 +08:00
|
|
|
type SuccessfulUserLoginResponse struct {
|
2021-04-30 06:14:13 +08:00
|
|
|
UserName string
|
|
|
|
AuthToken string
|
2021-03-26 00:17:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Claims is a struct that will be encoded to a JWT.
|
|
|
|
// jwt.StandardClaims is an embedded type to provide expiry time
|
|
|
|
type Claims struct {
|
2022-01-11 06:52:21 +08:00
|
|
|
ID string
|
2021-04-30 06:14:13 +08:00
|
|
|
MacAddress string
|
2022-01-11 06:52:21 +08:00
|
|
|
Network string
|
2022-06-27 22:47:28 +08:00
|
|
|
jwt.RegisteredClaims
|
2021-03-26 00:17:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// SuccessfulLoginResponse is struct to send the request response
|
|
|
|
type SuccessfulLoginResponse struct {
|
2022-05-23 22:57:42 +08:00
|
|
|
ID string
|
|
|
|
AuthToken string
|
2021-03-26 00:17:52 +08:00
|
|
|
}
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// ErrorResponse is struct for error
|
2021-03-26 00:17:52 +08:00
|
|
|
type ErrorResponse struct {
|
2021-04-30 06:14:13 +08:00
|
|
|
Code int
|
|
|
|
Message string
|
2021-03-26 00:17:52 +08:00
|
|
|
}
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// NodeAuth - struct for node auth
|
2021-03-26 00:17:52 +08:00
|
|
|
type NodeAuth struct {
|
2021-04-30 06:14:13 +08:00
|
|
|
Network string
|
|
|
|
Password string
|
2022-01-11 06:52:21 +08:00
|
|
|
MacAddress string // Depricated
|
|
|
|
ID string
|
2021-03-26 00:17:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// SuccessResponse is struct for sending error message with code.
|
|
|
|
type SuccessResponse struct {
|
2021-04-30 06:14:13 +08:00
|
|
|
Code int
|
|
|
|
Message string
|
|
|
|
Response interface{}
|
2021-03-26 00:17:52 +08:00
|
|
|
}
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// AccessKey - access key struct
|
2021-03-26 00:17:52 +08:00
|
|
|
type AccessKey struct {
|
2022-09-14 03:25:56 +08:00
|
|
|
Name string `json:"name" bson:"name" validate:"omitempty,max=345"`
|
|
|
|
Value string `json:"value" bson:"value" validate:"omitempty,alphanum,max=16"`
|
|
|
|
AccessString string `json:"accessstring" bson:"accessstring"`
|
|
|
|
Uses int `json:"uses" bson:"uses" validate:"numeric,min=0"`
|
|
|
|
Expiration *time.Time `json:"expiration" bson:"expiration"`
|
2021-03-26 00:17:52 +08:00
|
|
|
}
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// DisplayKey - what is displayed for key
|
2021-03-26 00:17:52 +08:00
|
|
|
type DisplayKey struct {
|
2021-04-30 06:14:13 +08:00
|
|
|
Name string `json:"name" bson:"name"`
|
|
|
|
Uses int `json:"uses" bson:"uses"`
|
2021-03-26 00:17:52 +08:00
|
|
|
}
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// GlobalConfig - global config
|
2021-04-06 02:47:07 +08:00
|
|
|
type GlobalConfig struct {
|
2022-04-22 03:53:44 +08:00
|
|
|
Name string `json:"name" bson:"name"`
|
2021-04-06 02:47:07 +08:00
|
|
|
}
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// CheckInResponse - checkin response
|
2021-04-30 06:14:13 +08:00
|
|
|
type CheckInResponse struct {
|
|
|
|
Success bool `json:"success" bson:"success"`
|
|
|
|
NeedPeerUpdate bool `json:"needpeerupdate" bson:"needpeerupdate"`
|
|
|
|
NeedConfigUpdate bool `json:"needconfigupdate" bson:"needconfigupdate"`
|
|
|
|
NeedKeyUpdate bool `json:"needkeyupdate" bson:"needkeyupdate"`
|
|
|
|
NeedDelete bool `json:"needdelete" bson:"needdelete"`
|
|
|
|
NodeMessage string `json:"nodemessage" bson:"nodemessage"`
|
|
|
|
IsPending bool `json:"ispending" bson:"ispending"`
|
2021-03-26 00:17:52 +08:00
|
|
|
}
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// PeersResponse - peers response
|
2021-03-26 00:17:52 +08:00
|
|
|
type PeersResponse struct {
|
2021-08-31 03:58:23 +08:00
|
|
|
PublicKey string `json:"publickey" bson:"publickey"`
|
|
|
|
Endpoint string `json:"endpoint" bson:"endpoint"`
|
|
|
|
Address string `json:"address" bson:"address"`
|
|
|
|
Address6 string `json:"address6" bson:"address6"`
|
|
|
|
LocalAddress string `json:"localaddress" bson:"localaddress"`
|
2022-04-19 20:07:21 +08:00
|
|
|
LocalListenPort int32 `json:"locallistenport" bson:"locallistenport"`
|
2021-08-31 03:58:23 +08:00
|
|
|
IsEgressGateway string `json:"isegressgateway" bson:"isegressgateway"`
|
2021-07-27 22:48:58 +08:00
|
|
|
EgressGatewayRanges string `json:"egressgatewayrange" bson:"egressgatewayrange"`
|
2021-08-31 03:58:23 +08:00
|
|
|
ListenPort int32 `json:"listenport" bson:"listenport"`
|
|
|
|
KeepAlive int32 `json:"persistentkeepalive" bson:"persistentkeepalive"`
|
2021-07-24 06:24:34 +08:00
|
|
|
}
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// ExtPeersResponse - ext peers response
|
2021-07-24 06:24:34 +08:00
|
|
|
type ExtPeersResponse struct {
|
2022-04-19 20:07:21 +08:00
|
|
|
PublicKey string `json:"publickey" bson:"publickey"`
|
|
|
|
Endpoint string `json:"endpoint" bson:"endpoint"`
|
|
|
|
Address string `json:"address" bson:"address"`
|
|
|
|
Address6 string `json:"address6" bson:"address6"`
|
|
|
|
LocalAddress string `json:"localaddress" bson:"localaddress"`
|
|
|
|
LocalListenPort int32 `json:"locallistenport" bson:"locallistenport"`
|
|
|
|
ListenPort int32 `json:"listenport" bson:"listenport"`
|
|
|
|
KeepAlive int32 `json:"persistentkeepalive" bson:"persistentkeepalive"`
|
2021-03-26 00:17:52 +08:00
|
|
|
}
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// EgressGatewayRequest - egress gateway request
|
2021-05-20 01:59:10 +08:00
|
|
|
type EgressGatewayRequest struct {
|
2022-09-01 20:10:49 +08:00
|
|
|
NodeID string `json:"nodeid" bson:"nodeid"`
|
|
|
|
NetID string `json:"netid" bson:"netid"`
|
|
|
|
NatEnabled string `json:"natenabled" bson:"natenabled"`
|
|
|
|
Ranges []string `json:"ranges" bson:"ranges"`
|
|
|
|
Interface string `json:"interface" bson:"interface"`
|
|
|
|
PostUp string `json:"postup" bson:"postup"`
|
|
|
|
PostDown string `json:"postdown" bson:"postdown"`
|
2021-04-13 11:19:01 +08:00
|
|
|
}
|
2021-09-14 08:25:27 +08:00
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// RelayRequest - relay request struct
|
2021-09-14 08:25:27 +08:00
|
|
|
type RelayRequest struct {
|
2021-09-18 22:33:14 +08:00
|
|
|
NodeID string `json:"nodeid" bson:"nodeid"`
|
|
|
|
NetID string `json:"netid" bson:"netid"`
|
2021-09-18 23:01:34 +08:00
|
|
|
RelayAddrs []string `json:"relayaddrs" bson:"relayaddrs"`
|
2021-09-18 22:33:14 +08:00
|
|
|
}
|
2022-01-18 22:06:43 +08:00
|
|
|
|
|
|
|
// ServerUpdateData - contains data to configure server
|
|
|
|
// and if it should set peers
|
|
|
|
type ServerUpdateData struct {
|
|
|
|
UpdatePeers bool `json:"updatepeers" bson:"updatepeers"`
|
2022-01-18 23:14:27 +08:00
|
|
|
Node Node `json:"servernode" bson:"servernode"`
|
2022-01-18 22:06:43 +08:00
|
|
|
}
|
2022-01-21 06:50:42 +08:00
|
|
|
|
2022-01-21 06:52:49 +08:00
|
|
|
// Telemetry - contains UUID of the server and timestamp of last send to posthog
|
2022-01-30 07:04:36 +08:00
|
|
|
// also contains assymetrical encryption pub/priv keys for any server traffic
|
2022-01-21 06:50:42 +08:00
|
|
|
type Telemetry struct {
|
2022-01-30 04:02:37 +08:00
|
|
|
UUID string `json:"uuid" bson:"uuid"`
|
|
|
|
LastSend int64 `json:"lastsend" bson:"lastsend"`
|
|
|
|
TrafficKeyPriv []byte `json:"traffickeypriv" bson:"traffickeypriv"`
|
|
|
|
TrafficKeyPub []byte `json:"traffickeypub" bson:"traffickeypub"`
|
2022-01-21 06:50:42 +08:00
|
|
|
}
|
2022-01-26 23:40:39 +08:00
|
|
|
|
|
|
|
// ServerAddr - to pass to clients to tell server addresses and if it's the leader or not
|
|
|
|
type ServerAddr struct {
|
|
|
|
IsLeader bool `json:"isleader" bson:"isleader" yaml:"isleader"`
|
|
|
|
Address string `json:"address" bson:"address" yaml:"address"`
|
|
|
|
}
|
2022-01-29 04:33:30 +08:00
|
|
|
|
|
|
|
// TrafficKeys - struct to hold public keys
|
|
|
|
type TrafficKeys struct {
|
2022-01-30 04:02:37 +08:00
|
|
|
Mine []byte `json:"mine" bson:"mine" yaml:"mine"`
|
|
|
|
Server []byte `json:"server" bson:"server" yaml:"server"`
|
2022-01-29 04:33:30 +08:00
|
|
|
}
|
2022-04-26 04:30:18 +08:00
|
|
|
|
|
|
|
// NodeGet - struct for a single node get response
|
|
|
|
type NodeGet struct {
|
2022-06-01 00:07:56 +08:00
|
|
|
Node Node `json:"node" bson:"node" yaml:"node"`
|
|
|
|
Peers []wgtypes.PeerConfig `json:"peers" bson:"peers" yaml:"peers"`
|
|
|
|
ServerConfig ServerConfig `json:"serverconfig" bson:"serverconfig" yaml:"serverconfig"`
|
2022-09-14 03:25:56 +08:00
|
|
|
PeerIDs PeerMap `json:"peerids,omitempty" bson:"peerids,omitempty" yaml:"peerids,omitempty"`
|
2022-06-01 00:07:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServerConfig - struct for dealing with the server information for a netclient
|
|
|
|
type ServerConfig struct {
|
|
|
|
CoreDNSAddr string `yaml:"corednsaddr"`
|
2022-06-03 23:31:57 +08:00
|
|
|
API string `yaml:"api"`
|
2022-06-01 00:07:56 +08:00
|
|
|
APIPort string `yaml:"apiport"`
|
|
|
|
ClientMode string `yaml:"clientmode"`
|
|
|
|
DNSMode string `yaml:"dnsmode"`
|
|
|
|
Version string `yaml:"version"`
|
|
|
|
MQPort string `yaml:"mqport"`
|
|
|
|
Server string `yaml:"server"`
|
2022-09-23 00:59:59 +08:00
|
|
|
Is_EE bool `yaml:"isee"`
|
2022-04-26 04:30:18 +08:00
|
|
|
}
|
2022-06-28 01:51:09 +08:00
|
|
|
|
|
|
|
// User.NameInCharset - returns if name is in charset below or not
|
|
|
|
func (user *User) NameInCharSet() bool {
|
|
|
|
charset := "abcdefghijklmnopqrstuvwxyz1234567890-."
|
|
|
|
for _, char := range user.UserName {
|
|
|
|
if !strings.Contains(charset, strings.ToLower(string(char))) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2022-09-12 20:36:17 +08:00
|
|
|
|
2022-09-12 20:41:21 +08:00
|
|
|
// ServerIDs - struct to hold server ids.
|
2022-09-12 20:36:17 +08:00
|
|
|
type ServerIDs struct {
|
|
|
|
ServerIDs []string `json:"server_ids"`
|
|
|
|
}
|