-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
120 lines (102 loc) · 3.76 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package config
import (
"crypto/ecdsa"
"crypto/x509"
"encoding/base64"
"encoding/json"
"encoding/pem"
"fmt"
"os"
)
// Config represents the application configuration structure, containing essential details such as keys, endpoints, and access tokens.
type Config struct {
PrivateKey string `json:"private_key"` // Base64-encoded ECDSA private key
EndpointV4 string `json:"endpoint_v4"` // IPv4 address of the endpoint
EndpointV6 string `json:"endpoint_v6"` // IPv6 address of the endpoint
EndpointPubKey string `json:"endpoint_pub_key"` // PEM-encoded ECDSA public key of the endpoint to verify against
License string `json:"license"` // Application license key
ID string `json:"id"` // Device unique identifier
AccessToken string `json:"access_token"` // Authentication token for API access
IPv4 string `json:"ipv4"` // Assigned IPv4 address
IPv6 string `json:"ipv6"` // Assigned IPv6 address
}
// AppConfig holds the global application configuration.
var AppConfig Config
// ConfigLoaded indicates whether the configuration has been successfully loaded.
var ConfigLoaded bool
// LoadConfig loads the application configuration from a JSON file.
//
// Parameters:
// - configPath: string - The path to the configuration JSON file.
//
// Returns:
// - error: An error if the configuration file cannot be loaded or parsed.
func LoadConfig(configPath string) error {
file, err := os.Open(configPath)
if err != nil {
return fmt.Errorf("failed to open config file: %v", err)
}
defer file.Close()
decoder := json.NewDecoder(file)
if err := decoder.Decode(&AppConfig); err != nil {
return fmt.Errorf("failed to decode config file: %v", err)
}
ConfigLoaded = true
return nil
}
// SaveConfig writes the current application configuration to a prettified JSON file.
//
// Parameters:
// - configPath: string - The path to save the configuration JSON file.
//
// Returns:
// - error: An error if the configuration file cannot be written.
func (*Config) SaveConfig(configPath string) error {
file, err := os.Create(configPath)
if err != nil {
return fmt.Errorf("failed to create config file: %v", err)
}
defer file.Close()
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ")
if err := encoder.Encode(AppConfig); err != nil {
return fmt.Errorf("failed to encode config file: %v", err)
}
return nil
}
// GetEcPrivateKey retrieves the ECDSA private key from the stored Base64-encoded string.
//
// Returns:
// - *ecdsa.PrivateKey: The parsed ECDSA private key.
// - error: An error if decoding or parsing the private key fails.
func (*Config) GetEcPrivateKey() (*ecdsa.PrivateKey, error) {
privKeyB64, err := base64.StdEncoding.DecodeString(AppConfig.PrivateKey)
if err != nil {
return nil, fmt.Errorf("failed to decode private key: %v", err)
}
privKey, err := x509.ParseECPrivateKey(privKeyB64)
if err != nil {
return nil, fmt.Errorf("failed to parse private key: %v", err)
}
return privKey, nil
}
// GetEcEndpointPublicKey retrieves the ECDSA public key from the stored PEM-encoded string.
//
// Returns:
// - *ecdsa.PublicKey: The parsed ECDSA public key.
// - error: An error if decoding or parsing the public key fails.
func (*Config) GetEcEndpointPublicKey() (*ecdsa.PublicKey, error) {
endpointPubKeyB64, _ := pem.Decode([]byte(AppConfig.EndpointPubKey))
if endpointPubKeyB64 == nil {
return nil, fmt.Errorf("failed to decode endpoint public key")
}
pubKey, err := x509.ParsePKIXPublicKey(endpointPubKeyB64.Bytes)
if err != nil {
return nil, fmt.Errorf("failed to parse public key: %v", err)
}
ecPubKey, ok := pubKey.(*ecdsa.PublicKey)
if !ok {
return nil, fmt.Errorf("failed to assert public key as ECDSA")
}
return ecPubKey, nil
}