proxy_settings.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package settings
  2. import (
  3. "fmt"
  4. "sync"
  5. "github.com/allanpk716/ChineseSubFinder/pkg/local_http_proxy_server"
  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) CopyOne() *ProxySettings {
  34. nowSettings := NewProxySettings(
  35. p.UseProxy, p.UseWhichProxyProtocol, p.LocalHttpProxyServerPort,
  36. p.InputProxyAddress, p.InputProxyPort,
  37. p.InputProxyUsername, p.InputProxyPassword)
  38. nowSettings.localHttpProxyServer = p.localHttpProxyServer
  39. return nowSettings
  40. }
  41. func (p *ProxySettings) GetLocalHttpProxyUrl() string {
  42. defer p.locker.Unlock()
  43. p.locker.Lock()
  44. if p.UseProxy == false {
  45. return ""
  46. }
  47. if p.localHttpProxyServer == nil {
  48. p.localHttpProxyServer = local_http_proxy_server.NewLocalHttpProxyServer()
  49. }
  50. if p.localHttpProxyServer.IsRunning() == true {
  51. return p.localHttpProxyServer.LocalHttpProxyUrl
  52. }
  53. inputInfo := []string{
  54. p.UseWhichProxyProtocol,
  55. p.InputProxyAddress,
  56. p.InputProxyPort,
  57. }
  58. if p.InputProxyUsername != "" && p.InputProxyPassword != "" {
  59. inputInfo = append(inputInfo, p.InputProxyUsername, p.InputProxyPassword)
  60. }
  61. localHttpProxyUrl, err := p.localHttpProxyServer.Start(inputInfo, p.LocalHttpProxyServerPort)
  62. if err != nil {
  63. panic(fmt.Sprintln("start local http proxy server error:", err))
  64. return ""
  65. }
  66. return localHttpProxyUrl
  67. }
  68. func (p *ProxySettings) CloseLocalHttpProxyServer() error {
  69. defer func() {
  70. println("CloseLocalHttpProxyServer Done")
  71. p.locker.Unlock()
  72. }()
  73. p.locker.Lock()
  74. if p.localHttpProxyServer == nil {
  75. return nil
  76. }
  77. if p.localHttpProxyServer.IsRunning() == false {
  78. return nil
  79. }
  80. return p.localHttpProxyServer.Stop()
  81. }