3proxy-linux-install.sh 22 KB

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