diff --git a/cmd/main.go b/cmd/main.go index e3588cc..5de9047 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -8,6 +8,7 @@ import ( func main() { + log.Infof("------------------- Starting Polaris ---------------------") dbClient, err := db.Open() if err != nil { log.Panicf("init db error: %v", err) diff --git a/db/const.go b/db/const.go index d847273..f10af8a 100644 --- a/db/const.go +++ b/db/const.go @@ -9,6 +9,7 @@ const ( SettingJacketApiKey = "jacket_api_key" SettingDownloadDir = "download_dir" SettingLogLevel = "log_level" + SettingProxy = "proxy" ) const ( diff --git a/server/server.go b/server/server.go index 7c5114b..88dc981 100644 --- a/server/server.go +++ b/server/server.go @@ -43,6 +43,8 @@ type Server struct { func (s *Server) Serve() error { s.scheduler() s.reloadTasks() + s.restoreProxy() + s.jwtSerect = s.db.GetSetting(db.JwtSerectKey) //st, _ := fs.Sub(ui.Web, "build/web") s.r.Use(static.Serve("/", static.EmbedFolder(ui.Web, "build/web"))) diff --git a/server/setting.go b/server/setting.go index cbd3e44..a832031 100644 --- a/server/setting.go +++ b/server/setting.go @@ -2,6 +2,8 @@ package server import ( "fmt" + "net/http" + "net/url" "polaris/db" "polaris/log" "polaris/pkg/transmission" @@ -15,6 +17,7 @@ type GeneralSettings struct { TmdbApiKey string `json:"tmdb_api_key"` DownloadDir string `json:"download_dir"` LogLevel string `json:"log_level"` + Proxy string `json:"proxy"` } func (s *Server) SetSetting(c *gin.Context) (interface{}, error) { @@ -29,6 +32,7 @@ func (s *Server) SetSetting(c *gin.Context) (interface{}, error) { } } if in.DownloadDir != "" { + log.Info("set download dir to %s", in.DownloadDir) if err := s.db.SetSetting(db.SettingDownloadDir, in.DownloadDir); err != nil { return nil, errors.Wrap(err, "save download dir") } @@ -40,9 +44,30 @@ func (s *Server) SetSetting(c *gin.Context) (interface{}, error) { } } + + s.setProxy(in.Proxy) return nil, nil } +func (s *Server) setProxy(proxy string) { + proxyUrl, err := url.Parse(proxy) + tp := http.DefaultTransport.(*http.Transport) + if proxy == "" || err != nil { + log.Warnf("proxy url not valid, disabling: %v", proxy) + tp.Proxy = nil + s.db.SetSetting(db.SettingProxy, "") + } else { + log.Infof("set proxy to %v", proxy) + tp.Proxy = http.ProxyURL(proxyUrl) + s.db.SetSetting(db.SettingProxy, proxy) + } +} + +func (s *Server) restoreProxy() { + p := s.db.GetSetting(db.SettingProxy) + s.setProxy(p) +} + func (s *Server) GetSetting(c *gin.Context) (interface{}, error) { tmdb := s.db.GetSetting(db.SettingTmdbApiKey) downloadDir := s.db.GetSetting(db.SettingDownloadDir) @@ -51,6 +76,7 @@ func (s *Server) GetSetting(c *gin.Context) (interface{}, error) { TmdbApiKey: tmdb, DownloadDir: downloadDir, LogLevel: logLevel, + Proxy: s.db.GetSetting(db.SettingProxy), }, nil } diff --git a/ui/lib/providers/settings.dart b/ui/lib/providers/settings.dart index 0d4b82b..1aebd82 100644 --- a/ui/lib/providers/settings.dart +++ b/ui/lib/providers/settings.dart @@ -50,14 +50,17 @@ class GeneralSetting { String? tmdbApiKey; String? downloadDIr; String? logLevel; + String? proxy; - GeneralSetting({this.tmdbApiKey, this.downloadDIr, this.logLevel}); + GeneralSetting( + {this.tmdbApiKey, this.downloadDIr, this.logLevel, this.proxy}); factory GeneralSetting.fromJson(Map json) { return GeneralSetting( tmdbApiKey: json["tmdb_api_key"], downloadDIr: json["download_dir"], - logLevel: json["log_level"]); + logLevel: json["log_level"], + proxy: json["proxy"]); } Map toJson() { @@ -65,6 +68,7 @@ class GeneralSetting { data['tmdb_api_key'] = tmdbApiKey; data['download_dir'] = downloadDIr; data["log_level"] = logLevel; + data["proxy"] = proxy; return data; } } diff --git a/ui/lib/settings.dart b/ui/lib/settings.dart index e84cd23..71a0fb7 100644 --- a/ui/lib/settings.dart +++ b/ui/lib/settings.dart @@ -36,14 +36,14 @@ class _SystemSettingsPageState extends ConsumerState { initialValue: { "tmdb_api": v.tmdbApiKey, "download_dir": v.downloadDIr, - "log_level": v.logLevel + "log_level": v.logLevel, + "proxy": v.proxy, }, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ FormBuilderTextField( name: "tmdb_api", - autofocus: true, decoration: Commons.requiredTextFieldStyle( text: "TMDB Api Key", icon: const Icon(Icons.key)), // @@ -51,12 +51,16 @@ class _SystemSettingsPageState extends ConsumerState { ), FormBuilderTextField( name: "download_dir", - autofocus: true, decoration: Commons.requiredTextFieldStyle( - text: "下载路径", icon: const Icon(Icons.folder)), + text: "下载路径", icon: const Icon(Icons.folder), helperText: "媒体文件临时下载路径,非最终存储路径"), // validator: FormBuilderValidators.required(), ), + FormBuilderTextField( + name: "proxy", + decoration: const InputDecoration( + labelText: "代理地址", icon: Icon(Icons.folder), helperText: "后台联网代理地址,留空表示不启用代理"), + ), SizedBox( width: 300, child: FormBuilderDropdown( @@ -66,13 +70,10 @@ class _SystemSettingsPageState extends ConsumerState { icon: Icon(Icons.file_present_rounded), ), items: const [ - DropdownMenuItem( - value: "debug", child: Text("DEBUG")), + DropdownMenuItem(value: "debug", child: Text("DEBUG")), DropdownMenuItem(value: "info", child: Text("INFO")), - DropdownMenuItem( - value: "warn", child: Text("WARNING")), - DropdownMenuItem( - value: "error", child: Text("ERROR")), + DropdownMenuItem(value: "warn", child: Text("WARN")), + DropdownMenuItem(value: "error", child: Text("ERROR")), ], validator: FormBuilderValidators.required(), ), @@ -93,7 +94,8 @@ class _SystemSettingsPageState extends ConsumerState { .updateSettings(GeneralSetting( tmdbApiKey: values["tmdb_api"], downloadDIr: values["download_dir"], - logLevel: values["log_level"])); + logLevel: values["log_level"], + proxy: values["proxy"])); f.then((v) { Utils.showSnakeBar("更新成功"); }).onError((e, s) { diff --git a/ui/lib/system_page.dart b/ui/lib/system_page.dart index 4ba78f7..b36b374 100644 --- a/ui/lib/system_page.dart +++ b/ui/lib/system_page.dart @@ -37,7 +37,7 @@ class _SystemPageState extends ConsumerState { columns: const [ DataColumn(label: Text("日志")), DataColumn(label: Text("大小")), - DataColumn(label: Text("*")) + DataColumn(label: Text("下载")) ], rows: List.generate(list.length, (i) { final item = list[i]; diff --git a/ui/lib/widgets/widgets.dart b/ui/lib/widgets/widgets.dart index 0a01942..d3b2f6c 100644 --- a/ui/lib/widgets/widgets.dart +++ b/ui/lib/widgets/widgets.dart @@ -4,8 +4,10 @@ class Commons { static InputDecoration requiredTextFieldStyle({ required String text, Widget? icon, + String? helperText, }) { return InputDecoration( + helperText: helperText, label: Row( children: [ Text(text),