proxy-setup.bac 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. REM setup proxy server for wget
  2. REM Barry Kauler, my first BaCon GTK program using HUG
  3. REM (c) Copyright Barry Kauler Nov 2010, license GPL v3 /usr/share/doc/legal
  4. REM 20110314 internationalization and dpi improvements in BaCon
  5. REM 20110416 label alignment improvements.
  6. REM 130204 include hug.bac, avoid needs recompiling when hug.so updated. (but much bigger!)
  7. REM 130209 change back to hug.so.
  8. OPTION INTERNATIONAL TRUE
  9. REM .po/.mo files have 'charset=UTF-8', so either set UTF-8 on in LANG variable, or do this...
  10. SETENVIRON "OUTPUT_CHARSET", "UTF-8"
  11. REM use shared /usr/lib/hug.so instead of including hug.bac...
  12. INCLUDE "/usr/share/BaCon/hug_imports.bac"
  13. REM INCLUDE "/usr/share/BaCon/hug.bac",ATTACH,BUTTON,CALLBACK,CHECK,DISABLE,DISPLAY,ENABLE,FOCUS,FONT,GET,GRAB$,HIDE,HUGENTRY,HUGOPTIONS,IMAGE,INIT,MARK,MSGDIALOG,PASSWORD,PROPERTY,QUIT,SET,SHOW,TEXT,WINDOW
  14. INIT
  15. REM fix window font layout regardless of dpi...
  16. REM 78 is the Xft.dpi in /root/.Xresources when I designed the layout...
  17. HUGOPTIONS("BASEXFTDPI 78")
  18. REM User may have GTK theme font size too big/small. set to required size...
  19. HUGOPTIONS("FONT DejaVu Sans 12")
  20. font_mono$="Monospace 13"
  21. REM read the previous setting...
  22. proxy_flag=0
  23. http_proxy$="http://foo.org:8080"
  24. ftp_proxy$="ftp://foo.org:8080"
  25. username$=""
  26. password$=""
  27. IF FILEEXISTS("/etc/profile.d/http_proxy") THEN
  28. REM env vars may have username/password in them, split up...
  29. http_proxy$=EXEC$("cat /etc/profile.d/http_proxy | cut -f 2 -d '=' | sed -e 's%//.*@%//%' | tr '\n' ' ' | tr -d ' '")
  30. username$=EXEC$("cat /etc/profile.d/http_proxy | cut -f 2 -d '=' | grep '@' | sed -e 's%^.*//%%' -e 's%@.*$%%' -e 's%:.*%%' | tr '\n' ' ' | tr -d ' '")
  31. password$=EXEC$("cat /etc/profile.d/http_proxy | cut -f 2 -d '=' | grep ':' | sed -e 's%^.*//%%' -e 's%@.*$%%' -e 's%^.*:%%' | tr '\n' ' ' | tr -d ' '")
  32. proxy_flag=1
  33. ENDIF
  34. IF FILEEXISTS("/etc/profile.d/ftp_proxy") THEN
  35. REM env vars may have username/password in them, split up...
  36. ftp_proxy$=EXEC$("cat /etc/profile.d/ftp_proxy | cut -f 2 -d '=' | sed -e 's%//.*@%//%' | tr '\n' ' ' | tr -d ' '")
  37. username$=EXEC$("cat /etc/profile.d/ftp_proxy | cut -f 2 -d '=' | grep '@' | sed -e 's%^.*//%%' -e 's%@.*$%%' -e 's%:.*%%' | tr '\n' ' ' | tr -d ' '")
  38. password$=EXEC$("cat /etc/profile.d/ftp_proxy | cut -f 2 -d '=' | grep ':' | sed -e 's%^.*//%%' -e 's%@.*$%%' -e 's%^.*:%%' | tr '\n' ' ' | tr -d ' '")
  39. proxy_flag=1
  40. ENDIF
  41. SUB check1_callback
  42. LOCAL status
  43. status=GET(check1)
  44. IF status = 1 THEN
  45. ENABLE(httpproxy)
  46. ENABLE(ftpproxy)
  47. ENABLE(username)
  48. ENABLE(password1)
  49. ENABLE(password2)
  50. ELSE
  51. DISABLE(httpproxy)
  52. DISABLE(ftpproxy)
  53. DISABLE(username)
  54. DISABLE(password1)
  55. DISABLE(password2)
  56. ENDIF
  57. ENDSUB
  58. REM update the environment variables...
  59. SUB ok_callback
  60. LOCAL pass1$,pass2$
  61. status2=GET(check1)
  62. IF status2 = 0 THEN
  63. SYSTEM "rm -f /etc/profile.d/http_proxy"
  64. SYSTEM "rm -f /etc/profile.d/ftp_proxy"
  65. HIDE(mainwin)
  66. SHOW(no_dlg1)
  67. GOTO label1
  68. ENDIF
  69. httpproxy$=GRAB$(httpproxy)
  70. ftpproxy$=GRAB$(ftpproxy)
  71. username$=GRAB$(username)
  72. pass1$=GRAB$(password1)
  73. pass2$=GRAB$(password2)
  74. IF pass1$ != pass2$ THEN
  75. SHOW(err_dlg1)
  76. ELSE
  77. IF httpproxy$ != "" THEN
  78. REM take off the http:// part...
  79. protocol1$=LEFT$(httpproxy$,5)
  80. IF protocol1$ = "http:" THEN
  81. protocol2$="http://"
  82. httpproxy1$=MID$(httpproxy$,8)
  83. ELSE
  84. IF protocol1$ = "https" THEN
  85. protocol2$="https://"
  86. httpproxy1$=MID$(httpproxy$,9)
  87. ELSE
  88. protocol2$="http://"
  89. httpproxy1$=httpproxy$
  90. ENDIF
  91. ENDIF
  92. IF username$ = "" THEN
  93. httpproxy2$=httpproxy1$
  94. ELSE
  95. IF pass1$ = "" THEN
  96. httpproxy2$=CONCAT$(username$,"@",httpproxy1$)
  97. ELSE
  98. httpproxy2$=CONCAT$(username$,":",pass1$,"@",httpproxy1$)
  99. ENDIF
  100. ENDIF
  101. http_proxy_profile$=CONCAT$("export http_proxy=",protocol2$,httpproxy2$)
  102. OPEN "/etc/profile.d/http_proxy" FOR WRITING AS handle
  103. WRITELN http_proxy_profile$ TO handle
  104. CLOSE FILE handle
  105. ENDIF
  106. REM exactly same thing for ftp...
  107. IF ftpproxy$ != "" THEN
  108. REM take off the ftp:// part...
  109. protocol1$=LEFT$(ftpproxy$,4)
  110. IF protocol1$ = "ftp:" THEN
  111. protocol2$="ftp://"
  112. ftpproxy1$=MID$(ftpproxy$,7)
  113. ELSE
  114. IF protocol1$ = "ftps" THEN
  115. protocol2$="ftps://"
  116. ftpproxy1$=MID$(ftpproxy$,8)
  117. ELSE
  118. protocol2$="ftp://"
  119. ftpproxy1$=ftpproxy$
  120. ENDIF
  121. ENDIF
  122. IF username$ = "" THEN
  123. ftpproxy2$=ftpproxy1$
  124. ELSE
  125. IF pass1$ = "" THEN
  126. ftpproxy2$=CONCAT$(username$,"@",ftpproxy1$)
  127. ELSE
  128. ftpproxy2$=CONCAT$(username$,":",pass1$,"@",ftpproxy1$)
  129. ENDIF
  130. ENDIF
  131. ftp_proxy_profile$=CONCAT$("export ftp_proxy=",protocol2$,ftpproxy2$)
  132. OPEN "/etc/profile.d/ftp_proxy" FOR WRITING AS handle
  133. WRITELN ftp_proxy_profile$ TO handle
  134. CLOSE FILE handle
  135. ENDIF
  136. HIDE(mainwin)
  137. SHOW(yes_dlg1)
  138. ENDIF
  139. LABEL label1
  140. ENDSUB
  141. REM callback when click close-button in info boxes...
  142. SUB close_dialog(NUMBER widget)
  143. HIDE(widget)
  144. FOCUS(password1)
  145. ENDSUB
  146. mainwin = WINDOW(INTL$("Proxy server setup"), 425, 285)
  147. image1=IMAGE("/usr/local/lib/X11/pixmaps/www48.png",48,48)
  148. ATTACH(mainwin,image1,10,10)
  149. label_hdr1=MARK(INTL$("If you connect to the Internet through a proxy"),370,15)
  150. ATTACH(mainwin,label_hdr1,58,10)
  151. label_hdr2=MARK(INTL$("server, tick the checkbox and fill in the fields"),370,15)
  152. ATTACH(mainwin,label_hdr2,58,25)
  153. label_hdr3=MARK(INTL$("(leave Username/Password blank if not needed)"),370,15)
  154. ATTACH(mainwin,label_hdr3,58,40)
  155. check1 = CHECK(INTL$("Enable Internet connection through proxy server"),350,20)
  156. ATTACH(mainwin,check1,20,70)
  157. label_http = MARK("HTTP:",100,20)
  158. REM param 0.0 means left-align label, 0.5 centre-align, 1.0 right-align...
  159. PROPERTY(label_http,"xalign",1.0)
  160. ATTACH(mainwin, label_http, 10, 100)
  161. httpproxy = ENTRY(http_proxy$,280,20)
  162. ATTACH(mainwin, httpproxy, 120, 100)
  163. FONT(httpproxy,font_mono$)
  164. label_ftp = MARK("FTP:",100,20)
  165. PROPERTY(label_ftp,"xalign",1.0)
  166. ATTACH(mainwin, label_ftp, 10, 130)
  167. ftpproxy = ENTRY(ftp_proxy$,280,20)
  168. ATTACH(mainwin, ftpproxy, 120, 130)
  169. FONT(ftpproxy,font_mono$)
  170. label_user=MARK(INTL$("Username:"),100,20)
  171. PROPERTY(label_user,"xalign",1.0)
  172. ATTACH(mainwin,label_user,10,160)
  173. username = ENTRY(username$,280,20)
  174. ATTACH(mainwin, username, 120, 160)
  175. FONT(username,font_mono$)
  176. label_pass=MARK(INTL$("Password:"),100,20)
  177. PROPERTY(label_pass,"xalign",1.0)
  178. ATTACH(mainwin,label_pass,10,190)
  179. password1=PASSWORD(150,20)
  180. ATTACH(mainwin,password1,120,190)
  181. TEXT(password1,password$)
  182. FONT(password1,font_mono$)
  183. label_pass2=MARK(INTL$("Enter password again:"),170,20)
  184. ATTACH(mainwin,label_pass2,10,220)
  185. password2=PASSWORD(150,20)
  186. ATTACH(mainwin,password2,180,220)
  187. TEXT(password2,password$)
  188. FONT(password2,font_mono$)
  189. ok_btn = BUTTON(INTL$("OKAY"), 65, 25)
  190. ATTACH(mainwin, ok_btn, 240, 250)
  191. cancel_btn = BUTTON(INTL$("Cancel"), 65, 25)
  192. ATTACH(mainwin, cancel_btn, 320, 250)
  193. REM define some info boxes...
  194. err_dlg1=MSGDIALOG(INTL$("Sorry, the password entries are not the same"),400,100,3,2)
  195. yes_dlg1=MSGDIALOG(INTL$("Great, you have modified the proxy server settings. Note, you can see the result in files /etc/profile.d/http_profile and /etc/profile.d/ftp_profile. However, MOST IMPORTANT, you must reboot for this to take effect"),500,160,0,1)
  196. no_dlg1=MSGDIALOG(INTL$("You have chosen NOT to use a proxy server. Note, if you previously did have a proxy server enabled, you MUST REBOOT for the change to take effect"),500,140,0,1)
  197. REM Define the callbacks
  198. CALLBACK(ok_btn,ok_callback)
  199. CALLBACK(cancel_btn,QUIT)
  200. CALLBACK(check1,check1_callback)
  201. CALLBACK(err_dlg1,close_dialog)
  202. CALLBACK(yes_dlg1,QUIT)
  203. CALLBACK(no_dlg1,QUIT)
  204. REM tick or untick the checkbox...
  205. SET(check1,1)
  206. SET(check1,proxy_flag)
  207. REM endless gtk loop...
  208. DISPLAY