proxy_settings.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package settings
  2. import (
  3. "fmt"
  4. "github.com/allanpk716/ChineseSubFinder/internal/pkg/local_http_proxy_server"
  5. "sync"
  6. )
  7. type ProxySettings struct {
  8. UseProxy bool `json:"use_proxy"` // 是否使用代理
  9. UseWhichProxyProtocol string `json:"use_which_proxy_protocol"` // 是使用 socks5 还是 http 代理
  10. LocalHttpProxyServerPort string `json:"local_http_proxy_server_port" default:"19036"` // 本地代理服务器端口
  11. InputProxyAddress string `json:"input_proxy_address"` // 输入的代理地址
  12. InputProxyPort string `json:"input_proxy_port"` // 输入的代理端口
  13. NeedPWD bool `json:"need_pwd"` // 是否使用用户名密码
  14. InputProxyUsername string `json:"input_proxy_username"` // 输入的代理用户名
  15. InputProxyPassword string `json:"input_proxy_password"` // 输入的代理密码
  16. Referer string `json:"-"` // 可能下载文件的时候需要设置
  17. localHttpProxyServer *local_http_proxy_server.LocalHttpProxyServer // 本地代理服务器
  18. locker sync.Mutex
  19. }
  20. func NewProxySettings(useProxy bool, useWhichProxyProtocol string,
  21. localHttpProxyServerPort string,
  22. inputProxyAddress string, inputProxyPort string,
  23. inputProxyUsername string, inputProxyPassword string) *ProxySettings {
  24. set := ProxySettings{UseProxy: useProxy, UseWhichProxyProtocol: useWhichProxyProtocol,
  25. LocalHttpProxyServerPort: localHttpProxyServerPort,
  26. InputProxyAddress: inputProxyAddress, InputProxyPort: inputProxyPort,
  27. InputProxyUsername: inputProxyUsername, InputProxyPassword: inputProxyPassword}
  28. if inputProxyUsername != "" && inputProxyPassword != "" {
  29. set.NeedPWD = true
  30. }
  31. return &set
  32. }
  33. func (p *ProxySettings) GetLocalHttpProxyUrl() string {
  34. defer p.locker.Unlock()
  35. p.locker.Lock()
  36. if p.UseProxy == false {
  37. return ""
  38. }
  39. if p.localHttpProxyServer == nil {
  40. p.localHttpProxyServer = local_http_proxy_server.NewLocalHttpProxyServer()
  41. }
  42. if p.localHttpProxyServer.IsRunning() == true {
  43. return p.localHttpProxyServer.LocalHttpProxyUrl
  44. }
  45. inputInfo := []string{
  46. p.UseWhichProxyProtocol,
  47. p.InputProxyAddress,
  48. p.InputProxyPort,
  49. }
  50. if p.InputProxyUsername != "" && p.InputProxyPassword != "" {
  51. inputInfo = append(inputInfo, p.InputProxyUsername, p.InputProxyPassword)
  52. }
  53. localHttpProxyUrl, err := p.localHttpProxyServer.Start(inputInfo, p.LocalHttpProxyServerPort)
  54. if err != nil {
  55. panic(fmt.Sprintln("start local http proxy server error:", err))
  56. return ""
  57. }
  58. return localHttpProxyUrl
  59. }
  60. func (p *ProxySettings) CloseLocalHttpProxyServer() error {
  61. defer p.locker.Unlock()
  62. p.locker.Lock()
  63. if p.localHttpProxyServer == nil {
  64. return nil
  65. }
  66. if p.localHttpProxyServer.IsRunning() == false {
  67. return nil
  68. }
  69. return p.localHttpProxyServer.Stop()
  70. }