-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtorctl.lib
executable file
·176 lines (146 loc) · 4.51 KB
/
torctl.lib
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env bash
#
# ############################################################################
# Project: torctl (none)
# File...: torctl.lib
# Created: Tuesday, 2022/11/29 - 20:46:56
# Author.: @fbnmtz, ([email protected])
# ~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~~·~·~·~·~·~·~·~
# Last Modified: Thursday, 2023/01/19 - 18:54:40
# Modified By..: @fbnmtz, ([email protected])
# ~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~~·~·~·~·~·~·~·~
# Version: 0.0.3.441
# ~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~~·~·~·~·~·~·~·~
# Description:
# >
# ############################################################################
# HISTORY:
#
#!/usr/bin/bash
_xLIB_TORCTL_=true
# define app directory
APP_DIR=$(dirname "$0")
# if executing out of $PATH system (./torctl), redifine APP_DIR
[ "$APP_DIR" == "." ] && cd "$PWD" && APP_DIR=$PWD
# function to load FW rules
load_fw_rules(){
case "$(xsys.os)" in
Linux )
source "$APP_DIR/config/fw-up-linux.sh"
source "$APP_DIR/config/fw-down-linux.sh"
;;
Darwin)
source "$APP_DIR/config/fw-up-darwin.sh"
source "$APP_DIR/config/fw-down-darwin.sh"
;;
esac
}
# get current IP Address
current_ip(){
curl --silent ifconfig.me
}
# get geo information of current ip address
ip_geo_information(){
curl --silent http://ip-api.com/json/"$(current_ip)"
}
# function to start TOR SERVER
start_tor_server(){
# start tor server with docker
docker run --rm \
-d \
--net host \
-v "$APP_DIR"/torctl.rc:/etc/tor/torrc \
-v "$APP_DIR"/torctl.resolv.conf:/etc/resolv.conf \
--entrypoint "" \
--name torctl_server \
m88v2/tor-server:latest \
/bin/sh -c 'tor -f /etc/tor/torrc' &> /dev/null
}
# setup firewall rules to host use 'tor_server'
configure_firewall(){
# load rules based on the current OS
load_fw_rules
# test current OS and apply rules
case "$(xsys.os)" in
Linux ) fwLinuxUp ;;
Darwin) fwDarwinUp ;;
esac
}
# restore default rules or setup a clear firewall
restore_firewall(){
# load rules based on the current OS
load_fw_rules
# test current OS and apply rules
case "$(xsys.os)" in
Linux ) fwLinuxDown ;;
Darwin) fwDarwinDown ;;
esac
}
# ~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~
# script actions
# ~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~
actionStart(){
# require ROOT to run
requireROOT
local title="-> Starting Tor Server"
local msg=""
echo "$title"
if start_tor_server; then
echo "-> setting up iptables rules"
configure_firewall
echo '-> Fetching current IP...'
sleep 2
tor_ip="$(current_ip)"
echo "-> CURRENT IP: $tor_ip"
# set variables for notification
title="torctl started"
msg="this is your current IP ($tor_ip)"
# call system function for notification
xsys.notify "$title" "$msg"
else
msg="(check your docker service)"
title="-> fail to start server..."
echo "$title $msg"
xsys.notify "torctl started" "$msg"
exit 2
fi
}
actionStop(){
# require ROOT to run
requireROOT
local title="-> Stopping Tor Server"
local msg=""
echo "$title"
echo "$title"
docker stop torctl_server &> /dev/null
echo "-> clear firewall rules"
restore_firewall
tor_ip="$(current_ip)"
echo "-> CURRENT IP: $tor_ip"
title="torctl stoped"
msg="using again your provider IP ($tor_ip)"
# sys.notify "torctl stoped" "using again yout provider IP $tor_ip"
xsys.notify "$title" "$msg"
}
actionStatus(){
local title="torctl status"
local msg=''
# check if TorServer is running via docker
if docker ps | grep torctl &> /dev/null; then
msg="-> Tor Server is RUNNING..."
else
msg="-> Tor Server is NOT RUNNING"
fi
echo "$msg"
tor_ip="$(current_ip)"
msg+=" (IP: $tor_ip)"
echo "-> CURRENT IP: ${tor_ip}..."
echo "-> Details:"
# sys.notify "torctl status" "$msg - IP: $(current_ip)"
xsys.notify "${title}" "$(echo ${msg/->/})"
# ip_geo_information | jq
}
actionRestart(){
actionStop
actionStart
}