proxy_settings.go 3.2 KB

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