Guest User

Untitled

a guest
Jul 28th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. # -*- coding: utf-8
  2.  
  3. miquire :addon, 'addon'
  4. miquire :core, 'environment'
  5. miquire :addon, 'settings'
  6. require 'net/http'
  7.  
  8. module Net
  9. class <<HTTP
  10. alias new_org new
  11. def new(host, port=80, proxy_addr=nil, proxy_port=nil, proxy_user=nil, proxy_pass=nil)
  12. case UserConfig[:proxy_enabled]
  13. when :specify
  14. pu, pp = nil, nil
  15. pu, pp = UserConfig[:proxy_user], UserConfig[:proxy_password] if UserConfig[:proxy_cert]
  16.  
  17. new_org(host, port, UserConfig[:proxy_server], UserConfig[:proxy_port].to_i, pu, pp)
  18. when :disable
  19. new_org(host, port)
  20. else
  21. new_org(host, port, *get_env_proxy_settings)end
  22. end
  23.  
  24. def get_env_proxy_settings
  25. env_proxy_settings = (ENV["HTTP_PROXY"] || '').sub(/http:\/\//, '').split(/[@:]/)
  26. case(env_proxy_settings.size)
  27. when 2
  28. [env_proxy_settings[0], env_proxy_settings[1].to_i]
  29. when 4
  30. [env_proxy_settings[2], env_proxy_settings[3].to_i, env_proxy_settings[0], env_proxy_settings[1]]
  31. else
  32. [] end end
  33. memoize :_env_proxy_settings
  34. end
  35. end
  36.  
  37. Module.new do
  38.  
  39. def self.boot
  40. plugin = Plugin::create(:proxy)
  41. plugin.add_event(:boot) { |service|
  42. Plugin.call(:setting_tab_regist, settings, "プロキシ")
  43. }
  44. UserConfig.connect(:proxy_enabled) { |key, new_val, before_val, id|
  45. #UserStreamを使っているなら繋ぎなおさせる
  46. if UserConfig[:realtime_rewind]
  47. Thread.new {
  48. UserConfig[:realtime_rewind] = false
  49. sleep(3)
  50. UserConfig[:realtime_rewind] = true
  51. }
  52. end
  53. }
  54. end
  55.  
  56. def self.settings
  57. box = Gtk::VBox.new(false, 8)
  58.  
  59. radio_tag = radio_specify = Gtk::RadioButton.new('自分で設定する')
  60. radio_envval = Gtk::RadioButton.new(radio_tag, '環境変数の設定を使う')
  61. radio_disable = Gtk::RadioButton.new(radio_tag, 'プロキシを使わない')
  62.  
  63. case UserConfig[:proxy_enabled]
  64. when :specify
  65. radio_specify.active = true
  66. when :disable
  67. radio_disable.active = true
  68. else
  69. radio_envval.active = true end
  70.  
  71. box.
  72. closeup(gen_group(:specify, radio_specify)).
  73. closeup(gen_group(:envval, radio_envval)).
  74. closeup(gen_record(:disable, radio_disable))
  75. end
  76.  
  77. def self.gen_record(name, radio)
  78. radio.signal_connect('toggled'){ |widget|
  79. UserConfig[:proxy_enabled] = name }
  80. radio
  81. end
  82.  
  83. def self.gen_group(name, radio)
  84. eventbox = __send__("gen_#{name}_ev")
  85. radio.signal_connect('toggled'){ |widget|
  86. UserConfig[:proxy_enabled] = name
  87. eventbox.sensitive = widget.active? }
  88. eventbox.sensitive = UserConfig[:proxy_enabled] == name
  89. Mtk::group(radio, eventbox)
  90. end
  91.  
  92. def self.gen_specify_ev
  93. sv = Mtk.input(:proxy_server, "サーバ")
  94. pt = Mtk.input(:proxy_port, "ポート")
  95. us = Mtk.input(:proxy_user, "ユーザ")
  96. pw = Mtk.input(:proxy_password, "パスワード")
  97. ct = Mtk.boolean(:proxy_cert, "ユーザ認証が必要")
  98.  
  99. auth_group = Mtk::group(ct, us, pw)
  100. UserConfig.connect(:proxy_cert){ |key, new_val, before_val, id|
  101. us.sensitive = pw.sensitive = new_val }
  102.  
  103. Gtk::EventBox.new.add(Gtk::VBox.new(false, 8).closeup(sv).closeup(pt).closeup(auth_group))
  104. end
  105.  
  106. def self.gen_envval_ev
  107. c = lambda{ |index|
  108. lambda{|x| Net::HTTP.get_env_proxy_settings[index].to_s } }
  109. sv = Mtk.input(c[0], "サーバ"){|c,i|i.sensitive = false}
  110. pt = Mtk.input(c[1], "ポート"){|c,i|i.sensitive = false}
  111. us = Mtk.input(c[2], "ユーザ"){|c,i|i.sensitive = false}
  112. pw = Mtk.input(c[3], "パスワード"){|c,i|i.sensitive = false}
  113. Gtk::EventBox.new.add(Gtk::VBox.new(false, 8).closeup(sv).closeup(pt).closeup(us).closeup(pw))
  114. end
  115.  
  116. boot
  117. end
Add Comment
Please, Sign In to add comment