-
Notifications
You must be signed in to change notification settings - Fork 8
/
ini.go
172 lines (161 loc) · 4.76 KB
/
ini.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package utils
import (
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
"github.com/go-ini/ini"
)
func ConvertJsonToIni(filePath string) error {
var err error
if err = WriteIniConfig(filePath, nil); err != nil {
panic(err)
}
return err
}
func IniGetTokenID(filePath string) (string, string, error) {
var err error
var cfg *ini.File
if cfg, err = ini.Load(filePath); err != nil {
panic(err)
}
section_Account := cfg.Section("Account")
id := section_Account.Key("ID").String()
token := section_Account.Key("Token").String()
return token, id, nil
}
func UpdateIniConfig(filePath string) error {
var token, id string
var err error
var body []byte
var ReadedFile Response
if token, id, err = IniGetTokenID(filePath); err != nil {
panic(err)
}
if body, err = request([]byte(``), token, id, "update"); err != nil {
panic(err)
}
if err = json.Unmarshal(body, &ReadedFile); err != nil {
panic(err)
}
ReadedFile.Token = token
if err = WriteIniConfig(filePath, &ReadedFile); err != nil {
panic(err)
}
return nil
}
func WriteIniConfig(filePath string, ReadedFile *Response) error {
var err error
var section_Account, section_Usage, section_Config *ini.Section
var body []byte
var cfg *ini.File
var fileType string
if fileType, err = GetFileType(filePath); err != nil {
panic(err)
}
if fileType == "ini" {
if cfg, err = ini.Load(filePath); err != nil {
panic(err)
}
section_Config = cfg.Section("Config")
ReadedFile.Config.ReservedDec, ReadedFile.Config.ReservedHex = clientIDtoReserved(section_Config.Key("ClientID").String())
ReadedFile.Config.PrivateKey = section_Config.Key("PrivateKey").String()
} else {
body = ReadConfig(filePath)
if err = json.Unmarshal(body, &ReadedFile); err != nil {
panic(err)
}
}
cfg = ini.Empty()
if section_Account, err = cfg.NewSection("Account"); err != nil {
panic(err)
}
section_Account.NewKey("ID", ReadedFile.ID)
section_Account.NewKey("Token", ReadedFile.Token)
if section_Usage, err = cfg.NewSection("Usage"); err != nil {
panic(err)
}
section_Usage.NewKey("PremiumData", fmt.Sprint(float32(ReadedFile.Account.PremiumData)/1000/1000/1000, "GB"))
section_Usage.NewKey("License", fmt.Sprint(ReadedFile.Account.License))
if section_Config, err = cfg.NewSection("Config"); err != nil {
panic(err)
}
section_Config.NewKey("PrivateKey", ReadedFile.Config.PrivateKey)
section_Config.NewKey("IPv4", fmt.Sprint(ReadedFile.Config.Interface.Addresses.V4))
section_Config.NewKey("IPv6", fmt.Sprint(ReadedFile.Config.Interface.Addresses.V6))
section_Config.NewKey("ClientID", fmt.Sprint(ReadedFile.Config.ClientID))
section_Config.NewKey("ReservedHex", fmt.Sprint(ReadedFile.Config.ReservedHex))
section_Config.NewKey("ReservedDec", strings.Join(strings.Fields(fmt.Sprint(ReadedFile.Config.ReservedDec)), ", "))
filePath = strings.Replace(filePath, ".json", ".ini", 1)
cfg.SaveTo(filePath)
os.Chmod(filePath, 0600)
return nil
}
func GetFileType(filePath string) (string, error) {
content, err := os.ReadFile(filePath)
if err != nil {
panic(err)
}
var jsonData Response
err = json.Unmarshal(content, &jsonData)
if err == nil {
return "json", nil
}
var cfg *ini.File
if cfg, err = ini.Load(filePath); err != nil {
panic(err)
}
section := cfg.Section("Account")
if id := section.Key("ID").String(); id != "" {
return "ini", nil
}
panic("File type not supported")
}
func ConvertIniToJson(filePath string) error {
var err error
var token, id string
if token, id, err = IniGetTokenID(filePath); err != nil {
panic(err)
}
jsonFilePath := strings.Replace(filePath, ".ini", ".json", 1)
if err = os.WriteFile(jsonFilePath, []byte(`{"id":"`+id+`","token":"`+token+`"}`), 0600); err != nil {
panic(err)
}
if UpdateConfigFile(jsonFilePath); err != nil {
panic(err)
}
os.Chmod(jsonFilePath, 0600)
var ReadedFile Response
if err = json.Unmarshal(ReadConfig(jsonFilePath), &ReadedFile); err != nil {
panic(err)
}
var cfg *ini.File
if cfg, err = ini.Load(filePath); err != nil {
panic(err)
}
section_Config := cfg.Section("Config")
ReadedFile.Config.PrivateKey = section_Config.Key("PrivateKey").String()
ReadedFile.Config.ClientID = section_Config.Key("ClientID").String()
ReadedFile.Config.ReservedHex = section_Config.Key("ReservedHex").String()
str := strings.Trim(section_Config.Key("ReservedDec").String(), "[]")
strSlice := strings.Split(str, ",")
intSlice := make([]int, len(strSlice))
for i, s := range strSlice {
s = strings.TrimSpace(s)
num, err := strconv.Atoi(s)
if err != nil {
panic(err)
}
intSlice[i] = num
}
ReadedFile.Config.ReservedDec = intSlice
var updatedContent []byte
if updatedContent, err = json.MarshalIndent(ReadedFile, "", " "); err != nil {
panic(err)
}
if err = os.WriteFile(jsonFilePath, updatedContent, 0600); err != nil {
panic(err)
}
return err
}