3proxy-linux-install.sh 22 KB

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