| 
						
						
							
								
							
						
						
					 | 
					@ -2,6 +2,7 @@ package assets | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					
 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					import ( | 
					 | 
					 | 
					import ( | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
						"XProxy/next/logger" | 
					 | 
					 | 
						"XProxy/next/logger" | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
						"github.com/robfig/cron" | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
						urlpkg "net/url" | 
					 | 
					 | 
						urlpkg "net/url" | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
						"strings" | 
					 | 
					 | 
						"strings" | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
						"sync" | 
					 | 
					 | 
						"sync" | 
				
			
			
		
	
	
		
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
					@ -12,63 +13,109 @@ var buildinAssets = map[string]string{ | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
						"geosite.dat": "/geosite.dat.xz", | 
					 | 
					 | 
						"geosite.dat": "/geosite.dat.xz", | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					} | 
					 | 
					 | 
					} | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					
 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					type UpdateSettings struct { | 
					 | 
					 | 
					type updateConfig struct { | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
						cron   string | 
					 | 
					 | 
						spec    string | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
						mutex  sync.Mutex | 
					 | 
					 | 
						cron    *cron.Cron | 
				
			
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
						renew   sync.Mutex | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
						running sync.Mutex | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
						proxy   *urlpkg.URL | 
					 | 
					 | 
						proxy   *urlpkg.URL | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
						assets  map[string]string | 
					 | 
					 | 
						assets  map[string]string | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					} | 
					 | 
					 | 
					} | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					
 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					var update UpdateSettings | 
					 | 
					 | 
					var update updateConfig | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					 | 
					 | 
					
 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					func assetsClone(raw map[string]string) map[string]string { | 
					 | 
					 | 
					//func assetsClone(raw map[string]string) map[string]string {
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
						assets := make(map[string]string, len(raw)) | 
					 | 
					 | 
					//	assets := make(map[string]string, len(raw))
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
						for file, url := range raw { | 
					 | 
					 | 
					//	for file, url := range raw {
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
							assets[file] = strings.Clone(url) | 
					 | 
					 | 
					//		assets[file] = strings.Clone(url)
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
						} | 
					 | 
					 | 
					//	}
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
						return assets | 
					 | 
					 | 
					//	return assets
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					} | 
					 | 
					 | 
					//}
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					 | 
					 | 
					
 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					func SetCron(cron string) error { | 
					 | 
					 | 
					// GetCron is used to obtain cron service specification.
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
						// TODO: setting up crond service
 | 
					 | 
					 | 
					func GetCron() string { | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
						return nil | 
					 | 
					 | 
						update.renew.Lock() | 
				
			
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
						spec := strings.Clone(update.spec) | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
						update.renew.Unlock() | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
						return spec | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					} | 
					 | 
					 | 
					} | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					
 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					func GetProxy() string { | 
					 | 
					 | 
					// SetCron is used to update cron service specification.
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
						update.mutex.Lock() | 
					 | 
					 | 
					func SetCron(spec string) error { | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
						proxy := update.proxy.String() | 
					 | 
					 | 
						if spec == update.spec { | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
						update.mutex.Unlock() | 
					 | 
					 | 
							return nil // cron spec without renew
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
						return proxy | 
					 | 
					 | 
						} | 
				
			
			
				
				
			
		
	
		
		
			
				
					 | 
					 | 
					} | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					 | 
					 | 
					
 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					func SetProxy(proxy string) error { | 
					 | 
					 | 
						var cs *cron.Cron | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
						var proxyUrl *urlpkg.URL // clear proxy by empty string
 | 
					 | 
					 | 
						if spec != "" { // update cron service
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
						if proxy != "" { | 
					 | 
					 | 
							cs = cron.New() | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
							url, err := urlpkg.Parse(proxy) | 
					 | 
					 | 
							err := cs.AddFunc(spec, func() { | 
				
			
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
								var entry *cron.Entry | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
								if entries := update.cron.Entries(); len(entries) != 0 && entries[0] != nil { | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
									entry = entries[0] | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
								} | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
								logger.Debugf("hello from cron") | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
								if entry != nil { | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
									logger.Debugf("Assets cron service next trigger -> `%s`", entry.Next) | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
								} | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
							}) | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
							if err != nil { | 
					 | 
					 | 
							if err != nil { | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
								logger.Errorf("Invalid proxy url `%s` -> %v", proxy, err) | 
					 | 
					 | 
								logger.Errorf("Invalid cron spec `%s` -> %v", spec, err) | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					 | 
					 | 
								return err | 
					 | 
					 | 
								return err | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
							} | 
					 | 
					 | 
							} | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
							proxyUrl = url | 
					 | 
					 | 
							cs.Start() | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					 | 
					 | 
						} | 
					 | 
					 | 
						} | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
						update.mutex.Lock() | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
						update.proxy = proxyUrl | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
						update.mutex.Unlock() | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
						return nil | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					} | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					
 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					func SetAssets(assets map[string]string) { | 
					 | 
					 | 
						update.renew.Lock() | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
						update.mutex.Lock() | 
					 | 
					 | 
						if update.cron != nil { | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
						update.assets = assetsClone(assets) | 
					 | 
					 | 
							update.cron.Stop() // stop old cron service
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
						update.mutex.Unlock() | 
					 | 
					 | 
						} | 
				
			
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
						update.cron = cs | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
						update.spec = spec | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
						if cs == nil { | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
							logger.Infof("Assets cron service has been terminated") | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
						} else { | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
							logger.Infof("Assets cron service has been updated -> `%s`", spec) | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
						} | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
						update.renew.Unlock() | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
						return nil | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					} | 
					 | 
					 | 
					} | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					
 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					func GetAssets() map[string]string { | 
					 | 
					 | 
					//func GetProxy() string {
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
						update.mutex.Lock() | 
					 | 
					 | 
					//	update.mutex.Lock()
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
						assets := assetsClone(update.assets) | 
					 | 
					 | 
					//	proxy := update.proxy.String()
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
						update.mutex.Unlock() | 
					 | 
					 | 
					//	update.mutex.Unlock()
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
						return assets | 
					 | 
					 | 
					//	return proxy
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					} | 
					 | 
					 | 
					//}
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					//
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					//func SetProxy(proxy string) error {
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					//	var proxyUrl *urlpkg.URL // clear proxy by empty string
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					//	if proxy != "" {
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					//		url, err := urlpkg.Parse(proxy)
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					//		if err != nil {
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					//			logger.Errorf("Invalid proxy url `%s` -> %v", proxy, err)
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					//			return err
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					//		}
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					//		proxyUrl = url
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					//	}
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					//	update.mutex.Lock()
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					//	update.proxy = proxyUrl
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					//	update.mutex.Unlock()
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					//	return nil
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					//}
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					//
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					//func SetAssets(assets map[string]string) {
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					//	update.mutex.Lock()
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					//	update.assets = assetsClone(assets)
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					//	update.mutex.Unlock()
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					//}
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					//
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					//func GetAssets() map[string]string {
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					//	update.mutex.Lock()
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					//	assets := assetsClone(update.assets)
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					//	update.mutex.Unlock()
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					//	return assets
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					 | 
					 | 
					 | 
					//}
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					
 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
					func LoadBuildin() { | 
					 | 
					 | 
					func LoadBuildin() { | 
				
			
			
		
	
		
		
			
				
					 | 
					 | 
						updateLocalAssets(buildinAssets, true) | 
					 | 
					 | 
						updateLocalAssets(buildinAssets, true) | 
				
			
			
		
	
	
		
		
			
				
					| 
						
							
								
							
						
						
						
					 | 
					
  |