Class: THC::RC

Inherits:
Object
  • Object
show all
Defined in:
lib/thc/rc.rb

Constant Summary collapse

DEFAULT_TORRC =
"/etc/tor/torrc"
HEADER =
"# THC"

Class Method Summary collapse

Class Method Details

.add_hs(dport, sport, id) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/thc/rc.rb', line 66

def self.add_hs(dport,sport,id)
	c,p=RC::check(dport.to_i,sport.to_i)
	if c<0
		File.open(@torrc, 'a+') do |f|
		  	f.puts "HiddenServiceDir /var/tor/.tor_hidden_"+id.to_s+"/\nHiddenServicePort "+dport+" 127.0.0.1:"+sport  
		end 
	else
		raise "Hidden Service already exist on "+p+" port: "+c.to_s
	end
end

.check(dp, sp) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/thc/rc.rb', line 44

def self.check(dp,sp)
	c=-1
	p=""
	File.open(@torrc, 'r') do |f|
		f.each_line do |line|
			if (line.include?("HiddenServicePort")) && (c == -1)
				if line.split(" ")[1].to_i == dp
					c=dp
					p="destination"
					break
				end
				if line.split(" ").last.split(":").last.to_i == sp
					c=sp
					p="source"
					break
				end
			end
		end
	end
	return c,p
end

.init(path) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/thc/rc.rb', line 9

def self.init(path)
	@torrc = path
	y=false
	File.open(@torrc, 'r') do |f|
		f.each_line do |line|
			if line.include?(HEADER)
				y=true
			end
		end
	end
	if !y 
		FileUtils.cp(@torrc, @torrc+"-old")
		File.open(@torrc, 'a+') do |f|
		  	f.puts HEADER
		end 
	end
end

.load(config) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/thc/rc.rb', line 27

def self.load(config)
	count=-1;
	File.open(@torrc, 'r') do |f|
		f.each_line do |line|
			if !line.start_with?("#") then
				if line.include?("HiddenServiceDir")
					config << {'path' => line.split(" ").last, 'service' => []}
					count+=1;
				elsif line.include?("HiddenServicePort")
					#puts config.to_s
					config[count]['service'] << {'dport' => line.split(" ")[1].to_i, 'sport' => line.split(" ").last.split(":").last.to_i}
				end
			end
		end
	end
end

.rm_hs(id) ⇒ Object



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
# File 'lib/thc/rc.rb', line 77

def self.rm_hs(id)
	y=false
	c=0
	File.open(@torrc+"_temp", "w") do |out_file|
	  File.foreach(@torrc) do |line|
	    if line.include?(HEADER)
	    	y=true
	    	out_file.puts line
	    elsif y && line.include?("HiddenServiceDir")
	    	c+=1
				if id != c
					out_file.puts line
				end
			elsif y && line.include?("HiddenServicePort")
				if id != c
					out_file.puts line
				end
	    else 
	    	out_file.puts line
	    end
	  end
	end

	FileUtils.mv(@torrc+"_temp", @torrc)
end