3proxy-linux-install.sh 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. #!/bin/bash
  2. # 3proxy build and install script for Debian Linux
  3. # Release 2.0 at 29.12.2016
  4. # (с) Evgeniy Solovyev
  5. # mail-to: eugen-soloviov@yandex.ru
  6. ScriptPath=""
  7. Src3proxyDirPath=""
  8. ScriptName=""
  9. ScriptFullName=""
  10. SourceRoot=""
  11. ResourcesData=""
  12. ProxyVersion=""
  13. LasestProxyVersion=""
  14. LasestProxyVersionLink=""
  15. UseSudo=0
  16. PacketFiles=""
  17. NeedSourceUpdate=0
  18. main()
  19. {
  20. local msgNewVersion
  21. local msgInsertYorN
  22. VarsInit
  23. LoadResources
  24. CheckRunConditions
  25. if [ $UseSudo == 1 ]
  26. then
  27. sudo bash "${0}"
  28. exit $?
  29. fi
  30. CheckLocation
  31. GetLasestVersionInfo
  32. SourceDownloadOrUpdate
  33. cd "${SourceRoot}"
  34. Build3Proxy
  35. BinInstall
  36. ManInstall
  37. CreateLogDir
  38. CopyConfig
  39. SetInit
  40. Pack3proxyFiles
  41. }
  42. VarsInit()
  43. {
  44. cd `dirname $0`
  45. ScriptPath="${PWD}"
  46. ScriptName=`basename $0`
  47. ScriptFullName="${ScriptPath}/${ScriptName}"
  48. }
  49. CheckLocation()
  50. {
  51. Src3proxyDirPath="${ScriptPath}"
  52. if echo ${ScriptPath} | grep -e "/scripts$"
  53. then
  54. if [ -e "../src/version.h" ]
  55. then
  56. ProxyVersion=`cat "../src/version.h" | awk '/VERSION/ { gsub("\"", "\n"); print; exit }' | grep "3proxy"`
  57. cd ../
  58. SourceRoot="${PWD}"
  59. cd ../
  60. Src3proxyDirPath="${PWD}"
  61. cd "${ScriptPath}"
  62. fi
  63. fi
  64. }
  65. GetLasestVersionInfo()
  66. {
  67. local Githublink
  68. local msg
  69. Githublink=`wget https://github.com/z3APA3A/3proxy/releases/latest -O /dev/stdout |
  70. awk '/<a.+href=.+\.tar\.gz/ { gsub("\"", "\n"); print; exit }' |
  71. grep -e ".tar.gz"`
  72. if [ $? != 0 ]
  73. then
  74. msg=`GetResource "msgInternetConnectionError"`
  75. echo -e "${msg}"
  76. exit 255
  77. fi
  78. LasestProxyVersionLink="https://github.com${Githublink}"
  79. LasestProxyVersion=`basename "${Githublink}" | awk 'gsub(".tar.gz", "") { print "3proxy-" $0 }'`
  80. }
  81. CheckRunConditions()
  82. {
  83. local UserName
  84. local answer
  85. local msg
  86. local msgContinueWork
  87. local msgInsertYorN
  88. UserName=`whoami`
  89. if [ $UID != 0 ]
  90. then
  91. if [ `CheckPacketInstall "sudo"` == 0 ]
  92. then
  93. msg=`GetResource "msgSudoNotInstalled"`
  94. echo -e "${msg}"
  95. exit 255
  96. fi
  97. UseSudo=1
  98. if [ -z `cat /etc/group | grep -e "^sudo" | grep "${UserName}"` ]
  99. then
  100. msg=`GetResource "msgUserNotMemberOfSudoGroup"`
  101. echo -e "${msg}"
  102. exit 255
  103. fi
  104. if [ `env | grep -e ^http_proxy` != "" ]
  105. then
  106. msg=`GetResource "msgSystemUseProxy"`
  107. echo -e "${msg}"
  108. msgContinueWork=`GetResource "msgDoYouWishContinue"`
  109. msgInsertYorN=`GetResource "msgPleaseInsertYorN"`
  110. while true; do
  111. read -s -n1 -p "${msgContinueWork}" answer
  112. case $answer in
  113. [Yy]* ) echo -ne "\n";break;;
  114. [Nn]* ) echo -ne "\n"; sleep 0; exit 0;;
  115. * ) echo -e "${msgInsertYorN}";;
  116. esac
  117. done
  118. fi
  119. fi
  120. }
  121. DonwnloadSource()
  122. {
  123. if [ ! -e "${Src3proxyDirPath}/${LasestProxyVersion}.tar.gz" ]
  124. then
  125. wget "${LasestProxyVersionLink}" -O "${Src3proxyDirPath}/${LasestProxyVersion}.tar.gz"
  126. fi
  127. ProxyVersion="${LasestProxyVersion}"
  128. }
  129. UnpackSource()
  130. {
  131. if [ ! -d "${Src3proxyDirPath}/${LasestProxyVersion}" ]
  132. then
  133. tar -xvf "${Src3proxyDirPath}/${LasestProxyVersion}.tar.gz" -C "${Src3proxyDirPath}"
  134. fi
  135. SourceRoot="${Src3proxyDirPath}/${LasestProxyVersion}"
  136. }
  137. SourceDownloadOrUpdate()
  138. {
  139. if [ -z "${ProxyVersion}" ]
  140. then
  141. NeedSourceUpdate=1
  142. else
  143. if [ "${ProxyVersion}" != "${LasestProxyVersion}" ]
  144. then
  145. msgNewVersion=`GetResource "msgNewVersion"`
  146. msgInsertYorN=`GetResource "msgPleaseInsertYorN"`
  147. echo -ne "\a"
  148. while true; do
  149. read -s -n1 -p "${msgNewVersion}" answer
  150. case $answer in
  151. [Yy]* ) echo -ne "\n"; NeedSourceUpdate=1; sleep 0; break;;
  152. [Nn]* ) echo -ne "\n"; NeedSourceUpdate=0; sleep 0; break;;
  153. * ) echo -e "${msgInsertYorN}";;
  154. esac
  155. done
  156. fi
  157. fi
  158. if [ $NeedSourceUpdate == 1 ]
  159. then
  160. DonwnloadSource
  161. UnpackSource
  162. fi
  163. }
  164. Build3Proxy()
  165. {
  166. local msg
  167. if [ `CheckPacketInstall "build-essential"` == 0 ]
  168. then
  169. apt-get -y install build-essential
  170. fi
  171. if [ `CheckPacketInstall "build-essential"` == 0 ]
  172. then
  173. msg=`GetResource "msgBuildEssentialNotInstalled"`
  174. echo -e "${msg}"
  175. exit 255
  176. fi
  177. make -f Makefile.Linux
  178. }
  179. BinInstall()
  180. {
  181. local binlist
  182. local liblist
  183. cd bin
  184. binlist=`ls -l --time-style="+%d.%m.%Y %H:%m" | awk '$1 ~ /x$/ && $1 ~ /^[^d]/ && $8 !~ /\.so$/ { print $8 }'`
  185. for file in $binlist
  186. do
  187. cp -vf "${file}" /usr/bin
  188. PacketFiles=`echo -e "${PacketFiles}\n/usr/bin/${file}"`
  189. done
  190. liblist=`ls -l --time-style="+%d.%m.%Y %H:%m" | awk '$1 ~ /x$/ && $1 ~ /^[^d]/ && $8 ~ /\.so$/ { print $8 }'`
  191. for file in $liblist
  192. do
  193. cp -vf "${file}" /usr/lib
  194. PacketFiles=`echo -e "${PacketFiles}\n/usr/lib/${file}"`
  195. done
  196. cd ..
  197. }
  198. ManInstall()
  199. {
  200. local man3list
  201. local man8list
  202. cd man
  203. man3list=`ls -l --time-style="+%d.%m.%Y %H:%m" | awk '$8 ~ /\.3$/ { print $8 }'`
  204. gzip -vfk $man3list
  205. man3list=`echo "${man3list}" | awk '{ print $1 ".gz" }'`
  206. for file in $man3list
  207. do
  208. mv -vf "${file}" /usr/share/man/man3
  209. PacketFiles="${PacketFiles}\n/usr/share/man/man3/${file}"
  210. done
  211. man8list=`ls -l --time-style="+%d.%m.%Y %H:%m" | awk '$8 ~ /\.8$/ { print $8 }'`
  212. gzip -vfk $man8list
  213. man8list=`echo "${man8list}" | awk '{ print $1 ".gz" }'`
  214. for file in $man8list
  215. do
  216. mv -vf "${file}" /usr/share/man/man8
  217. PacketFiles=`echo -e "${PacketFiles}\n/usr/share/man/man8/${file}"`
  218. done
  219. cd ..
  220. }
  221. CreateLogDir()
  222. {
  223. local LogDir
  224. LogDir="/var/log/3proxy"
  225. if [ ! -d "${LogDir}" ]
  226. then
  227. mkdir "${LogDir}"
  228. fi
  229. chown nobody:nogroup "${LogDir}"
  230. chmod 775 "${LogDir}"
  231. PacketFiles="${PacketFiles}\n${LogDir}"
  232. }
  233. CopyConfig()
  234. {
  235. local ConfigDir
  236. ConfigDir="/etc/3proxy"
  237. if [ ! -d "${ConfigDir}" ]
  238. then
  239. mkdir "${ConfigDir}"
  240. fi
  241. LoadGlobalResource "ConfigFile" > "${ConfigDir}/3proxy.cfg"
  242. PacketFiles=`echo -e "${PacketFiles}\n${ConfigDir}/3proxy.cfg"`
  243. }
  244. SetInit()
  245. {
  246. LoadGlobalResource "InitScript" > "/etc/init.d/3proxy"
  247. chown root:root "/etc/init.d/3proxy"
  248. chmod 755 "/etc/init.d/3proxy"
  249. PacketFiles=`echo -e "${PacketFiles}\n/etc/init.d/3proxy"`
  250. update-rc.d 3proxy defaults
  251. }
  252. Pack3proxyFiles()
  253. {
  254. local CPU_Arc
  255. CPU_Arc=`uname -m`
  256. cd ../
  257. tar -czPpvf "${ProxyVersion}-${CPU_Arc}.tar.gz" $PacketFiles
  258. }
  259. LoadResources()
  260. {
  261. local StartRow
  262. local EndRow
  263. local LngLabel
  264. local msgResourceErr="\aError! Script could not find resources!"
  265. if env | grep -q 'LANG=ru_RU.UTF-8'
  266. then
  267. LngLabel="RU"
  268. #LngLabel="EN"
  269. else
  270. LngLabel="EN"
  271. fi
  272. StartRow=`cat "${ScriptFullName}" | awk "/^#Resources_${LngLabel}/ { print NR; exit}"`
  273. if [ -z "${StartRow}" ]
  274. then
  275. echo -e "${msgResourceErr}"
  276. exit 255
  277. fi
  278. EndRow=`cat "${ScriptFullName}" | awk "NR > ${StartRow} && /^#Resources_${LngLabel}_end/ { print NR; exit}"`
  279. if [ -z "${EndRow}" ]
  280. then
  281. echo -e "${msgResourceErr}"
  282. exit 255
  283. fi
  284. ResourcesData=`cat "${ScriptFullName}" | awk -v StartRow="${StartRow}" -v EndRow="${EndRow}" 'NR > StartRow && NR < EndRow { print $0 }'`
  285. }
  286. # $1 - Name of Resource
  287. GetResource()
  288. {
  289. local StartRow
  290. local EndRow
  291. local msgResourceErr="\aError! Script could not find resource \"${1}\"!"
  292. StartRow=`echo "${ResourcesData}" | awk "/^#Resource=${1}/ { print NR; exit}"`
  293. if [ -z "${StartRow}" ]
  294. then
  295. echo -e "${msgResourceErr}" > /dev/stderr
  296. exit 255
  297. fi
  298. EndRow=`echo "${ResourcesData}" | awk "NR > ${StartRow} && /^#endResource=${1}/ { print NR; exit}"`
  299. if [ -z "${EndRow}" ]
  300. then
  301. echo -e "${msgResourceErr}" > /dev/stderr
  302. exit 255
  303. fi
  304. echo "${ResourcesData}" | awk -v StartRow="${StartRow}" -v EndRow="${EndRow}" 'NR > StartRow && NR < EndRow { print $0 }'
  305. }
  306. # $1 - Name of Resource
  307. LoadGlobalResource()
  308. {
  309. local StartRow
  310. local EndRow
  311. local LngLabel
  312. local msgResourceErr="\aError! Script could not find resource \"${1}\"!"
  313. StartRow=`cat "${ScriptFullName}" | awk "/^#Resource=${1}/ { print NR; exit}"`
  314. if [ -z "${StartRow}" ]
  315. then
  316. echo -e "${msgResourceErr}" > /dev/stderr
  317. exit 255
  318. fi
  319. EndRow=`cat "${ScriptFullName}" | awk "NR > ${StartRow} && /^#endResource=${1}/ { print NR; exit}"`
  320. if [ -z "${EndRow}" ]
  321. then
  322. echo -e "${msgResourceErr}" > /dev/stderr
  323. exit 255
  324. fi
  325. cat "${ScriptFullName}" | awk -v StartRow="${StartRow}" -v EndRow="${EndRow}" 'NR > StartRow && NR < EndRow { print $0 }'
  326. }
  327. CheckPacketInstall()
  328. {
  329. if [ `dpkg -l ${1} 2>&1 | wc -l` -le 1 ]
  330. then
  331. echo 0
  332. return
  333. fi
  334. if [ `dpkg -l ${1} | grep -e ^un | wc -l` == 1 ]
  335. then
  336. echo 0
  337. return
  338. fi
  339. echo 1
  340. }
  341. main
  342. exit 0
  343. #Resources_EN
  344. #Resource=msgSudoNotInstalled
  345. \aThe script is running under the account a non-privileged user.
  346. "Sudo" package is not installed in the system.
  347. The script can not continue, as the execution of operations,
  348. requiring rights "root" - is not possible!
  349. Please run the script under the account "root",
  350. or install and configure "sudo" package!
  351. #endResource=msgSudoNotInstalled
  352. #Resource=msgUserNotMemberOfSudoGroup
  353. \aThe script is running under account a non-privileged user.
  354. The account of the current user is not included in the "sudo" group!
  355. The script can not continue, as the execution of operations,
  356. requiring rights "root" - is not possible!
  357. Please run the script under the account "root",
  358. or configure "sudo" package!
  359. #endResource=msgUserNotMemberOfSudoGroup
  360. #Resource=msgSystemUseProxy
  361. \aAttention! The operating system uses proxy-server.
  362. For correctly work of package manager "apt"
  363. in the file "/etc/sudoers" should be present line:
  364. Defaults env_keep = "http_proxy https_proxy"
  365. #endResource=msgSystemUseProxy
  366. #Resource=msgDoYouWishContinue
  367. Do you wish to the script continued executing? (y/n):
  368. #endResource=msgDoYouWishContinue
  369. #Resource=msgPleaseInsertYorN
  370. \a\nPlease insert "y" or "n"!
  371. #endResource=msgPleaseInsertYorN
  372. #Resource=msgInternetConnectionError
  373. \aError downloading "https://github.com/z3APA3A/3proxy/releases/latest"!
  374. Please check the settings of the Internet connection.
  375. #endResource=msgInternetConnectionError
  376. #Resource=msgNewVersion
  377. The new version of "3proxy" detected, do you want download it?
  378. #endResource=msgNewVersion
  379. #Resource=msgBuildEssentialNotInstalled
  380. \aPackage "build-essential" was not installed.
  381. The installation can not be continued!
  382. #endResource=msgBuildEssentialNotInstalled
  383. #Resources_EN_end
  384. #Resources_RU
  385. #Resource=msgSudoNotInstalled
  386. \aСкрипт запущен под учётной записью обычного пользователя.
  387. В системе не установлен пакет "sudo".
  388. Скрипт не может продолжить работу, так как выполнение операций,
  389. требующих прав "root" - не представляется возможным!
  390. Пожалуйста, запустите скрипт под учётной записью "root",
  391. либо установите и настройте пакет "sudo"!
  392. #endResource=msgSudoNotInstalled
  393. #Resource=msgUserNotMemberOfSudoGroup
  394. \aСкрипт запущен под учётной записью обычного пользователя.
  395. Учётная запись текущего пользователя не включена в группу "sudo"!
  396. Скрипт не может продолжить работу, так как выполнение операций,
  397. требующих прав "root" - не представляется возможным!
  398. Пожалуйста, запустите скрипт под учётной записью "root",
  399. либо настройте пакет "sudo"!
  400. #endResource=msgUserNotMemberOfSudoGroup
  401. #Resource=msgSystemUseProxy
  402. \aВнимание! В системе используется прокси-сервер.
  403. Чтобы менеджер пакетов "apt" работал корректно,
  404. в файле "/etc/sudoers" должна присутствовать строка:
  405. Defaults env_keep = "http_proxy https_proxy"
  406. #endResource=msgSystemUseProxy
  407. #Resource=msgDoYouWishContinue
  408. Хотите чтобы скрипт дальше продолжил работу? (y/n):
  409. #endResource=msgDoYouWishContinue
  410. #Resource=msgPleaseInsertYorN
  411. \a\nПожалуйста введите "y" или "n"!
  412. #endResource=msgPleaseInsertYorN
  413. #Resource=msgInternetConnectionError
  414. \aОшибка закачки "https://github.com/z3APA3A/3proxy/releases/latest"!
  415. Пожалуйста, проверьте настройки интернет соединения.
  416. #endResource=msgInternetConnectionError
  417. #Resource=msgNewVersion
  418. Обнаружена новая версия "3proxy", скачать её (y/n)?
  419. #endResource=msgNewVersion
  420. #Resource=msgBuildEssentialNotInstalled
  421. \aПакет "build-essential" не был установлен.
  422. Дальнейшая установка не может быть продолжена!
  423. #endResource=msgBuildEssentialNotInstalled
  424. #Resources_RU_end
  425. #Resource=ConfigFile
  426. noconfig
  427. # If in this file have line "noconfig", then 3proxy not to be runned!
  428. # For usung this configuration file 3proxy you must to delete
  429. # or comment out the line with "noconfig".
  430. daemon
  431. # Parameter "daemon" - means run 3proxy as daemon
  432. pidfile /tmp/3proxy.pid
  433. # PID file location
  434. # This parameter must have the same value as
  435. # the variable "PidFile" in the script "/etc/init.d/3proxy"
  436. # Configuration file location
  437. config /etc/3proxy/3proxy.cfg
  438. internal 127.0.0.1
  439. # Internal is address of interface proxy will listen for incoming requests
  440. # 127.0.0.1 means only localhost will be able to use this proxy. This is
  441. # address you should specify for clients as proxy IP.
  442. # You MAY use 0.0.0.0 but you shouldn't, because it's a chance for you to
  443. # have open proxy in your network in this case.
  444. external 192.168.0.1
  445. # External is address 3proxy uses for outgoing connections. 0.0.0.0 means any
  446. # interface. Using 0.0.0.0 is not good because it allows to connect to 127.0.0.1
  447. # DNS IP addresses
  448. nserver 8.8.8.8
  449. nserver 8.8.4.4
  450. # DNS cache size
  451. nscache 65536
  452. # Timeouts settings
  453. timeouts 1 5 30 60 180 1800 15 60
  454. # log file location
  455. log /var/log/3proxy/3proxy.log D
  456. # log file format
  457. logformat "L%C - %U [%d-%o-%Y %H:%M:%S %z] ""%T"" %E %I %O %N/%R:%r"
  458. archiver gz /usr/bin/gzip %F
  459. # If archiver specified log file will be compressed after closing.
  460. # you should specify extension, path to archiver and command line, %A will be
  461. # substituted with archive file name, %f - with original file name.
  462. # Original file will not be removed, so archiver should care about it.
  463. rotate 30
  464. # We will keep last 30 log files
  465. proxy -p3128
  466. # Run http/https proxy on port 3128
  467. auth none
  468. # No authentication is requires
  469. setgid 65534
  470. setuid 65534
  471. # Run 3proxy under account "nobody" with group "nobody"
  472. #endResource=ConfigFile
  473. #Resource=InitScript
  474. #!/bin/sh
  475. #
  476. # 3proxy daemon control script
  477. #
  478. ### BEGIN INIT INFO
  479. # Provides: 3proxy
  480. # Required-Start: $network $remote_fs $syslog
  481. # Required-Stop: $network $remote_fs $syslog
  482. # Should-Start: $named
  483. # Should-Stop: $named
  484. # Default-Start: 2 3 4 5
  485. # Default-Stop: 0 1 6
  486. # Short-Description: 3proxy HTTP Proxy
  487. ### END INIT INFO
  488. ScriptName="3proxy"
  489. ScriptFullName="/etc/init.d/3proxy"
  490. ConfigFile="/etc/3proxy/3proxy.cfg"
  491. LogDir="/var/log/3proxy"
  492. PidFile="/tmp/3proxy.pid"
  493. ResourcesData=""
  494. main()
  495. {
  496. LoadResources
  497. if [ ! -d "${LogDir}" ]
  498. then
  499. mkdir -p "${LogDir}";
  500. fi
  501. case "$1" in
  502. start) Start ;;
  503. stop) Stop ;;
  504. restart) Stop; Start ;;
  505. status) Status ;;
  506. *) ShowHelp;;
  507. esac
  508. }
  509. Start()
  510. {
  511. local msg
  512. local ProxyPID
  513. if [ ! -f "${ConfigFile}" ]
  514. then
  515. msg=`GetResource "msgConfigFileNotFound"`
  516. printf "${msg}" "${ConfigFile}"
  517. return
  518. fi
  519. if cat "${ConfigFile}" | grep -qe "^noconfig"
  520. then
  521. msg=`GetResource "msgNoconfigDetected"`
  522. printf "${msg}" "${ConfigFile}"
  523. return
  524. fi
  525. ProxyPID=`Get3proxyPID`
  526. if [ ! -z "${ProxyPID}" ]
  527. then
  528. msg=`GetResource "msg3proxyAlreadyRunning"`
  529. printf "${msg}" "${ProxyPID}"
  530. return
  531. fi
  532. 3proxy "${ConfigFile}"
  533. sleep 1
  534. ProxyPID=`Get3proxyPID`
  535. if [ ! -f "${PidFile}" ]
  536. then
  537. msg=`GetResource "msg3proxyStartProblems"`
  538. printf "${msg}"
  539. return
  540. fi
  541. if [ `cat "${PidFile}"` != "${ProxyPID}" ]
  542. then
  543. msg=`GetResource "msg3proxyStartProblems"`
  544. printf "${msg}"
  545. return
  546. fi
  547. msg=`GetResource "msg3proxyStartedSuccessfully"`
  548. printf "${msg}" `date +%d-%m-%Y" "%H:%M:%S` "${ProxyPID}"
  549. }
  550. Stop()
  551. {
  552. local msg
  553. local ProxyPID
  554. ProxyPID=`Get3proxyPID`
  555. if [ -f "${PidFile}" ]
  556. then
  557. if [ `cat "${PidFile}"` = "${ProxyPID}" ]
  558. then
  559. kill -9 "${ProxyPID}"
  560. rm -f "${PidFile}"
  561. msg=`GetResource "msg3proxyStoppedSuccessfully"`
  562. printf "${msg}" `date +%d-%m-%Y" "%H:%M:%S`
  563. return
  564. fi
  565. fi
  566. if [ -z "${ProxyPID}" ]
  567. then
  568. msg=`GetResource "msg3proxyProxyNotDetected"`
  569. printf "${msg}"
  570. return
  571. fi
  572. pkill -o 3proxy
  573. msg=`GetResource "msg3proxyStoppedByKillall"`
  574. printf "${msg}" `date +%d-%m-%Y" "%H:%M:%S` "${PidFile}"
  575. }
  576. Status()
  577. {
  578. local msg
  579. local ProxyPID
  580. if [ -f "${PidFile}" ]
  581. then
  582. msg=`GetResource "msgPidFileExists"`
  583. printf "${msg}" "${PidFile}" `cat "${PidFile}"`
  584. else
  585. msg=`GetResource "msgPidFileNotExists"`
  586. printf "${msg}" "${PidFile}"
  587. fi
  588. ProxyPID=`Get3proxyPID`
  589. if [ ! -z "${ProxyPID}" ]
  590. then
  591. msg=`GetResource "msg3proxyProcessDetected"`
  592. printf "${msg}"
  593. ps -ef | awk '$8 ~ /^3proxy/ { print "User: " $1 "\tPID: " $2 }'
  594. else
  595. msg=`GetResource "msg3proxyProcessNotDetected"`
  596. printf "${msg}"
  597. fi
  598. }
  599. ShowHelp()
  600. {
  601. local msg
  602. msg=`GetResource "msg3proxyHelp"`
  603. printf "${msg}" "${ScriptFullName}" "${ScriptName}"
  604. }
  605. Get3proxyPID()
  606. {
  607. ps -ef | awk '$8 ~ /^3proxy/ { print $2; exit }'
  608. }
  609. LoadResources()
  610. {
  611. local StartRow
  612. local EndRow
  613. local LngLabel
  614. local msgResourceErr="\aError! Script could not find resources!"
  615. if env | grep -q 'LANG=ru_RU.UTF-8'
  616. then
  617. LngLabel="RU"
  618. else
  619. LngLabel="EN"
  620. fi
  621. StartRow=`cat "${ScriptFullName}" | awk "/^#Resources_${LngLabel}/ { print NR; exit}"`
  622. if [ -z "${StartRow}" ]
  623. then
  624. echo -e "${msgResourceErr}"
  625. exit 255
  626. fi
  627. EndRow=`cat "${ScriptFullName}" | awk "NR > ${StartRow} && /^#Resources_${LngLabel}_end/ { print NR; exit}"`
  628. if [ -z "${EndRow}" ]
  629. then
  630. echo -e "${msgResourceErr}"
  631. exit 255
  632. fi
  633. ResourcesData=`cat "${ScriptFullName}" | awk -v StartRow="${StartRow}" -v EndRow="${EndRow}" 'NR > StartRow && NR < EndRow { print $0 }'`
  634. }
  635. # $1 - Name of Resource
  636. GetResource()
  637. {
  638. local StartRow
  639. local EndRow
  640. local msgResourceErr="\aError! Script could not find resource \"${1}\"!"
  641. StartRow=`echo "${ResourcesData}" | awk "/^#Resource=${1}/ { print NR; exit}"`
  642. if [ -z "${StartRow}" ]
  643. then
  644. echo -e "${msgResourceErr}" > /dev/stderr
  645. exit 255
  646. fi
  647. EndRow=`echo "${ResourcesData}" | awk "NR > ${StartRow} && /^#endResource=${1}/ { print NR; exit}"`
  648. if [ -z "${EndRow}" ]
  649. then
  650. echo -e "${msgResourceErr}" > /dev/stderr
  651. exit 255
  652. fi
  653. echo "${ResourcesData}" | awk -v StartRow="${StartRow}" -v EndRow="${EndRow}" 'NR > StartRow && NR < EndRow { print $0 }'
  654. }
  655. main $@
  656. exit 0;
  657. #Resources_EN
  658. #Resource=msg3proxyHelp
  659. Usage:
  660. \t%s {start|stop|restart}
  661. or
  662. \tservice %s {start|stop|restart|status}\\n
  663. #endResource=msg3proxyHelp
  664. #Resource=msgConfigFileNotFound
  665. \a3proxy configuration file - "%s" is not found!\\n
  666. #endResource=msgConfigFileNotFound
  667. #Resource=msgNoconfigDetected
  668. Parameter "noconfig" found in 3proxy configuration file -
  669. "% s" !
  670. To run 3proxy this parameter should be disabled.\\n
  671. #endResource=msgNoconfigDetected
  672. #Resource=msg3proxyAlreadyRunning
  673. \a3proxy already running PID: %s\\n
  674. #endResource=msg3proxyAlreadyRunning
  675. #Resource=msg3proxyStartProblems
  676. With the start of 3proxy, something is wrong!
  677. Use: service 3proxy status\\n
  678. #endResource=msg3proxyStartProblems
  679. #Resource=msg3proxyStartedSuccessfully
  680. [ %s %s ] 3proxy started successfully! PID: %s\\n
  681. #endResource=msg3proxyStartedSuccessfully
  682. #Resource=msg3proxyStoppedSuccessfully
  683. [ %s %s ] 3proxy stopped successfully!\\n
  684. #endResource=msg3proxyStoppedSuccessfully
  685. #Resource=msg3proxyProxyNotDetected
  686. Process "3proxy" is not detected!\\n
  687. #endResource=msg3proxyProxyNotDetected
  688. #Resource=msg3proxyStoppedByKillall
  689. [ %s %s ] Command "pkill -o 3proxy" was executed,
  690. because process number was not stored in "%s",
  691. but in fact 3proxy was runned!\\n
  692. #endResource=msg3proxyStoppedByKillall
  693. #Resource=msgPidFileExists
  694. File "%s" exists. It contains the PID: %s\\n
  695. #endResource=msgPidFileExists
  696. #Resource=msgPidFileNotExists
  697. File "%s" not found, that is, PID 3proxy was not stored!\\n
  698. #endResource=msgPidFileNotExists
  699. #Resource=msg3proxyProcessDetected
  700. Process 3proxy detected:\\n
  701. #endResource=msg3proxyProcessDetected
  702. #Resource=msg3proxyProcessNotDetected
  703. Processes of 3proxy is not found!\\n
  704. #endResource=msg3proxyProcessNotDetected
  705. #Resources_EN_end
  706. #Resources_RU
  707. #Resource=msg3proxyHelp
  708. Используйте:
  709. \t%s {start|stop|restart}
  710. или
  711. \tservice %s {start|stop|restart|status}\\n
  712. #endResource=msg3proxyHelp
  713. #Resource=msgConfigFileNotFound
  714. \aФайл конфигурации 3proxy - "%s", не найден!\\n
  715. #endResource=msgConfigFileNotFound
  716. #Resource=msgNoconfigDetected
  717. \aОбнаружен параметр "noconfig" в файле конфигурации 3proxy -
  718. "%s" !
  719. Для запуска 3proxy этот параметр нужно отключить.\\n
  720. #endResource=msgNoconfigDetected
  721. #Resource=msg3proxyAlreadyRunning
  722. \a3proxy уже запущен PID: %s\\n
  723. #endResource=msg3proxyAlreadyRunning
  724. #Resource=msg3proxyStartProblems
  725. \aСо стартом 3proxy, что-то не так!
  726. Используйте: service 3proxy status\\n
  727. #endResource=msg3proxyStartProblems
  728. #Resource=msg3proxyStartedSuccessfully
  729. [ %s %s ] 3proxy успешно стартовал! PID: %s\\n
  730. #endResource=msg3proxyStartedSuccessfully
  731. #Resource=msg3proxyStoppedSuccessfully
  732. [ %s %s ] 3proxy успешно остановлен!\\n
  733. #endResource=msg3proxyStoppedSuccessfully
  734. #Resource=msg3proxyProxyNotDetected
  735. Процесс "3proxy" не обнаружен!\\n
  736. #endResource=msg3proxyProxyNotDetected
  737. #Resource=msg3proxyStoppedByKillall
  738. [ %s %s ] Выполнена команда "pkill -o 3proxy",
  739. т.к. номер процесса не записан в "%s",
  740. но по факту 3proxy рабатал!\\n
  741. #endResource=msg3proxyStoppedByKillall
  742. #Resource=msgPidFileExists
  743. Файл "%s" есть. Он содержит PID: %s\\n
  744. #endResource=msgPidFileExists
  745. #Resource=msgPidFileNotExists
  746. Файл "%s" не найден, т.е. PID 3proxy не был сохранён!\\n
  747. #endResource=msgPidFileNotExists
  748. #Resource=msg3proxyProcessDetected
  749. Обнаружен процесс 3proxy:\\n
  750. #endResource=msg3proxyProcessDetected
  751. #Resource=msg3proxyProcessNotDetected
  752. Процессов 3proxy не обнаружено!\\n
  753. #endResource=msg3proxyProcessNotDetected
  754. #Resources_RU_end
  755. #endResource=InitScript